KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > lang > DefaultThreadContextPolicy


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.lang;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 /**
15  * Default <code>ThreadContextPolicy</code> that just maintains the
16  * ContextClassLoader <code>ThreadLocal</code> variable. This is a useful
17  * class to extend for those wanting to write their own Policy.
18  *
19  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
20  */

21 public class DefaultThreadContextPolicy
22     implements ThreadContextPolicy
23 {
24     /**
25      * The activate method is called when the ThreadContext
26      * is associated with a thread. This method sets the ContextClassLoader
27      * if CLASSLOADER key is present in context.
28      *
29      * @param accessor the accessor to retrieve values from ThreadContext
30      */

31     public void activate( final ThreadContextAccessor accessor )
32     {
33         final ClassLoader JavaDoc classLoader = (ClassLoader JavaDoc)get( accessor, CLASSLOADER, null, ClassLoader JavaDoc.class );
34         if( null != classLoader ) Thread.currentThread().setContextClassLoader( classLoader );
35     }
36
37     /**
38      * The deactivate method is called when the ThreadContext is
39      * dis-associated with a thread.
40      *
41      * @param accessor the accessor to retrieve values from ThreadContext
42      */

43     public void deactivate( final ThreadContextAccessor accessor )
44     {
45     }
46
47     /**
48      * Verify that the key/value pair is valid.
49      *
50      * @param key The key
51      * @param value the value
52      * @exception IllegalArgumentException if pair is not valid
53      */

54     public void verifyKeyValue( final String JavaDoc key, final Object JavaDoc value )
55         throws IllegalArgumentException JavaDoc
56     {
57         if( key.equals( CLASSLOADER ) && !(value instanceof ClassLoader JavaDoc) )
58         {
59             throw new IllegalArgumentException JavaDoc( "Key " + key + " must be of type " +
60                                                 ClassLoader JavaDoc.class.getName() );
61         }
62     }
63
64     /**
65      * Get a value for specified key, using specified default if none present
66      * and making sure value is of specified type.
67      *
68      * @param key the key used to lookup value
69      * @param defaultValue the default value if the key does not specify value
70      * @param type the expected type of value
71      * @return the value
72      */

73     protected Object JavaDoc get( final ThreadContextAccessor accessor,
74                           final String JavaDoc key,
75                           final Object JavaDoc defaultValue,
76                           final Class JavaDoc type )
77     {
78         Object JavaDoc 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 JavaDoc( "Key " + key + " expected to access " +
88                                                 type.getName() + " but got " +
89                                                 result.getClass().getName() );
90         }
91
92         return result;
93     }
94 }
95
Popular Tags