(no commit message)
[utils] / test / enterprise / src / main / java / org / wamblee / support / jndi / JndiProxyFactory.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.support.jndi;
17
18 import java.lang.reflect.InvocationHandler;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Proxy;
22
23 import javax.naming.InitialContext;
24 import javax.naming.NamingException;
25
26 /**
27  * Proxy factory that can provide contextual references to objects retrieved
28  * from JNDI.
29  * 
30  * @param T
31  *            Interface to proxy.
32  * @author Erik Brakkee
33  * 
34  */
35 public class JndiProxyFactory<T> {
36     /**
37      * Exception thrown in case an object cannot be retrieved from JNDI. 
38      * 
39      * @author Erik Brakkee
40      */
41     public static class JndiWiringException extends RuntimeException {
42         public JndiWiringException(String aMsg, Throwable aCause) {
43             super(aMsg, aCause);
44         }
45
46         public JndiWiringException(String aMsg) {
47             super(aMsg);
48         }
49     }
50
51     /**
52      * Invocation handler that does a lookup in JNDI and invokes the method on the 
53      * object it found. 
54      * 
55      * @author Erik Brakkee
56      */
57     private class JndiInvocationHandler implements InvocationHandler {
58
59         @Override
60         /**
61          * @throws JndiWiringException in case the object could not be retrieved from JNDI. 
62          */
63         public Object invoke(Object aProxy, Method aMethod, Object[] aArgs)
64             throws Throwable {
65             Object svcObj = null;
66             try {
67                 InitialContext ctx = new InitialContext();
68                 svcObj = ctx.lookup(jndi);
69             } catch (NamingException e) {
70                 throw new JndiWiringException(
71                     "Error looking up object in JNDI at '" + jndi + "'", e);
72             }
73             if (svcObj == null) {
74                 throw new JndiWiringException("Object at '" + jndi +
75                     "' is null");
76             }
77             if (!clazz.isInstance(svcObj)) {
78                 throw new JndiWiringException("Object in JNDI tree at '" +
79                     jndi + "' not of type " + clazz.getName() +
80                     " but of type " + svcObj.getClass().getName());
81             }
82             T svc = (T) svcObj;
83             try {
84                 return aMethod.invoke(svc, aArgs);
85             } catch (InvocationTargetException e) {
86                 throw e.getCause();
87             }
88         }
89     }
90
91     private String jndi;
92     private Class clazz;
93
94     /**
95      * Constructs the factory.
96      * 
97      * @param aClass
98      *            Interface class of the service to proxy.
99      * @param aJndi JNDI name of the object to lookup. 
100      * 
101      */
102     public JndiProxyFactory(Class<T> aClass, String aJndi) {
103         if (!aClass.isInterface()) {
104             throw new IllegalArgumentException("Class " + aClass.getName() +
105                 " is not an interface");
106         }
107         clazz = aClass;
108         jndi = aJndi; 
109     }
110
111     /**
112      * Gets the proxy that delegates to the thread-specific instance set by
113      * {@link #set(Object)}
114      * 
115      * When at runtime the proxy cannot find lookup the object in JNDI, it 
116      * throws {@link JndiWiringException}. 
117      * 
118      * @return Proxy.
119      */
120     public T getProxy() {
121         InvocationHandler handler = new JndiInvocationHandler();
122         Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
123             new Class[] { clazz });
124         T proxy;
125         try {
126             proxy = (T) proxyClass.getConstructor(
127                 new Class[] { InvocationHandler.class }).newInstance(
128                 new Object[] { handler });
129             return proxy;
130         } catch (Exception e) {
131             throw new RuntimeException("Could not create proxy for " +
132                 clazz.getName(), e);
133         }
134     }
135 }