KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > management > server > AbstractManagedObject


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.server;
31
32 import com.caucho.jmx.Description;
33 import com.caucho.jmx.Jmx;
34
35 import javax.management.MalformedObjectNameException JavaDoc;
36 import javax.management.ObjectName JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 /**
42  * Parent mbean of all Resin's managed objects.
43  */

44 abstract public class AbstractManagedObject implements ManagedObjectMXBean {
45   private static final Logger JavaDoc log
46     = Logger.getLogger(AbstractManagedObject.class.getName());
47   
48   private ClassLoader JavaDoc _classLoader;
49   private ObjectName JavaDoc _objectName;
50
51   protected AbstractManagedObject()
52   {
53     this(Thread.currentThread().getContextClassLoader());
54   }
55
56   protected AbstractManagedObject(ClassLoader JavaDoc loader)
57   {
58     _classLoader = loader;
59   }
60   
61   /**
62    * Returns the {@link ObjectName} of the mbean.
63    */

64   @Description("The JMX ObjectName for the MBean")
65   public ObjectName JavaDoc getObjectName()
66   {
67     if (_objectName == null) {
68
69       try {
70         Map JavaDoc<String JavaDoc,String JavaDoc> props = Jmx.copyContextProperties(_classLoader);
71
72     props.put("type", getType());
73
74     String JavaDoc name = getName();
75     if (name != null) {
76       if (name.indexOf(':') >= 0)
77         name = ObjectName.quote(name);
78       
79       props.put("name", name);
80     }
81     //
82
_objectName = Jmx.getObjectName("resin", props);
83       } catch (MalformedObjectNameException JavaDoc e) {
84         throw new RuntimeException JavaDoc(e);
85       }
86     }
87
88     return _objectName;
89   }
90
91   /**
92    * The JMX name property of the mbean.
93    */

94   abstract public String JavaDoc getName();
95
96   /**
97    * The JMX type of this MBean, defaults to the prefix of the FooMXBean..
98    */

99   public String JavaDoc getType()
100   {
101     Class JavaDoc []interfaces = getClass().getInterfaces();
102
103     for (int i = 0; i < interfaces.length; i++) {
104       String JavaDoc className = interfaces[i].getName();
105       
106       if (className.endsWith("MXBean")) {
107     int p = className.lastIndexOf('.');
108     int q = className.indexOf("MXBean");
109
110     return className.substring(p + 1, q);
111       }
112     }
113
114     int p = getClass().getName().lastIndexOf('.');
115
116     return getClass().getName().substring(p + 1);
117   }
118
119   /**
120    * Registers the object with JMX.
121    */

122   protected boolean registerSelf()
123   {
124     try {
125       Jmx.register(this, getObjectName(), _classLoader);
126
127       return true;
128     } catch (RuntimeException JavaDoc e) {
129       throw e;
130     } catch (Exception JavaDoc e) {
131       log.log(Level.FINE, e.toString(), e);
132
133       return false;
134     }
135   }
136
137   /**
138    * Unregisters the object with JMX.
139    */

140   protected boolean unregisterSelf()
141   {
142     try {
143       Jmx.unregister(getObjectName(), _classLoader);
144
145       return true;
146     } catch (Throwable JavaDoc e) {
147       log.log(Level.FINE, e.toString(), e);
148
149       return false;
150     }
151   }
152 }
153
Popular Tags