2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.test.jndi;
18 import java.lang.reflect.InvocationHandler;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Proxy;
23 import javax.naming.InitialContext;
24 import javax.naming.NamingException;
27 * Proxy factory that can provide contextual references to objects retrieved
30 * NOTE: This class is probably better suited as a production class, not a test support class.
31 * It needs to find a new home in a Java EE production utilities library.
35 * @author Erik Brakkee
38 public class JndiProxyFactory<T> {
40 * Exception thrown in case an object cannot be retrieved from JNDI.
42 * @author Erik Brakkee
44 public static class JndiWiringException extends RuntimeException {
45 public JndiWiringException(String aMsg, Throwable aCause) {
49 public JndiWiringException(String aMsg) {
55 * Invocation handler that does a lookup in JNDI and invokes the method on the
58 * @author Erik Brakkee
60 private class JndiInvocationHandler implements InvocationHandler {
64 * @throws JndiWiringException in case the object could not be retrieved from JNDI.
66 public Object invoke(Object aProxy, Method aMethod, Object[] aArgs)
70 InitialContext ctx = new InitialContext();
71 svcObj = ctx.lookup(jndi);
72 } catch (NamingException e) {
73 throw new JndiWiringException(
74 "Error looking up object in JNDI at '" + jndi + "'", e);
77 throw new JndiWiringException("Object at '" + jndi +
80 if (!clazz.isInstance(svcObj)) {
81 throw new JndiWiringException("Object in JNDI tree at '" +
82 jndi + "' not of type " + clazz.getName() +
83 " but of type " + svcObj.getClass().getName());
87 return aMethod.invoke(svc, aArgs);
88 } catch (InvocationTargetException e) {
98 * Constructs the factory.
101 * Interface class of the service to proxy.
102 * @param aJndi JNDI name of the object to lookup.
105 public JndiProxyFactory(Class<T> aClass, String aJndi) {
106 if (!aClass.isInterface()) {
107 throw new IllegalArgumentException("Class " + aClass.getName() +
108 " is not an interface");
115 * Gets the proxy that delegates to the thread-specific instance set by
116 * {@link #set(Object)}
118 * When at runtime the proxy cannot find lookup the object in JNDI, it
119 * throws {@link JndiWiringException}.
123 public T getProxy() {
124 InvocationHandler handler = new JndiInvocationHandler();
125 Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
126 new Class[] { clazz });
129 proxy = (T) proxyClass.getConstructor(
130 new Class[] { InvocationHandler.class }).newInstance(
131 new Object[] { handler });
133 } catch (Exception e) {
134 throw new RuntimeException("Could not create proxy for " +