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.jmx;
031:
032: import com.caucho.log.Log;
033: import com.caucho.util.L10N;
034:
035: import javax.management.*;
036: import java.util.logging.Logger;
037:
038: /**
039: * Wrapper around the dynamic mbean to handle classloader lifecycle.
040: */
041: class MBeanWrapper implements DynamicMBean {
042: private static L10N L = new L10N(MBeanWrapper.class);
043: private static Logger log = Log.open(MBeanWrapper.class);
044:
045: private MBeanContext _context;
046:
047: private ObjectName _name;
048:
049: protected Object _object;
050: protected DynamicMBean _mbean;
051:
052: private ObjectInstance _instance;
053:
054: protected MBeanWrapper(MBeanContext context, ObjectName name,
055: Object object, DynamicMBean mbean) {
056: _context = context;
057:
058: _name = name;
059:
060: _object = object;
061: _mbean = mbean;
062: }
063:
064: /**
065: * Returns the object instance for the mbean.
066: */
067: public ObjectInstance getObjectInstance() {
068: if (_instance == null)
069: _instance = new ObjectInstance(_name, getMBeanInfo()
070: .getClassName());
071:
072: return _instance;
073: }
074:
075: /**
076: * Returns the context.
077: */
078: MBeanContext getContext() {
079: return _context;
080: }
081:
082: /**
083: * Returns the object.
084: */
085: Object getObject() {
086: return _object;
087: }
088:
089: /**
090: * Returns the object as a broadcaster.
091: */
092: private NotificationBroadcaster getBroadcaster() {
093: return (NotificationBroadcaster) _object;
094: }
095:
096: /**
097: * Returns the object as an emitter.
098: */
099: private NotificationEmitter getEmitter() {
100: return (NotificationEmitter) _object;
101: }
102:
103: /**
104: * Returns the name.
105: */
106: public ObjectName getObjectName() {
107: return _name;
108: }
109:
110: /**
111: * Returns the class loader.
112: */
113: public ClassLoader getClassLoader() {
114: return _context.getClassLoader();
115: }
116:
117: /**
118: * Returns the MBeanInfo meta-data.
119: */
120: public MBeanInfo getMBeanInfo() {
121: return _mbean.getMBeanInfo();
122: }
123:
124: /**
125: * Returns the named attribute.
126: */
127: public Object getAttribute(String name) throws ReflectionException,
128: AttributeNotFoundException, MBeanException {
129: return _mbean.getAttribute(name);
130: }
131:
132: /**
133: * Returns an array of attributes.
134: */
135: public AttributeList getAttributes(String[] names) {
136: return _mbean.getAttributes(names);
137: }
138:
139: /**
140: * Sets the named attribute.
141: */
142: public void setAttribute(Attribute attr)
143: throws ReflectionException, AttributeNotFoundException,
144: InvalidAttributeValueException, MBeanException {
145: _mbean.setAttribute(attr);
146: }
147:
148: /**
149: * Returns an array of attributes.
150: */
151: public AttributeList setAttributes(AttributeList list) {
152: return _mbean.setAttributes(list);
153: }
154:
155: /**
156: * Invokes the operation.
157: */
158: public Object invoke(String operation, Object[] params,
159: String[] signature) throws ReflectionException,
160: MBeanException {
161: return _mbean.invoke(operation, params, signature);
162: }
163:
164: /**
165: * Returns as listener.
166: */
167: public NotificationListener getListener() {
168: Object obj = getObject();
169:
170: if (obj instanceof NotificationListener)
171: return (NotificationListener) obj;
172: else
173: return null;
174: }
175:
176: /**
177: * Adds a notification listener.
178: */
179: public void addNotificationListener(NotificationListener listener,
180: NotificationFilter filter, Object handback) {
181: getBroadcaster().addNotificationListener(listener, filter,
182: handback);
183: }
184:
185: /**
186: * Removes a notification listener.
187: */
188: public void removeNotificationListener(NotificationListener listener)
189: throws ListenerNotFoundException {
190: getBroadcaster().removeNotificationListener(listener);
191: }
192:
193: /**
194: * Removes a notification listener.
195: */
196: public void removeNotificationListener(
197: NotificationListener listener, NotificationFilter filter,
198: Object handback) throws ListenerNotFoundException {
199: Object obj = getObject();
200:
201: if (obj instanceof NotificationEmitter)
202: getEmitter().removeNotificationListener(listener, filter,
203: handback);
204: else
205: getBroadcaster().removeNotificationListener(listener);
206: }
207: }
|