KickJava   Java API By Example, From Geeks To Geeks.

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


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 /* StatsHolderImpl.java
25  * $Id: StatsHolderImpl.java,v 1.3 2005/12/25 03:43:36 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:43:36 $
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 com.sun.enterprise.admin.monitor.registry.*;
42 import javax.management.j2ee.statistics.*;
43 import javax.management.*;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Hashtable JavaDoc;
46 import java.util.Enumeration JavaDoc;
47 import java.util.Collection JavaDoc;
48 import java.util.logging.*;
49 import com.sun.enterprise.admin.common.constant.AdminConstants;
50 import com.sun.enterprise.util.i18n.StringManager;
51 /**
52  * Creates and holds a hierarchy of Objects that represent various dependent
53  * components that require monitoring
54  * @author Shreedhar Ganapathy<mailto:shreedhar.ganapathy@sun.com>
55  */

56 public class StatsHolderImpl implements StatsHolder {
57     private final String JavaDoc name;
58     private final Hashtable JavaDoc children;
59     private final MBeanServer server;
60     private Class JavaDoc statsClass = null;
61     private String JavaDoc statsClassName = null;
62     /* these can be set after constructing the object */
63     MonitoredObjectType type = null;
64     Stats stats = null;
65     ObjectName objectName = null;
66     String JavaDoc dottedName = null;
67     /* these can be set after constructing the object */
68     private static final Logger logger = Logger.getLogger(AdminConstants.kLoggerName); //better way
69
private static final StringManager sm = StringManager.getManager(StatsHolderImpl.class);
70     /** Creates a new instance of StatsHolderImpl */
71     public StatsHolderImpl(String JavaDoc name) {
72         this.name = name;
73         this.children = new Hashtable JavaDoc();
74         this.server = findMBeanServer();
75         logger.fine("StatsHolderImpl initialized, name = " + name);
76     }
77     
78     public StatsHolderImpl(String JavaDoc name, MonitoredObjectType type) {
79         this(name);
80         this.setType(type);
81     }
82     
83     /**
84      * finds and returns an MBeanServer instance
85      */

86     private MBeanServer findMBeanServer() {
87         MBeanServer server = null;
88         ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
89         if(!servers.isEmpty()){
90             server = (MBeanServer)servers.get(0);
91         }
92         return server;
93     }
94     
95     /**
96      * Add a child node or leaf to this node.
97      * @param statsHolder
98      */

99     private StatsHolder addChild(StatsHolder sh) {
100         // The name of the child is the unique key
101
assert (sh != null) : "Null StatsHolder to be added";
102         if(!children.containsKey(sh.getName())){
103             children.put(sh.getName(), sh);
104             logger.fine("StatsHolder.addChild: New key created with statsHolder key name = " + sh.getName() + " type = " + sh.getType().toString());
105         }
106         else {
107             sh = this.getChild(sh.getName());
108             logger.fine("Not adding, StatsHolder.addChild: Child exists, name = " + sh.getName() + " type = " + sh.getType().toString());
109             //Ideally an exception should be thrown
110
}
111         return ( sh );
112     }
113     
114     /**
115      * Add a child node or leaf to this node
116      * @param name
117      * @param type
118      * @return StatsHolder
119      */

120     public StatsHolder addChild(String JavaDoc name, MonitoredObjectType type) {
121         assert ( name != null && type != null) : "Asked to add a null name-type child";
122         StatsHolder child = this.getChild(name);
123         if (child == null)
124             child = this.addChild(new StatsHolderImpl(name, type));
125         
126         return ( child );
127     }
128
129     /**
130      * return an array of StatHolder objects each representing a child
131      * of this node.
132      * @return Collection
133      */

134     public Collection JavaDoc getAllChildren() {
135         return children.values();
136     }
137
138     /**
139      * removes all children belonging to this node.
140      */

141      public void removeAllChildren() {
142          //removes all the children, grandchildren etc. recursively and deregisters mbeans
143
if (children.isEmpty()) {
144              this.unregisterMBean();
145          }
146          else {
147              final Enumeration JavaDoc e = children.elements();
148              while (e.hasMoreElements()) {
149                  final StatsHolder sh = (StatsHolder)e.nextElement();
150                  sh.removeAllChildren();
151              }
152              children.clear();
153          }
154      }
155
156     /**
157      * Remove a child node or leaf from this node.
158      * @param name
159      * @param type
160      */

161     public void removeChild(String JavaDoc name) {
162         children.remove(name);
163     }
164
165     /**
166      * Returns name of this hierarchical node
167      */

168     public String JavaDoc getName(){
169         return name;
170     }
171
172     /**
173      * Returns type of this hierarchical node
174      */

175     public MonitoredObjectType getType(){
176         return type;
177     }
178
179     /**
180      * sets this hierarchical node's associated stats object. Used when node was
181      * originally created without a Stats implementation or a new monitoring
182      * level has been set requiring a new Stats registration
183      */

184     public void setStats(Stats stats) {
185         this.stats=stats;
186     }
187     
188     public Stats getStats() {
189         return ( this.stats );
190     }
191     /**
192      * sets the hierarchically denoted dotted name for this node.
193      */

194     public void setObjectName(ObjectName name) {
195         this.objectName = name;
196     }
197     
198     public ObjectName getObjectName(){
199         return objectName;
200     }
201
202     public void setDottedName(String JavaDoc dottedName) {
203         this.dottedName = dottedName;
204     }
205     
206     public String JavaDoc getDottedName() {
207         return dottedName;
208     }
209     
210     /**
211      * registers a monitoring MBean with the MBeanServer
212      */

213     public void registerMBean() {
214         try {
215             if (server.isRegistered(this.objectName)) {
216                 logger.fine("ObjectName obj is already registered - this is an error, ignoring for now : " + this.objectName);
217                 return;
218             }
219             //final DynamicMBean mbean = MonitoringMBeansFactory.generateMonitoringMBean(stats);
220
//server.registerMBean(mbean, this.objectName);
221
final StatsHolderMBean mbean = new StatsHolderMBeanImpl(this);
222             server.registerMBean(mbean, this.objectName);
223             logger.finer("Registered the MBean for this StatsHolder: " + this.objectName);
224         }
225         catch(Exception JavaDoc e) {
226             logger.fine("SH.registerMBean: Exception for object-name " + this.objectName);
227             logger.throwing(StatsHolderImpl.class.getName(), "registerMBean()", e);
228             //squelching it for now - throw RuntimeException
229
}
230     }
231     
232     /**
233      * unregisters a monitoring MBean from the MBean Server
234      */

235     public void unregisterMBean() {
236         String JavaDoc msg = null;
237         try {
238             if (server.isRegistered(this.objectName)) {
239                 server.unregisterMBean(this.objectName);
240                 logger.fine("SH.unregisterMonitoringMBean(): unregistered - " + objectName);
241             }
242             else {
243                 logger.fine("SH.unregisterMonitoringMBean(): never registered - so not unregistering" + objectName);
244             }
245         }
246         catch(Exception JavaDoc e){
247             logger.fine("SH.unregisterMBeanUnregistration failed, objectName = " + objectName);
248             //squelching for now - throw RuntimeException
249
}
250     }
251     
252     public void setType(MonitoredObjectType type) {
253         this.type = type;
254     }
255     
256     public StatsHolder getChild(String JavaDoc name) {
257         final StatsHolder child = (StatsHolder) children.get(name);
258         if (child == null) {
259             logger.fine("SH.getChild - child is null with name = " + name);
260         }
261         return ( child );
262     }
263     
264     /** this is just a convenience - debugging method */
265     void write() {
266         //new DumpThread().start();
267
}
268     
269     public Class JavaDoc getStatsClass() {
270         return ( this.statsClass );
271     }
272     
273     public void setStatsClass(Class JavaDoc c) {
274         if (! javax.management.j2ee.statistics.Stats JavaDoc.class.isAssignableFrom(c)) {
275             final String JavaDoc msg = sm.getString("sh.not_a_stats_interface", c.getName());
276             throw new IllegalArgumentException JavaDoc(msg);
277         }
278         //the given class has to be an interface.
279
if (! c.isInterface()) {
280             final String JavaDoc msg = sm.getString("sh.should_be_an_interface", c.getName());
281             throw new IllegalArgumentException JavaDoc(msg);
282         }
283         
284         this.statsClass = c;
285         this.statsClassName = c.getName();
286     }
287     
288     public String JavaDoc getStatsClassName() {
289         return ( this.statsClassName );
290     }
291     
292     public void setStatsClassName(String JavaDoc cName) {
293         Class JavaDoc c = null;
294         try {
295             c = Class.forName(cName);
296         } catch (ClassNotFoundException JavaDoc cnfe) {
297             final String JavaDoc msg = sm.getString("invalidclassname.statsholder",
298             cName);
299             throw new IllegalArgumentException JavaDoc(msg);
300         }
301
302         if (! javax.management.j2ee.statistics.Stats JavaDoc.class.isAssignableFrom(c)) {
303             final String JavaDoc msg = sm.getString("sh.not_a_stats_interface", c.getName());
304             throw new IllegalArgumentException JavaDoc(msg);
305         }
306         //the given class has to be an interface.
307
if (! c.isInterface()) {
308             final String JavaDoc msg = sm.getString("sh.should_be_an_interface", c.getName());
309             throw new IllegalArgumentException JavaDoc(msg);
310         }
311         
312         this.statsClass = c;
313         this.statsClassName = cName;
314     }
315
316     private class DumpThread extends Thread JavaDoc {
317         public void run() {
318             while (true) {
319                 logger.fine("Start Element: " + name);
320                 if (children.isEmpty()) {
321                     logger.fine("Leaf Element");
322                 }
323                 else {
324                     final Enumeration JavaDoc e = children.elements();
325                     while (e.hasMoreElements()) {
326                         final StatsHolderImpl c = (StatsHolderImpl) e.nextElement();
327                         c.write();
328                     }
329                 }
330                 logger.fine("End Element: " + name);
331                 try {
332                     Thread.sleep(5000);
333                 }
334                 catch(InterruptedException JavaDoc e) {}
335             }
336         }
337     }
338 }
339
Popular Tags