KickJava   Java API By Example, From Geeks To Geeks.

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


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 /* StatsHolderMBeanImpl.java
25  * $Id: StatsHolderMBeanImpl.java,v 1.4 2006/03/14 11:11:54 sankara Exp $
26  * $Revision: 1.4 $
27  * $Date: 2006/03/14 11:11:54 $
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
41 import java.util.logging.Logger JavaDoc;
42 import java.util.Collection JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47 import javax.management.MBeanServer JavaDoc;
48 import javax.management.ObjectName JavaDoc;
49 import javax.management.MBeanInfo JavaDoc;
50 import javax.management.Attribute JavaDoc;
51 import javax.management.AttributeList JavaDoc;
52 import javax.management.DynamicMBean JavaDoc;
53 import javax.management.AttributeNotFoundException JavaDoc;
54 import javax.management.MBeanException JavaDoc;
55 import javax.management.ReflectionException JavaDoc;
56 import javax.management.InvalidAttributeValueException JavaDoc;
57 import javax.management.MBeanRegistration JavaDoc;
58 import javax.management.MBeanInfo JavaDoc;
59 import javax.management.MBeanAttributeInfo JavaDoc;
60 import javax.management.MBeanConstructorInfo JavaDoc;
61 import javax.management.MBeanNotificationInfo JavaDoc;
62 import javax.management.MBeanOperationInfo JavaDoc;
63 import javax.management.MBeanParameterInfo JavaDoc;
64
65 import javax.management.MBeanRegistrationException JavaDoc;
66
67 import com.sun.enterprise.admin.monitor.registry.StatsHolder;
68
69 import com.sun.enterprise.admin.common.constant.AdminConstants;
70 import com.sun.enterprise.util.i18n.StringManager;
71 import javax.management.j2ee.statistics.Statistic JavaDoc;
72 import javax.management.j2ee.statistics.Stats JavaDoc;
73
74 /**
75  * Provides the implementation for an MBean that represents a node to give statistical
76  * data in the form of its attributes.
77  * @author <a HREF="mailto:Kedar.Mhaswade@sun.com">Kedar Mhaswade</a>
78  * @since S1AS8.0
79  * @version $Revision: 1.4 $
80  */

