1 7 package org.jboss.ejb3.injection; 8 9 import java.lang.reflect.Field ; 10 import java.lang.reflect.Method ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 import javax.naming.InitialContext ; 14 import org.jboss.annotation.JndiInject; 15 import org.jboss.aop.Advisor; 16 import org.jboss.ejb3.Container; 17 18 24 public class JndiInjectHandler 25 { 26 public static List loadInjectors(Container container) throws Exception 27 { 28 Class clazz = container.getBeanClass(); 29 Method [] methods = clazz.getMethods(); 30 ArrayList list = new ArrayList (); 31 InitialContext ctx = new InitialContext (); 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 ("@Resource can only be used with a set method: " + methods[i]); 38 if (methods[i].getParameterTypes().length != 1) throw new RuntimeException ("@Resource can only be used with a set method of one parameter: " + methods[i]); 39 String 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 clazz, Container container, ArrayList list) throws Exception 52 { 53 if (clazz == null || clazz.equals(Object .class)) return; 54 loadFieldInjectors(clazz.getSuperclass(), container, list); 55 Field [] fields = clazz.getDeclaredFields(); 56 InitialContext ctx = new InitialContext (); 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 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 |