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