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.support.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
32 * @author Erik Brakkee
35 public class JndiProxyFactory<T> {
37 * Exception thrown in case an object cannot be retrieved from JNDI.
39 * @author Erik Brakkee
41 public static class JndiWiringException extends RuntimeException {
42 public JndiWiringException(String aMsg, Throwable aCause) {
46 public JndiWiringException(String aMsg) {
52 * Invocation handler that does a lookup in JNDI and invokes the method on the
55 * @author Erik Brakkee
57 private class JndiInvocationHandler implements InvocationHandler {
61 * @throws JndiWiringException in case the object could not be retrieved from JNDI.
63 public Object invoke(Object aProxy, Method aMethod, Object[] aArgs)
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);
74 throw new JndiWiringException("Object at '" + jndi +
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());
84 return aMethod.invoke(svc, aArgs);
85 } catch (InvocationTargetException e) {
95 * Constructs the factory.
98 * Interface class of the service to proxy.
99 * @param aJndi JNDI name of the object to lookup.
102 public JndiProxyFactory(Class<T> aClass, String aJndi) {
103 if (!aClass.isInterface()) {
104 throw new IllegalArgumentException("Class " + aClass.getName() +
105 " is not an interface");
112 * Gets the proxy that delegates to the thread-specific instance set by
113 * {@link #set(Object)}
115 * When at runtime the proxy cannot find lookup the object in JNDI, it
116 * throws {@link JndiWiringException}.
120 public T getProxy() {
121 InvocationHandler handler = new JndiInvocationHandler();
122 Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
123 new Class[] { clazz });
126 proxy = (T) proxyClass.getConstructor(
127 new Class[] { InvocationHandler.class }).newInstance(
128 new Object[] { handler });
130 } catch (Exception e) {
131 throw new RuntimeException("Could not create proxy for " +