KickJava   Java API By Example, From Geeks To Geeks.

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


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.ejb3.EJBContainer;
25 import org.jboss.logging.Logger;
26 import org.jboss.metamodel.descriptor.InjectionTarget;
27 import org.jboss.metamodel.descriptor.Ref;
28
29 import java.lang.reflect.AccessibleObject JavaDoc;
30 import java.lang.reflect.Field JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.lang.reflect.Modifier JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Set JavaDoc;
38
39 /**
40  * Comment
41  *
42  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
43  * @version $Revision: 58121 $
44  */

45 public class InjectionUtil
46 {
47    private static final Logger log = Logger
48            .getLogger(InjectionUtil.class);
49
50
51    /**
52     * This method will take a set of XML loaded injectors and collapse them based on spec inheritance rules
53     * It will remove injectors that should not be used in the injection of the base component class.
54     *
55     * @param visitedMethods
56     * @param clazz
57     * @param xmlDefinedInjectors
58     * @param classInjectors
59     */

60    public static void collapseXmlMethodInjectors(Set JavaDoc<String JavaDoc> visitedMethods, Class JavaDoc clazz, Map JavaDoc<String JavaDoc, Map JavaDoc<AccessibleObject JavaDoc, Injector>> xmlDefinedInjectors, Map JavaDoc<AccessibleObject JavaDoc, Injector> classInjectors)
61    {
62       if (clazz == null || clazz.equals(Object JavaDoc.class))
63       {
64          return;
65       }
66       Map JavaDoc<AccessibleObject JavaDoc, Injector> xmlInjectors = xmlDefinedInjectors.get(clazz.getName());
67       if (xmlInjectors != null)
68       {
69          Method JavaDoc[] methods = clazz.getDeclaredMethods();
70          for (Method JavaDoc method : methods)
71          {
72             if (method.getParameterTypes().length != 1) continue;
73
74             if (!Modifier.isPrivate(method.getModifiers()))
75             {
76                if (visitedMethods.contains(method.getName()))
77                {
78                   xmlInjectors.remove(method); // if not private then it has been overriden
79
continue;
80                }
81                visitedMethods.add(method.getName());
82             }
83          }
84          classInjectors.putAll(xmlInjectors);
85       }
86       // recursion needs to come last as the method could be overriden and we don't want the overriding method to be ignored
87
collapseXmlMethodInjectors(visitedMethods, clazz.getSuperclass(), xmlDefinedInjectors, classInjectors);
88    }
89
90    public static void processMethodAnnotations(InjectionContainer container, Collection JavaDoc<InjectionHandler> handlers, Set JavaDoc<String JavaDoc> visitedMethods, Class JavaDoc clazz, Map JavaDoc<AccessibleObject JavaDoc, Injector> classInjectors)
91    {
92       if (clazz == null || clazz.equals(Object JavaDoc.class))
93       {
94          return;
95       }
96       Method JavaDoc[] methods = clazz.getDeclaredMethods();
97       for (Method JavaDoc method : methods)
98       {
99          if (method.getParameterTypes().length != 1) continue;
100
101          if (!Modifier.isPrivate(method.getModifiers()))
102          {
103             if (visitedMethods.contains(method.getName()))
104             {
105                continue;
106             }
107             visitedMethods.add(method.getName());
108          }
109         
110          if (handlers != null)
111          {
112             for (InjectionHandler handler : handlers)
113             {
114                handler.handleMethodAnnotations(method, container, classInjectors);
115             }
116          }
117       }
118       // recursion needs to come last as the method could be overriden and we don't want the overriding method to be ignored
119
processMethodAnnotations(container, handlers, visitedMethods, clazz.getSuperclass(), classInjectors);
120    }
121
122    public static void processFieldAnnotations(InjectionContainer container, Collection JavaDoc<InjectionHandler> handlers, Class JavaDoc clazz, Map JavaDoc<AccessibleObject JavaDoc, Injector> classInjectors)
123    {
124       if (clazz == null || clazz.equals(Object JavaDoc.class))
125       {
126          return;
127       }
128  
129       if (handlers != null)
130       {
131          Field JavaDoc[] fields = clazz.getDeclaredFields();
132          for (Field JavaDoc field : fields)
133          {
134             log.trace("process field annotation for " + field.toGenericString());
135             for (InjectionHandler handler : handlers)
136             {
137                handler.handleFieldAnnotations(field, container, classInjectors);
138             }
139          }
140       }
141       
142       // recursion needs to come last as the method could be overriden and we don't want the overriding method to be ignored
143
processFieldAnnotations(container, handlers, clazz.getSuperclass(), classInjectors);
144    }
145
146    public static void processClassAnnotations(InjectionContainer container, Collection JavaDoc<InjectionHandler> handlers, Class JavaDoc clazz)
147    {
148       if (clazz == null || clazz.equals(Object JavaDoc.class))
149       {
150          return;
151       }
152     
153       if (handlers != null)
154       {
155          for (InjectionHandler handler : handlers)
156          {
157             handler.handleClassAnnotations(clazz, container);
158          }
159       }
160       
161       // recursion needs to come last as the method could be overriden and we don't want the overriding method to be ignored
162
processClassAnnotations(container, handlers, clazz.getSuperclass());
163    }
164
165    public static Map JavaDoc<AccessibleObject JavaDoc, Injector> processAnnotations(InjectionContainer container, Collection JavaDoc<InjectionHandler> handlers, Class JavaDoc clazz)
166    {
167       Map JavaDoc<AccessibleObject JavaDoc, Injector> classInjectors = new HashMap JavaDoc<AccessibleObject JavaDoc, Injector>();
168       HashSet JavaDoc<String JavaDoc> visitedMethods = new HashSet JavaDoc<String JavaDoc>();
169       collapseXmlMethodInjectors(visitedMethods, clazz, container.getEncInjections(), classInjectors);
170
171       processClassAnnotations(container, handlers, clazz);
172       visitedMethods = new HashSet JavaDoc<String JavaDoc>();
173       processMethodAnnotations(container, handlers, visitedMethods, clazz, classInjectors);
174       processFieldAnnotations(container, handlers, clazz, classInjectors);
175       return classInjectors;
176    }
177
178    public static AccessibleObject JavaDoc findInjectionTarget(ClassLoader JavaDoc loader, InjectionTarget target)
179    {
180       Class JavaDoc clazz = null;
181       try
182       {
183          clazz = loader.loadClass(target.getTargetClass());
184       }
185       catch (ClassNotFoundException JavaDoc e)
186       {
187          throw new RuntimeException JavaDoc("<injection-target> class: " + target.getTargetClass() + " was not found nin deployment");
188       }
189
190       for (Field JavaDoc field : clazz.getDeclaredFields())
191       {
192          if (target.getTargetName().equals(field.getName())) return field;
193       }
194
195       for (java.lang.reflect.Method JavaDoc method : clazz.getDeclaredMethods())
196       {
197          if (method.getName().equals(target.getTargetName())) return method;
198       }
199
200       throw new RuntimeException JavaDoc("<injection-target> could not be found: " + target.getTargetClass() + "." + target.getTargetName());
201
202    }
203
204    public static String JavaDoc getEncName(Class JavaDoc type)
205    {
206       return "env/" + type.getName();
207    }
208
209    public static String JavaDoc getEncName(Method JavaDoc method)
210    {
211       String JavaDoc encName = method.getName().substring(3);
212       if (encName.length() > 1)
213       {
214          encName = encName.substring(0, 1).toLowerCase() + encName.substring(1);
215       }
216       else
217       {
218          encName = encName.toLowerCase();
219       }
220
221       encName = getEncName(method.getDeclaringClass()) + "/" + encName;
222       return encName;
223    }
224
225    public static String JavaDoc getEncName(Field JavaDoc field)
226    {
227       return getEncName(field.getDeclaringClass()) + "/" + field.getName();
228    }
229
230    public static Object JavaDoc getAnnotation(Class JavaDoc annotation, EJBContainer container, Class JavaDoc annotatedClass, boolean isContainer)
231    {
232       if (isContainer)
233       {
234          return container.resolveAnnotation(annotation);
235       }
236       else
237       {
238          return annotatedClass.getAnnotation(annotation);
239       }
240    }
241
242    public static Object JavaDoc getAnnotation(Class JavaDoc annotation, EJBContainer container, Method JavaDoc method, boolean isContainer)
243    {
244       if (isContainer)
245       {
246          return container.resolveAnnotation(method, annotation);
247       }
248       else
249       {
250          return method.getAnnotation(annotation);
251       }
252    }
253
254    public static Object JavaDoc getAnnotation(Class JavaDoc annotation, EJBContainer container, Field JavaDoc field, boolean isContainer)
255    {
256       if (isContainer)
257       {
258          return container.resolveAnnotation(field, annotation);
259       }
260       else
261       {
262          return field.getAnnotation(annotation);
263       }
264    }
265
266    public static Class JavaDoc injectionTarget(String JavaDoc encName, Ref ref, InjectionContainer container, Map JavaDoc<String JavaDoc, Map JavaDoc<AccessibleObject JavaDoc, Injector>> classInjectors)
267    {
268       if (ref.getInjectionTarget() != null)
269       {
270          Class JavaDoc injectionType;
271          // todo, get injection target class
272
AccessibleObject JavaDoc ao = findInjectionTarget(container.getClassloader(), ref.getInjectionTarget());
273          Map JavaDoc<AccessibleObject JavaDoc, Injector> injectors = classInjectors.get(ref.getInjectionTarget().getTargetClass());
274          if (injectors == null)
275          {
276             injectors = new HashMap JavaDoc<AccessibleObject JavaDoc, Injector>();
277             classInjectors.put(ref.getInjectionTarget().getTargetClass().trim(), injectors);
278          }
279          if (ao instanceof Field JavaDoc)
280          {
281             injectionType = ((Field JavaDoc) ao).getType();
282             injectors.put(ao, new JndiFieldInjector((Field JavaDoc) ao, encName, container.getEnc()));
283          }
284          else
285          {
286             injectionType = ((Method JavaDoc) ao).getParameterTypes()[0];
287             injectors.put(ao, new JndiMethodInjector((Method JavaDoc) ao, encName, container.getEnc()));
288          }
289          return injectionType;
290       }
291       else
292       {
293          return null;
294       }
295
296    }
297 }
298
Popular Tags