KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sourceforge.ejtools.management.util;
2
3 import java.io.InputStream JavaDoc;
4 import java.util.Hashtable JavaDoc;
5 import java.util.Properties JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
7
8 import javax.management.ObjectName JavaDoc;
9
10 import org.apache.log4j.Category;
11
12 /**
13  * @author letiembl
14  *
15  * To change this generated comment edit the template variable "typecomment":
16  * Window>Preferences>Java>Templates.
17  * To enable and disable the creation of type comments go to
18  * Window>Preferences>Java>Code Generation.
19  */

20 public abstract class JSR77
21 {
22    /** Description of the Field */
23    static Category logger = Category.getInstance(JSR77.class);
24    /** Description of the Field */
25    static Properties JavaDoc hierarchy = null;
26    /** Description of the Field */
27    static Properties JavaDoc types = null;
28    /** Description of the Field */
29    static Properties JavaDoc attributes = null;
30    /** Description of the Field */
31    static final String JavaDoc J2EE_TYPE = "j2eeType";
32    /** Description of the Field */
33    static final String JavaDoc NAME = "name";
34
35     public static boolean isGenericManagedObject(ObjectName JavaDoc objectName) {
36         boolean result = true;
37         
38         if (!hasRequiredKeys(objectName)) {
39             return false;
40         }
41         
42         try {
43             // Extract the j2ee type of the MBean
44
String JavaDoc j2eeType = objectName.getKeyProperty(JSR77.J2EE_TYPE);
45             
46             return types.containsKey(j2eeType);
47         } catch(Exception JavaDoc e) {
48             result = false;
49         }
50         
51         return result;
52     }
53
54     public static boolean hasRequiredKeys(ObjectName JavaDoc objectName) {
55         boolean result = true;
56         
57         try {
58             // Get the keys of the MBean
59
Hashtable JavaDoc keys = objectName.getKeyPropertyList();
60             
61             // Does the MBean have the "j2eeType" key ?
62
result = result && keys.containsKey(JSR77.J2EE_TYPE);
63             // Does the MBean have the "name" key ?
64
result = result && keys.containsKey(JSR77.NAME);
65         } catch(Exception JavaDoc e) {
66             result = false;
67         }
68         
69         return result;
70     }
71     
72     public static String JavaDoc extractJ2EEType(ObjectName JavaDoc objectName) {
73         try {
74             // Get the type of MBean
75
return objectName.getKeyProperty(JSR77.J2EE_TYPE);
76         } catch(Exception JavaDoc e) {
77         }
78         return null;
79     }
80
81     public static String JavaDoc extractName(ObjectName JavaDoc objectName) {
82         try {
83             // Get the name of MBean
84
return objectName.getKeyProperty(JSR77.NAME);
85         } catch(Exception JavaDoc e) {
86         }
87         return null;
88     }
89
90     public static boolean hasRequiredParentTypes(ObjectName JavaDoc objectName) {
91         boolean result = true;
92
93         if (!hasRequiredKeys(objectName)) {
94             return false;
95         }
96         
97         // Cannot check the hierarchy of vendor specific mbeans
98
// So assume that it's ok.
99
if (isGenericManagedObject(objectName)) {
100             
101         try {
102             // Get the keys of the MBean
103
Hashtable JavaDoc keys = objectName.getKeyPropertyList();
104
105             String JavaDoc parents = (String JavaDoc) hierarchy.get(keys.get(JSR77.J2EE_TYPE));
106             if (parents != null) {
107                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(parents , ",");
108                 while(st.hasMoreTokens()) {
109                     String JavaDoc parent = st.nextToken();
110                     result = result && keys.containsKey(parent);
111                 }
112             }
113         } catch(Exception JavaDoc e) {
114             result = false;
115         }
116         
117         }
118         
119         return result;
120     }
121     
122     public static String JavaDoc getRequiredAttributes(ObjectName JavaDoc objectName) {
123         String JavaDoc result = "";
124         
125         if (!hasRequiredKeys(objectName)) {
126             return "";
127         }
128         
129         if (isGenericManagedObject(objectName)) {
130         try {
131             // Get the keys of the MBean
132
Hashtable JavaDoc keys = objectName.getKeyPropertyList();
133
134             result = (String JavaDoc) attributes.get(keys.get(JSR77.J2EE_TYPE));
135         } catch(Exception JavaDoc e) {
136             result = "";
137         }
138         }
139         
140         return result;
141     }
142
143     static {
144         InputStream JavaDoc in = null;
145         
146       try
147       {
148          in = JSR77.class.getResourceAsStream("/j2eeTypes.properties");
149          if (in != null)
150          {
151             logger.debug("Found j2eeTypes.properties in classpath");
152
153             types = new Properties JavaDoc();
154             types.load(in);
155             in.close();
156          }
157
158          in = JSR77.class.getResourceAsStream("/parent-j2eeTypes.properties");
159          if (in != null)
160          {
161             logger.debug("Found parent-j2eeTypes.properties in classpath");
162
163             hierarchy = new Properties JavaDoc();
164             hierarchy.load(in);
165             in.close();
166          }
167
168          in = JSR77.class.getResourceAsStream("/attributes.properties");
169          if (in != null)
170          {
171             logger.debug("Found attributes.properties in classpath");
172
173             attributes = new Properties JavaDoc();
174             attributes.load(in);
175             in.close();
176          }
177       }
178       catch (Exception JavaDoc e)
179       {
180          e.printStackTrace();
181       }
182       logger.debug("Types and Hierarchy loaded");
183     }
184 }
185
Popular Tags