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.Resource; 14 import javax.annotation.Resources; 15 import javax.ejb.EJBContext ; 16 import javax.ejb.TimerService ; 17 import javax.naming.Context ; 18 import javax.naming.InitialContext ; 19 import javax.naming.LinkRef ; 20 import javax.naming.NamingException ; 21 import javax.transaction.UserTransaction ; 22 import org.jboss.aop.Advisor; 23 import org.jboss.ejb3.Container; 24 import org.jboss.naming.Util; 25 26 32 public class ResourceHandler 33 { 34 private static void loadResourcesAnnotation(Container container, Context resourceCtx) throws Exception 35 { 36 Resources resources = (Resources) ((Advisor) container).resolveAnnotation(Resources.class); 37 if (resources == null) return; 38 for (Resource ref : resources.value()) 39 { 40 String encName = ref.name(); 41 if (encName == null || encName.equals("")) 42 { 43 throw new RuntimeException ("JBoss requires name() for class level @Resource"); 44 } 45 String jndiName = ref.name(); 46 InitialContext ctx = new InitialContext (); 47 try 48 { 49 ctx.lookup(jndiName); 50 } 51 catch (NamingException e) 52 { 53 jndiName = "java:/" + jndiName; 54 ctx.lookup(jndiName); 55 } 56 Util.bind(resourceCtx, 57 encName, 58 new LinkRef (jndiName)); 59 } 60 } 61 62 public static List loadInjectors(Container container) throws Exception 63 { 64 Class clazz = container.getBeanClass(); 65 Method [] methods = clazz.getMethods(); 66 ArrayList list = new ArrayList (); 67 InitialContext ctx = new InitialContext (); 68 Context resourceCtx = Util.createSubcontext(new InitialContext (), 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 ("@Resource can only be used with a set method: " + methods[i]); 75 if (methods[i].getParameterTypes().length != 1) throw new RuntimeException ("@Resource can only be used with a set method of one parameter: " + methods[i]); 76 Class type = methods[i].getParameterTypes()[0]; 77 if (!ref.type().equals(Object .class)) 78 { 79 type = ref.type(); 80 } 81 String encName = ref.name(); 82 if (encName == null || encName.equals("")) 83 { 84 encName = methods[i].getName().substring(3); 85 } 86 if (type.equals(UserTransaction .class)) 87 { 88 list.add(new UserTransactionMethodInjector(methods[i], container)); 89 } 90 else if (type.equals(TimerService .class)) 91 { 92 list.add(new TimerServiceMethodInjector(methods[i], container)); 93 } 94 else if (EJBContext .class.isAssignableFrom(type)) 95 { 96 list.add(new EJBContextMethodInjector(methods[i])); 97 } 98 else 99 { 100 String 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 e) 110 { 111 jndiName = "java:/" + jndiName; 112 ctx.lookup(jndiName); 113 } 114 Util.bind(resourceCtx, 115 encName, 116 new LinkRef (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 clazz, Container container, ArrayList list, Context resourceCtx) throws Exception 127 { 128 if (clazz == null || clazz.equals(java.lang.Object .class)) return; 129 loadFieldInjectors(clazz.getSuperclass(), container, list, resourceCtx); 130 Field [] fields = clazz.getDeclaredFields(); 131 InitialContext ctx = new InitialContext (); 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 type = fields[i].getType(); 139 if (!ref.type().equals(Object .class)) 140 { 141 type = ref.type(); 142 } 143 String encName = ref.name(); 144 if (encName == null || encName.equals("")) 145 { 146 encName = fields[i].getName(); 147 } 148 if (type.equals(UserTransaction .class)) 149 { 150 list.add(new UserTransactionFieldInjector(fields[i], container)); 151 } 152 else if (type.equals(TimerService .class)) 153 { 154 list.add(new TimerServiceFieldInjector(fields[i], container)); 155 } 156 else if (EJBContext .class.isAssignableFrom(type)) 157 { 158 list.add(new EJBContextFieldInjector(fields[i])); 159 } 160 else 161 { 162 String 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 e) 172 { 173 jndiName = "java:/" + jndiName; 174 ctx.lookup(jndiName); 175 } 176 Util.bind(resourceCtx, 177 encName, 178 new LinkRef (jndiName)); 179 list.add(new JndiFieldInjector(fields[i], jndiName, ctx)); 180 } 181 } 182 } 183 } 184 } 185 | Popular Tags |