KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > management > j2ee > J2EEManagedObject


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.management.j2ee;
31
32 import com.caucho.jmx.IntrospectionMBean;
33 import com.caucho.jmx.Jmx;
34 import com.caucho.server.host.Host;
35 import com.caucho.server.webapp.WebApp;
36 import com.caucho.util.Alarm;
37
38 import javax.management.MalformedObjectNameException JavaDoc;
39 import javax.management.ObjectName JavaDoc;
40 import java.util.Collection JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.Set JavaDoc;
44 import java.util.TreeSet JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 /**
49  * Base class management interface for all managed objects.
50  */

51 abstract public class J2EEManagedObject {
52   private static final Logger JavaDoc log
53     = Logger.getLogger(J2EEManagedObject.class.getName());
54
55   private static final String JavaDoc[] CONTEXT_KEYS = {
56     "J2EEServer",
57     "Host",
58     "J2EEApplication",
59     "WebModule"
60   };
61
62   private final long _startTime;
63
64   protected ObjectName JavaDoc _objectName;
65
66   public J2EEManagedObject()
67   {
68     _startTime = Alarm.getCurrentTime();
69   }
70
71   public String JavaDoc getObjectName()
72   {
73     return createObjectName().getCanonicalName();
74   }
75
76   ObjectName JavaDoc createObjectName()
77   {
78     if (_objectName == null) {
79       Hashtable JavaDoc<String JavaDoc,String JavaDoc> properties = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
80
81       try {
82         _objectName = createObjectName(properties);
83       }
84       catch (MalformedObjectNameException JavaDoc ex) {
85         if (log.isLoggable(Level.FINE)) {
86           StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
87
88           builder.append('\'');
89           for (Map.Entry JavaDoc<String JavaDoc,String JavaDoc> entry : properties.entrySet()) {
90             if (builder.length() > 0)
91               builder.append(',');
92
93             builder.append(entry.getKey());
94             builder.append('=');
95             builder.append(entry.getValue());
96           }
97
98           builder.append("' ");
99           builder.append(ex.toString());
100
101           log.log(Level.FINE, builder.toString(), ex);
102         }
103       }
104     }
105
106     return _objectName;
107   }
108
109   /**
110    * Returns the value to use for the the `name' key of the
111    * ObjectName. The returned value is raw, users of the method must escape
112    * the returned value for use in an ObjectName.
113    */

114   abstract protected String JavaDoc getName();
115
116   /**
117    * Returns true if the ObjectName should include the J2EEServer key.
118    * The default implementation returns true,
119    * derived class override to return false if there should not be a
120    * J2EEServer key.
121    */

122   protected boolean isJ2EEServer()
123   {
124     return true;
125   }
126
127   /**
128    * Returns true if the ObjectName should include the J2EEApplication key.
129    * The default implementation returns true,
130    * derived class override to return false if there should not be a
131    * J2EEApplication key.
132    */

133   protected boolean isJ2EEApplication()
134   {
135     return true;
136   }
137
138   protected String JavaDoc quote(String JavaDoc value)
139   {
140     if (value == null)
141       return "null";
142     else if (value.length() == 0)
143       return "default";
144     else {
145       for (int i = 0; i < value.length(); i++) {
146         char ch = value.charAt(i);
147
148         switch (ch) {
149           case ',':
150           case '=':
151           case '?':
152           case '"':
153           case ':':
154             return ObjectName.quote(value);
155         }
156       }
157
158       return value;
159     }
160   }
161
162   /**
163    * Creates the object name.
164    */

165   protected ObjectName JavaDoc createObjectName(Hashtable JavaDoc<String JavaDoc,String JavaDoc> properties)
166     throws MalformedObjectNameException JavaDoc
167   {
168     WebApp webApp = WebApp.getLocal();
169
170     if (webApp != null) {
171       String JavaDoc contextPath = webApp.getContextPath();
172
173       if (contextPath == null || contextPath.length() == 0)
174         contextPath = "/";
175
176       properties.put("WebModule", quote(contextPath));
177     }
178
179     Host host = Host.getLocal();
180
181     if (isJ2EEApplication()) {
182       J2EEApplication j2eeApplication = J2EEApplication.getLocal();
183
184       if (j2eeApplication == null)
185         properties.put("J2EEApplication", quote("null"));
186       else
187         properties.put("J2EEApplication", quote(j2eeApplication.getName()));
188     }
189
190     if (host != null)
191       properties.put("Host", quote(host.getName()));
192
193     if (isJ2EEServer()) {
194       J2EEServer j2eeServer = J2EEServer.getLocal();
195
196       if (j2eeServer != null)
197         properties.put("J2EEServer", quote(j2eeServer.getName()));
198     }
199
200     String JavaDoc j2eeType;
201
202     String JavaDoc className = getClass().getName();
203
204     int lastDot = className.lastIndexOf('.');
205
206     j2eeType = className.substring(lastDot + 1);
207
208     properties.put("j2eeType", quote(j2eeType));
209
210     String JavaDoc name = getName();
211
212     if (name == null)
213       name = "null";
214
215     properties.put("name", quote(name));
216
217     return new ObjectName JavaDoc("j2ee", properties);
218   }
219
220   /**
221    * Returns a list of ObjectNames that match the specified pattern composed
222    * of keys and values.
223    * The format of the arguments is <code>key1, value1, [keyN, valueN]</code>.
224    * The pattern does not need to include ",*", it is added automatically.
225    */

226   protected String JavaDoc[] queryObjectNames(String JavaDoc ... pattern)
227   {
228     TreeSet JavaDoc<String JavaDoc> objectNames = new TreeSet JavaDoc<String JavaDoc>();
229
230     queryObjectNames(objectNames, pattern);
231
232     return objectNames.toArray(new String JavaDoc[objectNames.size()]);
233   }
234
235   /**
236    * Returns a list of ObjectNames that match the specified patterns.
237    * Each pattern is an array of keys and values.
238    * The pattern does not need to include ",*", it is added automatically.
239    */

240   protected String JavaDoc[] queryObjectNamesSet(String JavaDoc[][] patterns)
241   {
242     TreeSet JavaDoc<String JavaDoc> objectNames = new TreeSet JavaDoc<String JavaDoc>();
243
244     for (String JavaDoc[] pattern : patterns) {
245       queryObjectNames(objectNames, pattern);
246     }
247
248     return objectNames.toArray(new String JavaDoc[objectNames.size()]);
249   }
250
251   private void queryObjectNames(Collection JavaDoc<String JavaDoc> objectNames, String JavaDoc[] pattern)
252   {
253     try {
254       StringBuilder JavaDoc patternBuilder = new StringBuilder JavaDoc();
255
256       patternBuilder.append("j2ee:");
257
258       int length = pattern.length;
259
260       for (int i = 0; i < length; i++) {
261         if (i != 0)
262           patternBuilder.append(',');
263
264         String JavaDoc key = pattern[i];
265         String JavaDoc value = pattern[++i];
266
267         patternBuilder.append(key);
268         patternBuilder.append('=');
269         patternBuilder.append(quote(value));
270       }
271
272       for (String JavaDoc contextKey : CONTEXT_KEYS) {
273         if (patternBuilder.indexOf(contextKey) >= 0)
274           continue;
275
276         String JavaDoc value = _objectName.getKeyProperty(contextKey);
277
278         if (value != null) {
279           patternBuilder.append(',');
280           patternBuilder.append(contextKey);
281           patternBuilder.append('=');
282           patternBuilder.append(quote(value));
283         }
284       }
285
286       patternBuilder.append(",*");
287
288       ObjectName JavaDoc queryObjectName = new ObjectName JavaDoc(patternBuilder.toString());
289
290       Set JavaDoc<ObjectName JavaDoc> matchingObjectNames
291         = Jmx.getGlobalMBeanServer().queryNames(queryObjectName, null);
292
293       for (ObjectName JavaDoc matchingObjectName : matchingObjectNames)
294         objectNames.add(matchingObjectName.getCanonicalName());
295     }
296     catch (Exception JavaDoc ex) {
297       if (log.isLoggable(Level.FINE))
298         log.log(Level.FINE, ex.toString(), ex);
299     }
300   }
301
302   /**
303    * Returns true if the state is manageable
304    */

305   public boolean isStateManageable()
306   {
307     return (this instanceof StateManageable);
308   }
309
310   /**
311    * Returns true if the object provides statistics
312    */

313   public boolean isStatisticsProvider()
314   {
315     return (this instanceof StatisticsProvider);
316   }
317
318   /**
319    * Returns true if the object provides events
320    */

321   public boolean isEventProvider()
322   {
323     return (this instanceof EventProvider);
324   }
325
326   long getStartTime()
327   {
328     return _startTime;
329   }
330
331   /**
332    * Register a {@link J2EEManagedObject}.
333    * This method never throws an exception, any {@link Throwable} is caught
334    * and logged.
335    *
336    * @return the managed object if it is registered, null if there is an error.
337    */

338   public static <T extends J2EEManagedObject> T register(T managedObject)
339   {
340
341     if (managedObject == null)
342       return null;
343
344     ObjectName JavaDoc objectName = null;
345
346     try {
347       objectName = managedObject.createObjectName();
348
349       Object JavaDoc mbean = new IntrospectionMBean(managedObject, managedObject.getClass(), true);
350
351       // XXX: wait for 3.1
352
if (false) {
353         if (objectName != null)
354           Jmx.register(mbean, objectName);
355       }
356
357       return managedObject;
358     }
359     catch (Exception JavaDoc ex) {
360       if (log.isLoggable(Level.FINE))
361         log.log(Level.FINE, managedObject.getClass() + " " + objectName + " " + ex.toString(), ex);
362
363       return null;
364     }
365   }
366
367   /**
368    * Unregister a {@link J2EEManagedObject}.
369    * This method never throws an exception, any {@link Throwable} is caught
370    * and logged.
371    *
372    * @param managedObject the managed object, can be null in which case
373    * nothing is done.
374    */

375   public static void unregister(J2EEManagedObject managedObject)
376   {
377     if (managedObject == null)
378       return;
379
380     ObjectName JavaDoc objectName = null;
381
382     try {
383       objectName = managedObject.createObjectName();
384
385       // XXX: wait for 3.1
386
if (false) {
387         Jmx.unregister(objectName);
388       }
389     }
390     catch (Throwable JavaDoc ex) {
391       if (log.isLoggable(Level.FINEST))
392         log.log(Level.FINEST, managedObject.getClass() + " " + objectName + " " + ex.toString(), ex);
393     }
394   }
395 }
396
Popular Tags