KickJava   Java API By Example, From Geeks To Geeks.

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


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  * $Id: BaseMonitorMBean.java,v 1.3 2005/12/25 04:14:12 tcfujii Exp $
31  */

32 package com.sun.enterprise.admin.monitor;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Hashtable JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Set JavaDoc;
40 import java.util.Vector JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 import javax.management.Attribute JavaDoc;
45 import javax.management.AttributeList JavaDoc;
46 import javax.management.AttributeNotFoundException JavaDoc;
47 import javax.management.DynamicMBean JavaDoc;
48 import javax.management.InstanceAlreadyExistsException JavaDoc;
49 import javax.management.InstanceNotFoundException JavaDoc;
50 import javax.management.InvalidAttributeValueException JavaDoc;
51 import javax.management.MBeanAttributeInfo JavaDoc;
52 import javax.management.MBeanConstructorInfo JavaDoc;
53 import javax.management.MBeanOperationInfo JavaDoc;
54 import javax.management.MBeanNotificationInfo JavaDoc;
55 import javax.management.MBeanRegistrationException JavaDoc;
56 import javax.management.MBeanException JavaDoc;
57 import javax.management.MBeanInfo JavaDoc;
58 import javax.management.MBeanServer JavaDoc;
59 import javax.management.MalformedObjectNameException JavaDoc;
60 import javax.management.NotCompliantMBeanException JavaDoc;
61 import javax.management.ObjectName JavaDoc;
62 import javax.management.ReflectionException JavaDoc;
63
64 import com.sun.enterprise.admin.common.ObjectNames;
65 import com.sun.enterprise.admin.common.constant.AdminConstants;
66 import com.sun.enterprise.admin.monitor.types.MonitoredAttributeType;
67 import com.sun.enterprise.admin.common.MBeanServerFactory;
68 import com.sun.enterprise.admin.server.core.jmx.MBeanServerImpl;
69
70 //i18n import
71
import com.sun.enterprise.util.i18n.StringManager;
72
73 /**
74  * Monitoring MBean in iAS. This is the superclass of all monitoring MBeans.
75  * <p>
76  * All monitoring MBeans are registered to MBeanServer. Even though MBeanServer
77  * does not enforce any hierarchy of MBeans, the implementation within iAS is
78  * of a monitoring MBean hierarchy (tree). Therefore, MonitoringMBean for a j2ee
79  * application contains monitoring MBeans for all ejb modules and web modules;
80  * a monitoring MBean for a ejb module contains monitoring MBeans for all beans
81  * (stateless and stateful session beans, entity beans and message driven beans).
82  * </p>
83  * <p>
84  * A monitoring MBean is fully identified within its context by a type and a
85  * name. A monitoring MBean exposed by the method getRootMonitorMBean in
86  * com.sun.enterprise.admin.server.AdminService is the root of the monitoring MBean
87  * hierarchy.
88  * </p>
89  * <p>
90  * The methods <code>getNodeName, setNodeName, getNodeType and setNodeType,
91  * addChild, removeChild, getChild</code> can be used to manage/access heirarchy
92  * of MBeans. However, the properties and operations represented by those methods
93  * is not available in mBeanInfo, so the heirarchy can not be managed/accessed
94  * through MBeanServer interface.
95  * </p>
96  */

97 public abstract class BaseMonitorMBean implements DynamicMBean JavaDoc, IMonitorable {
98
99     /**
100      * A reference to logger object
101      */

102     static Logger JavaDoc logger = Logger.getLogger(AdminConstants.kLoggerName);
103
104     /**
105      * A map of object names and MBean instance
106      */

107     static final HashMap JavaDoc objectNameMap = new HashMap JavaDoc();
108
109     /**
110      * Names of all attributes exposed by this MBean
111      */

112     private String JavaDoc[] attrNames = null;
113
114     /**
115      * Object name of this MBean as registered in MBean server
116      */

117     private ObjectName JavaDoc objectName;
118
119     /**
120      * Name of this MBean at its level in MBean heirarchy.
121      */

122     private String JavaDoc nodeName;
123
124     /**
125      * Type of this MBean as registered in MBean heirarchy.
126      */

127     private String JavaDoc nodeType;
128
129     /**
130      * List of child.
131      */

132     protected Vector JavaDoc childList = new Vector JavaDoc();
133
134     /**
135      * Set object name for this MBean. This should be same as the name used
136      * to register this MBean to MBeanServer.
137      * @param objName name of this MBean as registered in MBean Server
138      */

139     protected void setObjectName(ObjectName JavaDoc objName) {
140         objectName = objName;
141     }
142
143     /**
144      * Get name of this MBean as registered in MBean server.
145      */

146     public ObjectName JavaDoc getObjectName() {
147         return objectName;
148     }
149
150     /**
151      * Get name of this MBean. A typical example is - for a stateless session
152      * bean called fortune, this method will return fortune. The MBean has
153      * a fully qualified name in the MBeanServer, but this method only returns
154      * name from the current context.
155      * @return name of the MBean within its context.
156      */

157     public String JavaDoc getNodeName() {
158         return nodeName;
159     }
160
161     /**
162      * Set name of the node to specified value. Invoked from addChild method of
163      * the parent of this MBean. So, this method need not be called elsewhere.
164      * A call to this method does not affect the name in MBeanServer, so the
165      * caller of this method should also handle naming within MBeanServer.
166      * @param name name of this MBean
167      */

168     protected void setNodeName(String JavaDoc name) {
169         nodeName = name;
170     }
171
172     /**
173      * Get type of this MBean. This is the type used in the hierarchy of
174      * monitoring MBeans.
175      * @return type of this MBean.
176      */

177     public String JavaDoc getNodeType() {
178         return nodeType;
179     }
180
181     /**
182      * Set type of this MBean to specified value. This method is invoked from
183      * the method addChild of the parent of this MBean. A call to this method
184      * does not affect the naming within MBeanServer, so the caller of this
185      * method should also handle naming within MBeanServer.
186      * @param type type of the MBean
187      */

188     protected void setNodeType(String JavaDoc type) {
189         nodeType = type;
190     }
191
192     /**
193      * Add an MBean to the MBean tree below this MBean. This method also adds
194      * the specified child MBean to the MBeanServer. Adding monitoring MBean to
195      * MBeanServer requires object name and can be derived for the child, if
196      * this MBean (parent) was already added to MBeanServer.
197      * @param name name of the MBean
198      * @param type type of the MBean
199      * @param mBean the new MBean
200      * @throws InstanceAlreadyExistsException if an MBean of this name and type
201      * already exists or if this type is a singleton and one instance
202      * already exists.
203      * @throws MBeanRegistrationException preRegister (MBeanRegistration
204      * interface) method of the MBean has thrown an exception.
205      * @throws IllegalArgumentException if any of name, type or mBean is null
206      * @throws IllegalStateException if this MBean has not yet been added to
207      * tree of monitoring MBeans.
208      * @return name of the child MBean in MBeanServer.
209      */

210     public synchronized ObjectName JavaDoc addChild(String JavaDoc name, MonitoredObjectType type,
211             BaseMonitorMBean mBean)
212             throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc {
213         if (name == null || type == null || mBean == null) {
214             String JavaDoc str1 = (name == null) ? "Child Name (name); " : "";
215             String JavaDoc str2 = (type == null) ? "Child Type (type); " : "";
216             String JavaDoc str3 = (mBean == null) ? "MBean (mBean); " : "";
217
218             String JavaDoc msg = localStrings.getString( "admin.monitor.null_arguments", str1, str2, str3 );
219             throw new IllegalArgumentException JavaDoc( msg );
220         }
221         if (objectName == null || nodeName == null || nodeType == null) {
222             String JavaDoc msg = localStrings.getString( "admin.monitor.monitoring_mbean_not_added_to_mbeantree" );
223             throw new IllegalStateException JavaDoc( msg );
224         }
225         boolean exists = false;
226         int size = childList.size();
227         for (int i = 0; i < size; i++) {
228             BaseMonitorMBean m = (BaseMonitorMBean)childList.elementAt(i);
229             if (type.isSingleton()) {
230                 if (m.nodeType.equals(type.getTypeName())) {
231                     exists = true;
232                     break;
233                 }
234             } else {
235                 if (m.nodeType.equals(type.getTypeName())
236                         && m.nodeName.equals(name)) {
237                     exists = true;
238                     break;
239                 }
240             }
241         }
242         if (exists) {
243             String JavaDoc msg = localStrings.getString( "admin.monitor.mbean_already_exists", type, name );
244             throw new InstanceAlreadyExistsException JavaDoc( msg );
245         }
246         if (type.isSingleton()) {
247             name = type.getTypeName();
248         }
249         childList.add(mBean);
250         mBean.setNodeName(name);
251         mBean.setNodeType(type.getTypeName());
252         ObjectName JavaDoc childObjName = getChildObjectName(objectName, type, name);
253         mBean.setObjectName(childObjName);
254         MBeanServer JavaDoc mbs = getMBeanServer();
255        /**
256         * Commenting this block for now
257         try {
258             mbs.registerMBean(mBean, childObjName);
259         } catch (NotCompliantMBeanException ncme) {
260             logger.log(Level.WARNING, NON_COMPLIANT_MBEAN, childObjName);
261             throw new MBeanRegistrationException(ncme);
262         }
263         */

264         objectNameMap.put(childObjName, mBean);
265         if (type.isMonitoringEnabled()) {
266             mBean.startMonitoring();
267         }
268         return childObjName;
269     }
270
271     /**
272      * Derive Object Name for the child MBean.
273      */

274     private static ObjectName JavaDoc getChildObjectName(ObjectName JavaDoc parent,
275             MonitoredObjectType childType, String JavaDoc childName)
276             throws MBeanRegistrationException JavaDoc {
277         Hashtable JavaDoc props = parent.getKeyPropertyList();
278         Hashtable JavaDoc newProps = (Hashtable JavaDoc)props.clone();
279         String JavaDoc type = (String JavaDoc)props.get(MON_OBJTYPE);
280         String JavaDoc name = (String JavaDoc)props.get(MON_OBJNAME);
281         newProps.put(type, name);
282         newProps.put(MON_OBJTYPE, childType.getTypeName());
283         newProps.put(MON_OBJNAME, childName);
284         ObjectName JavaDoc newName = null;
285         try {
286             newName = new ObjectName JavaDoc(parent.getDomain(), newProps);
287         } catch (MalformedObjectNameException JavaDoc mone) {
288             throw new MBeanRegistrationException JavaDoc(mone, mone.getMessage());
289         }
290         return newName;
291     }
292
293     /**
294      * Get list of children of this MBean. This method returns all children of
295      * this MBean. If there is no child, the method returns an empty list.
296      * @return an arraylist containing all children
297      */

298     public ArrayList JavaDoc getChildList() {
299         ArrayList JavaDoc list = new ArrayList JavaDoc();
300         list.addAll(childList);
301         return list;
302     }
303
304     /**
305      * Get list of children of specified type for this MBean. This method
306      * returns all children of specified type for this MBean. If there is no
307      * child of specified type, the method returns an empty list.
308      * @param type type of the child MBean
309      * @throws IllegalArgumentException if type is null
310      * @return an arraylist containing all children
311      */

312     public ArrayList JavaDoc getChildList(MonitoredObjectType type) {
313         if (type == null) {
314             String JavaDoc msg = localStrings.getString( "admin.monitor.null_argument_mbean_type" );
315             throw new IllegalArgumentException JavaDoc( msg );
316         }
317         ArrayList JavaDoc list = new ArrayList JavaDoc();
318         Iterator JavaDoc iter = childList.iterator();
319         while (iter.hasNext()) {
320             BaseMonitorMBean mBean = (BaseMonitorMBean)iter.next();
321             if (mBean.nodeType.equals(type.getTypeName())) {
322                 list.add(mBean);
323             }
324         }
325         return list;
326     }
327
328     /**
329      * Get list of children of specified name for this MBean. This method
330      * returns all children of specified name for this MBean. If there is no
331      * child of specified name, the method returns an empty list.
332      * @param name name of the child MBean
333      * @return an arraylist containing all children
334      */

335     public ArrayList JavaDoc getChildList(String JavaDoc name) {
336         ArrayList JavaDoc list = new ArrayList JavaDoc();
337         Iterator JavaDoc iter = childList.iterator();
338         while (iter.hasNext()) {
339             BaseMonitorMBean mBean = (BaseMonitorMBean)iter.next();
340             if (mBean.nodeName.equals(name)) {
341                 list.add(mBean);
342             }
343         }
344         return list;
345     }
346
347     /**
348      * Get specified child MBean. If type is singleton, then name parameter is
349      * not used for searching.
350      * @param type type of the child MBean
351      * @param name name of the child MBean
352      * @return the child MBean
353      * @throws InstanceNotFoundException if child MBean of specified type and
354      * name is not found.
355      * @throws IllegalArgumentException if specified type is null.
356      */

357     public BaseMonitorMBean getChild(MonitoredObjectType type, String JavaDoc name)
358             throws InstanceNotFoundException JavaDoc {
359         BaseMonitorMBean found = getChildOrNull(type, name);
360         if (found == null) {
361             String JavaDoc msg = localStrings.getString( "admin.monitor.mbean_not_found", type, name );
362             throw new InstanceNotFoundException JavaDoc( msg );
363         }
364         return found;
365     }
366
367     /**
368      * Get specified child mbean or null if there is no such child.
369      * @param type type of the child MBean
370      * @param name name of the child MBean
371      * @throws IllegalArgumentException if specified type is null.
372      * @return the child MBean if it exists, null otherwise
373      */

374     BaseMonitorMBean getChildOrNull(MonitoredObjectType type, String JavaDoc name) {
375         if (type == null) {
376             String JavaDoc msg = localStrings.getString( "admin.monitor.monitored_object_type_null" );
377             throw new IllegalArgumentException JavaDoc( msg );
378         }
379         BaseMonitorMBean found = null;
380         Iterator JavaDoc iter = childList.iterator();
381         while (iter.hasNext()) {
382             BaseMonitorMBean mBean = (BaseMonitorMBean)iter.next();
383             if (type.isSingleton()) {
384                 if (mBean.nodeType.equals(type.getTypeName())) {
385                     found = mBean;
386                 }
387             } else {
388                 if (mBean.nodeType.equals(type.getTypeName())
389                         && mBean.nodeName.equals(name)) {
390                     found = mBean;
391                 }
392             }
393         }
394         return found;
395     }
396
397     /**
398      * Get first child MBean with the specified name.
399      * @throws InstanceNotFoundException if no child MBean of specified name
400      * exists.
401      * @return child MBean with specified name
402      */

403     BaseMonitorMBean getFirstChildByName(String JavaDoc name)
404             throws InstanceNotFoundException JavaDoc {
405         ArrayList JavaDoc list = getChildList(name);
406         if (list.isEmpty()) {
407             String JavaDoc msg = localStrings.getString( "admin.monitor.child_mbean_not_available", name, objectName );
408             throw new InstanceNotFoundException JavaDoc( msg );
409         }
410         return ((BaseMonitorMBean)list.get(0));
411     }
412
413     /**
414      * Remove specified monitoring MBean from MBean tree. This method will
415      * remove the specified MBean from MBeanServer as well. If the specified
416      * MBean has any child(ren) they are also removed from the MBean tree and
417      * MBeanServer.
418      * @param type the type of MBean
419      * @param name the name of MBean
420      * @throws InstanceNotFoundException if the specified MBean (name and type)
421      * is not a child of this MBean.
422      * @throws MBeanRegistrationException preDeregister (MBeanRegistration
423      * interface) method of the MBean has thrown an exception
424      * @throws IllegalArgumentException if specified type is null.
425      */

426     public synchronized void removeChild(MonitoredObjectType type, String JavaDoc name)
427             throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
428         BaseMonitorMBean mBean = getChild(type, name);
429         removeChild(mBean);
430     }
431
432     /**
433      * Remove specified child MBean from MBean tree. This method will
434      * remove the specified MBean from MBeanServer as well. If the specified
435      * MBean has any child(ren) they are also removed from the MBean tree and
436      * MBeanServer.
437      * @param child the child MBean
438      * @throws InstanceNotFoundException if the specified MBean is not a child
439      * of this MBean.
440      * @throws MBeanRegistrationException preDeregister (MBeanRegistration
441      * interface) method of the MBean has thrown an exception
442      */

