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.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
28 * through a lookup mechanism.
32 * @author Erik Brakkee
35 public class LookupProxyFactory<T> {
38 * Interface to lookup the object to delegate to.
40 * @author Erik Brakkee
42 public static interface Lookup {
44 * Looks up the object.
45 * @return Object (non-null)
46 * @throws Any exception in case the object cannot be found.
48 Object lookup() throws Exception;
52 * Exception thrown in case an object cannot be retrieved from JNDI.
54 * @author Erik Brakkee
56 public static class LookupException extends RuntimeException {
57 public LookupException(String aMsg, Throwable aCause) {
61 public LookupException(String aMsg) {
67 * Invocation handler that does a lookup in JNDI and invokes the method on the
70 * @author Erik Brakkee
72 private class LookupInvocationHandler implements InvocationHandler {
76 * @throws JndiWiringException in case the object could not be retrieved from JNDI.
78 public Object invoke(Object aProxy, Method aMethod, Object[] aArgs)
82 svcObj = lookup.lookup();
83 } catch (Exception e) {
84 throw new LookupException(
85 "Error looking up object", e);
88 throw new LookupException("Object at is null");
90 if (!clazz.isInstance(svcObj)) {
91 throw new LookupException("Object '" + svcObj + "' is not of type " + clazz.getName() +
92 " but of type " + svcObj.getClass().getName());
96 return aMethod.invoke(svc, aArgs);
97 } catch (InvocationTargetException e) {
103 private Lookup lookup;
107 * Constructs the factory.
110 * Interface class of the service to proxy.
111 * @param aJndi JNDI name of the object to lookup.
114 public LookupProxyFactory(Class<T> aClass, Lookup aLookup) {
115 if (!aClass.isInterface()) {
116 throw new IllegalArgumentException("Class " + aClass.getName() +
117 " is not an interface");
124 * Gets the proxy that delegates to the thread-specific instance set by
125 * {@link #set(Object)}
127 * When at runtime the proxy cannot find lookup the object in JNDI, it
128 * throws {@link LookupException}.
132 public T getProxy() {
133 InvocationHandler handler = new LookupInvocationHandler();
134 Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
135 new Class[] { clazz });
138 proxy = (T) proxyClass.getConstructor(
139 new Class[] { InvocationHandler.class }).newInstance(
140 new Object[] { handler });
142 } catch (Exception e) {
143 throw new RuntimeException("Could not create proxy for " +