KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.injection;
23
24 import org.jboss.annotation.IgnoreDependency;
25 import org.jboss.ejb3.EJBContainer;
26 import org.jboss.logging.Logger;
27 import org.jboss.metamodel.descriptor.BaseEjbRef;
28 import org.jboss.metamodel.descriptor.EjbLocalRef;
29 import org.jboss.metamodel.descriptor.EjbRef;
30 import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
31
32 import javax.ejb.EJB JavaDoc;
33 import javax.ejb.EJBs JavaDoc;
34 import javax.naming.NameNotFoundException JavaDoc;
35 import java.lang.reflect.AccessibleObject JavaDoc;
36 import java.lang.reflect.Field JavaDoc;
37 import java.lang.reflect.Method JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Map JavaDoc;
41
42 /**
43  * Searches bean class for all @Inject and create Injectors
44  *
45  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
46  * @version $Revision: 56489 $
47  */

48 public class EJBHandler implements InjectionHandler
49 {
50    private static final Logger log = Logger.getLogger(EJBHandler.class);
51
52    protected void addDependency(String JavaDoc refName, EJBContainer refcon, InjectionContainer container)
53    {
54       // Do not depend on myself
55
if(!container.equals(refcon))
56          container.getDependencyPolicy().addDependency(refcon.getObjectName().getCanonicalName());
57    }
58
59    public void loadXml(EnvironmentRefGroup xml, InjectionContainer container)
60    {
61       if (xml != null)
62       {
63          if (xml.getEjbLocalRefs() != null) loadEjbLocalXml(xml.getEjbLocalRefs(), container);
64          if (xml.getEjbRefs() != null) loadEjbRefXml(xml.getEjbRefs(), container);
65       }
66    }
67
68    protected void loadEjbLocalXml(Collection JavaDoc<EjbLocalRef> refs, InjectionContainer container)
69    {
70       for (EjbLocalRef ref : refs)
71       {
72          String JavaDoc interfaceName = ref.getLocal();
73          String JavaDoc errorType = "<ejb-local-ref>";
74
75          ejbRefXml(ref, interfaceName, container, errorType);
76       }
77    }
78
79    protected void loadEjbRefXml(Collection JavaDoc<EjbRef> refs, InjectionContainer container)
80    {
81       for (EjbRef ref : refs)
82       {
83          String JavaDoc interfaceName = ref.getRemote();
84          String JavaDoc errorType = "<ejb-ref>";
85
86          ejbRefXml(ref, interfaceName, container, errorType);
87       }
88    }
89
90    protected void ejbRefXml(BaseEjbRef ref, String JavaDoc interfaceName, InjectionContainer container, String JavaDoc errorType)
91    {
92       String JavaDoc encName = "env/" + ref.getEjbRefName();
93       InjectionUtil.injectionTarget(encName, ref, container, container.getEncInjections());
94       if (container.getEncInjectors().containsKey(encName))
95          return;
96
97       String JavaDoc mappedName = ref.getMappedName();
98       if (mappedName != null && mappedName.equals("")) mappedName = null;
99
100       String JavaDoc link = ref.getEjbLink();
101       if (link != null && link.trim().equals("")) link = null;
102
103       Class JavaDoc refClass = null;
104
105       if (interfaceName != null)
106       {
107          try
108          {
109             refClass = container.getClassloader().loadClass(interfaceName);
110          }
111          catch (ClassNotFoundException JavaDoc e)
112          {
113             throw new RuntimeException JavaDoc("could not find " + errorType + "'s local interface " + interfaceName + " in " + container.getDeploymentDescriptorType() + " of " + container.getIdentifier());
114          }
115       }
116       
117       //----- injectors
118

119       if (mappedName == null && refClass == null && link == null)
120       {
121          // must be jboss.xml only with @EJB used to define reference. jboss.xml used to tag for ignore dependency
122
// i think it is ok to assume this because the ejb-jar.xml schema should handle any missing elements
123
}
124       else
125       {
126          ejbRefEncInjector(mappedName, encName, refClass, link, errorType, container);
127          if (ref.isIgnoreDependency())
128          {
129             log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName);
130             return;
131          }
132
133          ejbRefDependency(link, container, refClass, errorType, encName);
134       }
135    }
136
137    protected void ejbRefDependency(String JavaDoc link, InjectionContainer container, Class JavaDoc refClass, String JavaDoc errorType, String JavaDoc encName)
138    {
139       EJBContainer refcon = null;
140
141       if (refClass != null && (refClass.equals(Object JavaDoc.class) || refClass.equals(void.class))) refClass = null;
142
143       if (refClass != null)
144       {
145          if (link != null && !link.trim().equals(""))
146          {
147             refcon = (EJBContainer) container.resolveEjbContainer(link, refClass);
148             if (refcon == null)
149             {
150                String JavaDoc msg = "IGNORING DEPENDENCY: unable to find " + errorType + " of interface " + refClass.getName() + " and ejbLink of " + link + " in " + container.getDeploymentDescriptorType() + " of " + container.getIdentifier() + " it might not be deployed yet";
151                log.warn(msg);
152             }
153          }
154          else
155          {
156             try
157             {
158                refcon = (EJBContainer) container.resolveEjbContainer(refClass);
159                if (refcon == null)
160                {
161                   String JavaDoc msg = "IGNORING DEPENDENCY: unable to find " + errorType + " from interface only " + refClass.getName() + " in " + container.getDeploymentDescriptorType() + " of " + container.getIdentifier();
162                   log.warn(msg);
163                }
164             }
165             catch (NameNotFoundException JavaDoc e)
166             {
167                String JavaDoc msg = "IGNORING DEPENDENCY: unable to find " + errorType + " from interface only " + refClass.getName() + " in " + container.getDeploymentDescriptorType() + " of " + container.getIdentifier() + e.getMessage();
168                log.warn(msg);
169             }
170          }
171       }
172       else
173       {
174          String JavaDoc msg = "IGNORING DEPENDENCY: unable to resolve dependency of EJB, there is too little information";
175          log.warn(msg);
176       }
177
178       if (refcon != null)
179       {
180          addDependency(encName, refcon, container);
181       }
182    }
183
184    protected void ejbRefEncInjector(String JavaDoc mappedName, String JavaDoc encName, Class JavaDoc refClass, String JavaDoc link, String JavaDoc errorType, InjectionContainer container)
185    {
186       if (refClass != null && (refClass.equals(Object JavaDoc.class) || refClass.equals(void.class))) refClass = null;
187       if (mappedName != null && mappedName.trim().equals("")) mappedName = null;
188
189       EncInjector injector = null;
190
191       if (mappedName == null)
192       {
193          injector = new EjbEncInjector(encName, refClass, link, errorType);
194       }
195       else
196       {
197          injector = new EjbEncInjector(encName, mappedName, errorType);
198       }
199
200       container.getEncInjectors().put(encName, injector);
201    }
202
203    public static EJBContainer getEjbContainer(EJB JavaDoc ref, InjectionContainer container, Class JavaDoc memberType)
204    {
205       EJBContainer rtn = null;
206
207       if (ref.mappedName() != null && !"".equals(ref.mappedName()))
208       {
209          return null;
210       }
211
212       if (ref.beanName().equals("") && memberType == null)
213          throw new RuntimeException JavaDoc("For deployment " + container.getIdentifier() + "not enough information for @EJB. Please fill out the beanName and/or businessInterface attributes");
214
215       Class JavaDoc businessInterface = memberType;
216       if (!ref.beanInterface().getName().equals(Object JavaDoc.class.getName()))
217       {
218          businessInterface = ref.beanInterface();
219       }
220
221       if (ref.beanName().equals(""))
222       {
223          try
224          {
225             rtn = (EJBContainer) container.resolveEjbContainer(businessInterface);
226          }
227          catch (NameNotFoundException JavaDoc e)
228          {
229             log.warn("For deployment " + container.getIdentifier() + " could not find jndi binding based on interface only for @EJB(" + businessInterface.getName() + ") " + e.getMessage());
230          }
231       }
232       else
233       {
234          rtn = (EJBContainer) container.resolveEjbContainer(ref.beanName(), businessInterface);
235       }
236
237       return rtn;
238    }
239
240    public static String JavaDoc getJndiName(EJB JavaDoc ref, InjectionContainer container, Class JavaDoc memberType)
241    {
242       String JavaDoc jndiName;
243
244       if (ref.mappedName() != null && !"".equals(ref.mappedName()))
245       {
246          return ref.mappedName();
247       }
248
249       if (ref.beanName().equals("") && memberType == null)
250          throw new RuntimeException JavaDoc("For deployment " + container.getIdentifier() + "not enough information for @EJB. Please fill out the beanName and/or businessInterface attributes");
251
252       Class JavaDoc businessInterface = memberType;
253       if (!ref.beanInterface().getName().equals(Object JavaDoc.class.getName()))
254       {
255          businessInterface = ref.beanInterface();
256       }
257
258       if (ref.beanName().equals(""))
259       {
260          try
261          {
262             jndiName = container.getEjbJndiName(businessInterface);
263          }
264          catch (NameNotFoundException JavaDoc e)
265          {
266             throw new RuntimeException JavaDoc("For deployment " + container.getIdentifier() + " could not find jndi binding based on interface only for @EJB(" + businessInterface.getName() + ") " + e.getMessage());
267          }
268          if (jndiName == null)
269          {
270             throw new RuntimeException JavaDoc("For deployment " + container.getIdentifier() + " could not find jndi binding based on interface only for @EJB(" + businessInterface.getName() + ")");
271          }
272       }
273       else
274       {
275          jndiName = container.getEjbJndiName(ref.beanName(), businessInterface);
276          if (jndiName == null)
277          {
278             throw new RuntimeException JavaDoc("For EJB " + container.getIdentifier() + "could not find jndi binding based on beanName and business interface for @EJB(" + ref.beanName() + ", " + businessInterface.getName() + ")");
279          }
280       }
281
282       return jndiName;
283    }
284
285    public void handleClassAnnotations(Class JavaDoc clazz, InjectionContainer container)
286    {
287       EJBs JavaDoc ref = container.getAnnotation(EJBs JavaDoc.class, clazz);
288       if (ref != null)
289       {
290          EJB JavaDoc[] ejbs = ref.value();
291
292          for (EJB JavaDoc ejb : ejbs)
293          {
294             handleClassAnnotation(ejb, clazz, container);
295          }
296       }
297       EJB JavaDoc ejbref = container.getAnnotation(EJB JavaDoc.class, clazz);
298       if (ejbref != null) handleClassAnnotation(ejbref, clazz, container);
299    }
300
301    protected void handleClassAnnotation(EJB JavaDoc ejb, Class JavaDoc clazz, InjectionContainer container)
302    {
303       String JavaDoc encName = ejb.name();
304       if (encName == null || encName.equals(""))
305       {
306          throw new RuntimeException JavaDoc("JBoss requires the name of the @EJB in the @EJBs: " + clazz);
307       }
308       encName = "env/" + encName;
309
310       if (container.getEncInjectors().containsKey(encName)) return;
311       ejbRefEncInjector(ejb.mappedName(), encName, ejb.beanInterface(), ejb.beanName(), "@EJB", container);
312
313       // handle dependencies
314

315       if (isIgnoreDependency(container, ejb))
316          log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName);
317       else
318          ejbRefDependency(ejb.beanName(), container, ejb.beanInterface(), "@EJB", encName);
319    }
320
321    public void handleMethodAnnotations(Method JavaDoc method, InjectionContainer container, Map JavaDoc<AccessibleObject JavaDoc, Injector> injectors)
322    {
323       
324       EJB JavaDoc ref = method.getAnnotation(EJB JavaDoc.class);
325       if (ref != null)
326       {
327          if (!method.getName().startsWith("set"))
328             throw new RuntimeException JavaDoc("@EJB can only be used with a set method: " + method);
329          String JavaDoc encName = ref.name();
330          if (encName == null || encName.equals(""))
331          {
332             encName = InjectionUtil.getEncName(method);
333          }
334          else
335          {
336             encName = "env/" + encName;
337          }
338          if (!container.getEncInjectors().containsKey(encName))
339          {
340             ejbRefEncInjector(ref.mappedName(), encName, method.getParameterTypes()[0], ref.beanName(), "@EJB", container);
341             
342             if (container.getAnnotation(IgnoreDependency.class, method) == null)
343             {
344                if (isIgnoreDependency(container, ref))
345                   log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName);
346                else
347                   ejbRefDependency(ref.beanName(), container, method.getParameterTypes()[0], "@EJB", encName);
348             }
349          }
350
351          injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc()));
352       }
353    }
354
355    public void handleFieldAnnotations(Field JavaDoc field, InjectionContainer container, Map JavaDoc<AccessibleObject JavaDoc, Injector> injectors)
356    {
357       EJB JavaDoc ref = field.getAnnotation(EJB JavaDoc.class);
358       if (ref != null)
359       {
360          String JavaDoc encName = ref.name();
361          if (encName == null || encName.equals(""))
362          {
363             encName = InjectionUtil.getEncName(field);
364          }
365          else
366          {
367             encName = "env/" + encName;
368          }
369          if (!container.getEncInjectors().containsKey(encName))
370          {
371             if (container.getAnnotation(IgnoreDependency.class, field) == null)
372             {
373                if (isIgnoreDependency(container, ref))
374                   log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName);
375                else
376                   ejbRefDependency(ref.beanName(), container, field.getType(), "@EJB", encName);
377             }
378             ejbRefEncInjector(ref.mappedName(), encName, field.getType(), ref.beanName(), "@EJB", container);
379          }
380          injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc()));
381
382       }
383    }
384
385    protected boolean isIgnoreDependency(InjectionContainer container, EJB JavaDoc ref)
386    {
387       EnvironmentRefGroup refGroup = (EnvironmentRefGroup)container.getEnvironmentRefGroup();
388       
389       if (refGroup != null)
390       {
391          Iterator JavaDoc<EjbRef> ejbRefs = refGroup.getEjbRefs().iterator();
392          while (ejbRefs.hasNext())
393          {
394             EjbRef ejbRef = ejbRefs.next();
395             if (ejbRef.getEjbRefName().equals(ref.name()))
396             {
397                if (ejbRef.isIgnoreDependency())
398                   return true;
399                else
400                   return false;
401             }
402          }
403       }
404       
405       return false;
406    }
407 }
408
Popular Tags