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.general;
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.lang.reflect.Proxy;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
28 * Proxy factory that can provide contextual references to objects retrieved
29 * through a lookup mechanism. The returned proxies are serializable.
33 * @author Erik Brakkee
36 public class LookupProxyFactory<T> {
39 * Interface to lookup the object to delegate to.
41 * @author Erik Brakkee
43 public static interface Lookup extends Serializable {
45 * Looks up the object.
47 * @return Object (non-null)
49 * exception in case the object cannot be found.
51 Object lookup() throws Exception;
55 * Exception thrown in case an object cannot be retrieved from JNDI.
57 * @author Erik Brakkee
59 public static class LookupException extends RuntimeException {
60 public LookupException(String aMsg, Throwable aCause) {
64 public LookupException(String aMsg) {
70 * Invocation handler that does a lookup in JNDI and invokes the method on
71 * the object it found.
73 * @author Erik Brakkee
75 private static class LookupInvocationHandler<T> implements
76 InvocationHandler, Serializable {
79 private Lookup lookup;
82 * Constructs the invocation handler.
87 public LookupInvocationHandler(Class aClass, Lookup aLookup) {
94 * @throws JndiWiringException in case the object could not be retrieved from JNDI.
96 public Object invoke(Object aProxy, Method aMethod, Object[] aArgs)
100 svcObj = lookup.lookup();
101 } catch (Exception e) {
102 throw new LookupException("Error looking up object", e);
104 if (svcObj == null) {
105 throw new LookupException("Object is null");
107 if (!clazz.isInstance(svcObj)) {
108 throw new LookupException("Object '" + svcObj +
109 "' is not of type " + clazz.getName() + " but of type " +
110 svcObj.getClass().getName());
114 return aMethod.invoke(svc, aArgs);
115 } catch (InvocationTargetException e) {
121 private Lookup lookup;
125 * Constructs the factory.
128 * Interface class of the service to proxy.
130 * JNDI name of the object to lookup.
133 public LookupProxyFactory(Class<T> aClass, Lookup aLookup) {
134 if (!aClass.isInterface()) {
135 throw new IllegalArgumentException("Class " + aClass.getName() +
136 " is not an interface");
143 * Gets the proxy that delegates to the thread-specific instance set by
144 * {@link #set(Object)}
146 * When at runtime the proxy cannot find lookup the object in JNDI, it
147 * throws {@link LookupException}.
151 public T getProxy() {
152 InvocationHandler handler = new LookupInvocationHandler(clazz, lookup);
153 Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
154 new Class[] { clazz });
157 proxy = (T) proxyClass.getConstructor(
158 new Class[] { InvocationHandler.class }).newInstance(
159 new Object[] { handler });
161 } catch (Exception e) {
162 throw new RuntimeException("Could not create proxy for " +