1 22 package org.jboss.injection; 23 24 import org.jboss.annotation.JndiInject; 25 import org.jboss.logging.Logger; 26 import org.jboss.metamodel.descriptor.EnvironmentRefGroup; 27 import org.jboss.metamodel.descriptor.JndiRef; 28 29 import java.lang.reflect.AccessibleObject ; 30 import java.lang.reflect.Field ; 31 import java.lang.reflect.Method ; 32 import java.util.Map ; 33 34 40 public class JndiInjectHandler implements InjectionHandler 41 { 42 private static final Logger log = Logger.getLogger(JndiInjectHandler.class); 43 44 public void loadXml(EnvironmentRefGroup xml, InjectionContainer container) 45 { 46 if (xml == null) return; 47 if (xml.getJndiRefs() == null) return; 48 for (JndiRef ref : xml.getJndiRefs()) 49 { 50 if (ref.getMappedName() == null || ref.getMappedName().equals("")) 51 throw new RuntimeException ("mapped-name is required for " + ref.getJndiRefName() + " of container " + container.getIdentifier()); 52 53 String encName = "env/" + ref.getJndiRefName(); 54 if (!container.getEncInjectors().containsKey(encName)) 55 { 56 container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.getMappedName(), "jndi ref")); 57 } 58 InjectionUtil.injectionTarget(encName, ref, container, container.getEncInjections()); 59 } 60 } 61 62 public void handleClassAnnotations(Class clazz, InjectionContainer container) 63 { 64 } 66 67 public void handleMethodAnnotations(Method method, InjectionContainer container, Map <AccessibleObject , Injector> injectors) 68 { 69 JndiInject ref = method.getAnnotation(JndiInject.class); 70 if (ref != null) 71 { 72 if (!method.getName().startsWith("set")) 73 throw new RuntimeException ("@EJB can only be used with a set method: " + method); 74 String encName = InjectionUtil.getEncName(method); 75 if (!container.getEncInjectors().containsKey(encName)) 76 { 77 container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.jndiName(), "@JndiInject")); 78 } 79 injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc())); 80 } 81 } 82 83 public void handleFieldAnnotations(Field field, InjectionContainer container, Map <AccessibleObject , Injector> injectors) 84 { 85 JndiInject ref = field.getAnnotation(JndiInject.class); 86 if (ref != null) 87 { 88 String encName = InjectionUtil.getEncName(field); 89 if (!container.getEncInjectors().containsKey(encName)) 90 { 91 container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.jndiName(), "@JndiInject")); 92 } 93 injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc())); 94 } 95 } 96 } 97 | Popular Tags |