81 class StatsHolderMBeanImpl implements DynamicMBean JavaDoc, StatsHolderMBean, MBeanRegistration JavaDoc {
82     
83     private static Logger JavaDoc logger = Logger.getLogger(AdminConstants.kLoggerName);
84     private static StringManager sm = StringManager.getManager(StatsHolderMBeanImpl.class);
85     private final StatsHolder delegate;
86     private final StatsMediator mediator;
87     private DottedNameRegistrar registrar;
88     private MBeanInfo JavaDoc mi;
89     private int state;
90     private Object JavaDoc lock = new Object JavaDoc();
91     public static final int INITIALIZED = 0;
92     public static final int REGISTERED = 1;
93     public static final int MBEANINFO_DONE = 2;
94     public static final String JavaDoc JTA_FREEZE = "freeze";
95     public static final String JavaDoc JTA_UNFREEZE = "unfreeze";
96     public static final String JavaDoc JTA_ROLLBACK = "rollback";
97     public static final String JavaDoc JTA_ACTIVE_TRANSACTIONS = "listActiveTransactions";
98     public static final String JavaDoc DOTTED_NAME = "dotted-name";
99
100     
101     StatsHolderMBeanImpl(StatsHolder delegate) {
102         assert (delegate != null);
103         this.delegate = delegate;
104         this.mediator = new StatsMediatorImpl(delegate.getStats(), delegate.getStatsClass());
105         changeState(INITIALIZED);
106     }
107     
108     public Object JavaDoc getAttribute(String JavaDoc name) throws AttributeNotFoundException JavaDoc,
109     MBeanException JavaDoc, ReflectionException JavaDoc {
110         if(name.equals(DOTTED_NAME))
111             return delegate.getDottedName();
112         else
113             return ( mediator.getAttribute(name) );
114     }
115     
116     public AttributeList JavaDoc getAttributes(String JavaDoc[] names) {
117         final AttributeList JavaDoc list = new AttributeList JavaDoc();
118         for (int i = 0 ; i < names.length ; i++) {
119             try {
120                 final Attribute JavaDoc a = new Attribute JavaDoc(names[i], this.getAttribute(names[i]));
121                 list.add(a);
122             }
123             catch(Exception JavaDoc e) {
124                 logger.finest("Error while accessing an attribute named: " + names[i]);
125                 //The exception SHOULD BE squelched per the contract of this method
126
}
127         }
128         return ( list );
129     }
130     
131     public MBeanInfo JavaDoc getMBeanInfo() {
132         synchronized(lock) {
133             if (state == MBEANINFO_DONE) {
134                 return mi;
135             }
136         }
137         build();
138         changeState(MBEANINFO_DONE);
139         return ( mi );
140     }
141     
142     private void build() {
143         final String JavaDoc name = StatsHolderMBeanImpl.class.getName();
144         final String JavaDoc desc = getDescription();
145         final MBeanAttributeInfo JavaDoc[] mais = mediator.getAttributeInfos();
146         final MBeanConstructorInfo JavaDoc[] mcis = this.getConstructorInfos();
147         final MBeanOperationInfo JavaDoc[] mois = this.getOperationInfos();
148         final MBeanNotificationInfo JavaDoc[] mnis = this.getNotificationInfos();
149         mi = new MBeanInfo JavaDoc(name, desc, mais, mcis, mois, mnis);
150     }
151     
152     private String JavaDoc getDescription() {
153         return "StatsHolder MBean for: " + StatsHolderMBeanImpl.class.getName();
154     }
155     
156     private MBeanConstructorInfo JavaDoc[] getConstructorInfos() {
157         final MBeanConstructorInfo JavaDoc[] cis = new MBeanConstructorInfo JavaDoc[0];
158         return ( cis ); //we don't want management applications to create instances of this MBean
159
}
160     private MBeanOperationInfo JavaDoc[] getOperationInfos() {
161         
162         final ArrayList JavaDoc opInfo = new ArrayList JavaDoc();
163
164         opInfo.add(getChildrenInfo());
165         opInfo.add(getNameInfo());
166         opInfo.add(getTypeInfo());
167         // Add the additional ops only for StatsHolders that have an actual Stats object
168
// associated with them
169
if(delegate.getStats() != null) {
170             opInfo.add(getStatisticNameInfo());
171             opInfo.add(getStatsInfo());
172         }
173         
174         MBeanOperationInfo JavaDoc[] mos = new MBeanOperationInfo JavaDoc[opInfo.size()];
175         mos = (MBeanOperationInfo JavaDoc[])opInfo.toArray(mos);
176         
177         // if we are dealing with JTAStats, we need to add the additional
178
// operations freeze, unfreeze & rollback to the MBeanOerationInfo
179
if(isJta())
180             return (getJTAOperationInfo(mos));
181         
182         return ( mos );
183     }
184     
185     public Object JavaDoc invoke(String JavaDoc method, Object JavaDoc[] params, String JavaDoc[] sign) throws
186     MBeanException JavaDoc, ReflectionException JavaDoc {
187         if ("getChildren".equals(method)) {
188             return ( this.getChildren() );
189         } else if ("getName".equals(method)) {
190             return (this.getName());
191         } else if ("getType".equals(method)) {
192             return (this.getType());
193         } else if ("getStatisticNames".equals(method)) {
194             return (this.getStatisticNames());
195         } else if ("getStatistics".equals(method)) {
196             return (this.getStatistics());
197         } else if(isJTAMethod(method)) {
198             return(mediator.invoke(method, params, sign));
199         } else {
200             final String JavaDoc msg = sm.getString("smi.no_such_method", method);
201                         final Exception JavaDoc ae = new UnsupportedOperationException JavaDoc(msg);
202             throw new MBeanException JavaDoc(ae);
203         }
204     }
205     
206     public void setAttribute(Attribute JavaDoc attribute) throws AttributeNotFoundException JavaDoc,
207     InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc {
208         throw new UnsupportedOperationException JavaDoc("NYI");
209     }
210     
211     public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes) {
212         throw new UnsupportedOperationException JavaDoc("NYI");
213     }
214     
215     public ObjectName JavaDoc[] getChildren() {
216         final Collection JavaDoc c = delegate.getAllChildren();
217         final ObjectName JavaDoc[] names = new ObjectName JavaDoc[c.size()];
218         final Iterator JavaDoc it = c.iterator();
219         int i = 0;
220         while (it.hasNext()) {
221             final StatsHolder s = (StatsHolder) it.next();
222             names[i++] = s.getObjectName();
223         }
224         assert (names.length == i) : "Sizes don't match";
225         return ( names );
226     }
227     
228     private MBeanOperationInfo JavaDoc getChildrenInfo() {
229         final MBeanOperationInfo JavaDoc info = new MBeanOperationInfo JavaDoc(
230             "getChildren",
231             "Gets the children of this StatsHolder",
232             null,
233             ObjectName JavaDoc[].class.getName(),
234             MBeanOperationInfo.INFO
235         );
236         return ( info );
237     }
238     
239     private MBeanNotificationInfo JavaDoc[] getNotificationInfos() {
240         final MBeanNotificationInfo JavaDoc[] mns = new MBeanNotificationInfo JavaDoc[0];
241         return ( mns );
242     }
243     
244     private boolean isJta() {
245         boolean isJta = false;
246         final Class JavaDoc cl = delegate.getStatsClass();
247         if (cl != null) {
248             if (com.sun.enterprise.admin.monitor.stats.JTAStats.class.getName().equals(cl.getName())) {
249                 isJta = true;
250             }
251         }
252         return ( isJta);
253     }
254     private boolean isJTAMethod(String JavaDoc methodName) {
255         return ((JTA_FREEZE.equals(methodName)) ||
256                (JTA_UNFREEZE.equals(methodName)) ||
257                (JTA_ACTIVE_TRANSACTIONS.equals(methodName)) ||
258                (JTA_ROLLBACK.equals(methodName)));
259     }
260     
261     //Implementation of MBeanRegistration - start
262
public void postDeregister() {
263     }
264     
265     public void postRegister(Boolean JavaDoc registered) {
266         if (registered.equals(Boolean.TRUE)) {
267             registrar.registerDottedName(delegate.getDottedName(),
268                 delegate.getObjectName());
269             changeState(REGISTERED);
270         }
271     }
272     
273     public void preDeregister() throws Exception JavaDoc {
274         registrar.unregisterDottedName(delegate.getDottedName());
275     }
276     
277     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc mBeanServer, ObjectName JavaDoc objectName) throws
278     Exception JavaDoc {
279         this.registrar = new DottedNameRegistrar(mBeanServer);
280         return objectName;
281     }
282     //Implementation of MBeanRegistration - end
283

