📄 defaultmbeanserverinterceptor.java
字号:
debugX("isInstanceOf",e); 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 { /* Permission check */ Object instance = getMBean(mbeanName); String classname = null; try { classname = meta.getMBeanClassName(instance); } catch (IntrospectionException e) { classname = null; } catch (NotCompliantMBeanException e) { classname = null; } checkMBeanPermission(classname, null, mbeanName, "getClassLoaderFor"); return 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 { if (loaderName == null) { checkMBeanPermission(null, null, null, "getClassLoader"); return server.getClass().getClassLoader(); } Object instance = getMBean(loaderName); String classname = null; try { classname = meta.getMBeanClassName(instance); } catch (IntrospectionException e) { classname = null; } catch (NotCompliantMBeanException e) { classname = null; } checkMBeanPermission(classname, null, loaderName, "getClassLoader"); /* Check if the given MBean is a ClassLoader */ if (!(instance instanceof ClassLoader)) throw new InstanceNotFoundException(loaderName.toString() + " is not a classloader"); return (ClassLoader) instance; } /** * Adds a MBean in the repository */ private void internal_addObject(Object object, ObjectName logicalName) throws InstanceAlreadyExistsException { // ------------------------------ // ------------------------------ // Let the repository do the work. synchronized(this) { try { repository.addMBean(object, logicalName); } catch (InstanceAlreadyExistsException e) { if (object instanceof MBeanRegistration ) { meta.postRegisterInvoker(object,false); } throw e; } } // --------------------- // Send create event // --------------------- if (isTraceOn()) { trace("addObject", "Send create notification of object " + logicalName.getCanonicalName()); } sendNotification(MBeanServerNotification.REGISTRATION_NOTIFICATION, logicalName ) ; } /** * Sends an MBeanServerNotifications with the specified type for the * MBean with the specified ObjectName */ private void sendNotification(String NotifType, ObjectName name) { // ------------------------------ // ------------------------------ // --------------------- // Create notification // --------------------- MBeanServerNotification notif = new MBeanServerNotification(NotifType,_MBSDelegateObjectName,0,name); if (isTraceOn()) { trace("sendNotification", NotifType + " " + name); } delegate.sendNotification(notif); } /** * Performs the necessary initializations for the MBeanServer. * Creates and registers the MetaData service and the MBeanServer * identification MBean */ private void initialize(String domain, MBeanServer outer, MBeanServerDelegate delegate, MBeanInstantiator inst, MetaData meta, Repository repos) { // ------------------------------ // ------------------------------ if (!this.domain.equals(repository.getDefaultDomain())) throw new IllegalArgumentException("Domain Name Mismatch"); try { queryByRepo = repository.isFiltering(); } catch (SecurityException e) { throw e; } catch (Exception e) { queryByRepo = false; } } /** * Applies the specified queries to the set of objects */ private Set filterListOfObjects(Set list, QueryExp query) { Set result = new HashSet(); // No query ... if (query == null ) { for (final Iterator i = list.iterator(); i.hasNext(); ) { final NamedObject no = (NamedObject) i.next(); final Object obj = no.getObject(); String className = null; try { className = meta.getMBeanClassName(obj); } catch (JMException x) { if (isDebugOn()) { debug("filterListOfObjects", "Can't obtain class name for " + no.getName() + ": " + x); debugX("filterListOfObjects",x); } } result.add(new ObjectInstance(no.getName(), className)); } } else { // Access the filter for (final Iterator i = list.iterator(); i.hasNext(); ) { final NamedObject no = (NamedObject) i.next(); final Object obj = no.getObject(); boolean res = false; MBeanServer oldServer = QueryEval.getMBeanServer(); query.setMBeanServer(server); try { res = query.apply(no.getName()); } catch (Exception e) { res = false; } finally { /* * query.setMBeanServer is probably * QueryEval.setMBeanServer so put back the old * value. Since that method uses a ThreadLocal * variable, this code is only needed for the * unusual case where the user creates a custom * QueryExp that calls a nested query on another * MBeanServer. */ query.setMBeanServer(oldServer); } if (res) { // if the MBean is a dynamic MBean ask its MBeanInfo // for the class name String className = null; try { className = meta.getMBeanClassName(obj); } catch (JMException x) { if (isDebugOn()) { debug("filterListOfObjects", "Can't obtain class name for " + no.getName() + ": " + x); debugX("filterListOfObjects",x); } } result.add(new ObjectInstance(no.getName(), className)); } } } return result; } /** * Applies the specified queries to the set of ObjectInstances. */ private Set filterListOfObjectInstances(Set list, QueryExp query) { // Null query. // if (query == null) { return list; } else { Set result = new HashSet(); // Access the filter. // for (final Iterator i = list.iterator(); i.hasNext(); ) { final ObjectInstance oi = (ObjectInstance) i.next(); boolean res = false; MBeanServer oldServer = QueryEval.getMBeanServer(); query.setMBeanServer(server); try { res = query.apply(oi.getObjectName()); } catch (Exception e) { res = false; } finally { /* * query.setMBeanServer is probably * QueryEval.setMBeanServer so put back the old * value. Since that method uses a ThreadLocal * variable, this code is only needed for the * unusual case where the user creates a custom * QueryExp that calls a nested query on another * MBeanServer. */ query.setMBeanServer(oldServer); } if (res) { result.add(oi); } } return result; } } /* * Get the existing wrapper for this listener, name, and mbean, if * there is one. Otherwise, if "create" is true, create and * return one. Otherwise, return null. * * We use a WeakHashMap so that if the only reference to a user * listener is in listenerWrappers, it can be garbage collected. * This requires a certain amount of care, because only the key in * a WeakHashMap is weak; the value is strong. We need to recover * the existing wrapper object (not just an object that is equal * to it), so we would like listenerWrappers to map any * ListenerWrapper to the canonical ListenerWrapper for that * (listener,name,mbean) set. But we do not want this canonical * wrapper to be referenced strongly. Therefore we put it inside * a WeakReference and that is the value in the WeakHashMap. */ private NotificationListener getListenerWrapper(NotificationListener l, ObjectName name, Object mbean, boolean create) { NotificationListener wrapper = new ListenerWrapper(l, name, mbean); synchronized (listenerWrappers) { WeakReference ref = (WeakReference) listenerWrappers.get(wrapper); if (ref != null) { NotificationListener existing = (NotificationListener) ref.get(); if (existing != null) return existing; } if (create) { listenerWrappers.put(wrapper, new WeakReference(wrapper)); return wrapper; } else return null; } } private static class ListenerWrapper implements NotificationListener { ListenerWrapper(NotificationListener l, ObjectName name, Object mbean) { this.listener = l; this.name = name; this.mbean = mbean; } public void handleNotification(Notification notification, Object handback) { if (notification != null) { if (notification.getSource() == mbean) notification.setSource(name); } /* * Listeners are not supposed to throw exceptions. If * this one does, we could remove it from the MBean. It * might indicate that a connector has stopped working, * for instance, and there is no point in sending future * notifications over that connection. However, this * seems rather drastic, so instead we propagate the * exception and let the broadcaster handle it. */ listener.handleNotification(notification, handback); } public boolean equals(Object o) { if (!(o instanceof ListenerWrapper)) return false; ListenerWrapper w = (ListenerWrapper) o; return (w.listener == listener && w.mbean == mbean && w.name.equals(name)); /* * We compare all three, in case the same MBean object * gets unregistered and then reregistered under a * different name, or the same name gets assigned to two * different MBean objects at different times. We do the * comparisons in this order to avoid the slow * ObjectName.equals when possible. */ } public int hashCode() { return (System.identityHashCode(listener) ^ System.identityHashCode(mbean)); /* * We do not include name.hashCode() in the hash because * computing it is slow and usually we will not have two * instances of ListenerWrapper with the same mbean but * different ObjectNames. That can happen if the MBean is * unregistered from one name and reregistered with * another, and there is no garbage collection between; or * if the same object is registered under two names (which * is not recommended because MBeanRegistration will * break). But even in these unusual cases the hash code * does not have to be unique. */ } private NotificationListener listener; private ObjectName name; private Object mbean; } // SECURITY CHECKS //---------------- private static void checkMBeanPermission(String classname, String member, ObjectName objectName, String actions) throws SecurityException { SecurityManager sm = System.getSecurityManager(); if (sm != null) { Permission perm = new MBeanPermission(classname, member, objectName, actions); sm.checkPermission(perm); } } private static void checkMBeanTrustPermission(final Class theClass) throws SecurityException { SecurityManager sm = System.getSecurityManager(); if (sm != null) { Permission perm = new MBeanTrustPermission("register"); ProtectionDomain pd = (ProtectionDomain) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return theClass.getProtectionDomain(); } }); AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] { pd }); sm.checkPermission(perm, acc); } } // TRACES & DEBUG //--------------- private static boolean isTraceOn() { return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER); } private static void trace(String clz, String func, String info) { Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info); } private static void trace(String func, String info) { trace(dbgTag, func, info); } private static void error(String func, String info) { Trace.send(Trace.LEVEL_ERROR,Trace.INFO_MBEANSERVER,dbgTag,func,info); } private static boolean isDebugOn() { return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER); } private static void debug(String clz, String func, String info) { Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info); } private static void debug(String func, String info) { debug(dbgTag, func, info); } private static void debugX(String func,Throwable e) { if (isDebugOn()) { final StringWriter s = new StringWriter(); e.printStackTrace(new PrintWriter(s)); final String stack = s.toString(); debug(dbgTag,func,"Exception caught in "+ func+"(): "+e); debug(dbgTag,func,stack); // java.lang.System.err.println("**** Exception caught in "+ // func+"(): "+e); // java.lang.System.err.println(stack); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -