1 22 package org.jboss.injection; 23 24 import java.lang.reflect.Field ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.AccessibleObject ; 27 import java.util.Map ; 28 import javax.management.ObjectName ; 29 import javax.management.MalformedObjectNameException ; 30 31 import org.jboss.annotation.ejb.Depends; 32 import org.jboss.logging.Logger; 33 import org.jboss.metamodel.descriptor.EnvironmentRefGroup; 34 35 39 public class DependsHandler implements InjectionHandler 40 { 41 private static final Logger log = Logger.getLogger(DependsHandler.class); 42 43 public void loadXml(EnvironmentRefGroup xml, InjectionContainer container) 44 { 45 } 46 47 public void handleMethodAnnotations(Method method, InjectionContainer container, Map <AccessibleObject , Injector> injectors) 48 { 49 Depends dep = container.getAnnotation(Depends.class, method.getDeclaringClass(), method); 50 if (dep != null) 51 { 52 if (!method.getName().startsWith("set")) 53 throw new RuntimeException ("@EJB can only be used with a set method: " + method); 54 String [] names = dep.value(); 55 if (names.length != 1) 56 throw new RuntimeException ("@Depends on a field can only take one object name: " + method); 57 ObjectName on = null; 58 try 59 { 60 on = new ObjectName (names[0]); 61 } 62 catch (MalformedObjectNameException e) 63 { 64 throw new RuntimeException (e); 65 } 66 injectors.put(method, new DependsMethodInjector(method, on)); 67 container.getDependencyPolicy().addDependency(names[0]); 68 } 69 } 70 71 public void handleFieldAnnotations(Field field, InjectionContainer container, Map <AccessibleObject , Injector> injectors) 72 { 73 Depends dep = container.getAnnotation(Depends.class, field.getDeclaringClass(), field); 74 if (dep != null) 75 { 76 String [] names = dep.value(); 77 if (names.length != 1) 78 throw new RuntimeException ("@Depends on a field can only take one object name: " + field); 79 ObjectName on = null; 80 try 81 { 82 on = new ObjectName (names[0]); 83 } 84 catch (MalformedObjectNameException e) 85 { 86 throw new RuntimeException (e); 87 } 88 injectors.put(field, new DependsFieldInjector(field, on)); 89 container.getDependencyPolicy().addDependency(names[0]); 90 } 91 } 92 93 public void handleClassAnnotations(Class clazz, InjectionContainer container) 94 { 95 Depends dep = (Depends)container.getAnnotation(Depends.class, clazz); 96 if (dep == null) return; 97 for (String dependency : dep.value()) 98 { 99 container.getDependencyPolicy().addDependency(dependency); 100 } 101 } 102 103 } 104 | Popular Tags |