⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultmbeanserverinterceptor.java

📁 JAVA 所有包
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            if (obj == null) {		if (isTraceOn()) {		    trace("getMBean", name+": Found no object");		}		throw new InstanceNotFoundException(name.toString());            }        }        return obj;    }        private static Object getResource(DynamicMBean mbean) {        if (mbean instanceof DynamicMBean2)            return ((DynamicMBean2) mbean).getResource();        else            return mbean;    }    private ObjectName nonDefaultDomain(ObjectName name) {	if (name == null || name.getDomain().length() > 0)	    return name;	/* The ObjectName looks like ":a=b", and that's what its	   toString() will return in this implementation.  So	   we can just stick the default domain in front of it	   to get a non-default-domain name.  We depend on the	   fact that toString() works like that and that it	   leaves wildcards in place (so we can detect an error	   if one is supplied where it shouldn't be).  */	final String completeName = domain + name;	try {	    return new ObjectName(completeName);	} catch (MalformedObjectNameException e) {	    final String msg =		"Unexpected default domain problem: " + completeName + ": " +		e;	    throw EnvHelp.initCause(new IllegalArgumentException(msg), e);	}    }    public String getDefaultDomain()  {        return domain;    }    /*     * Notification handling.     *     * This is not trivial, because the MBeanServer translates the     * source of a received notification from a reference to an MBean     * into the ObjectName of that MBean.  While that does make     * notification sending easier for MBean writers, it comes at a     * considerable cost.  We need to replace the source of a     * notification, which is basically wrong if there are also     * listeners registered directly with the MBean (without going     * through the MBean server).  We also need to wrap the listener     * supplied by the client of the MBeanServer with a listener that     * performs the substitution before forwarding.  This is why we     * strongly discourage people from putting MBean references in the     * source of their notifications.  Instead they should arrange to     * put the ObjectName there themselves.     *     * However, existing code relies on the substitution, so we are     * stuck with it.     *     * Here's how we handle it.  When you add a listener, we make a     * ListenerWrapper around it.  We look that up in the     * listenerWrappers map, and if there was already a wrapper for     * that listener with the given ObjectName, we reuse it.  This map     * is a WeakHashMap, so a listener that is no longer registered     * with any MBean can be garbage collected.     *     * We cannot use simpler solutions such as always creating a new     * wrapper or always registering the same listener with the MBean     * and using the handback to find the client's original listener.     * The reason is that we need to support the removeListener     * variant that removes all (listener,filter,handback) triples on     * a broadcaster that have a given listener.  And we do not have     * any way to inspect a broadcaster's internal list of triples.     * So the same client listener must always map to the same     * listener registered with the broadcaster.     *     * Another possible solution would be to map from ObjectName to     * list of listener wrappers (or IdentityHashMap of listener     * wrappers), making this list the first time a listener is added     * on a given MBean, and removing it when the MBean is removed.     * This is probably more costly in memory, but could be useful if     * some day we don't want to rely on weak references.     */    public void addNotificationListener(ObjectName name,					NotificationListener listener,					NotificationFilter filter,					Object handback)	    throws InstanceNotFoundException {	// ------------------------------	// ------------------------------        if (isTraceOn()) {            trace("addNotificationListener", "obj= " + name);        }        DynamicMBean instance = getMBean(name);	checkMBeanPermission(instance, null, name, "addNotificationListener");        NotificationBroadcaster broadcaster =                getNotificationBroadcaster(name, instance,                                           NotificationBroadcaster.class);        // ------------------        // Check listener        // ------------------        if (listener == null) {	    throw new RuntimeOperationsException(new		IllegalArgumentException("Null listener"),"Null listener");	}	NotificationListener listenerWrapper =	    getListenerWrapper(listener, name, broadcaster, true);	broadcaster.addNotificationListener(listenerWrapper, filter, handback);    }    public void addNotificationListener(ObjectName name,					ObjectName listener,					NotificationFilter filter,					Object handback)	    throws InstanceNotFoundException {	// ------------------------------	// ------------------------------        // ----------------        // Get listener object        // ----------------        DynamicMBean instance = getMBean(listener);        Object resource = getResource(instance);        if (!(resource instanceof NotificationListener)) {	    throw new RuntimeOperationsException(new		IllegalArgumentException(listener.getCanonicalName()),		"The MBean " + listener.getCanonicalName() +		"does not implement the NotificationListener interface") ;        }        // ----------------        // Add a listener on an MBean        // ----------------        if (isTraceOn()) {            trace("addNotificationListener", "obj= " + name + " listener= " +		  listener);        }        server.addNotificationListener(name,(NotificationListener) resource,				       filter, handback) ;    }    public void removeNotificationListener(ObjectName name,					   NotificationListener listener)	    throws InstanceNotFoundException, ListenerNotFoundException {	removeNotificationListener(name, listener, null, null, true);    }    public void removeNotificationListener(ObjectName name,					   NotificationListener listener,					   NotificationFilter filter,					   Object handback)	    throws InstanceNotFoundException, ListenerNotFoundException {	removeNotificationListener(name, listener, filter, handback, false);    }    public void removeNotificationListener(ObjectName name,					   ObjectName listener)	    throws InstanceNotFoundException, ListenerNotFoundException {	NotificationListener instance = getListener(listener);        if (isTraceOn()) {            trace("removeNotificationListener", "obj= " + name +		  " listener= " + listener);        }	server.removeNotificationListener(name, instance);    }    public void removeNotificationListener(ObjectName name,					   ObjectName listener,					   NotificationFilter filter,					   Object handback)	    throws InstanceNotFoundException, ListenerNotFoundException {	NotificationListener instance = getListener(listener);        if (isTraceOn()) {            trace("removeNotificationListener", "obj= " + name +		  " listener= " + listener);        }	server.removeNotificationListener(name, instance, filter, handback);    }    private NotificationListener getListener(ObjectName listener) 	throws ListenerNotFoundException {        // ----------------        // Get listener object        // ----------------        DynamicMBean instance;        try {	    instance = getMBean(listener);	} catch (InstanceNotFoundException e) {	    throw EnvHelp.initCause(			  new ListenerNotFoundException(e.getMessage()), e);	}        Object resource = getResource(instance);        if (!(resource instanceof NotificationListener)) {	    final RuntimeException exc =		new IllegalArgumentException(listener.getCanonicalName());	    final String msg =		"MBean " + listener.getCanonicalName() + " does not " +		"implement " + NotificationListener.class.getName();            throw new RuntimeOperationsException(exc, msg);        }	return (NotificationListener) resource;    }    private void removeNotificationListener(ObjectName name,					    NotificationListener listener,					    NotificationFilter filter,					    Object handback,					    boolean removeAll)	    throws InstanceNotFoundException, ListenerNotFoundException {        if (isTraceOn()) {            trace("removeNotificationListener", "obj= " + name);        }        DynamicMBean instance = getMBean(name);	checkMBeanPermission(instance, null, name,			     "removeNotificationListener");        Object resource = getResource(instance);	/* We could simplify the code by assigning broadcaster after	   assigning listenerWrapper, but that would change the error	   behavior when both the broadcaster and the listener are	   erroneous.  */                Class<? extends NotificationBroadcaster> reqClass =            removeAll ? NotificationBroadcaster.class : NotificationEmitter.class;        NotificationBroadcaster broadcaster =            getNotificationBroadcaster(name, instance, reqClass);	NotificationListener listenerWrapper =	    getListenerWrapper(listener, name, resource, false);        if (listenerWrapper == null)            throw new ListenerNotFoundException("Unknown listener");	if (removeAll)	    broadcaster.removeNotificationListener(listenerWrapper);	else {            NotificationEmitter emitter = (NotificationEmitter) broadcaster;	    emitter.removeNotificationListener(listenerWrapper,					       filter,					       handback);	}    }    private static <T extends NotificationBroadcaster>            T getNotificationBroadcaster(ObjectName name, Object instance,                                         Class<T> reqClass) {        if (instance instanceof DynamicMBean2)            instance = ((DynamicMBean2) instance).getResource();        if (reqClass.isInstance(instance))            return reqClass.cast(instance);        final RuntimeException exc =            new IllegalArgumentException(name.getCanonicalName());        final String msg =            "MBean " + name.getCanonicalName() + " does not " +            "implement " + reqClass.getName();        throw new RuntimeOperationsException(exc, msg);    }    public MBeanInfo getMBeanInfo(ObjectName name)	throws InstanceNotFoundException, IntrospectionException,	       ReflectionException {	// ------------------------------	// ------------------------------        DynamicMBean moi = getMBean(name);	final MBeanInfo mbi;        try {            mbi = moi.getMBeanInfo();        } catch (RuntimeMBeanException e) {            throw e;        } catch (RuntimeErrorException e) {            throw e;        } catch (RuntimeException e) {            throw new RuntimeMBeanException(e,                    "getMBeanInfo threw RuntimeException");        } catch (Error e) {            throw new RuntimeErrorException(e, "getMBeanInfo threw Error");        }	if (mbi == null)	    throw new JMRuntimeException("MBean " + name +					 "has no MBeanInfo");	checkMBeanPermission(mbi.getClassName(), null, name, "getMBeanInfo");	return mbi;    }    public boolean isInstanceOf(ObjectName name, String className)	throws InstanceNotFoundException {        DynamicMBean instance = getMBean(name);	checkMBeanPermission(instance, null, name, "isInstanceOf");                try {            if (instance instanceof DynamicMBean2) {                Object resource = ((DynamicMBean2) instance).getResource();                ClassLoader loader = resource.getClass().getClassLoader();                Class<?> c = Class.forName(className, false, loader);                return c.isInstance(resource);            }                        final String cn = getClassName(instance);            if (cn.equals(className))                return true;            final ClassLoader cl = instance.getClass().getClassLoader();                        final Class<?> classNameClass = Class.forName(className, false, cl);            if (classNameClass.isInstance(instance))                return true;                        final Class<?> instanceClass = Class.forName(cn, false, cl);            return classNameClass.isAssignableFrom(instanceClass);        } catch (Exception x) {            /* Could be SecurityException or ClassNotFoundException */            debugX("isInstanceOf",x);            return false;        }    }    /**     * <p>Return the {@link java.lang.ClassLoader} that was used for     * loading the class of the named MBean.     * @param mbeanName The ObjectName of the MBean.     * @return The ClassLoader used for that MBean.     * @exception InstanceNotFoundException if the named MBean is not found.     */    public ClassLoader getClassLoaderFor(ObjectName mbeanName) 	throws InstanceNotFoundException {        DynamicMBean instance = getMBean(mbeanName);	checkMBeanPermission(instance, null, mbeanName, "getClassLoaderFor");        return getResource(instance).getClass().getClassLoader();    }       /**     * <p>Return the named {@link java.lang.ClassLoader}.     * @param loaderName The ObjectName of the ClassLoader.     * @return The named ClassLoader.     * @exception InstanceNotFoundException if the named ClassLoader     * is not found.     */    public ClassLoader getClassLoader(ObjectName loaderName)	    throws InstanceNotFoundException {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -