KickJava   Java API By Example, From Geeks To Geeks.

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


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 /* GeneratedMonitoringMBeanImpl.java
25  * $Id: GeneratedMonitoringMBeanImpl.java,v 1.3 2005/12/25 03:43:33 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:43:33 $
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 com.sun.enterprise.admin.monitor.registry.StatsHolder;
41
import javax.management.*;
42 import javax.management.j2ee.statistics.Stats JavaDoc;
43 import javax.management.j2ee.statistics.*;
44 import java.util.*;
45 import java.util.logging.*;
46 import java.lang.reflect.*;
47 import java.lang.*;
48
49 /**
50  * A generic dynamic mbean implementation for Monitoring which instruments a
51  * JSR77 compliant Stats object to be managed by this JMX managed object.
52  * @author <a HREF=mailto:shreedhar.ganapathy@sun.com>Shreedhar Ganapathy</a>
53  */

54 public class GeneratedMonitoringMBeanImpl implements DynamicMBean {
55     MBeanInfo mbeanInfo;
56     Stats JavaDoc resourceInstance;
57     public static final String JavaDoc LOGGER_NAME="this.is.console";
58     final Logger logger;
59     Hashtable attributeMap;
60     //StatsHolder statsHolder = null;
61
/**
62      * constructs a monitoring mbean that manages an underlying Stats resource
63      */

64     public GeneratedMonitoringMBeanImpl(Stats JavaDoc stats) {
65         logger = Logger.getLogger(LOGGER_NAME);
66         this.resourceInstance=stats;
67         attributeMap=new Hashtable();
68     }
69     
70     /**
71      * introspects the underlying Stats resource and generates an MBeanInfo object
72      * and populates attributes internally for later use culled from the
73      * Statistic objects returned by the Stats object.
74      * @param javax.management.j2ee.statistics.Stats
75      * @param javax.management.MBeanInfo
76      */

77     MBeanInfo introspect(){
78         ManagedResourceIntrospector mri = new ManagedResourceIntrospector(this);
79         mbeanInfo = mri.introspect(this.resourceInstance);
80         setUpAttributeMap();
81         return mbeanInfo;
82     }
83     /**
84      * sets up the internal data structure to hold attributes derived from
85      * Statistic objects returned by the Stats object.
86      */

87     private void setUpAttributeMap(){
88         MBeanAttributeInfo[] attrInfo = mbeanInfo.getAttributes();
89         String JavaDoc attr =null;
90         for(int i=0;i<attrInfo.length;i++){
91             attr = attrInfo[i].getName();
92             attributeMap.put(attr,attr);
93         }
94     }
95     
96     /**
97      * Implementation of DynamicMBean interface's method. Parses the passed in
98      * attribute into two portions.
99      * One portion invokes the corresponding method in the Stats object and
100      * the second portion is used to invoke the underlying method in the
101      * Statistic object returned by the Stats object in the first invocation.
102      * Example of an attribute pattern: For JVMStats, HeapSize_UpperBound would
103      * translate into JVMStats.getHeapSize() which returns a BoundedRangeStatistic
104      * object. The BoundedRangeStatistic.getUpperBound() method is invoked
105      * to return the value of the attribute to the client.
106      * @param java.lang.String
107      * @return java.lang.Object
108      * @throws javax.management.AttributeNotFoundException
109      * @throws javax.management.MBeanException
110      * @throws javax.management.ReflectionException
111      */

112     public Object JavaDoc getAttribute(String JavaDoc str)
113     throws javax.management.AttributeNotFoundException JavaDoc,
114     javax.management.MBeanException JavaDoc, javax.management.ReflectionException JavaDoc {
115         if(str == null){
116             throw new NullPointerException JavaDoc("An attribute needs to be specified to get value");
117         }
118         if(mbeanInfo == null){
119             introspect();
120         }
121         if(!isValidAttribute(str)){
122             throw new AttributeNotFoundException("The requested attribute is not recognized");
123         }
124         Statistic a = null;
125         Object JavaDoc retval=null;
126         String JavaDoc[] attrParts = AttributeStringHelper.splitAttribute(str);
127         logger.log(Level.INFO,"accessing the Stats object with attr="+attrParts[0]);
128         a = (Statistic)resourceInstance.getStatistic(attrParts[0]);
129         if(a == null){
130             try{
131                 a = (Statistic)resourceInstance.getClass().
132                 getMethod("get"+str).
133                 invoke(resourceInstance);
134             }
135             catch(Exception JavaDoc e){
136                 logger.log(Level.INFO,e.getLocalizedMessage());
137             }
138         }
139         //note: do not change this to an "else{ }" block
140
if(a != null){
141             try{
142                 retval = a.getClass().getMethod("get"+attrParts[1]).invoke(a);
143             }
144             catch(Exception JavaDoc e){
145                 logger.log(Level.INFO,e.getLocalizedMessage());
146                 logger.log(Level.FINE,e.getStackTrace().toString());
147             }
148         }
149         return retval;
150     }
151     
152     /**
153      * checks if the passed in string is a recognized attribute
154      * @param java.lang.String
155      * @return boolean
156      */

157     private boolean isValidAttribute(String JavaDoc str){
158         if(attributeMap.containsValue(str))
159             return true;
160         return false;
161     }
162     
163     /**
164      * Implementation of DynamicMBean interface's method. Loops through the
165      * passed in String[] and calls getAttribute(str) for each element.
166      * @param java.lang.String[]
167      * @return javax.management.AttributeList
168      */

169     public javax.management.AttributeList JavaDoc getAttributes(String JavaDoc[] str) {
170         if(mbeanInfo == null)
171             introspect();
172         AttributeList list = new AttributeList();
173         try{
174             for(int i=0; i<str.length;i++){
175                 list.add(i, new Attribute(str[i],getAttribute(str[i])));
176             }
177         }
178         catch(Exception JavaDoc e){
179             logger.log(Level.INFO,e.getMessage()+"\n"+e.getCause().toString());
180         }
181         return list;
182     }
183     
184     /**
185      * Implementation of DynamicMBean interface's method. Returns the MBeanInfo
186      * object that was generated during introspection of the underlying Stats
187      * resource.
188      * @return javax.management.MBeanInfo
189      */

190     public javax.management.MBeanInfo JavaDoc getMBeanInfo() {
191         if(mbeanInfo== null){
192             introspect();
193         }
194         return mbeanInfo;
195     }
196     
197     public Object JavaDoc invoke(String JavaDoc str, Object JavaDoc[] obj, String JavaDoc[] str2) throws
198     javax.management.MBeanException JavaDoc, javax.management.ReflectionException JavaDoc{
199         if(mbeanInfo == null)
200             introspect();
201         Object JavaDoc a =null;
202         Class JavaDoc[] c = new Class JavaDoc[]{};
203         for(int i=0; i<str2.length;i++){
204             c[i] = str2[i].getClass();
205         }
206         try{
207             a = (Object JavaDoc) resourceInstance.getClass().getMethod(str, c).invoke(resourceInstance, obj);
208         }
209         catch(Exception JavaDoc e){
210             logger.log(Level.INFO,e.getLocalizedMessage());
211             logger.log(Level.FINE,e.getStackTrace().toString());
212         }
213         return a;
214     }
215     
216     /**
217      * Implementation of DynamicMBean interface's method - NO-OP.
218      * @param javax.management.Attribute
219      * @throws javax.management.AttributeNotFoundException
220      * @throws javax.management.InvalidAttributeValueException
221      * @throws javax.management.MBeanException
222      * @throws javax.management.ReflectionException
223      */

224     public void setAttribute(javax.management.Attribute JavaDoc attribute) throws
225     javax.management.AttributeNotFoundException JavaDoc,
226     javax.management.InvalidAttributeValueException JavaDoc,
227     javax.management.MBeanException JavaDoc,
228     javax.management.ReflectionException JavaDoc { }
229     
230     /**
231      * Implementation of DynamicMBean interface's method. Sets a list of attributes.
232      * Iterates through the list and calls setAttribute() for each element.
233      * @param javax.management.AttributeList
234      * @return javax.management.AttributeList
235      */

236     public javax.management.AttributeList JavaDoc setAttributes(
237         javax.management.AttributeList JavaDoc attributeList) {
238         return new AttributeList();
239     }
240     
241     /**
242      * Returns an String[] of attribute names.
243      * @return String[]
244      */

245     public String JavaDoc[] listAttributes(){
246         return (String JavaDoc[]) attributeMap.values().toArray();
247     }
248 }
249
Popular Tags