KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > injection > DependsFieldInjector


1 /*
2  * JBoss, the OpenSource EJB server
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.ejb3.injection;
8
9 import org.jboss.ejb3.BeanContext;
10 import org.jboss.mx.util.MBeanProxyExt;
11 import org.jboss.system.ServiceControllerMBean;
12
13 import javax.management.ObjectName JavaDoc;
14 import javax.management.MBeanServerFactory JavaDoc;
15 import javax.management.MBeanServer JavaDoc;
16 import java.lang.reflect.Method JavaDoc;
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.lang.reflect.Field JavaDoc;
19
20 /**
21  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
22  * @version $Revision: 1.1.4.2 $
23  */

24 public class DependsFieldInjector implements Injector
25 {
26    Field JavaDoc field;
27    ObjectName JavaDoc on;
28
29    public DependsFieldInjector(Field JavaDoc field, ObjectName JavaDoc on)
30    {
31       this.field = field;
32       this.on = on;
33    }
34
35    public void inject(BeanContext ctx)
36    {
37       Class JavaDoc clazz = field.getType();
38       Object JavaDoc value = null;
39
40       if (clazz == ObjectName JavaDoc.class)
41       {
42          value = on;
43       }
44       else
45       {
46          MBeanServer JavaDoc server = (MBeanServer JavaDoc)MBeanServerFactory.findMBeanServer(null).get(0);
47          value = MBeanProxyExt.create(clazz, on, server);
48       }
49
50       try
51       {
52          field.set(ctx.getInstance(), value);
53       }
54       catch (IllegalAccessException JavaDoc e)
55       {
56          throw new RuntimeException JavaDoc(e);
57       }
58    }
59 }
60
Popular Tags