1 package org.apache.archiva.redback.policy; 2 3 /* 4 * Copyright 2001-2006 The Apache Software Foundation. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 import java.util.HashMap; 20 import java.util.Map; 21 22 /** 23 * PolicyContext - A Thread Local Context. 24 * Useful for managing policy operations on a thread local point of view. 25 * 26 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> 27 * 28 */ 29 public class PolicyContext 30 { 31 static ThreadLocal<PolicyContext> policyContext = new PolicyContextThreadLocal(); 32 33 Map<Object, Object> context; 34 35 public PolicyContext( Map<Object, Object> map ) 36 { 37 context = map; 38 } 39 40 public static void setContext( PolicyContext context ) 41 { 42 policyContext.set( context ); 43 } 44 45 public static PolicyContext getContext() 46 { 47 PolicyContext ctx = (PolicyContext) policyContext.get(); 48 if ( ctx == null ) 49 { 50 ctx = new PolicyContext( new HashMap<Object, Object>() ); 51 setContext( ctx ); 52 } 53 54 return ctx; 55 } 56 57 public Object get( Object key ) 58 { 59 return context.get( key ); 60 } 61 62 public void put( Object key, Object value ) 63 { 64 context.put( key, value ); 65 } 66 67 private static class PolicyContextThreadLocal 68 extends ThreadLocal<PolicyContext> 69 { 70 protected PolicyContext initialValue() 71 { 72 return new PolicyContext( new HashMap<Object, Object>() ); 73 } 74 } 75 }