001package org.apache.archiva.redback.policy; 002 003/* 004 * Copyright 2001-2006 The Apache Software Foundation. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019import java.util.HashMap; 020import java.util.Map; 021 022/** 023 * PolicyContext - A Thread Local Context. 024 * Useful for managing policy operations on a thread local point of view. 025 * 026 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> 027 * 028 */ 029public class PolicyContext 030{ 031 static ThreadLocal<PolicyContext> policyContext = new PolicyContextThreadLocal(); 032 033 Map<Object, Object> context; 034 035 public PolicyContext( Map<Object, Object> map ) 036 { 037 context = map; 038 } 039 040 public static void setContext( PolicyContext context ) 041 { 042 policyContext.set( context ); 043 } 044 045 public static PolicyContext getContext() 046 { 047 PolicyContext ctx = (PolicyContext) policyContext.get(); 048 if ( ctx == null ) 049 { 050 ctx = new PolicyContext( new HashMap<Object, Object>() ); 051 setContext( ctx ); 052 } 053 054 return ctx; 055 } 056 057 public Object get( Object key ) 058 { 059 return context.get( key ); 060 } 061 062 public void put( Object key, Object value ) 063 { 064 context.put( key, value ); 065 } 066 067 private static class PolicyContextThreadLocal 068 extends ThreadLocal<PolicyContext> 069 { 070 protected PolicyContext initialValue() 071 { 072 return new PolicyContext( new HashMap<Object, Object>() ); 073 } 074 } 075}