0001: /*
0002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
0003: *
0004: * This file is part of Resin(R) Open Source
0005: *
0006: * Each copy or derived work must preserve the copyright notice and this
0007: * notice unmodified.
0008: *
0009: * Resin Open Source is free software; you can redistribute it and/or modify
0010: * it under the terms of the GNU General Public License as published by
0011: * the Free Software Foundation; either version 2 of the License, or
0012: * (at your option) any later version.
0013: *
0014: * Resin Open Source is distributed in the hope that it will be useful,
0015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
0017: * of NON-INFRINGEMENT. See the GNU General Public License for more
0018: * details.
0019: *
0020: * You should have received a copy of the GNU General Public License
0021: * along with Resin Open Source; if not, write to the
0022: * Free SoftwareFoundation, Inc.
0023: * 59 Temple Place, Suite 330
0024: * Boston, MA 02111-1307 USA
0025: *
0026: * @author Scott Ferguson
0027: */
0028:
0029: package com.caucho.jmx;
0030:
0031: import com.caucho.loader.Environment;
0032: import com.caucho.loader.WeakCloseListener;
0033: import com.caucho.util.L10N;
0034:
0035: import javax.management.*;
0036: import javax.management.loading.ClassLoaderRepository;
0037: import java.io.ObjectInputStream;
0038: import java.lang.reflect.Constructor;
0039: import java.lang.reflect.InvocationTargetException;
0040: import java.lang.reflect.Modifier;
0041: import java.util.Set;
0042: import java.util.logging.Level;
0043: import java.util.logging.Logger;
0044:
0045: /**
0046: * The main interface for retrieving and managing JMX objects.
0047: */
0048: abstract public class AbstractMBeanServer implements MBeanServer {
0049: private static final L10N L = new L10N(AbstractMBeanServer.class);
0050: private static final Logger log = Logger
0051: .getLogger(AbstractMBeanServer.class.getName());
0052:
0053: static ObjectName SERVER_DELEGATE_NAME;
0054:
0055: // default domain
0056: private String _defaultDomain;
0057:
0058: /**
0059: * Creats a new MBeanServer implementation.
0060: */
0061: public AbstractMBeanServer(String defaultDomain) {
0062: _defaultDomain = defaultDomain;
0063:
0064: Environment.addClassLoaderListener(new WeakCloseListener(this ));
0065: }
0066:
0067: /**
0068: * Returns the context implementation.
0069: */
0070: protected MBeanContext createContext() {
0071: return createContext(Thread.currentThread()
0072: .getContextClassLoader());
0073: }
0074:
0075: /**
0076: * Returns the context implementation.
0077: */
0078: protected MBeanContext getExistingContext() {
0079: return getExistingContext(Thread.currentThread()
0080: .getContextClassLoader());
0081: }
0082:
0083: /**
0084: * Returns the context implementation.
0085: */
0086: protected MBeanContext getGlobalContext() {
0087: return createContext(ClassLoader.getSystemClassLoader());
0088: }
0089:
0090: /**
0091: * Returns the context implementation, creating if necessary.
0092: */
0093: abstract protected MBeanContext createContext(ClassLoader loader);
0094:
0095: /**
0096: * Returns the context implementation.
0097: */
0098: abstract protected MBeanContext getExistingContext(
0099: ClassLoader loader);
0100:
0101: /**
0102: * Returns the context implementation.
0103: */
0104: abstract protected MBeanContext getContext(ClassLoader loader);
0105:
0106: /**
0107: * Removes the context implementation.
0108: */
0109: protected void removeContext(MBeanContext context,
0110: ClassLoader loader) {
0111: }
0112:
0113: /**
0114: * Returns the view implementation.
0115: */
0116: protected MBeanView getView() {
0117: return createContext().getView();
0118: }
0119:
0120: /**
0121: * Returns the view implementation.
0122: */
0123: protected MBeanView getGlobalView() {
0124: return getGlobalContext().getView();
0125: }
0126:
0127: /**
0128: * Returns the view implementation.
0129: */
0130: protected MBeanView getParentView() {
0131: return null;
0132: }
0133:
0134: /**
0135: * Instantiate an MBean object to be registered with the server.
0136: *
0137: * @param className the className to be instantiated.
0138: *
0139: * @return the instantiated object.
0140: */
0141: public Object instantiate(String className)
0142: throws ReflectionException, MBeanException {
0143: try {
0144: Class cl = getClassLoaderRepository().loadClass(className);
0145:
0146: return cl.newInstance();
0147: } catch (ClassNotFoundException e) {
0148: throw new ReflectionException(e);
0149: } catch (InstantiationException e) {
0150: throw new ReflectionException(e);
0151: } catch (ExceptionInInitializerError e) {
0152: Throwable cause = e.getCause();
0153: if (cause instanceof Exception)
0154: throw new MBeanException((Exception) cause);
0155: else
0156: throw e;
0157: } catch (IllegalAccessException e) {
0158: throw new ReflectionException(e);
0159: }
0160: }
0161:
0162: /**
0163: * Instantiate an MBean object to be registered with the server.
0164: *
0165: * @param className the className to be instantiated.
0166: * @param loaderName names the classloader to be used
0167: *
0168: * @return the instantiated object.
0169: */
0170: public Object instantiate(String className, ObjectName loaderName)
0171: throws ReflectionException, MBeanException,
0172: InstanceNotFoundException {
0173: MBeanWrapper mbean = getMBean(loaderName);
0174:
0175: if (mbean == null)
0176: throw new InstanceNotFoundException(String
0177: .valueOf(loaderName));
0178: else if (!(mbean.getObject() instanceof ClassLoader))
0179: throw new InstanceNotFoundException(L.l(
0180: "{0} is not a class loader", loaderName));
0181:
0182: try {
0183: ClassLoader loader = (ClassLoader) mbean.getObject();
0184:
0185: Class cl = loader.loadClass(className);
0186:
0187: return cl.newInstance();
0188: } catch (ClassNotFoundException e) {
0189: throw new ReflectionException(e);
0190: } catch (InstantiationException e) {
0191: throw new ReflectionException(e);
0192: } catch (IllegalAccessException e) {
0193: throw new ReflectionException(e);
0194: }
0195: }
0196:
0197: /**
0198: * Instantiate an MBean object with the given arguments to be
0199: * passed to the constructor.
0200: *
0201: * @param className the className to be instantiated.
0202: * @param params the parameters for the constructor.
0203: * @param signature the signature of the constructor
0204: *
0205: * @return the instantiated object.
0206: */
0207: public Object instantiate(String className, Object[] params,
0208: String[] signature) throws ReflectionException,
0209: MBeanException {
0210: try {
0211: Class cl = getClassLoaderRepository().loadClass(className);
0212:
0213: Constructor constructor = getConstructor(cl, signature);
0214:
0215: return constructor.newInstance(params);
0216: } catch (ClassNotFoundException e) {
0217: throw new ReflectionException(e);
0218: } catch (InstantiationException e) {
0219: throw new ReflectionException(e);
0220: } catch (InvocationTargetException e) {
0221: throw new MBeanException(e);
0222: } catch (IllegalAccessException e) {
0223: throw new ReflectionException(e);
0224: }
0225: }
0226:
0227: /**
0228: * Instantiate an MBean object with the given arguments to be
0229: * passed to the constructor.
0230: *
0231: * @param className the className to be instantiated.
0232: * @param loaderName names the classloader to be used
0233: * @param params the parameters for the constructor.
0234: * @param signature the signature of the constructor
0235: *
0236: * @return the instantiated object.
0237: */
0238: public Object instantiate(String className, ObjectName loaderName,
0239: Object[] params, String[] signature)
0240: throws ReflectionException, MBeanException,
0241: InstanceNotFoundException {
0242: MBeanWrapper mbean = getMBean(loaderName);
0243:
0244: if (mbean == null)
0245: throw new InstanceNotFoundException(String
0246: .valueOf(loaderName));
0247: else if (!(mbean.getObject() instanceof ClassLoader))
0248: throw new InstanceNotFoundException(L.l(
0249: "{0} is not a class loader", loaderName));
0250:
0251: try {
0252: ClassLoader loader = (ClassLoader) mbean.getObject();
0253:
0254: Class cl = loader.loadClass(className);
0255:
0256: Constructor constructor = getConstructor(cl, signature);
0257:
0258: return constructor.newInstance(params);
0259: } catch (ClassNotFoundException e) {
0260: throw new ReflectionException(e);
0261: } catch (InstantiationException e) {
0262: throw new ReflectionException(e);
0263: } catch (InvocationTargetException e) {
0264: throw new MBeanException(e);
0265: } catch (IllegalAccessException e) {
0266: throw new ReflectionException(e);
0267: }
0268: }
0269:
0270: /**
0271: * Returns the class's constructor with the matching sig.
0272: */
0273: private Constructor getConstructor(Class cl, String[] sig) {
0274: Constructor[] constructors = cl.getConstructors();
0275:
0276: for (int i = 0; i < constructors.length; i++) {
0277: if (!Modifier.isPublic(constructors[i].getModifiers()))
0278: continue;
0279:
0280: if (isMatch(constructors[i].getParameterTypes(), sig))
0281: return constructors[i];
0282: }
0283:
0284: return null;
0285: }
0286:
0287: /**
0288: * Matches the parameters the sig.
0289: */
0290: private boolean isMatch(Class[] param, String[] sig) {
0291: if (param.length != sig.length)
0292: return false;
0293:
0294: for (int i = 0; i < param.length; i++) {
0295: if (!param[i].getName().equals(sig[i]))
0296: return false;
0297: }
0298:
0299: return true;
0300: }
0301:
0302: /**
0303: * Instantiate and register an MBean.
0304: *
0305: * @param className the className to be instantiated.
0306: * @param name the name of the mbean.
0307: *
0308: * @return the instantiated object.
0309: */
0310: public ObjectInstance createMBean(String className, ObjectName name)
0311: throws ReflectionException, InstanceAlreadyExistsException,
0312: MBeanException, NotCompliantMBeanException {
0313: return registerMBean(instantiate(className), name);
0314: }
0315:
0316: /**
0317: * Instantiate and register an MBean.
0318: *
0319: * @param className the className to be instantiated.
0320: * @param name the name of the mbean.
0321: * @param loaderName the name of the class loader to user
0322: *
0323: * @return the instantiated object.
0324: */
0325: public ObjectInstance createMBean(String className,
0326: ObjectName name, ObjectName loaderName)
0327: throws ReflectionException, InstanceAlreadyExistsException,
0328: MBeanException, NotCompliantMBeanException,
0329: InstanceNotFoundException {
0330: return registerMBean(instantiate(className, loaderName), name);
0331: }
0332:
0333: /**
0334: * Instantiate and register an MBean.
0335: *
0336: * @param className the className to be instantiated.
0337: * @param name the name of the mbean.
0338: * @param params the parameters for the constructor.
0339: * @param signature the signature of the constructor
0340: *
0341: * @return the instantiated object.
0342: */
0343: public ObjectInstance createMBean(String className,
0344: ObjectName name, Object[] params, String[] signature)
0345: throws ReflectionException, InstanceAlreadyExistsException,
0346: MBeanException, NotCompliantMBeanException {
0347: return registerMBean(instantiate(className, params, signature),
0348: name);
0349: }
0350:
0351: /**
0352: * Instantiate and register an MBean.
0353: *
0354: * @param className the className to be instantiated.
0355: * @param name the name of the mbean.
0356: * @param loaderName the loader name for the mbean.
0357: * @param params the parameters for the constructor.
0358: * @param signature the signature of the constructor
0359: *
0360: * @return the instantiated object.
0361: */
0362: public ObjectInstance createMBean(String className,
0363: ObjectName name, ObjectName loaderName, Object[] params,
0364: String[] signature) throws ReflectionException,
0365: InstanceAlreadyExistsException, MBeanException,
0366: NotCompliantMBeanException, InstanceNotFoundException {
0367: return registerMBean(instantiate(className, loaderName, params,
0368: signature), name);
0369: }
0370:
0371: /**
0372: * Registers an MBean with the server.
0373: *
0374: * @param object the object to be registered as an MBean
0375: * @param name the name of the mbean.
0376: *
0377: * @return the instantiated object.
0378: */
0379: public ObjectInstance registerMBean(Object object, ObjectName name)
0380: throws InstanceAlreadyExistsException,
0381: MBeanRegistrationException, NotCompliantMBeanException {
0382: if (object == null)
0383: throw new NullPointerException();
0384:
0385: MBeanContext context;
0386:
0387: context = createContext();
0388:
0389: if (context.getMBean(name) != null) {
0390: throw new InstanceAlreadyExistsException(String
0391: .valueOf(name));
0392: }
0393:
0394: DynamicMBean dynMBean = createMBean(object, name);
0395:
0396: if (object instanceof IntrospectionMBean)
0397: object = ((IntrospectionMBean) object).getImplementation();
0398: else if (object instanceof StandardMBean) {
0399: object = ((StandardMBean) object).getImplementation();
0400: }
0401:
0402: MBeanWrapper mbean = new MBeanWrapper(context, name, object,
0403: dynMBean);
0404: return context.registerMBean(mbean, name);
0405: }
0406:
0407: /**
0408: * Creates the dynamic mbean.
0409: */
0410: private DynamicMBean createMBean(Object obj, ObjectName name)
0411: throws NotCompliantMBeanException {
0412: if (obj == null)
0413: throw new NotCompliantMBeanException(L.l(
0414: "{0} mbean is null", name));
0415: else if (obj instanceof DynamicMBean)
0416: return (DynamicMBean) obj;
0417:
0418: Class ifc = getMBeanInterface(obj.getClass());
0419:
0420: if (ifc == null)
0421: throw new NotCompliantMBeanException(L.l(
0422: "{0} mbean has no MBean interface for class {1}",
0423: name, obj.getClass().getName()));
0424:
0425: return new IntrospectionMBean(obj, ifc);
0426: }
0427:
0428: /**
0429: * Returns the mbean interface.
0430: */
0431: private Class getMBeanInterface(Class cl) {
0432: for (; cl != null; cl = cl.getSuperclass()) {
0433: Class[] interfaces = cl.getInterfaces();
0434:
0435: String mbeanName = cl.getName() + "MBean";
0436: String mxbeanName = cl.getName() + "MXBean";
0437:
0438: int p = mbeanName.lastIndexOf('.');
0439: mbeanName = mbeanName.substring(p);
0440:
0441: p = mxbeanName.lastIndexOf('.');
0442: mxbeanName = mxbeanName.substring(p);
0443:
0444: for (int i = 0; i < interfaces.length; i++) {
0445: Class ifc = interfaces[i];
0446:
0447: if (ifc.getName().endsWith(mbeanName)
0448: || ifc.getName().endsWith(mxbeanName))
0449: return ifc;
0450: }
0451: }
0452:
0453: return null;
0454: }
0455:
0456: /**
0457: * Unregisters an MBean from the server.
0458: *
0459: * @param name the name of the mbean.
0460: */
0461: public void unregisterMBean(ObjectName name)
0462: throws InstanceNotFoundException,
0463: MBeanRegistrationException {
0464: MBeanContext context = getExistingContext();
0465:
0466: if (context != null) {
0467: context.unregisterMBean(name);
0468:
0469: log.finer(name + " unregistered from " + this );
0470: }
0471:
0472: // XXX: getDelegate().sendUnregisterNotification(name);
0473: }
0474:
0475: /**
0476: * Returns the MBean registered with the given name.
0477: *
0478: * @param name the name of the mbean.
0479: *
0480: * @return the matching mbean object.
0481: */
0482: public ObjectInstance getObjectInstance(ObjectName name)
0483: throws InstanceNotFoundException {
0484: MBeanWrapper mbean = getMBean(name);
0485:
0486: if (mbean == null)
0487: throw new InstanceNotFoundException(String.valueOf(name));
0488:
0489: return mbean.getObjectInstance();
0490: }
0491:
0492: /**
0493: * Returns a set of MBeans matching the query.
0494: *
0495: * @param name the name of the mbean to match.
0496: * @param query the queryd to match.
0497: *
0498: * @return the set of matching mbean object.
0499: */
0500: public Set<ObjectInstance> queryMBeans(ObjectName name,
0501: QueryExp query) {
0502: try {
0503: if (query != null) {
0504: query.setMBeanServer(this );
0505: }
0506:
0507: return getView().queryMBeans(name, query);
0508: } catch (Exception e) {
0509: log.log(Level.WARNING, e.toString(), e);
0510:
0511: return null;
0512: }
0513: }
0514:
0515: /**
0516: * Returns a set of names for MBeans matching the query.
0517: *
0518: * @param name the name of the mbean to match.
0519: * @param query the query to match.
0520: *
0521: * @return the set of matching mbean names.
0522: */
0523: public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
0524: try {
0525: if (query != null) {
0526: query.setMBeanServer(this );
0527: }
0528:
0529: return getView().queryNames(name, query);
0530: } catch (Exception e) {
0531: log.log(Level.WARNING, e.toString(), e);
0532:
0533: return null;
0534: }
0535: }
0536:
0537: /**
0538: * Returns true if the given object is registered with the server.
0539: *
0540: * @param name the name of the mbean to test.
0541: *
0542: * @return true if the object is registered.
0543: */
0544: public boolean isRegistered(ObjectName name) {
0545: return getView().getMBean(name) != null;
0546: }
0547:
0548: /**
0549: * Returns the number of MBeans registered.
0550: *
0551: * @return the number of registered mbeans.
0552: */
0553: public Integer getMBeanCount() {
0554: return new Integer(getView().getMBeanCount());
0555: }
0556:
0557: /**
0558: * Returns a specific attribute of a named MBean.
0559: *
0560: * @param name the name of the mbean to test
0561: * @param attribute the name of the attribute to retrieve
0562: *
0563: * @return the attribute value
0564: */
0565: public Object getAttribute(ObjectName name, String attribute)
0566: throws MBeanException, AttributeNotFoundException,
0567: InstanceNotFoundException, ReflectionException {
0568: MBeanWrapper mbean = getMBean(name);
0569:
0570: if (mbean == null) {
0571: throw new InstanceNotFoundException(String.valueOf(name));
0572: }
0573:
0574: return mbean.getAttribute(attribute);
0575: }
0576:
0577: /**
0578: * Returns a list of several MBean attributes.
0579: *
0580: * @param name the name of the mbean
0581: * @param attributes the name of the attributes to retrieve
0582: *
0583: * @return the attribute value
0584: */
0585: public AttributeList getAttributes(ObjectName name,
0586: String[] attributes) throws InstanceNotFoundException,
0587: ReflectionException {
0588: MBeanWrapper mbean = getMBean(name);
0589:
0590: if (mbean == null)
0591: throw new InstanceNotFoundException(String.valueOf(name));
0592:
0593: return mbean.getAttributes(attributes);
0594: }
0595:
0596: /**
0597: * Sets an attribute in the MBean.
0598: *
0599: * @param name the name of the mbean
0600: * @param attribute the name/value of the attribute to set.
0601: */
0602: public void setAttribute(ObjectName name, Attribute attribute)
0603: throws InstanceNotFoundException,
0604: AttributeNotFoundException, InvalidAttributeValueException,
0605: MBeanException, ReflectionException {
0606: MBeanWrapper mbean = getMBean(name);
0607:
0608: if (mbean == null)
0609: throw new InstanceNotFoundException(String.valueOf(name));
0610:
0611: mbean.setAttribute(attribute);
0612: }
0613:
0614: /**
0615: * Set an attributes in the MBean.
0616: *
0617: * @param name the name of the mbean
0618: * @param attributes the name/value list of the attribute to set.
0619: */
0620: public AttributeList setAttributes(ObjectName name,
0621: AttributeList attributes) throws InstanceNotFoundException,
0622: ReflectionException {
0623: MBeanWrapper mbean = getMBean(name);
0624:
0625: if (mbean == null)
0626: throw new InstanceNotFoundException(String.valueOf(name));
0627:
0628: return mbean.setAttributes(attributes);
0629: }
0630:
0631: /**
0632: * Invokers an operation on an MBean.
0633: *
0634: * @param name the name of the mbean
0635: * @param operationName the name of the method to invoke
0636: * @param params the parameters for the invocation
0637: * @param signature the signature of the operation
0638: */
0639: public Object invoke(ObjectName name, String operationName,
0640: Object[] params, String[] signature)
0641: throws InstanceNotFoundException, MBeanException,
0642: ReflectionException {
0643: MBeanWrapper mbean = getMBean(name);
0644:
0645: if (mbean == null)
0646: throw new InstanceNotFoundException(String.valueOf(name));
0647:
0648: return mbean.invoke(operationName, params, signature);
0649: }
0650:
0651: /**
0652: * Returns the default domain for naming the MBean
0653: */
0654: public String getDefaultDomain() {
0655: return _defaultDomain;
0656: }
0657:
0658: /**
0659: * Adds a listener to a registered MBean
0660: *
0661: * @param name the name of the mbean
0662: * @param listener the listener object
0663: * @param filter filters events the listener is interested in
0664: * @param handback context to be returned to the listener
0665: */
0666: public void addNotificationListener(ObjectName name,
0667: NotificationListener listener, NotificationFilter filter,
0668: Object handback) throws InstanceNotFoundException {
0669: MBeanWrapper mbean = getMBean(name);
0670:
0671: if (mbean == null)
0672: throw new InstanceNotFoundException(String.valueOf(name));
0673:
0674: mbean.addNotificationListener(listener, filter, handback);
0675:
0676: createContext().addNotificationListener(name, listener, filter,
0677: handback);
0678: }
0679:
0680: /**
0681: * Adds a listener to a registered MBean
0682: *
0683: * @param name the name of the mbean
0684: * @param listenerName the name of the listener
0685: * @param filter filters events the listener is interested in
0686: * @param handback context to be returned to the listener
0687: */
0688: public void addNotificationListener(ObjectName name,
0689: ObjectName listenerName, NotificationFilter filter,
0690: Object handback) throws InstanceNotFoundException {
0691: MBeanWrapper listenerMBean = getMBean(listenerName);
0692:
0693: if (listenerMBean == null)
0694: throw new InstanceNotFoundException(String
0695: .valueOf(listenerName));
0696:
0697: NotificationListener listener = listenerMBean.getListener();
0698:
0699: if (listener == null) {
0700: IllegalArgumentException exn = new IllegalArgumentException(
0701: L.l("{0} does not implement NotificationListener.",
0702: listenerName));
0703: throw new RuntimeOperationsException(exn);
0704: }
0705:
0706: addNotificationListener(name, listener, filter, handback);
0707: }
0708:
0709: /**
0710: * Removes a listener from a registered MBean
0711: *
0712: * @param name the name of the mbean
0713: * @param listener the listener object
0714: */
0715: public void removeNotificationListener(ObjectName name,
0716: NotificationListener listener)
0717: throws InstanceNotFoundException, ListenerNotFoundException {
0718: MBeanWrapper mbean = getMBean(name);
0719:
0720: if (mbean == null)
0721: throw new InstanceNotFoundException(String.valueOf(name));
0722:
0723: mbean.removeNotificationListener(listener);
0724:
0725: createContext().removeNotificationListener(name, listener);
0726: }
0727:
0728: /**
0729: * Removes a listener from a registered MBean
0730: *
0731: * @param name the name of the mbean
0732: * @param listenerName the name of the listener
0733: */
0734: public void removeNotificationListener(ObjectName name,
0735: ObjectName listenerName) throws InstanceNotFoundException,
0736: ListenerNotFoundException {
0737: MBeanWrapper listenerMBean = getMBean(listenerName);
0738:
0739: if (listenerMBean == null)
0740: throw new InstanceNotFoundException(String
0741: .valueOf(listenerName));
0742:
0743: NotificationListener listener = listenerMBean.getListener();
0744:
0745: if (listener == null) {
0746: IllegalArgumentException exn = new IllegalArgumentException(
0747: L.l("{0} does not implement NotificationListener."));
0748: throw new RuntimeOperationsException(exn);
0749: }
0750:
0751: removeNotificationListener(name, listener);
0752: }
0753:
0754: /**
0755: * Removes a listener from a registered MBean
0756: *
0757: * @param name the name of the mbean
0758: * @param listenerName the name of the listener
0759: * @param filter the notification filter
0760: * @param handback context to the listener
0761: *
0762: * @since JMX 1.2
0763: */
0764: public void removeNotificationListener(ObjectName name,
0765: ObjectName listenerName, NotificationFilter filter,
0766: Object handback) throws InstanceNotFoundException,
0767: ListenerNotFoundException {
0768: MBeanWrapper listenerMBean = getMBean(listenerName);
0769:
0770: if (listenerMBean == null)
0771: throw new InstanceNotFoundException(String
0772: .valueOf(listenerName));
0773:
0774: NotificationListener listener = listenerMBean.getListener();
0775:
0776: if (listener == null) {
0777: IllegalArgumentException exn = new IllegalArgumentException(
0778: L.l("{0} does not implement NotificationListener."));
0779: throw new RuntimeOperationsException(exn);
0780: }
0781:
0782: removeNotificationListener(name, listener, filter, handback);
0783: }
0784:
0785: /**
0786: * Removes a listener from a registered MBean
0787: *
0788: * @param name the name of the mbean
0789: * @param listenerName the name of the listener
0790: * @param filter the notification filter
0791: * @param handback context to the listener
0792: *
0793: * @since JMX 1.2
0794: */
0795: public void removeNotificationListener(ObjectName name,
0796: NotificationListener listener, NotificationFilter filter,
0797: Object handback) throws InstanceNotFoundException,
0798: ListenerNotFoundException {
0799: MBeanWrapper mbean = getMBean(name);
0800:
0801: if (mbean == null)
0802: throw new InstanceNotFoundException(String.valueOf(name));
0803:
0804: createContext().removeNotificationListener(name, listener,
0805: filter, handback);
0806:
0807: mbean.removeNotificationListener(listener, filter, handback);
0808: }
0809:
0810: /**
0811: * Returns the analyzed information for an MBean
0812: *
0813: * @param name the name of the mbean
0814: *
0815: * @return the introspected information
0816: */
0817: public MBeanInfo getMBeanInfo(ObjectName name)
0818: throws InstanceNotFoundException, IntrospectionException,
0819: ReflectionException {
0820: MBeanWrapper mbean = getMBean(name);
0821:
0822: if (mbean == null)
0823: throw new InstanceNotFoundException(String.valueOf(name));
0824:
0825: MBeanInfo info = mbean.getMBeanInfo();
0826:
0827: return info;
0828: }
0829:
0830: /**
0831: * Returns true if the MBean is an instance of the specified class.
0832: *
0833: * @param name the name of the mbean
0834: * @param className the className to test.
0835: *
0836: * @return true if the bean is an instance
0837: */
0838: public boolean isInstanceOf(ObjectName name, String className)
0839: throws InstanceNotFoundException {
0840: MBeanWrapper mbean = getMBean(name);
0841:
0842: if (mbean == null)
0843: throw new InstanceNotFoundException(String.valueOf(name));
0844:
0845: Object obj = mbean.getObject();
0846: Class cl = obj.getClass();
0847:
0848: return isInstanceOf(cl, className);
0849: }
0850:
0851: private boolean isInstanceOf(Class cl, String className) {
0852: if (cl == null)
0853: return false;
0854:
0855: if (cl.getName().equals(className))
0856: return true;
0857:
0858: if (isInstanceOf(cl.getSuperclass(), className))
0859: return true;
0860:
0861: Class[] ifs = cl.getInterfaces();
0862: for (int i = 0; i < ifs.length; i++) {
0863: if (isInstanceOf(ifs[i], className))
0864: return true;
0865: }
0866:
0867: return false;
0868: }
0869:
0870: /**
0871: * Returns the ClassLoader that was used for loading the MBean.
0872: *
0873: * @param mbeanName the name of the mbean
0874: *
0875: * @return the class loader
0876: *
0877: * @since JMX 1.2
0878: */
0879: public ClassLoader getClassLoaderFor(ObjectName name)
0880: throws InstanceNotFoundException {
0881: MBeanWrapper mbean = getMBean(name);
0882:
0883: if (mbean == null)
0884: throw new InstanceNotFoundException(String.valueOf(name));
0885:
0886: return mbean.getContext().getClassLoader();
0887: }
0888:
0889: /**
0890: * Returns the named ClassLoader.
0891: *
0892: * @param loaderName the name of the class loader
0893: *
0894: * @return the class loader
0895: *
0896: * @since JMX 1.2
0897: */
0898: public ClassLoader getClassLoader(ObjectName loaderName)
0899: throws InstanceNotFoundException {
0900: return null;
0901: }
0902:
0903: /**
0904: * Returns the ClassLoaderRepository for this MBeanServer
0905: *
0906: * @since JMX 1.2
0907: */
0908: public ClassLoaderRepository getClassLoaderRepository() {
0909: return createContext().getClassLoaderRepository();
0910: }
0911:
0912: /**
0913: * Deserializes a byte array in the class loader of the mbean.
0914: *
0915: * @param name the name of the mbean
0916: * @param data the data to deserialize
0917: *
0918: * @return the deserialization stream
0919: */
0920: public ObjectInputStream deserialize(ObjectName name, byte[] data)
0921: throws InstanceNotFoundException, OperationsException {
0922: throw new UnsupportedOperationException();
0923: }
0924:
0925: /**
0926: * Deserializes a byte array in the class loader of the mbean.
0927: *
0928: * @param className the className matches to the loader
0929: * @param data the data to deserialize
0930: *
0931: * @return the deserialization stream
0932: */
0933: public ObjectInputStream deserialize(String className, byte[] data)
0934: throws OperationsException, ReflectionException {
0935: throw new UnsupportedOperationException();
0936: }
0937:
0938: /**
0939: * Deserializes a byte array in the class loader of the mbean.
0940: *
0941: * @param className the className matches to the loader
0942: * @param loaderName the loader to use for deserialization
0943: * @param data the data to deserialize
0944: *
0945: * @return the deserialization stream
0946: */
0947: public ObjectInputStream deserialize(String className,
0948: ObjectName loaderName, byte[] data)
0949: throws OperationsException, ReflectionException,
0950: InstanceNotFoundException {
0951: throw new UnsupportedOperationException();
0952: }
0953:
0954: /**
0955: * Returns the domains for all registered MBeans
0956: *
0957: * @since JMX 1.2
0958: */
0959: public String[] getDomains() {
0960: return getView().getDomains();
0961: }
0962:
0963: /**
0964: * Finds the MBean implementation.
0965: */
0966: MBeanWrapper getMBean(ObjectName name) {
0967: return getView().getMBean(name);
0968: }
0969:
0970: /**
0971: * Handles the case where a class loader is dropped.
0972: */
0973: public void destroy() {
0974: try {
0975: MBeanServerFactory.releaseMBeanServer(this );
0976: } catch (IllegalArgumentException e) {
0977: log.log(Level.FINEST, e.toString(), e);
0978: } catch (Throwable e) {
0979: log.log(Level.FINER, e.toString(), e);
0980: }
0981: }
0982:
0983: /**
0984: * Returns the string form.
0985: */
0986: @Override
0987: public String toString() {
0988: if (_defaultDomain != null)
0989: return "MBeanServerImpl[domain=" + _defaultDomain + "]";
0990: else
0991: return "MBeanServerImpl[]";
0992: }
0993:
0994: static {
0995: try {
0996: SERVER_DELEGATE_NAME = new ObjectName(
0997: "JMImplementation:type=MBeanServerDelegate");
0998: } catch (Throwable e) {
0999: e.printStackTrace();
1000: }
1001: }
1002: }
|