443     synchronized void removeChild(BaseMonitorMBean child)
444             throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
445         removeAllChild(child);
446         childList.remove(child);
447         objectNameMap.remove(child.objectName);
448         MBeanServer JavaDoc mbs = getMBeanServer();
449         // Commenting this line for now
450
// mbs.unregisterMBean(child.objectName);
451
}
452
453     /**
454      * Remove all child MBeans of specified mbean. This methos also unregisters
455      * all child Mbeans from MBean server. It recusrively calls itself if the
456      * child mbeans also have child(ren).
457      * @throws InstanceNotFoundException if any of the child MBean is not
458      * registered to MBeanSever.
459      * @throws MBeanRegistrationException preDeregister (MBeanRegistration
460      * interface) method of any of the child MBean throws an exception
461      */

462     private static void removeAllChild(BaseMonitorMBean mbean)
463             throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
464         Iterator JavaDoc iter = mbean.childList.iterator();
465         while (iter.hasNext()) {
466             BaseMonitorMBean child = (BaseMonitorMBean)iter.next();
467             removeAllChild(child);
468             objectNameMap.remove(child.objectName);
469             MBeanServer JavaDoc mbs = getMBeanServer();
470             // Commenting this line for now
471
// mbs.unregisterMBean(child.objectName);
472
}
473         mbean.childList.removeAllElements();
474     }
475
476     /**
477      * Get MBeanServer. If MBeanServer is not found then this method throws
478      * RuntimeException.
479      * @throws RuntimeException if there is any error in getting MBeanServer
480      * @return an implementation of MBeanServer interface
481      */