284     private void changeState(int to) {
285         synchronized(lock) {
286             state = to;
287         }
288     }
289     
290     private MBeanOperationInfo JavaDoc[] getJTAOperationInfo(MBeanOperationInfo JavaDoc[] mos) {
291         ArrayList JavaDoc opInfo = new ArrayList JavaDoc();
292         for(int i = 0; i < mos.length ; i++)
293             opInfo.add(mos[i]);
294         // not performing any reflection for now, as it is assumed that
295
// only 3 methods will be added to the MBeanOperationInfo and
296
// their names and signatures are fixed.
297
MBeanOperationInfo JavaDoc mInfo = new MBeanOperationInfo JavaDoc(JTA_FREEZE,
298                                                           "Freezes the transaction service",
299                                                           null,
300                                                           void.class.getName(),
301                                                           MBeanOperationInfo.ACTION);
302     
303         opInfo.add(mInfo);
304         
305         mInfo = new MBeanOperationInfo JavaDoc(JTA_UNFREEZE,
306                                        "Unfreezes the transaction service",
307                                         null,
308                                         void.class.getName(),
309                                         MBeanOperationInfo.ACTION);
310         
311         opInfo.add(mInfo);
312         mInfo = new MBeanOperationInfo JavaDoc(JTA_ROLLBACK,
313                                        "Rollsback a given transaction",
314                                         new MBeanParameterInfo JavaDoc[] {
315                                             new MBeanParameterInfo JavaDoc("txnId",
316                                                                    String JavaDoc.class.getName(),
317                                                                    "Id of the transaction to be rolled back"
318                                                                    )},
319                                         void.class.getName(),
320                                         MBeanOperationInfo.ACTION);
321         opInfo.add(mInfo);
322         mInfo = new MBeanOperationInfo JavaDoc(JTA_ACTIVE_TRANSACTIONS,
323                                        "Gets Active Transactions in a Map",
324                                         null,
325                                         List JavaDoc.class.getName(),
326                                         MBeanOperationInfo.ACTION_INFO);
327         opInfo.add(mInfo);
328         MBeanOperationInfo JavaDoc[] jtaOpInfo = new MBeanOperationInfo JavaDoc[opInfo.size()];
329         return (MBeanOperationInfo JavaDoc[])opInfo.toArray(jtaOpInfo);
330         
331     }
332     
333     public String JavaDoc getName() {
334         return delegate.getName();
335     }
336     
337     public String JavaDoc getType() {
338         return delegate.getType().getTypeName();
339     }
340     
341     private MBeanOperationInfo JavaDoc getNameInfo() {
342         MBeanOperationInfo JavaDoc mInfo = new MBeanOperationInfo JavaDoc("getName",
343                                                           "Gets the name of this StatsHolder",
344                                                           null,
345                                                           String JavaDoc.class.getName(),
346                                                           MBeanOperationInfo.INFO);
347         return mInfo;
348     }
349     
350     private MBeanOperationInfo JavaDoc getTypeInfo() {
351         MBeanOperationInfo JavaDoc mInfo = new MBeanOperationInfo JavaDoc("getType",
352                                                           "Gets the type of this StatsHolder",
353                                                           null,
354                                                           String JavaDoc.class.getName(),
355                                                           MBeanOperationInfo.INFO);
356         return mInfo;
357     }
358     
359     public String JavaDoc[] getStatisticNames() {
360         Stats JavaDoc stats = delegate.getStats();
361         if (stats != null) {
362             return stats.getStatisticNames();
363         } else {
364             return null;
365         }
366     }
367     
368     public Statistic JavaDoc[] getStatistics() {
369         Stats JavaDoc stats = delegate.getStats();
370         if (stats == null) {
371             return null;
372         }
373
374         Statistic JavaDoc[] statArray = stats.getStatistics();
375         boolean isSerializable = checkSerializability(statArray);
376         if(isSerializable) {
377             final Statistic JavaDoc[] hackedArray = StatisticWorkaround.populateDescriptions(statArray);
378             return hackedArray;
379         }
380         else
381             return null;
382     }
383     
384     private MBeanOperationInfo JavaDoc getStatisticNameInfo() {
385         MBeanOperationInfo JavaDoc mInfo = new MBeanOperationInfo JavaDoc("getStatisticNames",
386                                                           "Gets the names of all the statistics in the given Stats Object",
387                                                           null,
388                                                           String JavaDoc[].class.getName(),
389                                                           MBeanOperationInfo.INFO);
390         return mInfo;
391     }
392     
393     private MBeanOperationInfo JavaDoc getStatsInfo() {
394         MBeanOperationInfo JavaDoc mInfo = new MBeanOperationInfo JavaDoc("getStatistics",
395                                                           "returns the results of all the getXXX methods, in the given Stats object",
396                                                           null,
397                                                           Statistic JavaDoc[].class.getName(),
398                                                           MBeanOperationInfo.INFO);
399         return mInfo;
400     }
401     
402     private boolean checkSerializability(Object JavaDoc[] objArray) {
403         boolean isSerializable = true;
404         for(int i = 0; i < objArray.length; i++) {
405             isSerializable = (isSerializable) && (objArray[i] instanceof java.io.Serializable JavaDoc);
406         }
407         return isSerializable;
408     }
409     
410 }
411
Popular Tags