KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > MonitoredObjectType


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /**
25  * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
26  *
27  * Copyright 2001-2002 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  */

31 package com.sun.enterprise.admin.monitor;
32
33 import java.util.HashMap JavaDoc;
34 import com.sun.enterprise.config.serverbeans.ServerTags;
35
36 //i18n import
37
import com.sun.enterprise.util.i18n.StringManager;
38
39 /**
40  * MonitoredObjectType represents the type of a monitored object. Many of
41  * monitored objects have same properties, even while they monitor different
42  * user objects. For example - application and standalone ejb module are both
43  * nothing more than containers of other objects -- application contains web
44  * module and ejb modules, whereas standalone ejb module contains beans of
45  * various types. This object facilitates use of same GenericMonitorMBean
46  * as MBean for both of them, but still distinguishes them.
47  * @see com.sun.enterprise.admin.monitor.GenericMonitorMBean
48  */

49 public class MonitoredObjectType {
50
51     /**
52      * A map to store all objects of type MonitoredObjectType using their string
53      * representation as key.
54      */

55     private static final HashMap JavaDoc objectMap = new HashMap JavaDoc();
56
57     /**
58      * A root monitored object type
59      */

60     public static final MonitoredObjectType ROOT =
61             new MonitoredObjectType("root", true);
62
63     /**
64      * A monitored object type of Application
65      */

66     public static final MonitoredObjectType APPLICATION =
67             new MonitoredObjectType("application");
68
69     /**
70      * A monitored object type of EJB Module. This denotes ejb modules within
71      * an application. For stand alone ejb modules, please use the type
72      * MonitoredObjectType.STANDALONE_EJBMODULE
73      */

74     public static final MonitoredObjectType EJBMODULE =
75             new MonitoredObjectType("ejb-module");
76
77     /**
78      * A monitored object type of standalone EJB Module. This denotes ejb
79      * modules not deployed as part of any application. For ejb modules deployed
80      * as part of applications, please use the type MonitoredObjectType.EJBMODULE
81      */

82     public static final MonitoredObjectType STANDALONE_EJBMODULE =
83             new MonitoredObjectType("standalone-ejb-module");
84
85     /**
86      * A monitored object type of Web Module. This denotes web module within
87      * an application. For stand alone web modules, please use the type
88      * MonitoredObjectType.STANDALONE_WEBMODULE
89      */

90     public static final MonitoredObjectType WEBMODULE =
91             new MonitoredObjectType("web-module");
92
93     /**
94      * A monitored object type of standalone Web Module. This denotes web
95      * modules not deployed as part of any application. For web modules deployed
96      * as part of applications, please use the type MonitoredObjectType.WEBMODULE
97      */

98     public static final MonitoredObjectType STANDALONE_WEBMODULE =
99             new MonitoredObjectType("standalone-web-module");
100
101     /**
102      * A monitored object type of stateless session bean
103      */

104     public static final MonitoredObjectType STATELESS_BEAN =
105             new MonitoredObjectType("stateless-session-bean");
106
107     /**
108      * A monitored object type of stateful session bean
109      */

110     public static final MonitoredObjectType STATEFUL_BEAN =
111             new MonitoredObjectType("stateful-session-bean");
112
113     /**
114      * A monitored object type of entity bean
115      */

116     public static final MonitoredObjectType ENTITY_BEAN =
117             new MonitoredObjectType("entity-bean");
118
119     /**
120      * A monitored object type of message driven bean
121      */

122     public static final MonitoredObjectType MESSAGE_DRIVEN_BEAN =
123             new MonitoredObjectType("message-driven-bean");
124
125     /**
126      * A monitored object type of bean pool. For every bean, there can be atmost
127      * one object of type bean pool monitor.
128      */

129     public static final MonitoredObjectType BEAN_POOL =
130             new MonitoredObjectType("bean-pool", true);
131
132     /**
133      * A monitored object type of bean cache. For every bean, there can be
134      * atmost one object of type bean cache monitor.
135      */

136     public static final MonitoredObjectType BEAN_CACHE =
137             new MonitoredObjectType("bean-cache", true);
138
139     /**
140      * A monitored object type of bean method
141      */

142     public static final MonitoredObjectType BEAN_METHOD =
143             new MonitoredObjectType("bean-method");
144
145
146     /**
147      * A monitored object type of http-server (core)
148      */

149     public static final MonitoredObjectType HTTP_SERVER =
150             new MonitoredObjectType("http-server", true);
151
152
153     /**
154      * A monitored object type of virtual-server
155      */

156     public static final MonitoredObjectType VIRTUAL_SERVER =
157             new MonitoredObjectType("virtual-server", true);
158
159
160     /**
161      * A monitored object type of process
162      */

163     public static final MonitoredObjectType PROCESS =
164             new MonitoredObjectType("process", true);
165
166     /**
167      * A monitored object type of jts monitor. There is only one instance of
168      * jts monitor object.
169      */

170     public static final MonitoredObjectType TXNMGR =
171             new MonitoredObjectType("transaction-service", true);
172
173     /**
174      * A monitored object type of iiop service
175      */

176     public static final MonitoredObjectType IIOP_SERVICE =
177             new MonitoredObjectType("iiop-service", true);
178
179     /**
180      * A monitored object type of orb.
181      */

182     public static final MonitoredObjectType ORB = new MonitoredObjectType("orb");
183
184     /**
185      * A monitored object type of orb connection
186      */

187     public static final MonitoredObjectType ORB_CONNECTION =
188             new MonitoredObjectType("orb-connection", true);
189
190     /**
191      * A monitored object type of orb thread pool
192      */

193     public static final MonitoredObjectType ORB_THREAD_POOL =
194             new MonitoredObjectType("orb-thread-pool", true);
195             
196     /**
197      * A monitored object type of resources
198      */

199     public static final MonitoredObjectType RESOURCES =
200             new MonitoredObjectType("resources", true);
201             
202     /**
203      * A monitored object type of jdbc-connection-pool
204      */

205     public static final MonitoredObjectType JDBC_CONN_POOL =
206             new MonitoredObjectType("jdbc-connection-pool", false);
207
208     /**
209      * value of this object as a string
210      */

211     private String JavaDoc typeName;
212
213     /**
214      * Denotes whether this type allows more than one instance at any level.
215      */

216     private boolean isSingleton;
217
218     /**
219      * Number of components that need this type to be enabled. If the number is
220      * more than zero then monitoring is started on MBeans of this type.
221      */

222     private int enableCount = 0;
223
224     /**
225      * Creates a new instance of MonitoredObjectType using specified string type
226      * @param type string representing the name of monitored object type
227      */

228     private MonitoredObjectType(String JavaDoc type) {
229         this(type, false);
230     }
231
232     /**
233      * Creates a new instance of MonitoredObjectType using specified string type
234      * and specified flag for singleton
235      * @param type string representing the name of monitored object type
236      * @param isSingleton denotes whether this type of monitored object has
237      * only one instance (in its context)
238      */

239     private MonitoredObjectType(String JavaDoc type, boolean isSingleton) {
240         this.typeName = type;
241         this.isSingleton = isSingleton;
242         objectMap.put(this.typeName, this);
243     }
244
245     /**
246      * Get type of this "MonitoredObjectType" as string
247      * @return Monitored object type as string
248      */

249     public String JavaDoc getTypeName() {
250         return typeName;
251     }
252
253     /**
254      * Is instance of this type of MonitorMBean singleton. For example, there
255      * can only be one pool for every stateless session bean, so a
256      * MonitoredObjectType of type MonitoredObjectType.BEAN_POOL is a singleton.
257      * @return true if this type of object can have atmost one instance within
258      * its context, false otherwise.
259      */

260     public boolean isSingleton() {
261         return isSingleton;
262     }
263
264     /**
265      * A string representation. The return value of this method is
266      * same as that of method getTypeName.
267      * @return A string representation of this MonitoredObjectType
268      */

269     public String JavaDoc toString() {
270         return typeName;
271     }
272
273     /**
274      * Is monitoring enabled for this type.
275      */

276     public boolean isMonitoringEnabled() {
277         return (enableCount > 0);
278     }
279
280     /**
281      * Get a MonitoredObjectType instance for the specified string type.
282      * @param typeName string representing MonitoredObjectType
283      * @throws IllegalArgumentException if the specified type name is not
284      * known.
285      */

286     public static MonitoredObjectType getMonitoredObjectType(String JavaDoc typeName) {
287         MonitoredObjectType type = getMonitoredObjectTypeOrNull(typeName);
288         if (type == null) {
289             String JavaDoc msg = localStrings.getString( "admin.monitor.unknown_type_name", typeName );
290             throw new IllegalArgumentException JavaDoc( msg );
291         }
292         return type;
293     }
294
295     /**
296      * Get a MonitoredObjectType instance for the specified string type. If the
297      * specified type is not known, the method returns null.
298      * @param typeName string representing MonitoredObjectType
299      */

300     static MonitoredObjectType getMonitoredObjectTypeOrNull(String JavaDoc typeName) {
301         MonitoredObjectType type = null;
302         if (objectMap != null && typeName != null) {
303             type = (MonitoredObjectType)objectMap.get(typeName);
304         }
305         return type;
306     }
307
308     /**
309      * List of monitored object types for ejb container
310      */

311     public static final MonitoredObjectType[] EJB_TYPES =
312             new MonitoredObjectType[] {
313                     MonitoredObjectType.ROOT,
314                     MonitoredObjectType.APPLICATION,
315                     MonitoredObjectType.EJBMODULE,
316                     MonitoredObjectType.STANDALONE_EJBMODULE,
317                     MonitoredObjectType.STATELESS_BEAN,
318                     MonitoredObjectType.STATEFUL_BEAN,
319                     MonitoredObjectType.ENTITY_BEAN,
320                     MonitoredObjectType.BEAN_POOL,
321                     MonitoredObjectType.BEAN_CACHE,
322                     MonitoredObjectType.BEAN_METHOD};
323
324     /**
325      * List of monitored object types for mdb container
326      */

327     public static final MonitoredObjectType[] MDB_TYPES =
328             new MonitoredObjectType[] {
329                     MonitoredObjectType.ROOT,
330                     MonitoredObjectType.APPLICATION,
331                     MonitoredObjectType.EJBMODULE,
332                     MonitoredObjectType.STANDALONE_EJBMODULE,
333                     MonitoredObjectType.MESSAGE_DRIVEN_BEAN,
334                     MonitoredObjectType.BEAN_POOL,
335                     MonitoredObjectType.BEAN_CACHE,
336                     MonitoredObjectType.BEAN_METHOD};
337
338     /**
339      * List of monitored object types for orb
340      */

341     public static final MonitoredObjectType[] ORB_TYPES =
342             new MonitoredObjectType[] {
343                     MonitoredObjectType.ROOT,
344                     MonitoredObjectType.IIOP_SERVICE,
345                     MonitoredObjectType.ORB,
346                     MonitoredObjectType.ORB_CONNECTION,
347                     MonitoredObjectType.ORB_THREAD_POOL};
348
349     /**
350      * List of monitored object types for JTS
351      */

352     public static final MonitoredObjectType[] JTS_TYPES =
353             new MonitoredObjectType[] {
354                     MonitoredObjectType.ROOT,
355                     MonitoredObjectType.TXNMGR};
356
357     /**
358      * Enable monitoring for specified types. Note that a type can be enabled
359      * more than once as it can be used by different monitorable components.
360      */

361     private static synchronized void enableTypes(MonitoredObjectType[] types) {
362         int size = types.length;
363         for (int i = 0; i < size; i++) {
364             types[i].enableCount++;
365         }
366     }
367
368     /**
369      * Disble monitoring for specified types. Note that a type can be disbled
370      * more than once as it can be used by different monitorable components.
371      */

372     private static synchronized void disableTypes(MonitoredObjectType[] types) {
373         int size = types.length;
374         for (int i = 0; i < size; i++) {
375             if (types[i].enableCount > 0) {
376                 types[i].enableCount--;
377             } else {
378                 String JavaDoc msg = localStrings.getString( "admin.monitor.monitored_object_type_already_disabled", types[i].toString() );
379                 throw new IllegalStateException JavaDoc( msg );
380             }
381         }
382     }
383
384     /**
385      * Tracks whether monitoring is enabled on ejb container
386      */

387     private static boolean ejbMonitoringEnabled = false;
388
389     /**
390      * Tracks whether monitoring is enabled on mdb container
391      */

392     private static boolean mdbMonitoringEnabled = false;
393
394     /**
395      * Tracks whether monitoring is enabled on orb
396      */

397     private static boolean orbMonitoringEnabled = false;
398
399     /**
400      * Tracks whether monitoring is enabled on JTS
401      */

402     private static boolean jtsMonitoringEnabled = false;
403
404     // i18n StringManager
405
private static StringManager localStrings =
406         StringManager.getManager( MonitoredObjectType.class );
407
408     /**
409      * Set monitoring enabled or disabled for ejb container
410      * @param enable if true monitoring is enabled, otherwise it is disabled
411      */

412     public static void setEjbMonitoringEnabled(boolean enable) {
413         ejbMonitoringEnabled = setMonitoringEnabled(EJB_TYPES,
414                 ejbMonitoringEnabled, enable);
415     }
416
417     /**
418      * Set monitoring enabled or disabled for mdb container
419      * @param enable if true monitoring is enabled, otherwise it is disabled
420      */

421     public static void setMdbMonitoringEnabled(boolean enable) {
422         mdbMonitoringEnabled = setMonitoringEnabled(MDB_TYPES,
423                 mdbMonitoringEnabled, enable);
424     }
425
426     /**
427      * Set monitoring enabled or disabled for orb
428      * @param enable if true monitoring is enabled, otherwise it is disabled
429      */

430     public static void setOrbMonitoringEnabled(boolean enable) {
431         orbMonitoringEnabled = setMonitoringEnabled(ORB_TYPES,
432                 orbMonitoringEnabled, enable);
433     }
434
435     /**
436      * Set monitoring enabled or disabled for jts
437      * @param enable if true monitoring is enabled, otherwise it is disabled
438      */

439     public static void setJtsMonitoringEnabled(boolean enable) {
440         jtsMonitoringEnabled = setMonitoringEnabled(JTS_TYPES,
441                 jtsMonitoringEnabled, enable);
442     }
443
444     /**
445      * Set monitoring enabled or disabled on specified component.
446      * @param comp the component name - this is the xpath of the component
447      * that has monitoring enabled.
448      * @param enable turn monitoring on (if true) or off (if false)
449      */

450     static void setMonitoringEnabled(String JavaDoc comp, boolean enable) {
451         if (ServerTags.EJB_CONTAINER.equals(comp)) {
452             setEjbMonitoringEnabled(enable);
453         } else if (ServerTags.MDB_CONTAINER.equals(comp)) {
454             setMdbMonitoringEnabled(enable);
455         } else if (ServerTags.ORB.equals(comp)) {
456             setOrbMonitoringEnabled(enable);
457         } else if (ServerTags.TRANSACTION_SERVICE.equals(comp)) {
458             setJtsMonitoringEnabled(enable);
459         }
460     }
461
462     /**
463      * Enable or Disable monitoring for specified monitored object types. If
464      * oldEnabled and newEnabled are same then this method is a NO-OP.
465      * @param types list of monitored object types
466      * @param oldEnabled old value of monitoring enabled
467      * @param newEnabled new value of monitoring enabled
468      * @return new value of monitoring enabled.
469      */

470     private static boolean setMonitoringEnabled(MonitoredObjectType[] types,
471             boolean oldEnabled, boolean newEnabled) {
472         if (newEnabled != oldEnabled) {
473             oldEnabled = newEnabled;
474             if (oldEnabled) {
475                 enableTypes(types);
476             } else {
477                 disableTypes(types);
478             }
479         }
480         return newEnabled;
481     }
482 }
483
Popular Tags