482     private static MBeanServer JavaDoc getMBeanServer() {
483         return MBeanServerFactory.getMBeanServer();
484     }
485
486     /**
487      * Obtains the value of a specific monitored attribute.
488      * @param attribute The name of the attribute to be retrieved
489      * @returns The value of the attribute retrieved.
490      * @throws AttributeNotFoundException if attribute name is not valid
491      */

492     public abstract Object JavaDoc getAttribute(String JavaDoc attribute)
493             throws AttributeNotFoundException JavaDoc;
494
495     /**
496      * Get the values of several attributes of the monitoring MBean.
497      * @param attributes A list of the attributes to be retrieved.
498      * @returns The list of attributes retrieved.
499      */

500     public abstract AttributeList JavaDoc getAttributes(String JavaDoc[] attributes);
501
502     /**
503      * Provides the exposed attributes and actions of the monitoring MBean using
504      * an MBeanInfo object. The implementation for this method should ensure
505      * that the returned value is always same (not necessarily same reference,
506      * but same contained values).
507      * @returns An instance of MBeanInfo with all attributes and actions exposed
508      * by this monitoring MBean.
509      */

510     public abstract MBeanInfo JavaDoc getMBeanInfo();
511
512     /**
513      * Invoke a operation on this MBean.
514      */

