KickJava   Java API By Example, From Geeks To Geeks.

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


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.Field JavaDoc;
14
15 /**
16  * Comment
17  *
18  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
19  * @version $Revision: 1.3.2.2 $
20  *
21  **/

22 public class JndiFieldInjector implements Injector
23 {
24    private Field JavaDoc field;
25    private String JavaDoc jndiName;
26    private Context ctx;
27
28    public JndiFieldInjector(Field JavaDoc field, String JavaDoc jndiName, Context ctx)
29    {
30       this.field = field;
31       this.field.setAccessible(true);
32       this.jndiName = jndiName;
33       this.ctx = ctx;
34    }
35
36    public JndiFieldInjector(Field JavaDoc field, Context ctx)
37    {
38       this(field, field.getName(), ctx);
39    }
40
41    public void inject(BeanContext bctx)
42    {
43       Object JavaDoc dependency = null;
44       try
45       {
46          dependency = ctx.lookup(jndiName);
47       }
48       catch (NamingException JavaDoc e)
49       {
50          throw new RuntimeException JavaDoc("Unable to @Inject jndi dependency: " + jndiName + " into field " + field + " of class " + field.getDeclaringClass().getName(), e);
51       }
52       try
53       {
54          field.set(bctx.getInstance(), dependency);
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 field: " + field.toString() + " for type: " + type, e); //To change body of catch statement use Options | File Templates.
61
}
62       catch (IllegalAccessException JavaDoc e)
63       {
64          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
65
}
66    }
67 }
68
Popular Tags