001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.management.server;
031:
032: import com.caucho.jmx.Description;
033: import com.caucho.jmx.Jmx;
034:
035: import javax.management.MalformedObjectNameException;
036: import javax.management.ObjectName;
037: import java.util.Map;
038: import java.util.logging.Level;
039: import java.util.logging.Logger;
040:
041: /**
042: * Parent mbean of all Resin's managed objects.
043: */
044: abstract public class AbstractManagedObject implements
045: ManagedObjectMXBean {
046: private static final Logger log = Logger
047: .getLogger(AbstractManagedObject.class.getName());
048:
049: private ClassLoader _classLoader;
050: private ObjectName _objectName;
051:
052: protected AbstractManagedObject() {
053: this (Thread.currentThread().getContextClassLoader());
054: }
055:
056: protected AbstractManagedObject(ClassLoader loader) {
057: _classLoader = loader;
058: }
059:
060: /**
061: * Returns the {@link ObjectName} of the mbean.
062: */
063: @Description("The JMX ObjectName for the MBean")
064: public ObjectName getObjectName() {
065: if (_objectName == null) {
066:
067: try {
068: Map<String, String> props = Jmx
069: .copyContextProperties(_classLoader);
070:
071: props.put("type", getType());
072:
073: String name = getName();
074: if (name != null) {
075: if (name.indexOf(':') >= 0)
076: name = ObjectName.quote(name);
077:
078: props.put("name", name);
079: }
080:
081: addObjectNameProperties(props);
082:
083: //
084: _objectName = Jmx.getObjectName("resin", props);
085: } catch (MalformedObjectNameException e) {
086: throw new RuntimeException(e);
087: }
088: }
089:
090: return _objectName;
091: }
092:
093: protected void addObjectNameProperties(Map<String, String> props)
094: throws MalformedObjectNameException {
095: }
096:
097: /**
098: * The JMX name property of the mbean.
099: */
100: abstract public String getName();
101:
102: /**
103: * The JMX type of this MBean, defaults to the prefix of the FooMXBean..
104: */
105: public String getType() {
106: Class[] interfaces = getClass().getInterfaces();
107:
108: for (int i = 0; i < interfaces.length; i++) {
109: String className = interfaces[i].getName();
110:
111: if (className.endsWith("MXBean")) {
112: int p = className.lastIndexOf('.');
113: int q = className.indexOf("MXBean");
114:
115: return className.substring(p + 1, q);
116: }
117: }
118:
119: int p = getClass().getName().lastIndexOf('.');
120:
121: return getClass().getName().substring(p + 1);
122: }
123:
124: /**
125: * Registers the object with JMX.
126: */
127: protected boolean registerSelf() {
128: try {
129: Jmx.register(this , getObjectName(), _classLoader);
130:
131: return true;
132: } catch (RuntimeException e) {
133: throw e;
134: } catch (Exception e) {
135: log.log(Level.FINER, e.toString(), e);
136:
137: return false;
138: }
139: }
140:
141: /**
142: * Unregisters the object with JMX.
143: */
144: protected boolean unregisterSelf() {
145: try {
146: Jmx.unregister(getObjectName(), _classLoader);
147:
148: return true;
149: } catch (Throwable e) {
150: log.log(Level.FINE, e.toString(), e);
151:
152: return false;
153: }
154: }
155: }
|