KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > injection > JndiMethodInjector


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.ejb3.injection;
8
9 import org.jboss.ejb3.BeanContext;
10
11 import javax.naming.Context JavaDoc;
12 import javax.naming.NamingException JavaDoc;
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15
16 /**
17  * Comment
18  *
19  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
20  * @version $Revision: 1.2.2.2 $
21  *
22  **/

23 public class JndiMethodInjector implements Injector
24 {
25    private Method JavaDoc setMethod;
26    private String JavaDoc jndiName;
27    private Context ctx;
28
29    public JndiMethodInjector(Method JavaDoc setMethod, String JavaDoc jndiName, Context ctx)
30    {
31       this.setMethod = setMethod;
32       this.jndiName = jndiName;
33       this.ctx = ctx;
34    }
35
36    public void inject(BeanContext bctx)
37    {
38       Object JavaDoc dependency = null;
39       try
40       {
41          dependency = ctx.lookup(jndiName);
42       }
43       catch (NamingException JavaDoc e)
44       {
45          throw new RuntimeException JavaDoc("Unable to @Inject jndi dependency: " + jndiName + " into method " + setMethod, e);
46       }
47       Object JavaDoc[] args = {dependency};
48       try
49       {
50          setMethod.invoke(bctx.getInstance(), args);
51       }
52       catch (IllegalAccessException JavaDoc e)
53       {
54          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
55
}
56       catch (IllegalArgumentException JavaDoc e)
57       {
58          String JavaDoc type = "UNKNOWN";
59          if (dependency != null) type = dependency.getClass().getName();
60          throw new RuntimeException JavaDoc("Non matching type for @Inject of setter: " + setMethod + " for type: " + type, e); //To change body of catch statement use Options | File Templates.
61
}
62       catch (InvocationTargetException JavaDoc e)
63       {
64          throw new RuntimeException JavaDoc(e.getCause()); //To change body of catch statement use Options | File Templates.
65
}
66    }
67 }
68
Popular Tags