515     public Object JavaDoc invoke(String JavaDoc str, Object JavaDoc[] obj, String JavaDoc[] str2)
516             throws MBeanException JavaDoc, ReflectionException JavaDoc {
517         throw new UnsupportedOperationException JavaDoc(
518                 getLocalString(UNSUPPORTED_ERRCODE, UNSUPPORTED_ERRMSG));
519     }
520
521     /**
522      * Set an attribute on this MBean.
523      */

524     public final void setAttribute(Attribute JavaDoc attribute)
525             throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc,
526             MBeanException JavaDoc, ReflectionException JavaDoc {
527         throw new UnsupportedOperationException JavaDoc(
528                 getLocalString(UNSUPPORTED_ERRCODE, UNSUPPORTED_ERRMSG));
529     }
530
531     /**
532      * Set specified attributes on this MBean
533      */

534     public final AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributeList) {
535         throw new UnsupportedOperationException JavaDoc(
536                 getLocalString(UNSUPPORTED_ERRCODE, UNSUPPORTED_ERRMSG));
537     }
538
539     /**
540      * Get type of the specified monitored attribute.
541      */

542     public abstract MonitoredAttributeType getAttributeType(String JavaDoc attrName);
543
544     /**
545      * Start monitoring on this component. This will be called when monitoring
546      * is enabled on this component (or the group containing this component)
547      * through user interface.
548      * @see stopMonitoring
549      */

