📄 objectmbean.java
字号:
//========================================================================//Copyright 2004 Mort Bay Consulting Pty. Ltd.//------------------------------------------------------------------------//Licensed under the Apache License, Version 2.0 (the "License");//you may not use this file except in compliance with the License.//You may obtain a copy of the License at//http://www.apache.org/licenses/LICENSE-2.0//Unless required by applicable law or agreed to in writing, software//distributed under the License is distributed on an "AS IS" BASIS,//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.//See the License for the specific language governing permissions and//limitations under the License.//========================================================================package org.mortbay.management;import java.lang.reflect.Array;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.util.Enumeration;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Locale;import java.util.Map;import java.util.MissingResourceException;import java.util.ResourceBundle;import java.util.Set;import javax.management.Attribute;import javax.management.AttributeList;import javax.management.AttributeNotFoundException;import javax.management.DynamicMBean;import javax.management.InvalidAttributeValueException;import javax.management.MBeanAttributeInfo;import javax.management.MBeanConstructorInfo;import javax.management.MBeanException;import javax.management.MBeanInfo;import javax.management.MBeanNotificationInfo;import javax.management.MBeanOperationInfo;import javax.management.MBeanParameterInfo;import javax.management.ObjectName;import javax.management.ReflectionException;import javax.management.modelmbean.ModelMBean;import org.mortbay.log.Log;import org.mortbay.util.LazyList;import org.mortbay.util.Loader;import org.mortbay.util.TypeUtil;/* ------------------------------------------------------------ *//** ObjectMBean. * A dynamic MBean that can wrap an arbitary Object instance. * the attributes and methods exposed by this bean are controlled by * the merge of property bundles discovered by names related to all * superclasses and all superinterfaces. * * Attributes and methods exported may be "Object" and must exist on the * wrapped object, or "MBean" and must exist on a subclass of OBjectMBean * or "MObject" which exists on the wrapped object, but whose values are * converted to MBean object names. * */public class ObjectMBean implements DynamicMBean{ private static Class[] OBJ_ARG = new Class[]{Object.class}; protected Object _managed; private MBeanInfo _info; private Map _getters=new HashMap(); private Map _setters=new HashMap(); private Map _methods=new HashMap(); private Set _convert=new HashSet(); private ClassLoader _loader; private MBeanContainer _mbeanContainer; private static String OBJECT_NAME_CLASS = ObjectName.class.getName(); private static String OBJECT_NAME_ARRAY_CLASS = ObjectName[].class.getName(); /* ------------------------------------------------------------ */ /** * Create MBean for Object. Attempts to create an MBean for the object by searching the package * and class name space. For example an object of the type * * <PRE> * class com.acme.MyClass extends com.acme.util.BaseClass implements com.acme.Iface * </PRE> * * Then this method would look for the following classes: * <UL> * <LI>com.acme.management.MyClassMBean * <LI>com.acme.util.management.BaseClassMBean * <LI>org.mortbay.management.ObjectMBean * </UL> * * @param o The object * @return A new instance of an MBean for the object or null. */ public static Object mbeanFor(Object o) { try { Class oClass = o.getClass(); Object mbean = null; while (mbean == null && oClass != null) { String pName = oClass.getPackage().getName(); String cName = oClass.getName().substring(pName.length() + 1); String mName = pName + ".management." + cName + "MBean"; try { Class mClass = (Object.class.equals(oClass))?oClass=ObjectMBean.class:Loader.loadClass(oClass,mName,true); if (Log.isDebugEnabled()) Log.debug("mbeanFor " + o + " mClass=" + mClass); try { Constructor constructor = mClass.getConstructor(OBJ_ARG); mbean=constructor.newInstance(new Object[]{o}); } catch(Exception e) { Log.ignore(e); if (ModelMBean.class.isAssignableFrom(mClass)) { mbean=mClass.newInstance(); ((ModelMBean)mbean).setManagedResource(o, "objectReference"); } } if (Log.isDebugEnabled()) Log.debug("mbeanFor " + o + " is " + mbean); return mbean; } catch (ClassNotFoundException e) { if (e.toString().endsWith("MBean")) Log.ignore(e); else Log.warn(e); } catch (Error e) { Log.warn(e); mbean = null; } catch (Exception e) { Log.warn(e); mbean = null; } oClass = oClass.getSuperclass(); } } catch (Exception e) { Log.ignore(e); } return null; } public ObjectMBean(Object managedObject) { _managed = managedObject; _loader = Thread.currentThread().getContextClassLoader(); } public Object getManagedObject() { return _managed; } public ObjectName getObjectName() { return null; } public String getObjectNameBasis() { return null; } protected void setMBeanContainer(MBeanContainer container) { this._mbeanContainer = container; } public MBeanContainer getMBeanContainer () { return this._mbeanContainer; } public MBeanInfo getMBeanInfo() { try { if (_info==null) { // Start with blank lazy lists attributes etc. String desc=null; Object attributes=null; Object constructors=null; Object operations=null; Object notifications=null; // Find list of classes that can influence the mbean Class o_class=_managed.getClass(); Object influences = findInfluences(null, _managed.getClass()); // Set to record defined items Set defined=new HashSet(); // For each influence for (int i=0;i<LazyList.size(influences);i++) { Class oClass = (Class)LazyList.get(influences, i); // look for a bundle defining methods if (Object.class.equals(oClass)) oClass=ObjectMBean.class; String pName = oClass.getPackage().getName(); String cName = oClass.getName().substring(pName.length() + 1); String rName = pName.replace('.', '/') + "/management/" + cName+"-mbean"; try { Log.debug(rName); ResourceBundle bundle = Loader.getResourceBundle(o_class, rName,true,Locale.getDefault()); // Extract meta data from bundle Enumeration e = bundle.getKeys(); while (e.hasMoreElements()) { String key = (String)e.nextElement(); String value = bundle.getString(key); // Determin if key is for mbean , attribute or for operation if (key.equals(cName)) { // set the mbean description if (desc==null) desc=value; } else if (key.indexOf('(')>0) { // define an operation if (!defined.contains(key) && key.indexOf('[')<0) { defined.add(key); operations=LazyList.add(operations,defineOperation(key, value, bundle)); } } else { // define an attribute if (!defined.contains(key)) { defined.add(key); attributes=LazyList.add(attributes,defineAttribute(key, value)); } } } } catch(MissingResourceException e) { Log.ignore(e); } } _info = new MBeanInfo(o_class.getName(), desc, (MBeanAttributeInfo[])LazyList.toArray(attributes, MBeanAttributeInfo.class), (MBeanConstructorInfo[])LazyList.toArray(constructors, MBeanConstructorInfo.class), (MBeanOperationInfo[])LazyList.toArray(operations, MBeanOperationInfo.class), (MBeanNotificationInfo[])LazyList.toArray(notifications, MBeanNotificationInfo.class)); } } catch(RuntimeException e) { Log.warn(e); throw e; } return _info; } /* ------------------------------------------------------------ */ public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException { Method getter = (Method) _getters.get(name); if (getter == null) throw new AttributeNotFoundException(name); try { Object o = _managed; if (getter.getDeclaringClass().isInstance(this)) o = this; // mbean method // get the attribute Object r=getter.invoke(o, (java.lang.Object[]) null); // convert to ObjectName if need be. if (r!=null && _convert.contains(name)) { if (r.getClass().isArray()) { ObjectName[] on = new ObjectName[Array.getLength(r)]; for (int i=0;i<on.length;i++) on[i]=_mbeanContainer.findMBean(Array.get(r, i)); r=on; } else { ObjectName mbean = _mbeanContainer.findMBean(r); if (mbean==null) return null; r=mbean; } } return r; } catch (IllegalAccessException e) { Log.warn(Log.EXCEPTION, e); throw new AttributeNotFoundException(e.toString()); } catch (InvocationTargetException e) { Log.warn(Log.EXCEPTION, e); throw new ReflectionException((Exception) e.getTargetException()); } } /* ------------------------------------------------------------ */ public AttributeList getAttributes(String[] names) { AttributeList results = new AttributeList(names.length); for (int i = 0; i < names.length; i++) { try { results.add(new Attribute(names[i], getAttribute(names[i]))); } catch (Exception e) { Log.warn(Log.EXCEPTION, e); } } return results; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -