KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Field JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13 import javax.naming.InitialContext JavaDoc;
14 import org.jboss.annotation.JndiInject;
15 import org.jboss.aop.Advisor;
16 import org.jboss.ejb3.Container;
17
18 /**
19  * Searches bean class for all @Inject and create Injectors
20  *
21  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
22  * @version $Revision: 1.1.2.2 $
23  */

24 public class JndiInjectHandler
25 {
26    public static List JavaDoc loadInjectors(Container container) throws Exception JavaDoc
27    {
28       Class JavaDoc clazz = container.getBeanClass();
29       Method JavaDoc[] methods = clazz.getMethods();
30       ArrayList JavaDoc list = new ArrayList JavaDoc();
31       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
32       for (int i = 0; i < methods.length; i++)
33       {
34          JndiInject ref = (JndiInject) ((Advisor) container).resolveAnnotation(methods[i], JndiInject.class);
35          if (ref != null)
36          {
37             if (!methods[i].getName().startsWith("set")) throw new RuntimeException JavaDoc("@Resource can only be used with a set method: " + methods[i]);
38             if (methods[i].getParameterTypes().length != 1) throw new RuntimeException JavaDoc("@Resource can only be used with a set method of one parameter: " + methods[i]);
39             String JavaDoc jndiName = methods[i].getName().substring(3);
40             if (ref.jndiName() != null && !ref.jndiName().equals(""))
41             {
42                jndiName = ref.jndiName();
43             }
44             list.add(new JndiMethodInjector(methods[i], jndiName, ctx));
45          }
46       }
47       loadFieldInjectors(clazz, container, list);
48       return list;
49    }
50
51    private static void loadFieldInjectors(Class JavaDoc clazz, Container container, ArrayList JavaDoc list) throws Exception JavaDoc
52    {
53       if (clazz == null || clazz.equals(Object JavaDoc.class)) return;
54       loadFieldInjectors(clazz.getSuperclass(), container, list);
55       Field JavaDoc[] fields = clazz.getDeclaredFields();
56       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
57       for (int i = 0; i < fields.length; i++)
58       {
59          JndiInject ref = (JndiInject) ((Advisor) container).resolveAnnotation(fields[i], JndiInject.class);
60          if (ref != null)
61          {
62             String JavaDoc jndiName = fields[i].getName();
63             if (ref.jndiName() != null && !ref.jndiName().equals(""))
64             {
65                jndiName = ref.jndiName();
66             }
67             list.add(new JndiFieldInjector(fields[i], jndiName, ctx));
68          }
69       }
70    }
71 }
72
Popular Tags