KickJava   Java API By Example, From Geeks To Geeks.

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


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.annotation.EJB;
14 import javax.annotation.EJBs;
15 import javax.naming.Context JavaDoc;
16 import javax.naming.InitialContext JavaDoc;
17 import javax.naming.LinkRef JavaDoc;
18 import org.jboss.aop.Advisor;
19 import org.jboss.ejb3.Container;
20 import org.jboss.naming.Util;
21
22 /**
23  * Searches bean class for all @Inject and create Injectors
24  *
25  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
26  * @version $Revision: 1.2.2.3 $
27  */

28 public class EJBHandler
29 {
30    public static List JavaDoc loadInjectors(Container container) throws Exception JavaDoc
31    {
32       Class JavaDoc clazz = container.getBeanClass();
33       ArrayList JavaDoc list = new ArrayList JavaDoc();
34       Context JavaDoc ctx = Util.createSubcontext(new InitialContext JavaDoc(), Container.ENC_CTX_NAME + "/env/ejb");
35       loadClassLevelEnc(clazz, container, ctx);
36       loadMethodInjectors(clazz, container, list, ctx);
37       loadFieldInjectors(clazz, container, list, ctx);
38       return list;
39    }
40
41    private static String JavaDoc getJndiName(EJB ref, Container referencingContainer, Class JavaDoc memberType)
42    {
43       if (ref.beanName().equals("") && memberType == null) throw new RuntimeException JavaDoc("not enough information for @EJB. Please fill out the beanName and businessInterface attributes");
44
45       Class JavaDoc businessInterface = memberType;
46       if (!ref.businessInterface().getName().equals(Object JavaDoc.class.getName())) businessInterface = ref.businessInterface();
47
48       if (ref.beanName().equals(""))
49       {
50          return businessInterface.getName();
51       }
52
53       return referencingContainer.getEjbLinkResolver().getEjbJndiName(ref.beanName(), businessInterface);
54    }
55
56    private static void loadClassLevelEnc(Class JavaDoc clazz, Container container, Context JavaDoc ctx) throws Exception JavaDoc
57    {
58       EJBs ref = (EJBs) ((Advisor) container).resolveAnnotation(EJBs.class);
59       if (ref != null)
60       {
61          EJB[] ejbs = ref.value();
62          for (int i = 0; i < ejbs.length; i++)
63          {
64             String JavaDoc jndiName = getJndiName(ejbs[i], container, null);
65             String JavaDoc encName = ejbs[i].name();
66             if (encName == null || encName.equals(""))
67             {
68                throw new RuntimeException JavaDoc("JBoss requires the name of the @EJB in the @EJBs: " + clazz);
69             }
70             Util.bind(ctx,
71                       encName,
72                       new LinkRef JavaDoc(jndiName));
73          }
74       }
75    }
76
77    private static void loadMethodInjectors(Class JavaDoc clazz, Container container, ArrayList JavaDoc list, Context JavaDoc ctx) throws Exception JavaDoc
78    {
79       Method JavaDoc[] methods = clazz.getMethods();
80       for (int i = 0; i < methods.length; i++)
81       {
82          EJB ref = (EJB) ((Advisor) container).resolveAnnotation(methods[i], EJB.class);
83          if (ref != null)
84          {
85             if (!methods[i].getName().startsWith("set")) throw new RuntimeException JavaDoc("@EJB can only be used with a set method: " + methods[i]);
86             String JavaDoc jndiName = getJndiName(ref, container, methods[i].getParameterTypes()[0]);
87             String JavaDoc encName = ref.name();
88             if (encName == null || encName.equals(""))
89             {
90                encName = methods[i].getName().substring(3);
91             }
92             Util.bind(ctx,
93                       encName,
94                       new LinkRef JavaDoc(jndiName));
95             list.add(new JndiMethodInjector(methods[i], encName, ctx));
96          }
97       }
98    }
99
100    private static void loadFieldInjectors(Class JavaDoc clazz, Container container, ArrayList JavaDoc list, Context JavaDoc ctx) throws Exception JavaDoc
101    {
102       if (clazz == null || clazz.equals(Object JavaDoc.class)) return;
103       loadFieldInjectors(clazz.getSuperclass(), container, list, ctx);
104       Field JavaDoc[] fields = clazz.getDeclaredFields();
105       for (int i = 0; i < fields.length; i++)
106       {
107          EJB ref = (EJB) ((Advisor) container).resolveAnnotation(fields[i], EJB.class);
108          if (ref != null)
109          {
110             String JavaDoc jndiName = getJndiName(ref, container, fields[i].getType());
111             String JavaDoc encName = ref.name();
112             if (encName == null || encName.equals(""))
113             {
114                encName = fields[i].getName();
115             }
116             Util.bind(ctx,
117                       encName,
118                       new LinkRef JavaDoc(jndiName));
119
120             list.add(new JndiFieldInjector(fields[i], encName, ctx));
121          }
122       }
123    }
124 }
125
Popular Tags