KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > ejtools > management > util > MEJB


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package net.sourceforge.ejtools.management.util;
8
9 import java.io.InputStream JavaDoc;
10 import java.util.Properties JavaDoc;
11 import java.util.Set JavaDoc;
12
13 import javax.management.ObjectName JavaDoc;
14 import javax.management.j2ee.Management JavaDoc;
15 import javax.management.j2ee.ManagementHome JavaDoc;
16 import javax.naming.Context JavaDoc;
17 import javax.naming.InitialContext JavaDoc;
18 import javax.rmi.PortableRemoteObject JavaDoc;
19
20 import org.ejtools.util.JNDI;
21 import org.apache.log4j.Category;
22
23 /**
24  * Description of the Class
25  *
26  * @author letiembl
27  * @created 19 juin 2002
28  */

29 public abstract class MEJB
30 {
31    /** Description of the Field */
32    static Category logger = Category.getInstance(MEJB.class);
33    /** Description of the Field */
34    static String JavaDoc name = "ejb/mgmt/MEJB";
35
36
37    /**
38     * Description of the Method
39     *
40     * @return Description of the Return Value
41     */

42    public static Management JavaDoc createMEJB()
43    {
44       try
45       {
46          return lookupMEJB().create();
47       }
48       catch (Exception JavaDoc e)
49       {
50          e.printStackTrace();
51       }
52       return null;
53    }
54
55
56    /**
57     * Gets the defaultDomain attribute of the MEJB class
58     *
59     * @return The defaultDomain value
60     */

61    public static String JavaDoc getDefaultDomain()
62    {
63       try
64       {
65          Management JavaDoc mejb = createMEJB();
66          return mejb.getDefaultDomain();
67       }
68       catch (Exception JavaDoc e)
69       {
70          e.printStackTrace();
71       }
72       return null;
73    }
74
75
76    /**
77     * Gets the mBeanCount attribute of the MEJB class
78     *
79     * @return The mBeanCount value
80     */

81    public static Integer JavaDoc getMBeanCount()
82    {
83       try
84       {
85          Management JavaDoc mejb = createMEJB();
86          return mejb.getMBeanCount();
87       }
88       catch (Exception JavaDoc e)
89       {
90          e.printStackTrace();
91       }
92       return null;
93    }
94
95
96    /**
97     * Description of the Method
98     *
99     * @return Description of the Return Value
100     */

101    public static Set JavaDoc listManagedObject()
102    {
103       try
104       {
105          Management JavaDoc mejb = createMEJB();
106 // ObjectName pattern = new ObjectName(getDefaultDomain() + ":*");
107
ObjectName JavaDoc pattern = new ObjectName JavaDoc("*:*");
108          return mejb.queryNames(pattern, null);
109       }
110       catch (Exception JavaDoc e)
111       {
112          e.printStackTrace();
113       }
114       return null;
115    }
116
117
118    /**
119     * Description of the Method
120     *
121     * @return Description of the Return Value
122     */

123    public static ManagementHome JavaDoc lookupMEJB()
124    {
125       try
126       {
127          // Lookup the MEJB
128
Context JavaDoc context = new InitialContext JavaDoc();
129          Object JavaDoc ref = context.lookup(name);
130          ManagementHome JavaDoc home = (ManagementHome JavaDoc) PortableRemoteObject.narrow(ref, ManagementHome JavaDoc.class);
131
132          return home;
133       }
134       catch (Exception JavaDoc e)
135       {
136          e.printStackTrace();
137       }
138       return null;
139    }
140
141     public static String JavaDoc getMEJBBinding() {
142         return name;
143     }
144
145    static
146    {
147       try
148       {
149          // Load the jndi properties file
150
JNDI.setJNDIProperties();
151
152          InputStream JavaDoc in = MEJB.class.getResourceAsStream("/mejb.properties");
153
154          if (in != null)
155          {
156             logger.debug("Found mejb.properties in classpath");
157
158             // Load the mejb.properties file
159
Properties JavaDoc props = new Properties JavaDoc();
160             props.load(in);
161             in.close();
162
163             name = props.getProperty("mejb.name");
164          }
165       }
166       catch (Exception JavaDoc e)
167       {
168          e.printStackTrace();
169       }
170       logger.debug("JNDI binding is " + name);
171    }
172 }
173
Popular Tags