550     public void startMonitoring() {
551     }
552
553     /**
554      * Stop monitoring on this component. Called when monitoring is disabled on
555      * user interface.
556      */

557     public void stopMonitoring() {
558     }
559
560     /**
561      * Get a map of monitored attribute names and their types. The keys in
562      * the map are names of the attribute and the values are their types. The
563      * type value are instances of class
564      * com.iplanet.ias.monitor.type.MonitoredAttributeType (or its sub-classes)
565      *
566      * @return map of names and types of all monitored attributes
567      */

568     public abstract Map JavaDoc getMonitoringMetaData();
569
570     /**
571      * Get value of specified monitored attribute. This method is from interface
572      * IMonitorable and is implemented to use method getAttribute from
573      * DynamicMBean interface.
574      * @param monitorAttributeName name of the monitored attribute
575      * @return value of the specified monitored attribute, if it exists, null
576      * otherwise.
577      */

578     public final Object JavaDoc getMonitoredAttributeValue(String JavaDoc monitorAttributeName) {
579         Object JavaDoc result = null;
580         try {
581             result = getAttribute(monitorAttributeName);
582         } catch (AttributeNotFoundException JavaDoc anfe) {
583         }
584         return result;
585     }
586
587     /**
588      * Get values of specified monitored attributes. This method returns a
589      * map of monitored attribute names and their corresponding values. This
590      * method is from interface IMonitorable and is implemented to use method
591      * getAttributes from DynamicMBean interface.
592      *
593      * @param monitorAttributeNameSet set of monitored attribute names
594      *
595      * @return map of attribute names and their values
596      */

597     public final Map JavaDoc getMonitoredAttributeValues(Set JavaDoc monitorAttributeNameSet) {
598         return null;
599     }
600
601     /**
602      * Convenience method to create MBeanInfo from a HashMap of attribute
603      * names and attribute types. This method can be called from static
604      * initializer of sub-classes to initialize a static variable with
605      * MBeanInfo (which can then be returned by the method getMBeanInfo). This
606      * method assumes that sub-classes of BaseMonitorMBean do not implement any
607      * additional JMX operations or notifications. If the sub-classes need to
608      * implement their own operations and/or notifications the MBeanInfo
609      * returned from this method should be updated to add those.
610      *
611      * @param map of attribute names (instances of String) and attribute
612      * types (instances of MonitoredAttributeTypes)
613      *
614      * @return info for this MBean.
615      */

616     protected static MBeanInfo JavaDoc createMBeanInfo(Map JavaDoc attrNameTypeMap) {
617         return (createMBeanInfo(attrNameTypeMap, new MBeanOperationInfo JavaDoc[0]));
618     }
619
620     /**
621      * Convenience method to create MBeanInfo from a HashMap of attribute
622      * names and attribute types. This method can be called from static
623      * initializer of sub-classes to initialize a static variable with
624      * MBeanInfo (which can then be returned by the method getMBeanInfo). This
625      * method assumes that sub-classes of BaseMonitorMBean do not implement any
626      * additional JMX operations or notifications. If the sub-classes need to
627      * implement their own operations and/or notifications the MBeanInfo
628      * returned from this method should be updated to add those.
629      *
630      * @param map of attribute names (instances of String) and attribute
631      * types (instances of MonitoredAttributeTypes)
632      *
633      * @return info for this MBean.
634      */

