KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > registry > spi > ManagedResourceIntrospector


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 /* ManagedResourceIntrospector.java
25  * $Id: ManagedResourceIntrospector.java,v 1.3 2005/12/25 03:43:34 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:43:34 $
28  * Indentation Information:
29  * 0. Please (try to) preserve these settings.
30  * 1. Tabs are preferred over spaces.
31  * 2. In vi/vim -
32  * :set tabstop=4 :set shiftwidth=4 :set softtabstop=4
33  * 3. In S1 Studio -
34  * 1. Tools->Options->Editor Settings->Java Editor->Tab Size = 4
35  * 2. Tools->Options->Indentation Engines->Java Indentation Engine->Expand Tabs to Spaces = False.
36  * 3. Tools->Options->Indentation Engines->Java Indentation Engine->Number of Spaces per Tab = 4.
37  */

38
39 package com.sun.enterprise.admin.monitor.registry.spi;
40 import javax.management.*;
41 import javax.management.j2ee.statistics.*;
42 import java.lang.reflect.*;
43 import java.util.*;
44 import java.util.logging.*;
45 /**
46  * A helper class that introspects a Managed Resource that is being
47  * instrumented by a JMX MBean for manageability.
48  * @author sg112326
49  */

50 public class ManagedResourceIntrospector {
51     final boolean READABLE = true;
52     final boolean WRITABLE = true;
53     final boolean ISGETTER = true;
54   
55     DynamicMBean mbean;
56     Vector attributes;
57     public static final String JavaDoc LOGGER_NAME="this.is.console";
58     final Logger logger;
59     
60     ManagedResourceIntrospector(DynamicMBean mbean){
61         this.mbean=mbean;
62         attributes=new Vector();
63         logger = Logger.getLogger(LOGGER_NAME);
64     }
65     
66     MBeanInfo introspect(Stats stats){
67         return new MBeanInfo(
68                 mbean.getClass().getName(),//className
69
"Managed Object for "+stats.getClass().getName()+ " managed resource",//description
70
getAttributeInfo(stats), //AttributeInfo
71
null, //constructorInfo
72
getOperationInfo(stats), //operationInfo
73
null //notifications
74
);
75
76     }
77     
78     /**
79      * Creates array of MBeanAttributeInfo objects for attributes
80      * standing for Statistic objects derived from the managed resource's
81      * getStatisticNames() method
82      */

83     MBeanAttributeInfo[] getAttributeInfo(Stats stats){
84         MBeanAttributeInfo[] attrInfo=null;
85         if(stats != null){
86             Object JavaDoc[] attrs = deriveUnderlyingAttributes(stats);
87             attrInfo = new MBeanAttributeInfo[attrs.length];
88             for(int i= 0; i < attrs.length; i++){
89                 attrInfo[i] = new MBeanAttributeInfo((String JavaDoc)attrs[i],Statistic.class.getName(),
90                                 "Attribute"+attrs[i], READABLE, !WRITABLE, !ISGETTER);
91
92             }
93         }
94         return attrInfo;
95     }
96
97     /**
98      * From the passed in Stats object, this method determines the underlying
99      * Statistic type and derives from it, attributes that return primitive values.
100      * @param Stats
101      */

102     Object JavaDoc[] deriveUnderlyingAttributes(Stats stats){
103         String JavaDoc[] attrs = stats.getStatisticNames();
104     
105         for(int i=0; i< attrs.length; i++){
106             introspectEachStatistic((stats.getStatistic(attrs[i])).getClass(), attrs[i]);
107         }
108         String JavaDoc[] a = new String JavaDoc[attributes.size()];
109         return attributes.toArray(a);
110     }
111     
112     void introspectEachStatistic(Class JavaDoc statistic, String JavaDoc statName){
113         Set a = new HashSet(Arrays.asList(statistic.getMethods()));
114         Iterator it = a.iterator();
115         while(it.hasNext()){
116             String JavaDoc s = (String JavaDoc)((Method) it.next()).getName();
117             if(s.startsWith("get")&& !s.equals("getClass")){
118                 s = s.replaceFirst("get","");
119                 attributes.add(AttributeStringHelper.joinAttributes(statName,s));
120             }
121         }
122     }
123     
124     /**
125      * creates array of MBeanOperationInfo objects to determine operations
126      * to be exposed. Excludes the underlying managed resource's methods
127      * pertaining to
128      */

129     MBeanOperationInfo[] getOperationInfo(Stats stats){
130         Method[] opers = stats.getClass().getMethods();
131         MBeanOperationInfo[] operInfo = new MBeanOperationInfo[opers.length];
132         for(int i= 0; i < opers.length; i++){
133             if(!isAttrGetterOrSetter(opers[i])){
134                 operInfo[i]= createOperationInfo(opers[i]);
135             }
136         }
137         operInfo = addMoreMBeanOperations(operInfo);
138         return operInfo;
139     }
140
141     /**
142      * Add any operations defined in the MBean other than ones pertaining
143      * directly to Stats or Statistic operations. example: listAtrributes()
144      */

145     private MBeanOperationInfo[] addMoreMBeanOperations(MBeanOperationInfo[] operInfo){
146         MBeanOperationInfo oper = new MBeanOperationInfo("listAttributes",//Name
147
"Method listAttributes",//Description
148
null,//MBeanParameterInfo
149
String JavaDoc.class.getName(), //Return Type in String
150
MBeanOperationInfo.INFO // Action representing read-only operation
151
);
152         MBeanOperationInfo[] opers = new MBeanOperationInfo[operInfo.length+1];
153         opers = operInfo;
154         opers[opers.length-1] = oper;
155         return operInfo;
156     }
157     
158     /**
159      * returns true for an operation if it meets the JMX equivalent spec
160      * of distinguishing an get(set)Attribute() or get(set)Attributes() from a
161      * non getter/setter operation.
162      */

163     boolean isAttrGetterOrSetter(Method operation){
164         if(operation.getName().startsWith("get")
165             || operation.getName().startsWith("set")){
166             return true;
167         }
168         return false;
169     }
170     
171     /**
172      * returns an OperationInfo Object given a Method object
173      *
174      */

175     MBeanOperationInfo createOperationInfo(Method oper){
176         return new MBeanOperationInfo(oper.getName(),//Name
177
"Method "+oper.getName(),//Description
178
getParameterInfo(oper.getParameterTypes()),//MBeanParameterInfo
179
oper.getReturnType().getName(), //Return Type in String
180
MBeanOperationInfo.INFO // Action representing read-only operation
181
);
182     }
183     
184     /**
185      * creates an array of MBeanParameterInfo objects that represent
186      * parameters and their signatures for a given operation
187      */

188     MBeanParameterInfo[] getParameterInfo(Class JavaDoc[] paramTypes){
189         MBeanParameterInfo[] params=null;
190         if(paramTypes != null){
191             params = new MBeanParameterInfo[paramTypes.length];
192             
193             for(int i=0; i<paramTypes.length;i++){
194                 try{
195                     params[i] = new MBeanParameterInfo("param"+i,
196                                 paramTypes[i].getName(),
197                                 paramTypes[i].getName());
198                 }
199                 catch(java.lang.IllegalArgumentException JavaDoc e){
200                     logger.log(Level.INFO, e.toString());
201                 }
202             }
203         }
204         return params;
205     }
206 }
207
Popular Tags