1 22 package org.jboss.injection; 23 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import javax.management.MBeanServer ; 27 import javax.management.MBeanServerFactory ; 28 import javax.management.ObjectName ; 29 import org.jboss.ejb3.BeanContext; 30 import org.jboss.mx.util.MBeanProxyExt; 31 32 36 public class DependsMethodInjector implements Injector 37 { 38 Method method; 39 ObjectName on; 40 41 public DependsMethodInjector(Method method, ObjectName on) 42 { 43 this.method = method; 44 this.on = on; 45 method.setAccessible(true); 46 } 47 48 public void inject(BeanContext ctx) 49 { 50 Object instance = ctx.getInstance(); 51 inject(instance); 52 } 53 54 public void inject(Object instance) 55 { 56 Class clazz = method.getParameterTypes()[0]; 57 Object value = null; 58 59 if (clazz == ObjectName .class) 60 { 61 value = on; 62 } 63 else 64 { 65 MBeanServer server = (MBeanServer ) MBeanServerFactory.findMBeanServer(null).get(0); 66 value = MBeanProxyExt.create(clazz, on, server); 67 } 68 69 try 70 { 71 method.invoke(instance, value); 72 } 73 catch (InvocationTargetException e) 74 { 75 throw new RuntimeException (e); 76 } 77 catch (IllegalAccessException e) 78 { 79 throw new RuntimeException (e); 80 } 81 } 82 83 public Class getInjectionClass() 84 { 85 return method.getParameterTypes()[0]; 86 } 87 } 88 | Popular Tags |