mbeancontext.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 702 行 · 第 1/2 页
JAVA
702 行
{ return _mbeans.size(); } /** * Adds an object. */ private void addMBean(ObjectName name, MBeanWrapper mbean) { if (_mbeans == null) throw new IllegalStateException(L.l("Adding MBean when context is closed")); if (mbean == null) { log.warning(L.l("'{0}' is an empty mbean", name)); return; } // at finest to avoid double logging for context and global if (log.isLoggable(Level.FINEST)) log.finest(getDebugName(name, mbean) + " registered in " + this); //log.fine(L.l("{0} registered in {1}", getDebugName(name, mbean), this)); _mbeans.put(name, mbean); _view.add(name, mbean, true); _globalView.add(name, mbean, true); sendRegisterNotification(name); MBeanContext context = this; while (context._loader != null) { ClassLoader parentLoader = context._loader.getParent(); MBeanContext parentContext = _mbeanServer.createContext(parentLoader); if (parentContext == null || parentContext == context) break; if (parentContext._globalView == null) { log.finer("global view is empty"); } else if (parentContext._globalView.add(name, mbean, false)) parentContext.sendRegisterNotification(name); context = parentContext; } } /** * Removes an object. */ private MBeanWrapper removeMBean(ObjectName name) { MBeanWrapper mbean = null; if (_mbeans != null) mbean = _mbeans.remove(name); if (_globalContext != null) _globalContext._mbeans.remove(name); if (_view != null) _view.remove(name); if (_globalView != null && _globalView.remove(name) != null) { try { sendUnregisterNotification(name); } catch (Throwable e) { log.log(Level.WARNING, e.toString(), e); } } ClassLoader loader = _loader.getParent(); for (; loader != null; loader = loader.getParent()) { MBeanContext parentContext = _mbeanServer.getContext(loader); if (this == parentContext) return mbean; else if (parentContext != null) { parentContext.removeMBean(name); break; } } return mbean; } /** * Adds a listener to a registered MBean * * @param name the name of the mbean * @param listener the listener object * @param filter filters events the listener is interested in * @param handback context to be returned to the listener */ void addNotificationListener(ObjectName name, NotificationListener listener, NotificationFilter filter, Object handback) { synchronized (_listeners) { _listeners.add(new Listener(name, listener, filter, handback)); } } /** * Removes a listener to a registered MBean * * @param mbean the name of the mbean * @param listener the listener object */ public void removeNotificationListener(ObjectName mbean, NotificationListener listener) { synchronized (_listeners) { for (int i = _listeners.size() - 1; i >= 0; i--) { Listener oldListener = _listeners.get(i); if (oldListener.match(mbean, listener)) _listeners.remove(i); } } } /** * Removes a listener to a registered MBean * * @param mbean the name of the mbean * @param listener the listener object * @param filter filters events the listener is interested in * @param handback context to be returned to the listener */ public void removeNotificationListener(ObjectName mbean, NotificationListener listener, NotificationFilter filter, Object handback) { synchronized (_listeners) { for (int i = _listeners.size() - 1; i >= 0; i--) { Listener oldListener = _listeners.get(i); if (oldListener.match(mbean, listener, filter, handback)) _listeners.remove(i); } } } /** * Sends the register notification. */ void sendRegisterNotification(ObjectName name) { serverNotification(name, MBeanServerNotification.REGISTRATION_NOTIFICATION); } /** * Sends the register notification. */ void sendUnregisterNotification(ObjectName name) { serverNotification(name, MBeanServerNotification.UNREGISTRATION_NOTIFICATION); } /** * Sends the notification */ private void serverNotification(ObjectName name, String type) { MBeanServerNotification notif; ObjectName delegateName = _mbeanServer.SERVER_DELEGATE_NAME; notif = new MBeanServerNotification(type, delegateName, _seq++, name); _delegate.sendNotification(notif); } /** * Closes the context server. */ public void destroy() { if (_mbeans == null) return; log.finest(this + " destroy"); ArrayList<ObjectName> list = new ArrayList<ObjectName>(_mbeans.keySet()); ArrayList<Listener> listeners = new ArrayList<Listener>(_listeners); for (int i = 0; i < listeners.size(); i++) { Listener listener = listeners.get(i); try { MBeanWrapper mbean = _globalView.getMBean(listener.getName()); if (mbean != null) mbean.removeNotificationListener(listener.getListener(), listener.getFilter(), listener.getHandback()); } catch (Throwable e) { log.log(Level.FINER, e.toString(), e); } } for (int i = 0; i < list.size(); i++) { ObjectName name = list.get(i); try { unregisterMBean(name); } catch (Throwable e) { log.log(Level.FINE, e.toString(), e); } } _mbeanServer.removeContext(this, _loader); _listeners = null; _mbeans = null; _view = null; _globalView = null; } /** * Returns the debug name for a registered mbean. * * @param name the name of the mbean * @param mbean the mbean instance * @return */ private String getDebugName(ObjectName name, MBeanWrapper mbean) { String className = mbean.getMBeanInfo().getClassName(); int p = className.lastIndexOf('.'); if (p > 0) className = className.substring(p + 1); return className + "[" + name + "]"; } /** * Display name. */ public String toString() { return "MBeanContext[" + _loader + "]"; } /** * Finalizer. */ /* protected void finalize() throws Throwable { super.finalize(); destroy(); } */ /** * Listener references. */ static class Listener { private ObjectName _name; private WeakReference<NotificationListener> _listenerRef; private WeakReference<NotificationFilter> _filterRef; private WeakReference<Object> _handbackRef; Listener(ObjectName name, NotificationListener listener, NotificationFilter filter, Object handback) { _name = name; _listenerRef = new WeakReference<NotificationListener>(listener); if (filter != null) _filterRef = new WeakReference<NotificationFilter>(filter); if (handback != null) _handbackRef = new WeakReference<Object>(handback); } ObjectName getName() { return _name; } NotificationListener getListener() { return _listenerRef.get(); } NotificationFilter getFilter() { return _filterRef != null ? _filterRef.get() : null; } Object getHandback() { return _handbackRef != null ? _handbackRef.get() : null; } boolean match(ObjectName name, NotificationListener listener, NotificationFilter filter, Object handback) { if (! _name.equals(name)) return false; else if (listener != _listenerRef.get()) return false; else if (filter == null && _filterRef != null) return false; else if (_filterRef != null && _filterRef.get() != filter) return false; else if (handback == null && _handbackRef != null) return false; else if (_handbackRef != null && _handbackRef.get() != handback) return false; else return true; } boolean match(ObjectName name, NotificationListener listener) { if (! _name.equals(name)) return false; else if (listener != _listenerRef.get()) return false; else return true; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?