KickJava   Java API By Example, From Geeks To Geeks.

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


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

32 public class ResourceHandler
33 {
34    private static void loadResourcesAnnotation(Container container, Context JavaDoc resourceCtx) throws Exception JavaDoc
35    {
36       Resources resources = (Resources) ((Advisor) container).resolveAnnotation(Resources.class);
37       if (resources == null) return;
38       for (Resource ref : resources.value())
39       {
40          String JavaDoc encName = ref.name();
41          if (encName == null || encName.equals(""))
42          {
43             throw new RuntimeException JavaDoc("JBoss requires name() for class level @Resource");
44          }
45          String JavaDoc jndiName = ref.name();
46          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
47          try
48          {
49             ctx.lookup(jndiName);
50          }
51          catch (NamingException JavaDoc e)
52          {
53             jndiName = "java:/" + jndiName;
54             ctx.lookup(jndiName);
55          }
56          Util.bind(resourceCtx,
57                    encName,
58                    new LinkRef JavaDoc(jndiName));
59       }
60    }
61
62    public static List JavaDoc loadInjectors(Container container) throws Exception JavaDoc
63    {
64       Class JavaDoc clazz = container.getBeanClass();
65       Method JavaDoc[] methods = clazz.getMethods();
66       ArrayList JavaDoc list = new ArrayList JavaDoc();
67       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
68       Context JavaDoc resourceCtx = Util.createSubcontext(new InitialContext JavaDoc(), Container.ENC_CTX_NAME + "/env/resource");
69       for (int i = 0; i < methods.length; i++)
70       {
71          Resource ref = (Resource) ((Advisor) container).resolveAnnotation(methods[i], Resource.class);
72          if (ref != null)
73          {
74             if (!methods[i].getName().startsWith("set")) throw new RuntimeException JavaDoc("@Resource can only be used with a set method: " + methods[i]);
75             if (methods[i].getParameterTypes().length != 1) throw new RuntimeException JavaDoc("@Resource can only be used with a set method of one parameter: " + methods[i]);
76             Class JavaDoc type = methods[i].getParameterTypes()[0];
77             if (!ref.type().equals(Object JavaDoc.class))
78             {
79                type = ref.type();
80             }
81             String JavaDoc encName = ref.name();
82             if (encName == null || encName.equals(""))
83             {
84                encName = methods[i].getName().substring(3);
85             }
86             if (type.equals(UserTransaction JavaDoc.class))
87             {
88                list.add(new UserTransactionMethodInjector(methods[i], container));
89             }
90             else if (type.equals(TimerService JavaDoc.class))
91             {
92                list.add(new TimerServiceMethodInjector(methods[i], container));
93             }
94             else if (EJBContext JavaDoc.class.isAssignableFrom(type))
95             {
96                list.add(new EJBContextMethodInjector(methods[i]));
97             }
98             else
99             {
100                String JavaDoc jndiName = ref.name();
101                if (jndiName == null || jndiName.equals(""))
102                {
103                   jndiName = methods[i].getName().substring(3);
104                }
105                try
106                {
107                   ctx.lookup(jndiName);
108                }
109                catch (NamingException JavaDoc e)
110                {
111                   jndiName = "java:/" + jndiName;
112                   ctx.lookup(jndiName);
113                }
114                Util.bind(resourceCtx,
115                          encName,
116                          new LinkRef JavaDoc(jndiName));
117                list.add(new JndiMethodInjector(methods[i], jndiName, ctx));
118             }
119          }
120       }
121       loadResourcesAnnotation(container, resourceCtx);
122       loadFieldInjectors(clazz, container, list, resourceCtx);
123       return list;
124    }
125
126    private static void loadFieldInjectors(Class JavaDoc clazz, Container container, ArrayList JavaDoc list, Context JavaDoc resourceCtx) throws Exception JavaDoc
127    {
128       if (clazz == null || clazz.equals(java.lang.Object JavaDoc.class)) return;
129       loadFieldInjectors(clazz.getSuperclass(), container, list, resourceCtx);
130       Field JavaDoc[] fields = clazz.getDeclaredFields();
131       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
132       for (int i = 0; i < fields.length; i++)
133       {
134          Resource ref = (Resource) ((Advisor) container).resolveAnnotation(fields[i], Resource.class);
135          if (ref != null)
136          {
137             fields[i].setAccessible(true);
138             Class JavaDoc type = fields[i].getType();
139             if (!ref.type().equals(Object JavaDoc.class))
140             {
141                type = ref.type();
142             }
143             String JavaDoc encName = ref.name();
144             if (encName == null || encName.equals(""))
145             {
146                encName = fields[i].getName();
147             }
148             if (type.equals(UserTransaction JavaDoc.class))
149             {
150                list.add(new UserTransactionFieldInjector(fields[i], container));
151             }
152             else if (type.equals(TimerService JavaDoc.class))
153             {
154                list.add(new TimerServiceFieldInjector(fields[i], container));
155             }
156             else if (EJBContext JavaDoc.class.isAssignableFrom(type))
157             {
158                list.add(new EJBContextFieldInjector(fields[i]));
159             }
160             else
161             {
162                String JavaDoc jndiName = ref.name();
163                if (jndiName == null || jndiName.equals(""))
164                {
165                   jndiName = fields[i].getName();
166                }
167                try
168                {
169                   ctx.lookup(jndiName);
170                }
171                catch (NamingException JavaDoc e)
172                {
173                   jndiName = "java:/" + jndiName;
174                   ctx.lookup(jndiName);
175                }
176                Util.bind(resourceCtx,
177                          encName,
178                          new LinkRef JavaDoc(jndiName));
179                list.add(new JndiFieldInjector(fields[i], jndiName, ctx));
180             }
181          }
182       }
183    }
184 }
185
Popular Tags