635     protected static MBeanInfo JavaDoc createMBeanInfo(Map JavaDoc attrNameTypeMap,
636                             MBeanOperationInfo JavaDoc[] operationInfoArray) {
637         String JavaDoc className = GenericMonitorMBean.class.getName();
638         String JavaDoc description = "Generic Monitoring MBean";
639         attrNameTypeMap = convertKeysToCamelCase(attrNameTypeMap);
640         Set JavaDoc keys = attrNameTypeMap.keySet();
641         Iterator JavaDoc names = keys.iterator();
642         MBeanAttributeInfo JavaDoc[] attrList = new MBeanAttributeInfo JavaDoc[keys.size()];
643         int i = 0;
644         while (names.hasNext()) {
645             String JavaDoc name = (String JavaDoc)names.next();
646             String JavaDoc type = ((MonitoredAttributeType)attrNameTypeMap.get(name)).getJavaTypeName();
647             String JavaDoc desc = "Monitored attribute " + name;
648             attrList[i] = new MBeanAttributeInfo JavaDoc(name, type, desc, true, false, false);
649             i++;
650         }
651         MBeanConstructorInfo JavaDoc[] constructorList = new MBeanConstructorInfo JavaDoc[0];
652         //MBeanOperationInfo[] operationList = new MBeanOperationInfo[0];
653
MBeanNotificationInfo JavaDoc[] notificationList = new MBeanNotificationInfo JavaDoc[0];
654         return new MBeanInfo JavaDoc(className, description, attrList, constructorList,
655                operationInfoArray, notificationList);
656     }
657     
658     /**
659      * Get name of all attributes exposed by this MBean. The method returns
660      * a string array of length zero or more. The default implementation
661      * uses MBeanInfo to derive list of attribute names and throws an
662      * IllegalStateException if MBeanInfo is null.
663      * @return a string array containing names of all exposed attributes.
664      */

665     public String JavaDoc[] getAllAttributeNames() {
666         return getAllAttributeNamesFromMBeanInfo();
667     }
668
669     /**
670      * Get name of all attributes exposed by this MBean. This method uses
671      * MBeanInfo to derive list of all attributes and caches it. Therefore,
672      * if MBeanInfo is changed after a call to this method it will not affect
673      * the return value of this method. JMX specification requires that
674      * MBeanInfo returned by DynamicMBean implementation does not change once
675      * the MBean has been registered.
676      * @throws IllegalStateException if MBeanInfo returned by the method
677      * <code>getMBeanInfo</code> is null.
678      */

679     private String JavaDoc[] getAllAttributeNamesFromMBeanInfo() {
680         if (attrNames == null) {
681             MBeanInfo JavaDoc info = this.getMBeanInfo();
682             if (info == null) {
683                 String JavaDoc msg = localStrings.getString( "admin.monitor.null_mbean_info", this.getClass() );
684                 throw new IllegalStateException JavaDoc( msg );
685             }
686             MBeanAttributeInfo JavaDoc[] attrInfoList = info.getAttributes();
687             if (attrInfoList == null) {
688                 String JavaDoc msg = localStrings.getString( "admin.monitor.null_attribute_info_in_mbeaninfo", this.getClass() );
689                 throw new IllegalStateException JavaDoc( msg );
690             }
691             int size = attrInfoList.length;
692             attrNames = new String JavaDoc[size];
693             for (int i = 0; i < size; i++) {
694                 attrNames[i] = attrInfoList[i].getName();
695             }
696         }
697         return attrNames;
698     }
699
700     /**
701      * Convenience method to create a map of monitorable attribute names and
702      * their types from a 2-d array of attribute names and attribute types.
703      * Attribute types are instances of class MonitoredAttributeType in the
704      * package com.sun.enterprise.admin.monitor.types. This method can be called
705      * from static initializer of sub-classes to initialize a static variable
706      * with a Map of attribute names and types (which can then be returned by
707      * the method getMonitoringMetaData)
708      *
709      * @param attrNameTypeArray 2-d array of objects containing lists of
710      * atttribute names (instances of String) and their corresponding
711      * types (instances of MonitoredAttributeTypes)
712      *
713      * @return map of attribute names and their corresponding types.
714      */

