mbeancontext.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 702 行 · 第 1/2 页
JAVA
702 行
/* * 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 Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.jmx;import com.caucho.loader.*;import com.caucho.util.L10N;import javax.management.*;import javax.management.loading.ClassLoaderRepository;import java.lang.ref.WeakReference;import java.util.ArrayList;import java.util.Hashtable;import java.util.LinkedHashMap;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;/** * The context containing mbeans registered at a particular level. */public class MBeanContext{ private static final Logger log = Logger.getLogger(MBeanContext.class.getName()); private static final L10N L = new L10N(MBeanContext.class); // The owning MBeanServer private AbstractMBeanServer _mbeanServer; private MBeanContext _globalContext; private MBeanServerDelegate _delegate; private long _seq; // class loader for this server private ClassLoader _loader; private String _domain = "resin"; private LinkedHashMap<String,String> _properties = new LinkedHashMap<String,String>(); private ClassLoaderRepositoryImpl _classLoaderRepository = new ClassLoaderRepositoryImpl(); // map of all mbeans private Hashtable<ObjectName,MBeanWrapper> _mbeans = new Hashtable<ObjectName,MBeanWrapper>(); private ArrayList<Listener> _listeners = new ArrayList<Listener>(); // The local view associated with the context private MBeanView _view; // The global view associated with the context private MBeanView _globalView; MBeanContext(AbstractMBeanServer mbeanServer, ClassLoader loader, MBeanServerDelegate delegate, MBeanContext globalContext) { for (; loader != null && loader != ClassLoader.getSystemClassLoader() && ! (loader instanceof EnvironmentClassLoader); loader = loader.getParent()) { } if (loader == null) loader = ClassLoader.getSystemClassLoader(); _mbeanServer = mbeanServer; _loader = loader; _delegate = delegate; _globalContext = globalContext; Environment.addClassLoaderListener(new CloseListener(this), _loader); //Environment.addClassLoaderListener(new WeakCloseListener(this), _loader); _classLoaderRepository.addClassLoader(_loader); _view = new MBeanView(mbeanServer, _loader, "resin"); _globalView = new MBeanView(mbeanServer, _loader, "resin"); } /** * Returns the parent view. */ protected MBeanView getParentView() { return _mbeanServer.getParentView(); } /** * Returns the ClassLoaderRepository. */ public ClassLoaderRepository getClassLoaderRepository() { return _classLoaderRepository; } /** * Returns the class loader. */ public ClassLoader getClassLoader() { return _loader; } /** * Returns the view for this context. */ MBeanView getView() { return _view; } /** * Returns the view for this context. */ MBeanView getGlobalView() { return _globalView; } /** * Sets the properties. */ public void setProperties(Map<String,String> props) { _properties.clear(); _properties.putAll(props); } /** * Sets the properties. */ public LinkedHashMap<String,String> copyProperties() { return new LinkedHashMap<String,String>(_properties); } /** * Returns the object name. */ public ObjectName getObjectName(String name) throws MalformedObjectNameException { int len = name.length(); for (int i = 0; i < len; i++) { char ch = name.charAt(i); if (ch == ':') return new ObjectName(name); else if ('a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || '0' <= ch && ch <= '9' || ch == '-' || ch == '_' || ch == '.') { continue; } else break; } LinkedHashMap<String,String> properties; properties = new LinkedHashMap<String,String>(); properties.putAll(_properties); Jmx.parseProperties(properties, name); return Jmx.getObjectName(_domain, properties); } /** * Finds an admin object. */ MBeanWrapper getMBean(ObjectName name) { if (name != null) return _mbeans.get(name); else return null; } /** * Registers an MBean with the server. * * @param mbean the object to be registered as an MBean * @param name the name of the mbean. * * @return the instantiated object. */ ObjectInstance registerMBean(MBeanWrapper mbean, ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { if (mbean == null) throw new NotCompliantMBeanException(L.l("{0} is a null mbean", name)); if (_mbeans.get(name) != null) throw new InstanceAlreadyExistsException(String.valueOf(name)); Object object = mbean.getObject(); MBeanRegistration registration = null; if (object instanceof MBeanRegistration) registration = (MBeanRegistration) object; try { if (registration != null) name = registration.preRegister(_mbeanServer, name); } catch (Exception e) { throw new MBeanRegistrationException(e); } if (log.isLoggable(Level.FINER) && ! name.equals(_mbeanServer.SERVER_DELEGATE_NAME)) { log.finer(getDebugName(name, mbean) + " registered in " + this); } addMBean(name, mbean); try { if (registration != null) registration.postRegister(new Boolean(true)); } catch (Exception e) { throw new MBeanRegistrationException(e); } if (_globalContext != null) _globalContext.addMBean(name, mbean); return mbean.getObjectInstance(); } /** * Unregisters an MBean from the server. * * @param name the name of the mbean. */ public void unregisterMBean(ObjectName name) throws InstanceNotFoundException, MBeanRegistrationException { if (_mbeans == null) { removeMBean(name); return; } if (name.getDomain().equals("JMImplementation")) return; MBeanWrapper mbean = _mbeans.get(name); if (mbean == null) throw new InstanceNotFoundException(String.valueOf(name)); Object obj = mbean.getObject(); MBeanRegistration registration = null; if (obj instanceof MBeanRegistration) registration = (MBeanRegistration) obj; try { if (registration != null) { try { registration.preDeregister(); } catch (Throwable e) { log.log(Level.WARNING, e.toString()); } } removeMBean(name); if (registration != null) { try { registration.postDeregister(); } catch (Throwable e) { log.log(Level.WARNING, e.toString()); } } } catch (Exception e) { throw new MBeanRegistrationException(e); } } /** * 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 true if the given object is registered with the server. * * @param name the name of the mbean to test. * * @return true if the object is registered. */ public boolean isRegistered(ObjectName name) { return _mbeans.get(name) != null; } /** * Returns the number of MBeans registered. * * @return the number of registered mbeans. */ public int getMBeanCount()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?