abstractmbeanserver.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,037 行 · 第 1/2 页
JAVA
1,037 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * Free SoftwareFoundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.jmx;import com.caucho.loader.Environment;import com.caucho.loader.WeakCloseListener;import com.caucho.util.L10N;import javax.management.*;import javax.management.loading.ClassLoaderRepository;import java.io.ObjectInputStream;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Modifier;import java.util.Set;import java.util.logging.Level;import java.util.logging.Logger;/** * The main interface for retrieving and managing JMX objects. */abstract public class AbstractMBeanServer implements MBeanServer { private static final L10N L = new L10N(AbstractMBeanServer.class); private static final Logger log = Logger.getLogger(AbstractMBeanServer.class.getName()); static ObjectName SERVER_DELEGATE_NAME; // default domain private String _defaultDomain; /** * Creats a new MBeanServer implementation. */ public AbstractMBeanServer(String defaultDomain) { _defaultDomain = defaultDomain; Environment.addClassLoaderListener(new WeakCloseListener(this)); } /** * Returns the context implementation. */ protected MBeanContext createContext() { return createContext(Thread.currentThread().getContextClassLoader()); } /** * Returns the context implementation. */ protected MBeanContext getExistingContext() { return getExistingContext(Thread.currentThread().getContextClassLoader()); } /** * Returns the context implementation. */ protected MBeanContext getGlobalContext() { return createContext(ClassLoader.getSystemClassLoader()); } /** * Returns the context implementation, creating if necessary. */ abstract protected MBeanContext createContext(ClassLoader loader); /** * Returns the context implementation. */ abstract protected MBeanContext getExistingContext(ClassLoader loader); /** * Returns the context implementation. */ abstract protected MBeanContext getContext(ClassLoader loader); /** * Removes the context implementation. */ protected void removeContext(MBeanContext context, ClassLoader loader) { } /** * Returns the view implementation. */ protected MBeanView getView() { return createContext().getView(); } /** * Returns the view implementation. */ protected MBeanView getGlobalView() { return getGlobalContext().getView(); } /** * Returns the view implementation. */ protected MBeanView getParentView() { return null; } /** * Instantiate an MBean object to be registered with the server. * * @param className the className to be instantiated. * * @return the instantiated object. */ public Object instantiate(String className) throws ReflectionException, MBeanException { try { Class cl = getClassLoaderRepository().loadClass(className); return cl.newInstance(); } catch (ClassNotFoundException e) { throw new ReflectionException(e); } catch (InstantiationException e) { throw new ReflectionException(e); } catch (ExceptionInInitializerError e) { Throwable cause = e.getCause(); if (cause instanceof Exception) throw new MBeanException((Exception) cause); else throw e; } catch (IllegalAccessException e) { throw new ReflectionException(e); } } /** * Instantiate an MBean object to be registered with the server. * * @param className the className to be instantiated. * @param loaderName names the classloader to be used * * @return the instantiated object. */ public Object instantiate(String className, ObjectName loaderName) throws ReflectionException, MBeanException, InstanceNotFoundException { MBeanWrapper mbean = getMBean(loaderName); if (mbean == null) throw new InstanceNotFoundException(String.valueOf(loaderName)); else if (! (mbean.getObject() instanceof ClassLoader)) throw new InstanceNotFoundException(L.l("{0} is not a class loader", loaderName)); try { ClassLoader loader = (ClassLoader) mbean.getObject(); Class cl = loader.loadClass(className); return cl.newInstance(); } catch (ClassNotFoundException e) { throw new ReflectionException(e); } catch (InstantiationException e) { throw new ReflectionException(e); } catch (IllegalAccessException e) { throw new ReflectionException(e); } } /** * Instantiate an MBean object with the given arguments to be * passed to the constructor. * * @param className the className to be instantiated. * @param params the parameters for the constructor. * @param signature the signature of the constructor * * @return the instantiated object. */ public Object instantiate(String className, Object []params, String []signature) throws ReflectionException, MBeanException { try { Class cl = getClassLoaderRepository().loadClass(className); Constructor constructor = getConstructor(cl, signature); return constructor.newInstance(params); } catch (ClassNotFoundException e) { throw new ReflectionException(e); } catch (InstantiationException e) { throw new ReflectionException(e); } catch (InvocationTargetException e) { throw new MBeanException(e); } catch (IllegalAccessException e) { throw new ReflectionException(e); } } /** * Instantiate an MBean object with the given arguments to be * passed to the constructor. * * @param className the className to be instantiated. * @param loaderName names the classloader to be used * @param params the parameters for the constructor. * @param signature the signature of the constructor * * @return the instantiated object. */ public Object instantiate(String className, ObjectName loaderName, Object []params, String []signature) throws ReflectionException, MBeanException, InstanceNotFoundException { MBeanWrapper mbean = getMBean(loaderName); if (mbean == null) throw new InstanceNotFoundException(String.valueOf(loaderName)); else if (! (mbean.getObject() instanceof ClassLoader)) throw new InstanceNotFoundException(L.l("{0} is not a class loader", loaderName)); try { ClassLoader loader = (ClassLoader) mbean.getObject(); Class cl = loader.loadClass(className); Constructor constructor = getConstructor(cl, signature); return constructor.newInstance(params); } catch (ClassNotFoundException e) { throw new ReflectionException(e); } catch (InstantiationException e) { throw new ReflectionException(e); } catch (InvocationTargetException e) { throw new MBeanException(e); } catch (IllegalAccessException e) { throw new ReflectionException(e); } } /** * Returns the class's constructor with the matching sig. */ private Constructor getConstructor(Class cl, String []sig) { Constructor []constructors = cl.getConstructors(); for (int i = 0; i < constructors.length; i++) { if (! Modifier.isPublic(constructors[i].getModifiers())) continue; if (isMatch(constructors[i].getParameterTypes(), sig)) return constructors[i]; } return null; } /** * Matches the parameters the sig. */ private boolean isMatch(Class []param, String []sig) { if (param.length != sig.length) return false; for (int i = 0; i < param.length; i++) { if (! param[i].getName().equals(sig[i])) return false; } return true; } /** * Instantiate and register an MBean. * * @param className the className to be instantiated. * @param name the name of the mbean. * * @return the instantiated object. */ public ObjectInstance createMBean(String className, ObjectName name) throws ReflectionException, InstanceAlreadyExistsException, MBeanException, NotCompliantMBeanException { return registerMBean(instantiate(className), name); } /** * Instantiate and register an MBean. * * @param className the className to be instantiated. * @param name the name of the mbean. * @param loaderName the name of the class loader to user * * @return the instantiated object. */ public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName) throws ReflectionException, InstanceAlreadyExistsException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException { return registerMBean(instantiate(className, loaderName), name); } /** * Instantiate and register an MBean. * * @param className the className to be instantiated. * @param name the name of the mbean. * @param params the parameters for the constructor. * @param signature the signature of the constructor * * @return the instantiated object. */ public ObjectInstance createMBean(String className, ObjectName name, Object []params, String []signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanException, NotCompliantMBeanException { return registerMBean(instantiate(className, params, signature), name); } /** * Instantiate and register an MBean. * * @param className the className to be instantiated. * @param name the name of the mbean. * @param loaderName the loader name for the mbean. * @param params the parameters for the constructor. * @param signature the signature of the constructor * * @return the instantiated object. */ public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName, Object []params, String []signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException { return registerMBean(instantiate(className, loaderName, params, signature), name); } /** * Registers an MBean with the server. * * @param object the object to be registered as an MBean * @param name the name of the mbean. * * @return the instantiated object. */ public ObjectInstance registerMBean(Object object, ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { if (object == null) throw new NullPointerException(); MBeanContext context; context = createContext(); if (context.getMBean(name) != null) { throw new InstanceAlreadyExistsException(String.valueOf(name)); } DynamicMBean dynMBean = createMBean(object, name); if (object instanceof IntrospectionMBean) object = ((IntrospectionMBean) object).getImplementation(); else if (object instanceof StandardMBean) { object = ((StandardMBean) object).getImplementation(); } MBeanWrapper mbean = new MBeanWrapper(context, name, object, dynMBean); return context.registerMBean(mbean, name); } /** * Creates the dynamic mbean. */ private DynamicMBean createMBean(Object obj, ObjectName name) throws NotCompliantMBeanException { if (obj == null) throw new NotCompliantMBeanException(L.l("{0} mbean is null", name)); else if (obj instanceof DynamicMBean) return (DynamicMBean) obj; Class ifc = getMBeanInterface(obj.getClass()); if (ifc == null) throw new NotCompliantMBeanException(L.l("{0} mbean has no MBean interface for class {1}", name, obj.getClass().getName())); return new IntrospectionMBean(obj, ifc); } /** * Returns the mbean interface. */ private Class getMBeanInterface(Class cl) { for (; cl != null; cl = cl.getSuperclass()) { Class []interfaces = cl.getInterfaces(); String mbeanName = cl.getName() + "MBean"; String mxbeanName = cl.getName() + "MXBean"; int p = mbeanName.lastIndexOf('.'); mbeanName = mbeanName.substring(p); p = mxbeanName.lastIndexOf('.'); mxbeanName = mxbeanName.substring(p); for (int i = 0; i < interfaces.length; i++) { Class ifc = interfaces[i]; if (ifc.getName().endsWith(mbeanName) || ifc.getName().endsWith(mxbeanName)) return ifc; } } return null; } /** * Unregisters an MBean from the server. * * @param name the name of the mbean. */ public void unregisterMBean(ObjectName name) throws InstanceNotFoundException, MBeanRegistrationException { MBeanContext context = getExistingContext(); if (context != null) { context.unregisterMBean(name); log.finer(name + " unregistered from " + this); } // XXX: getDelegate().sendUnregisterNotification(name); } /** * Returns the MBean registered with the given name. * * @param name the name of the mbean. * * @return the matching mbean object. */ public ObjectInstance getObjectInstance(ObjectName name) throws InstanceNotFoundException { MBeanWrapper mbean = getMBean(name); if (mbean == null) throw new InstanceNotFoundException(String.valueOf(name)); return mbean.getObjectInstance(); } /** * Returns a set of MBeans matching the query. * * @param name the name of the mbean to match. * @param query the queryd to match. * * @return the set of matching mbean object. */ public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) { try { if (query != null) { query.setMBeanServer(this); } return getView().queryMBeans(name, query); } catch (Exception e) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?