1 8 package org.apache.avalon.excalibur.lang; 9 10 import java.util.HashMap ; 11 import java.util.Map ; 12 import java.util.Iterator ; 13 14 21 public class DefaultThreadContextPolicy 22 implements ThreadContextPolicy 23 { 24 31 public void activate( final ThreadContextAccessor accessor ) 32 { 33 final ClassLoader classLoader = (ClassLoader )get( accessor, CLASSLOADER, null, ClassLoader .class ); 34 if( null != classLoader ) Thread.currentThread().setContextClassLoader( classLoader ); 35 } 36 37 43 public void deactivate( final ThreadContextAccessor accessor ) 44 { 45 } 46 47 54 public void verifyKeyValue( final String key, final Object value ) 55 throws IllegalArgumentException 56 { 57 if( key.equals( CLASSLOADER ) && !(value instanceof ClassLoader ) ) 58 { 59 throw new IllegalArgumentException ( "Key " + key + " must be of type " + 60 ClassLoader .class.getName() ); 61 } 62 } 63 64 73 protected Object get( final ThreadContextAccessor accessor, 74 final String key, 75 final Object defaultValue, 76 final Class type ) 77 { 78 Object result = defaultValue; 79 80 if( accessor.containsKey( key ) ) 81 { 82 result = accessor.get( key ); 83 } 84 85 if( null != result && !type.isInstance( result ) ) 86 { 87 throw new IllegalArgumentException ( "Key " + key + " expected to access " + 88 type.getName() + " but got " + 89 result.getClass().getName() ); 90 } 91 92 return result; 93 } 94 } 95 | Popular Tags |