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.annotation.EJB; 14 import javax.annotation.EJBs; 15 import javax.naming.Context ; 16 import javax.naming.InitialContext ; 17 import javax.naming.LinkRef ; 18 import org.jboss.aop.Advisor; 19 import org.jboss.ejb3.Container; 20 import org.jboss.naming.Util; 21 22 28 public class EJBHandler 29 { 30 public static List loadInjectors(Container container) throws Exception 31 { 32 Class clazz = container.getBeanClass(); 33 ArrayList list = new ArrayList (); 34 Context ctx = Util.createSubcontext(new InitialContext (), 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 getJndiName(EJB ref, Container referencingContainer, Class memberType) 42 { 43 if (ref.beanName().equals("") && memberType == null) throw new RuntimeException ("not enough information for @EJB. Please fill out the beanName and businessInterface attributes"); 44 45 Class businessInterface = memberType; 46 if (!ref.businessInterface().getName().equals(Object .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 clazz, Container container, Context ctx) throws Exception 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 jndiName = getJndiName(ejbs[i], container, null); 65 String encName = ejbs[i].name(); 66 if (encName == null || encName.equals("")) 67 { 68 throw new RuntimeException ("JBoss requires the name of the @EJB in the @EJBs: " + clazz); 69 } 70 Util.bind(ctx, 71 encName, 72 new LinkRef (jndiName)); 73 } 74 } 75 } 76 77 private static void loadMethodInjectors(Class clazz, Container container, ArrayList list, Context ctx) throws Exception 78 { 79 Method [] 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 ("@EJB can only be used with a set method: " + methods[i]); 86 String jndiName = getJndiName(ref, container, methods[i].getParameterTypes()[0]); 87 String 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 (jndiName)); 95 list.add(new JndiMethodInjector(methods[i], encName, ctx)); 96 } 97 } 98 } 99 100 private static void loadFieldInjectors(Class clazz, Container container, ArrayList list, Context ctx) throws Exception 101 { 102 if (clazz == null || clazz.equals(Object .class)) return; 103 loadFieldInjectors(clazz.getSuperclass(), container, list, ctx); 104 Field [] 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 jndiName = getJndiName(ref, container, fields[i].getType()); 111 String 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 (jndiName)); 119 120 list.add(new JndiFieldInjector(fields[i], encName, ctx)); 121 } 122 } 123 } 124 } 125 | Popular Tags |