49c2e177c71a5af22b59b49b5c125ae17589c64b
[utils] / support / general / src / main / java / org / wamblee / general / ThreadSpecificInvocationHandler.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.general;
17
18 import java.io.Serializable;
19 import java.lang.reflect.InvocationHandler;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.util.Map;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.atomic.AtomicInteger;
25
26 /**
27  * Invocation handler for thread-specific proxies. 
28  * 
29  * @author Erik Brakkee
30  *
31  * @param <T>
32  */
33 class ThreadSpecificInvocationHandler<T> implements InvocationHandler, Serializable {
34     
35     /**
36      * We store a map of unique ids of invocation handlers to thread local storage of the 
37      * service. In this way, serialiability of the generated proxy is obtained (required by 
38      * framweorks such as wicket). Also, different factories will still be separate and never
39      * use the same threadlocal storage. 
40      */
41     private static Map<Integer,ThreadLocal> STORAGE = initializeThreadLocal();
42     
43     private static AtomicInteger COUNTER = new AtomicInteger();
44     
45     private static Map<Integer, ThreadLocal> initializeThreadLocal() {
46         Map<Integer,ThreadLocal> map = new ConcurrentHashMap<Integer,ThreadLocal>();
47         return map;
48     }
49
50     
51     private int id; 
52     private Class clazz; 
53     
54     /**
55      * Constructs the handler. 
56      * @param aSvc Thread local for the service. 
57      * @param aClass Service interface class. 
58      */
59     public ThreadSpecificInvocationHandler(ThreadLocal aSvc, Class aClass) { 
60         id = COUNTER.incrementAndGet();
61         clazz = aClass;
62         STORAGE.put(id, aSvc);
63     }
64
65     @Override
66     public Object invoke(Object aProxy, Method aMethod, Object[] aArgs)
67         throws Throwable {
68
69         ThreadLocal<T> local = STORAGE.get(id);
70         T actualSvc = local.get();
71         if ( aMethod.getName().equals("toString") && actualSvc == null) { 
72             return "Thread-specific proxy for '" + clazz.getName() + "' id = " + id;
73         }
74         try {
75             return aMethod.invoke(actualSvc, aArgs);
76         } catch (InvocationTargetException e) {
77             throw e.getCause();
78         }
79     }
80 }