715     protected static Map JavaDoc createAttrNameTypeMap(Object JavaDoc[][] attrNameTypeArray) {
716         HashMap JavaDoc map = new HashMap JavaDoc();
717         for (int i = attrNameTypeArray.length; i > 0; i--) {
718             map.put(attrNameTypeArray[i-1][0], attrNameTypeArray[i-1][1]);
719         }
720         return map;
721     }
722
723     private static String JavaDoc getLocalString(String JavaDoc key, String JavaDoc dflt) {
724         return dflt;
725     }
726
727     /**
728         Converts the keys in the passed map into camel-case strings.
729         The given map must not be null.
730         @return Map that contains keys as valid java identifiers.
731         @param attrNameTypeMap a map with keys being invalid java-identifiers,
732             an empty map in case of empty map passed.
733     */

734     private static Map JavaDoc convertKeysToCamelCase(Map JavaDoc attrNameTypeMap) {
735         Iterator JavaDoc keys = attrNameTypeMap.keySet().iterator();
736         Iterator JavaDoc values = attrNameTypeMap.values().iterator();
737         Map JavaDoc newMap = new HashMap JavaDoc();
738         while(keys.hasNext()) {
739             String JavaDoc key = (String JavaDoc)keys.next();
740             String JavaDoc camelCaseKey = toCamelCase(key);
741             newMap.put(camelCaseKey, values.next());
742         }
743         return newMap;
744     }
745     
746     /**
747         Converts the given string with characters that are illegal identifier parts and converts it
748         into a String with camel-case. The entire BNF is out of the scope.
749         Following are the basic rules:
750         <li> gets rid of all the illegal characters in given string.
751         <li> all consecutive illegal characters are collapsed.
752         <li> leading and trailing illegal characters are ignored.
753         <li> given string should have no other illegal characters (that make it
754             an illegal java-identifier). This is asserted.
755         <li> The case of an already upper-case character is NOT changed.
756        @throws NullPointerException in case of null string.
757        @returns the String with camel-case version of passed string.
758     */

759     private static String JavaDoc toCamelCase(String JavaDoc illegalIdentifier) {
760         final StringBuffer JavaDoc from = new StringBuffer JavaDoc(illegalIdentifier);
761         final StringBuffer JavaDoc to = new StringBuffer JavaDoc();
762         final int length = from.length();
763         boolean illegalFound = false;
764         for (int i = 0 ; i < length ; i++) {
765         char currentChar = from.charAt(i);
766         /* First char should be valid start and char at any other position
767              should be valid part, otherwise ignore it. */

768             if (i == 0 && !Character.isJavaIdentifierStart(currentChar) ||
769                 i > 0 && !Character.isJavaIdentifierPart(currentChar)) {
770                 illegalFound = true;
771                 continue;
772             }
773             if (illegalFound) {
774                 to.append(Character.toUpperCase(currentChar));
775                 illegalFound = false;
776             }
777             else {
778                 to.append(currentChar);
779             }
780         }
781         return (to.toString());
782     }
783
784     // i18n StringManager
785
private static StringManager localStrings =
786         StringManager.getManager( BaseMonitorMBean.class );
787
788     protected static final String JavaDoc UNSUPPORTED_ERRCODE = localStrings.getString( "admin.monitor.unsupported_mbean_monitor" );
789     protected static final String JavaDoc UNSUPPORTED_ERRMSG = localStrings.getString( "admin.monitor.unsupported_action_on_monitoring_mbean" );
790
791     private static final String JavaDoc MON_OBJTYPE = ObjectNames.kMonitoringClassName;
792     private static final String JavaDoc MON_OBJNAME = ObjectNames.kNameKeyName;
793
794     private static final String JavaDoc NON_COMPLIANT_MBEAN = "monitor.non_compliant_mbean";
795     private static final String JavaDoc MBS_INIT_ERROR = "monitor.mbs_init_error";
796
797 }
798
Popular Tags