0001: /**
0002: * The XMOJO Project 5
0003: * Copyright © 2003 XMOJO.org. All rights reserved.
0004:
0005: * NO WARRANTY
0006:
0007: * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
0008: * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
0009: * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
0010: * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
0011: * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0012: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
0013: * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
0014: * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
0015: * REPAIR OR CORRECTION.
0016:
0017: * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
0018: * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
0019: * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
0020: * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
0021: * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
0022: * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
0023: * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
0024: * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
0025: * SUCH DAMAGES.
0026: **/package com.adventnet.jmx;
0027:
0028: //----------------------Importing the Java Packages --------------------------//
0029: import java.lang.reflect.Constructor;
0030: import java.lang.reflect.Modifier;
0031: import java.lang.reflect.InvocationTargetException;
0032: import java.io.ByteArrayInputStream;
0033: import java.io.IOException;
0034: import java.util.Set;
0035: import java.util.ArrayList;
0036: import java.util.Enumeration;
0037: import java.util.HashSet;
0038: import java.util.Hashtable;
0039: import java.util.StringTokenizer;
0040:
0041: //----------------------Importing the JMX Packages --------------------------//
0042: import javax.management.Attribute;
0043: import javax.management.AttributeList;
0044: import javax.management.DynamicMBean;
0045: import javax.management.ObjectName;
0046: import javax.management.ObjectInstance;
0047: import javax.management.MBeanServer;
0048: import javax.management.MBeanServerDelegate;
0049: import javax.management.MBeanServerNotification;
0050: import javax.management.MBeanRegistration;
0051: import javax.management.MBeanInfo;
0052: import javax.management.Notification;
0053: import javax.management.NotificationListener;
0054: import javax.management.NotificationFilter;
0055: import javax.management.NotificationBroadcaster;
0056: import javax.management.QueryExp;
0057: import javax.management.AttributeNotFoundException;
0058: import javax.management.ListenerNotFoundException;
0059: import javax.management.InstanceNotFoundException;
0060: import javax.management.InstanceAlreadyExistsException;
0061: import javax.management.InvalidAttributeValueException;
0062: import javax.management.IntrospectionException;
0063: import javax.management.JMRuntimeException;
0064: import javax.management.MBeanException;
0065: import javax.management.MBeanRegistrationException;
0066: import javax.management.NotCompliantMBeanException;
0067: import javax.management.OperationsException;
0068: import javax.management.ReflectionException;
0069: import javax.management.RuntimeErrorException;
0070: import javax.management.RuntimeMBeanException;
0071: import javax.management.RuntimeOperationsException;
0072:
0073: import com.adventnet.agent.logging.Log;
0074: import com.adventnet.agent.logging.LogFactory;
0075: import com.adventnet.jmx.utils.JmxUtilities;
0076:
0077: /**
0078: * This is the implementation class for MBean manipulation on the agent side.
0079: * It contains the necessary methods for the creation, registration,
0080: * and deletion of MBeans as well as the access methods for registered
0081: * MBeans. This is the core component of the JMX infrastructure.
0082: * @version C_VERSION
0083: */
0084:
0085: public class MBeanServerImpl implements MBeanServer {
0086: //-----------------------Variable Declaration-----------------------//
0087:
0088: /** The default domain of the MBeanServer **/
0089: private String defaultDomain = new String("DefaultDomain");
0090:
0091: /** The static list which maintains the MBeanServer instances **/
0092: private static ArrayList servers = new ArrayList();
0093:
0094: /**
0095: * ServerTable maintains all the MBeans registered with the JMX agent
0096: * key = ObjectName(mbean objectname) ; value = Object(mbean)
0097: */
0098: private Hashtable serverTable = null;
0099:
0100: /** The delegate object for the MBeanServer **/
0101: private MBeanServerDelegate delegate = null;
0102:
0103: /**
0104: * The table that maintains the Loader reference for each of the MBeans
0105: * Key = String(mbeans name); value = Object(loader)
0106: */
0107: private Hashtable loaderTable = null;
0108:
0109: /** The Notification seqenceNumber for the MBeanServerDelegate **/
0110: private long sequenceNumber = 1;
0111:
0112: private Log log = null;
0113:
0114: /**
0115: * Default Constructor
0116: */
0117: public MBeanServerImpl() {
0118: this ("defaultdomain");
0119:
0120: try {
0121: createLogger();
0122: } catch (Exception e) {
0123: System.out.println("FATAL:Log Creation Failed");
0124: }
0125: }
0126:
0127: /** Creates an MBeanServer with the specified default domain name.**/
0128: public MBeanServerImpl(String defaultDomain) {
0129: try {
0130: createLogger();
0131: } catch (Exception e) {
0132: System.out.println("FATAL:Log Creation Failed");
0133: }
0134:
0135: //PERF
0136: //< 28-11-2001 serverTable is going to store all the MBeans. The default hashtable allocates space only for 10 objects. Practical applications must be having 100s of MBeans !!! -- Balaji>
0137: serverTable = new Hashtable();
0138: loaderTable = new Hashtable();
0139:
0140: delegate = new MBeanServerDelegate();
0141:
0142: try {
0143: registerMBean(delegate, new ObjectName(
0144: "JMImplementation:type=MBeanServerDelegate"));
0145: } catch (Exception e) {
0146: //TODO
0147: log.error("MBeanServerDelegate Registration Failed", e);
0148: //e.printStackTrace();
0149: }
0150:
0151: //TODO
0152: //< 28-11-2001 IS this really needed ??? It will keep on populating the loaders in the MBeanServer with objects of same type.--Bala >
0153: //DefaultLoaderRepositoryExt.addLoader( new DefaultClassLoader());
0154: DefaultLoaderRepositoryExt.addLoader(Thread.currentThread()
0155: .getContextClassLoader());
0156:
0157: this .defaultDomain = defaultDomain;
0158: }
0159:
0160: /**
0161: * Checks whether an MBean, identified by its object name, is already
0162: * registered with the MBeanServer.
0163: *
0164: * @param name The object name of the MBean to be checked.
0165: *
0166: * @return true if the MBean is already controlled by the MBeanServer,
0167: * false otherwise.
0168: */
0169: public boolean isRegistered(ObjectName name) {
0170: return serverTable.containsKey(name);
0171: }
0172:
0173: /**
0174: * Returns true if the MBean specified is an instance of the specified
0175: * class, false otherwise.
0176: * @param name The object name of the MBean to be checked.
0177: * @param className The name of the class.
0178: * @return true if the MBean is the instance of the specified class, false otherwise.
0179: * @exception InstanceNotFoundException The MBean specified is not registered in the MBean server.
0180: */
0181: public boolean isInstanceOf(ObjectName name, String className)
0182: throws InstanceNotFoundException {
0183: Object obj = serverTable.get(name);
0184: Class clazz = null;
0185:
0186: if (obj == null)
0187: throw new InstanceNotFoundException();
0188:
0189: if (obj instanceof javax.management.modelmbean.RequiredModelMBean) {
0190: try {
0191: MBeanInfo info = this .getMBeanInfo(name);
0192:
0193: if (info.getClassName().equals(className))
0194: return true;
0195:
0196: clazz = Thread.currentThread().getContextClassLoader()
0197: .loadClass(className);
0198: if (clazz.isInstance(obj))
0199: return true;
0200: else
0201: return false;
0202: } catch (Exception e1) {
0203: //TODO
0204: log.warn("RMM loading failed", e1);
0205: return false;
0206: }
0207: } else if (obj instanceof com.adventnet.jmx.DefaultDynamicMBean) {
0208: Object toCheck = ((DefaultDynamicMBean) (obj))
0209: .getStandardMBeanObject();
0210: clazz = null;
0211:
0212: try {
0213: clazz = Thread.currentThread().getContextClassLoader()
0214: .loadClass(className);
0215:
0216: if (clazz != null && clazz.isInstance(toCheck)) {
0217: return true;
0218: } else {
0219: return false;
0220: }
0221: } catch (Exception e1) {
0222: log.warn("Failed to loadClass"
0223: + toCheck.getClass().getName(), e1);
0224: //e1.printStackTrace();
0225: return false;
0226: }
0227: } else {
0228: clazz = null;
0229:
0230: try {
0231: clazz = Thread.currentThread().getContextClassLoader()
0232: .loadClass(className);
0233: } catch (Exception e) {
0234: log.warn("isInstanceOf() : Failed to loadClass "
0235: + className);
0236: return false;
0237: }
0238: if (clazz != null && clazz.isInstance(obj)) {
0239: return true;
0240: } else {
0241: return false;
0242: }
0243: }
0244: }
0245:
0246: /**
0247: * Returns the default domain used for the MBean naming.
0248: */
0249: public String getDefaultDomain() {
0250: return defaultDomain;
0251: }
0252:
0253: /**
0254: * Gets MBeans controlled by the MBeanServer. This method allows any of
0255: * the following to be obtained: All MBeans, a set of MBeans specified by
0256: * pattern matching on the ObjectName and/or a Query expression, a specific
0257: * MBean. When the object name is null or empty, all objects are to be
0258: * selected (and filtered if a query is specified). It returns the set of
0259: * ObjectInstance objects (containing the ObjectName and the Java Class
0260: * name) for the selected MBeans.
0261: *
0262: * @param name The object name pattern identifying the MBeans to be
0263: * retrieved. If null orempty all the MBeans registered
0264: * will be retrieved.
0265: *
0266: * @param query The query expression to be applied for selecting MBeans.
0267: *
0268: * @return A set containing the ObjectInstance objects for the selected MBeans.
0269: * If no MBean satisfies the query an empty list is returned.
0270: */
0271: public Set queryMBeans(ObjectName name, QueryExp query) {
0272: return queryFromTable(name, query, false);
0273: }
0274:
0275: /**
0276: * All the pattern matching stuff with ObjectName is done here
0277: */
0278: private Set queryFromTable(ObjectName name, QueryExp query,
0279: boolean queryName) {
0280: boolean wildCard = false;
0281:
0282: StringBuffer buff = new StringBuffer();
0283:
0284: HashSet querySet = new HashSet();
0285:
0286: try {
0287: if (name == null)
0288: name = new ObjectName("*:*");
0289: } catch (Exception e1) {
0290: //log.warn("queryFromTable");
0291: return querySet;
0292: }
0293:
0294: if (name != null && name.toString().length() != 0) {
0295: String strPattern = name.toString();
0296:
0297: if ((strPattern.indexOf("*") != -1)
0298: || (strPattern.indexOf("?") != -1)) {
0299: wildCard = true;
0300: }
0301:
0302: int length = strPattern.length();
0303: for (int i = 0; i < length; i++) {
0304: char ch = strPattern.charAt(i);
0305:
0306: /*if(ch == '*' || ch == '?')
0307: buff.append("$");
0308: */
0309:
0310: buff.append(ch);
0311: }
0312: }
0313:
0314: String sname = buff.toString();
0315:
0316: StringTokenizer st = new StringTokenizer(sname, ":");
0317:
0318: if (st.countTokens() != 2)
0319: return querySet;
0320:
0321: String dom = st.nextToken();
0322: String des = st.nextToken();
0323:
0324: if (dom == null || des == null)
0325: return querySet;
0326:
0327: for (Enumeration e = serverTable.keys(); e.hasMoreElements();) {
0328: ObjectName obj = (ObjectName) e.nextElement();
0329: if (name == null
0330: || (name != null && name.toString().length() == 0)) {
0331: if (queryName) {
0332: Object mbean = serverTable.get(obj);
0333: try {
0334: if (query == null)
0335: querySet.add(obj);
0336: else {
0337: query.setMBeanServer(this );
0338: if (query.apply(obj))
0339: querySet.add(obj);
0340: }
0341: } catch (Exception e1) {
0342: if (!(e1 instanceof NullPointerException))
0343: log.warn("Null Pointer Exception", e1);
0344: }
0345: } else {
0346: Object mbean = serverTable.get(obj);
0347: String classname = null;
0348:
0349: try {
0350: classname = ((DynamicMBean) mbean)
0351: .getMBeanInfo().getClassName();
0352: } catch (Exception ex) {
0353: }
0354:
0355: try {
0356: if (query == null)
0357: querySet.add(new ObjectInstance(obj,
0358: classname));
0359: else {
0360: query.setMBeanServer(this );
0361: if (query.apply(obj))
0362: querySet.add(new ObjectInstance(obj,
0363: classname));
0364: }
0365: } catch (Exception e2) {
0366: if (!(e2 instanceof NullPointerException))
0367: log.warn("NullPointer Exception", e2);
0368: }
0369: }
0370: } else //if(obj.toString().indexOf(name.toString()) != -1)
0371: {
0372: String sobj = obj.toString();
0373:
0374: StringTokenizer st1 = new StringTokenizer(sobj, ":");
0375:
0376: String lobj = st1.nextToken();
0377: String robj = st1.nextToken();
0378:
0379: boolean retVal1 = false;
0380: boolean retVal2 = false;
0381:
0382: retVal1 = JmxUtilities
0383: .checkPattern(lobj, dom, wildCard);
0384:
0385: if (retVal1)
0386: retVal2 = JmxUtilities.checkPattern(robj, des,
0387: wildCard);
0388:
0389: if (retVal2) {
0390: if (queryName) {
0391: Object mbean = serverTable.get(obj);
0392:
0393: try {
0394: if (query == null) {
0395: querySet.add(obj);
0396: } else {
0397: query.setMBeanServer(this );
0398: if (query.apply(obj))
0399: querySet.add(obj);
0400: }
0401: } catch (Exception e3) {
0402: if (!(e3 instanceof NullPointerException))
0403: log.warn("Null Pointer Exception", e3);
0404: }
0405: } else {
0406: Object mbean = serverTable.get(obj);
0407: String classname = null;
0408:
0409: try {
0410: classname = ((DynamicMBean) mbean)
0411: .getMBeanInfo().getClassName();
0412: } catch (Exception ex) {
0413: }
0414:
0415: try {
0416: if (query == null)
0417: querySet.add(new ObjectInstance(obj,
0418: classname));
0419: else {
0420: query.setMBeanServer(this );
0421: if (query.apply(obj))
0422: querySet.add(new ObjectInstance(
0423: obj, classname));
0424: }
0425: } catch (Exception e4) {
0426: if (!(e4 instanceof NullPointerException))
0427: log.warn("Null Pointer Exception", e4);
0428: }
0429: }
0430: }
0431: }
0432: }
0433:
0434: return querySet;
0435: }
0436:
0437: /**
0438: * Gets the names of MBeans controlled by the MBeanServer.
0439: * This method enables any of the following to be obtained:
0440: * The names of all MBeans, the names of a set of MBeans specified by
0441: * pattern matching on the ObjectName and/or a Query expression, a specific
0442: * MBean name(equivalent to testing whether an MBean is registered). When
0443: * the object name is null or empty, all objects are to be selected (and
0444: * filtered if a query is specified). It returns the set of ObjectNames for
0445: * the MBeans selected.
0446: *
0447: * @param name The object name pattern identifying the MBean names
0448: * to be retrieved. If null or empty, the name of all
0449: * registered MBeans will be retrieved.
0450: *
0451: * @param query The query expression to be applied for selecting MBeans.
0452: *
0453: * @return A set containing the ObjectNames for the MBeans selected.
0454: * If no MBean satisfies the query an empty list is returned.
0455: */
0456: public Set queryNames(ObjectName name, QueryExp query) {
0457: return queryFromTable(name, query, true);
0458: }
0459:
0460: /**
0461: * Registers a pre-existing object as an MBean with the MBeanServer.
0462: * If the object name given is null, the MBean may automatically
0463: * provide its own name by implementing the MBeanRegistration interface.
0464: * The call returns the MBean name.
0465: *
0466: * @param object The Java Bean to be registered as an MBean.
0467: *
0468: * @param name The object name of the MBean. May be null.
0469: *
0470: * @return The ObjectInstance for the MBean that has been registered.
0471: *
0472: * @exception javax.management.InstanceAlreadyExistsException The MBean
0473: * is already under the control of the MBeanServer.
0474: *
0475: * @exception javax.management.MBeanRegistrationException The preRegister
0476: * (MBeanRegistration interface) method of the MBean has
0477: * thrown an exception. The MBean will not be registered.
0478: *
0479: * @exception javax.management.NotCompliantMBeanException This object is
0480: * not an JMX compliant MBean
0481: */
0482: public ObjectInstance registerMBean(Object object, ObjectName name)
0483: throws InstanceAlreadyExistsException,
0484: MBeanRegistrationException, NotCompliantMBeanException {
0485: try {
0486: //Hyther:TCK - when user gives DefaultDomain will be
0487: //overidden by server.defaultdomain at any cost
0488: ObjectName temp = name;
0489:
0490: try {
0491: if (name.getDomain().equals("DefaultDomain")
0492: || name.getDomain().equals("")) {
0493: name = new ObjectName(defaultDomain + ":"
0494: + name.getKeyPropertyListString());
0495: }
0496: } catch (Exception ee) {
0497: name = temp;
0498: }
0499:
0500: if (object == null)
0501: throw new IllegalArgumentException();
0502:
0503: if (object instanceof DynamicMBean) {
0504: try {
0505: MBeanInfo mbi = ((DynamicMBean) (object))
0506: .getMBeanInfo();
0507: log.trace("registerMBean:object instanceof RMM");
0508: if (mbi == null) {
0509: log.debug("MBeanInfo is Null");
0510: throw new NotCompliantMBeanException(
0511: "Exception occured while invoking the getMBeanInfo() of DynamicMBean");
0512: }
0513:
0514: if (!isValidMBeanInfo(mbi)) {
0515: throw new NotCompliantMBeanException(
0516: "MBeanInfo is not proper ");
0517: }
0518: } catch (Exception ee) {
0519: throw new NotCompliantMBeanException(
0520: "Exception occured while invoking the getMBeanInfo() of DynamicMBean");
0521: }
0522: }
0523:
0524: //calling the preRegister before registering the MBean
0525: boolean isStandard = false;
0526: Class className = object.getClass();
0527: String fileName = className.getName();
0528: Class[] interfaces = className.getInterfaces();
0529: for (int i = 0; i < interfaces.length; i++) {
0530: if (interfaces[i].getName().equals(fileName + "MBean")) {
0531: isStandard = true;
0532: break;
0533: }
0534: }
0535:
0536: //if(interfaces.length == 0){
0537: if (!isStandard) {
0538: Class super Class = className;
0539: //for(;superClass != null;superClass = superClass.getSuperclass()){
0540: while (super Class != null) {
0541: super Class = super Class.getSuperclass();
0542: if (super Class == null) {
0543: break;
0544: }
0545:
0546: String super ClassName = super Class.getName();
0547: interfaces = super Class.getInterfaces();
0548:
0549: for (int i = 0; i < interfaces.length; i++) {
0550: if (interfaces[i].getName().equals(
0551: super ClassName + "MBean")) {
0552: isStandard = true;
0553: break;
0554: }
0555: }
0556: }
0557: }
0558:
0559: if (object instanceof MBeanRegistration) {
0560: log
0561: .trace("registerMBean:object instanceOf MBeanRegistration");
0562: ObjectName objName = null;
0563:
0564: try {
0565: objName = ((MBeanRegistration) object).preRegister(
0566: this , name);
0567: } catch (RuntimeException re) {
0568: throw new RuntimeMBeanException(re);
0569: } catch (Exception e) {
0570: log.warn("MBeanRegistration Failed", e);
0571: if (e instanceof MBeanRegistrationException) {
0572: throw (MBeanRegistrationException) e;
0573: }
0574:
0575: throw new MBeanRegistrationException(e, e
0576: .getMessage());
0577: }
0578:
0579: name = objName;
0580: }
0581:
0582: if (name == null) {
0583: try {
0584: if (object instanceof MBeanRegistration)
0585: ((MBeanRegistration) object)
0586: .postRegister(new Boolean(false));
0587: } catch (RuntimeException re) {
0588: throw new RuntimeMBeanException(re);
0589: } catch (Exception e) {
0590: throw new MBeanRegistrationException(e, e
0591: .getMessage());
0592: }
0593:
0594: throw new RuntimeOperationsException(
0595: new IllegalArgumentException(
0596: "Object name is null"));
0597: }
0598:
0599: if (serverTable.get(name) != null) {
0600: try {
0601: if (object instanceof MBeanRegistration)
0602: ((MBeanRegistration) object)
0603: .postRegister(new Boolean(false));
0604:
0605: throw new InstanceAlreadyExistsException();
0606: } catch (Exception e) {
0607: /** This required to handle even if postREgiter() throws any Exception */
0608: if (e instanceof RuntimeException) {
0609: throw new RuntimeMBeanException(
0610: (RuntimeException) e);
0611: } else if (e instanceof InstanceAlreadyExistsException) {
0612: throw (InstanceAlreadyExistsException) e;
0613: } else {
0614: throw new MBeanRegistrationException(e);
0615: }
0616: }
0617: }
0618:
0619: /** No Other MBeans except MBeanServerDelegate can be registered with the domain JMImplementation */
0620: if (!(name.toString()
0621: .equals("JMImplementation:type=MBeanServerDelegate"))) {
0622: if (name.getDomain().equals("JMImplementation")) {
0623: throw new JMRuntimeException();
0624: }
0625: }
0626:
0627: String oname = object.getClass().getName();
0628: //ClassLoader l = object.getClass().getClassLoader();
0629: ClassLoader l = getClassLoader(object);
0630: testForCompliance(oname, l);
0631: //if(! (object instanceof DynamicMBean) )
0632: if (isStandard) {
0633: log.trace("Object Not an instance of DynamicMBean");
0634: try {
0635: object = new DefaultDynamicMBean(object);
0636: } catch (Exception e) {
0637: try {
0638: if (object instanceof MBeanRegistration)
0639: ((MBeanRegistration) object)
0640: .postRegister(new Boolean(false));
0641: } catch (Exception poe) {
0642: throw new MBeanRegistrationException(poe,
0643: "Exception occured in postRegister");
0644: }
0645:
0646: throw new NotCompliantMBeanException();
0647: }
0648: }
0649:
0650: if (name.isPattern()) {
0651: try {
0652: if (object instanceof MBeanRegistration)
0653: ((MBeanRegistration) object)
0654: .postRegister(new Boolean(false));
0655: } catch (RuntimeException re) {
0656: throw new RuntimeMBeanException(re);
0657: } catch (Exception poe) {
0658: throw new MBeanRegistrationException(poe,
0659: "Exception occured in postRegister");
0660: }
0661:
0662: throw new RuntimeOperationsException(
0663: new IllegalArgumentException(
0664: "ObjectName pattern fails"));
0665: }
0666:
0667: serverTable.put(name, object);
0668: loaderTable.put(oname, l);
0669:
0670: try {
0671: if (object instanceof MBeanRegistration)
0672: ((MBeanRegistration) object)
0673: .postRegister(new Boolean(true));
0674: } catch (RuntimeException re) {
0675: throw new RuntimeMBeanException(re);
0676: } catch (Exception poe) {
0677: throw new MBeanRegistrationException(poe,
0678: "Exception occured in postRegister");
0679: } catch (Throwable th) {
0680: if (th instanceof Error) {
0681: throw new RuntimeErrorException((Error) th);
0682: }
0683: Exception e = (Exception) th;
0684: throw new MBeanRegistrationException(e,
0685: "Exception occured in postRegister");
0686: }
0687:
0688: if (object instanceof ClassLoader)
0689: DefaultLoaderRepositoryExt
0690: .addLoader((ClassLoader) object);
0691:
0692: if (object instanceof DefaultDynamicMBean
0693: && ((DefaultDynamicMBean) object)
0694: .getStandardMBeanObject() instanceof ClassLoader) {
0695: DefaultLoaderRepositoryExt
0696: .addLoader((ClassLoader) ((DefaultDynamicMBean) object)
0697: .getStandardMBeanObject());
0698: }
0699:
0700: //send Registration Notification
0701: Notification notif = new MBeanServerNotification(
0702: MBeanServerNotification.REGISTRATION_NOTIFICATION,
0703: delegate.getClass().getName(), sequenceNumber++,
0704: name);
0705:
0706: notif.setTimeStamp(System.currentTimeMillis());
0707: delegate.sendNotification(notif);
0708:
0709: if (object instanceof DefaultDynamicMBean) {
0710: return new ObjectInstance(name,
0711: ((DefaultDynamicMBean) object)
0712: .getStandardMBeanObject().getClass()
0713: .getName());
0714: } else {
0715: return new ObjectInstance(name, object.getClass()
0716: .getName());
0717: }
0718: } catch (IllegalArgumentException ie) {
0719: throw new RuntimeOperationsException(ie);
0720: }
0721: }
0722:
0723: /**
0724: * De-registers an MBean from the MBeanServer. The MBean is identified by
0725: * its object name. Once the method has been invoked, the MBean may no
0726: * longer be accessed by its object name.
0727: *
0728: * @param name The object name of the MBean to be de-registered.
0729: *
0730: * @exception javax.management.InstanceNotFoundException The
0731: * specified MBean is not registered in the MBeanServer.
0732: *
0733: * @exception javax.management.MBeanRegistrationException The
0734: * preDeregister (MBeanRegistration interface) method of the
0735: * MBean has thrown an exception.
0736: **/
0737: public void unregisterMBean(ObjectName name)
0738: throws InstanceNotFoundException,
0739: MBeanRegistrationException {
0740: log.trace("Unregistering MBean with object name " + name);
0741:
0742: if (name == null)
0743: throw new RuntimeOperationsException(
0744: new IllegalArgumentException(
0745: "ObjectName cannot be null!!!"));
0746:
0747: Object object = null;
0748:
0749: if ((object = serverTable.get(name)) == null)
0750: throw new InstanceNotFoundException();
0751:
0752: if (object instanceof MBeanRegistration) {
0753: try {
0754: ((MBeanRegistration) object).preDeregister();
0755: } catch (RuntimeException re) {
0756: throw new RuntimeMBeanException(re);
0757: } catch (Exception e) {
0758: log.warn("Exception while unregistering the MBean ");
0759: if (e instanceof MBeanRegistrationException) {
0760: throw (MBeanRegistrationException) e;
0761: }
0762:
0763: throw new MBeanRegistrationException(e);
0764: }
0765: }
0766:
0767: if (name.toString().equals(
0768: "JMImplementation:type=MBeanServerDelegate"))
0769: throw new RuntimeOperationsException(
0770: new IllegalArgumentException(
0771: "MBeanServerDelegate Cannot be unregistered"));
0772:
0773: Object o = serverTable.get(name);
0774: serverTable.remove(name);
0775: loaderTable.remove(name);
0776:
0777: if (o instanceof DefaultDynamicMBean) {
0778: if (((DefaultDynamicMBean) o).getStandardMBeanObject() instanceof ClassLoader) {
0779: DefaultLoaderRepositoryExt
0780: .removeLoader((ClassLoader) (((DefaultDynamicMBean) o)
0781: .getStandardMBeanObject()));
0782: }
0783: }
0784:
0785: if (o instanceof ClassLoader) {
0786: DefaultLoaderRepositoryExt.removeLoader((ClassLoader) o);
0787: }
0788:
0789: if (object instanceof MBeanRegistration) {
0790: try {
0791: ((MBeanRegistration) object).postDeregister();
0792: } catch (Exception e) {
0793: if (e instanceof RuntimeException) {
0794: RuntimeException rt = (RuntimeException) e;
0795: throw new RuntimeMBeanException(rt);
0796: }
0797: } catch (Error e) {
0798: throw new RuntimeErrorException(e);
0799: }
0800: }
0801:
0802: //send registration Notification...
0803: Notification notif = new MBeanServerNotification(
0804: MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
0805: delegate.getClass().getName(), sequenceNumber++, name);
0806:
0807: delegate.sendNotification(notif);
0808: }
0809:
0810: /**
0811: * Gets the value of a specific attribute of a named MBean.
0812: * The MBean is identified by its object name.
0813: *
0814: * @param name The object name of the MBean from which the
0815: * attribute is to be retrieved.
0816: *
0817: * @param attribute A String specifying the name of the
0818: * attribute to be retrieved.
0819: *
0820: * @return The value of the retrieved attribute.
0821: *
0822: * @exception javax.management.AttributeNotFoundException The
0823: * specified attribute is not accessible in the MBean.
0824: *
0825: * @exception javax.management.MBeanException Wraps an exception
0826: * thrown by the MBean's getter.
0827: *
0828: * @exception javax.management.InstanceNotFoundException The
0829: * specified MBean is not registered in the MBeanServer.
0830: *
0831: * @exception javax.management.ReflectionException Wraps an
0832: * java.lang.Exception thrown while trying to invoke the setter.
0833: **/
0834: public Object getAttribute(ObjectName name, String attribute)
0835: throws MBeanException, AttributeNotFoundException,
0836: InstanceNotFoundException, ReflectionException {
0837: log.trace("getAttribute - getting value for attribute "
0838: + attribute + " of MBean with ObjectName " + name);
0839:
0840: if (name == null || attribute == null)
0841: throw new RuntimeOperationsException(
0842: new IllegalArgumentException(
0843: "Null Values not possible"));
0844:
0845: Object object = null;
0846: if ((object = serverTable.get(name)) == null)
0847: throw new InstanceNotFoundException();
0848:
0849: if (object instanceof DynamicMBean) {
0850: Object toRet = null;
0851:
0852: synchronized (object) {
0853: try {
0854: toRet = ((DynamicMBean) object)
0855: .getAttribute(attribute);
0856: } catch (AttributeNotFoundException ae) {
0857: throw ae;
0858: } catch (ReflectionException re) {
0859: Exception e = re.getTargetException();
0860: if (e instanceof InvocationTargetException) {
0861: InvocationTargetException it = (InvocationTargetException) e;
0862: Exception ee = (Exception) it
0863: .getTargetException();
0864: if (ee instanceof RuntimeException) {
0865: RuntimeException rt = (RuntimeException) ee;
0866: throw new RuntimeMBeanException(rt);
0867: } else {
0868: throw new MBeanException(ee);
0869: }
0870: }
0871:
0872: throw new MBeanException(e);
0873: } catch (Exception e) {
0874: if (e instanceof RuntimeErrorException) {
0875: RuntimeErrorException rt = (RuntimeErrorException) e;
0876: throw rt;
0877: }
0878: throw new MBeanException(e);
0879: }
0880: }
0881:
0882: return toRet;
0883: }
0884:
0885: return null;
0886: }
0887:
0888: /**
0889: * Enables the values of several attributes of a named MBean.
0890: * The MBean is identified by its object name.
0891: *
0892: * @param name The object name of the MBean from which the
0893: * attributes are to be retrieved.
0894: *
0895: * @param attributes A list of the attributes to be retrieved.
0896: *
0897: * @return The list of the retrieved attributes.
0898: *
0899: * @exception javax.management.InstanceNotFoundException The
0900: * specified MBean is not registered in the MBeanServer.
0901: **/
0902: public AttributeList getAttributes(ObjectName name,
0903: String[] attributes) throws InstanceNotFoundException,
0904: ReflectionException {
0905: log.trace("getAttributes entered");
0906:
0907: int length = 0;
0908:
0909: if (name == null || attributes == null)
0910: throw new RuntimeOperationsException(
0911: new IllegalArgumentException(
0912: "Null values not possible"));
0913:
0914: Object object = null;
0915:
0916: if ((object = serverTable.get(name)) == null)
0917: throw new InstanceNotFoundException();
0918:
0919: length = attributes.length;
0920:
0921: for (int i = 0; i < length; i++) {
0922: if (attributes[i] == null)
0923: throw new RuntimeOperationsException(
0924: new IllegalArgumentException(
0925: "Null values not possible"));
0926: }
0927:
0928: if (object instanceof DynamicMBean) {
0929: synchronized (object) {
0930: try {
0931: return ((DynamicMBean) object)
0932: .getAttributes(attributes);
0933: } catch (Exception ex) {
0934: new ReflectionException(ex);
0935: }
0936: }
0937: }
0938:
0939: return null;
0940: }
0941:
0942: /**
0943: * Invokes an action on an MBean.
0944: *
0945: * @param name The object name of the MBean on which the method is to be invoked.
0946: *
0947: * @param actionName The name of the action to be invoked.
0948: *
0949: * @param params An array containing the parameters to be set when the action is invoked
0950: *
0951: * @param signature An array containing the signature of the action.
0952: * The class objects will be loaded using the same class loader
0953: * as the one used for loading the MBean on which the action was invoked.
0954: *
0955: * @return The object returned by the action, which represents the result
0956: * of invoking the action on the specified MBean.
0957: *
0958: * @exception javax.management.InstanceNotFoundException The
0959: * specified MBean is not registered in the MBeanServer.
0960: *
0961: * @exception javax.management.MBeanException Wraps an exception
0962: * thrown by the MBean's invoked method.
0963: *
0964: * @exception javax.management.ReflectionException Wraps an
0965: * java.lang.Exception thrown while trying to invoke the method.
0966: **/
0967: public Object invoke(ObjectName name, String actionName,
0968: Object[] params, String[] signature)
0969: throws InstanceNotFoundException, MBeanException,
0970: ReflectionException {
0971: Object object = null;
0972:
0973: if ((object = serverTable.get(name)) == null) {
0974: throw new InstanceNotFoundException();
0975: }
0976:
0977: if (object instanceof DynamicMBean) {
0978: synchronized (object) {
0979: Object retObj = null;
0980: if (object instanceof DefaultDynamicMBean) {
0981: try {
0982: retObj = ((DynamicMBean) object).invoke(
0983: actionName, params, signature);
0984: } catch (RuntimeOperationsException roe) {
0985: throw roe;
0986: } catch (RuntimeException re) {
0987: RuntimeException rt = (RuntimeException) re;
0988: if (rt instanceof RuntimeOperationsException) {
0989: throw (RuntimeOperationsException) rt;
0990: }
0991:
0992: if (rt instanceof RuntimeErrorException) {
0993: throw (RuntimeErrorException) rt;
0994: }
0995:
0996: if (rt instanceof RuntimeMBeanException) {
0997: throw rt;
0998: }
0999:
1000: throw new RuntimeMBeanException(rt);
1001: } catch (ReflectionException re) {
1002: throw re;
1003: } catch (MBeanException me) {
1004: Exception e = me.getTargetException();
1005:
1006: if (e instanceof RuntimeException) {
1007: RuntimeException rt = (RuntimeException) e;
1008: throw new RuntimeMBeanException(rt);
1009: }
1010:
1011: throw me;
1012: }
1013:
1014: } else {
1015: try {
1016: retObj = ((DynamicMBean) object).invoke(
1017: actionName, params, signature);
1018: } catch (ReflectionException re) {
1019: Exception e = re.getTargetException();
1020: if (e instanceof MBeanException) {
1021: MBeanException mb = (MBeanException) e;
1022: throw mb;
1023: } else {
1024: throw re;
1025: }
1026: }
1027: }
1028:
1029: return retObj;
1030: }
1031: }
1032:
1033: return null;
1034: }
1035:
1036: /**
1037: * Sets the value of a specific attribute of a named MBean.
1038: * The MBean is identified by its object name.
1039: *
1040: * @param name The name of the MBean within which the attribute is to be set.
1041: *
1042: * @param attribute The identification of the attribute to
1043: * be set and the value it is to be set to.
1044: *
1045: * @return The value of the attribute that has been set.
1046: *
1047: * @exception javax.management.InstanceNotFoundException The
1048: * specified MBean is not registered in the MBeanServer.
1049: *
1050: * @exception javax.management.AttributeNotFoundException The
1051: * specified attribute is not accessible in the MBean.
1052: *
1053: * @exception javax.management.InvalidAttributeValueException The
1054: * specified value for the attribute is not valid.
1055: *
1056: * @exception javax.management.MBeanException Wraps an exception
1057: * thrown by the MBean's setter.
1058: *
1059: * @exception javax.management.ReflectionException Wraps an
1060: * java.lang.Exception thrown while trying to invoke the setter.
1061: **/
1062: public void setAttribute(ObjectName name, Attribute attribute)
1063: throws InstanceNotFoundException,
1064: AttributeNotFoundException, InvalidAttributeValueException,
1065: MBeanException, ReflectionException {
1066: log.trace("setAttribute - setting attribute");
1067:
1068: if (name == null || attribute == null)
1069: throw new RuntimeOperationsException(
1070: new IllegalArgumentException(
1071: "Null Values not possible"));
1072:
1073: Object object = null;
1074: if ((object = serverTable.get(name)) == null)
1075: throw new InstanceNotFoundException("No Mbean " + name
1076: + "present in the JMXServer");
1077:
1078: synchronized (object) {
1079: ((DynamicMBean) object).setAttribute(attribute);
1080: }
1081: }
1082:
1083: /**
1084: * Sets the values of several attributes of a named MBean.
1085: * The MBean is identified by its object name.
1086: *
1087: * @param name The object name of the MBean within which the attributes are to be set.
1088: *
1089: * @param attributes A list of attributes: The identification of the
1090: * attributes to be set and the values they are to be set to.
1091: *
1092: * @return The list of attributes that were set, with their new values.
1093: *
1094: * @exception javax.management.InstanceNotFoundException The specified
1095: * MBean is not registered in the MBeanServer.
1096: */
1097: public AttributeList setAttributes(ObjectName name,
1098: AttributeList attributes) throws InstanceNotFoundException,
1099: ReflectionException {
1100: log.trace("setAttributes called for MBean with ObjectName "
1101: + name);
1102:
1103: if (name == null || attributes == null)
1104: throw new RuntimeOperationsException(
1105: new IllegalArgumentException(
1106: "Either ObjectName / attributes is null"));
1107:
1108: Object object = null;
1109:
1110: if ((object = serverTable.get(name)) == null)
1111: throw new InstanceNotFoundException();
1112:
1113: if (object instanceof DynamicMBean) {
1114: synchronized (object) {
1115: return ((DynamicMBean) object)
1116: .setAttributes(attributes);
1117: }
1118: }
1119:
1120: return null;
1121: }
1122:
1123: /**
1124: * This method discovers the attributes and operations that an MBean exposes
1125: * for management. When flatten is false, inherited attributes are not returned.
1126: *
1127: * @param name The name of the MBean to analyze
1128: *
1129: * @return An instance of MBeanInfo allowing to retrieve all attributes
1130: * and operations of this MBean.
1131: *
1132: * @exception java.beans.IntrospectionException An exception
1133: * occurs during introspection.
1134: *
1135: * @exception javax.management.InstanceNotFoundException The specified
1136: * MBean is not found.
1137: **/
1138: public MBeanInfo getMBeanInfo(ObjectName name)
1139: throws InstanceNotFoundException, IntrospectionException,
1140: ReflectionException {
1141: log
1142: .trace("getting MBeanInfo for MBean with ObjectName "
1143: + name);
1144:
1145: Object object = null;
1146:
1147: if ((object = serverTable.get(name)) == null)
1148: throw new InstanceNotFoundException();
1149:
1150: if (object instanceof DynamicMBean) {
1151: synchronized (object) {
1152: MBeanInfo info = null;
1153: try {
1154: info = ((DynamicMBean) object).getMBeanInfo();
1155: if (info == null) {
1156: throw new JMRuntimeException();
1157: }
1158: } catch (Exception e) {
1159: if (e instanceof JMRuntimeException) {
1160: throw (JMRuntimeException) e;
1161: }
1162: if (e instanceof RuntimeException) {
1163: RuntimeException rt = (RuntimeException) e;
1164: throw new RuntimeMBeanException(rt);
1165: }
1166: }
1167:
1168: return info;
1169: }
1170: }
1171:
1172: return null;
1173: }
1174:
1175: /**
1176: * Gets the ObjectInstance for a given MBean registered with the MBean server.
1177: *
1178: * @param name The object name of the MBean.
1179: *
1180: * @return The ObjectInstance associated to the MBean specified by name.
1181: *
1182: * @exception javax.management.InstanceNotFoundException The MBean specified
1183: * is not registered in the MBean server.
1184: */
1185: public ObjectInstance getObjectInstance(ObjectName name)
1186: throws InstanceNotFoundException {
1187: if (name == null)
1188: throw new InstanceNotFoundException(
1189: "The specified ObjectName is null");
1190:
1191: if (!isRegistered(name))
1192: throw new InstanceNotFoundException(name.toString());
1193:
1194: Object mbean = serverTable.get(name);
1195: String classname = null;
1196:
1197: try {
1198: if (mbean instanceof javax.management.modelmbean.ModelMBean)
1199: classname = "javax.management.ModelMBean";
1200: else
1201: classname = ((DynamicMBean) mbean).getMBeanInfo()
1202: .getClassName();
1203: } catch (Exception ex) {
1204: }
1205:
1206: try {
1207: return new ObjectInstance(name, classname);
1208: } catch (Exception ee) {
1209: log.error("ObjectInstance Creation failed", ee);
1210: return null;
1211: }
1212: }
1213:
1214: /**
1215: * Enables a couple (listener,handback) for a registered MBean to be added.
1216: * @param name The name of the MBean on which the listener should be added.
1217: * @param listener The listener object which will handles notifications
1218: * emitted by the registered MBean.
1219: * @param filter The filter object. If not specified, no filtering will be
1220: * performed before handling notifications.
1221: * @param handback The context to be sent to the listener when a notification
1222: * is emitted.
1223: * @exception javax.management.InstanceNotFoundException The MBean name
1224: * doesn't correspond to a registered MBean.
1225: * @exception java.lang.IllegalArgumentException The object with name "name"
1226: * doesn't implements NotificationBroadcaster
1227: * @exception javax.management.InstanceAlreadyExistsException The couple
1228: * (listener,handback) is already registered in the MBean.
1229: **/
1230: public void addNotificationListener(ObjectName name,
1231: NotificationListener listener, NotificationFilter filter,
1232: Object handback) throws InstanceNotFoundException {
1233: Object object = null;
1234:
1235: if (name == null) {
1236: throw new RuntimeOperationsException(
1237: new IllegalArgumentException(
1238: "ObjectName cannot be null!!!"));
1239: }
1240:
1241: object = serverTable.get(name);
1242:
1243: if (object == null) {
1244: throw new InstanceNotFoundException(
1245: "No object with the name " + name
1246: + " present in the JMXServer");
1247: }
1248:
1249: if (!(object instanceof NotificationBroadcaster)) {
1250: throw new RuntimeOperationsException(
1251: new IllegalArgumentException(
1252: "MBean "
1253: + name
1254: + " does not implement NotificationBroadCaster"));
1255: }
1256:
1257: if (listener == null) {
1258: throw new RuntimeOperationsException(
1259: new IllegalArgumentException(
1260: "Null Listener is being passed !!!"));
1261: }
1262:
1263: synchronized (object) {
1264: ((NotificationBroadcaster) object).addNotificationListener(
1265: listener, filter, handback);
1266: }
1267: }
1268:
1269: /**
1270: * Enables a couple (listener,handback) for a registered MBean to be added.
1271: *
1272: * @param name The name of the MBean on which the listener should be added.
1273: *
1274: * @param listener The listener name which will handles notifications
1275: * emitted by the registered MBean.
1276: *
1277: * @param filter The filter object. If not specified, no filtering will be
1278: * performed before handling notifications.
1279: *
1280: * @param handback The context to be sent to the listener when a
1281: * notification is emitted.
1282: *
1283: * @exception javax.management.InstanceNotFoundException The MBean name
1284: * doesn't correspond to a registered MBean.
1285: *
1286: * @exception java.lang.IllegalArgumentException The object with name "name"
1287: * doesn't implements NotificationBroadcaster
1288: *
1289: * @exception javax.management.InstanceAlreadyExistsException The couple
1290: * (listener,handback) is already registered in the MBean.
1291: **/
1292: public void addNotificationListener(ObjectName name,
1293: ObjectName listener, NotificationFilter filter,
1294: Object handback) throws InstanceNotFoundException {
1295: Object object = null;
1296:
1297: if (name == null) {
1298: throw new RuntimeOperationsException(
1299: new IllegalArgumentException(
1300: "ObjectName cannot be null!!!"));
1301: }
1302:
1303: object = serverTable.get(name);
1304:
1305: if (object == null) {
1306: throw new InstanceNotFoundException(
1307: "No object with the name " + name
1308: + " present in the JMXServer");
1309: }
1310:
1311: if (!(object instanceof NotificationBroadcaster)) {
1312: throw new RuntimeOperationsException(
1313: new IllegalArgumentException(
1314: "MBean "
1315: + name
1316: + " does not implement NotificationBroadCaster"));
1317: }
1318:
1319: Object lobject = null;
1320: lobject = serverTable.get(listener);
1321:
1322: if (lobject == null) {
1323: throw new InstanceNotFoundException(
1324: "No listener with the name " + listener
1325: + " present in the JMXServer!!!");
1326: }
1327:
1328: if (lobject instanceof DefaultDynamicMBean) {
1329: if (!(((DefaultDynamicMBean) (lobject))
1330: .getStandardMBeanObject() instanceof NotificationListener)) {
1331: throw new RuntimeOperationsException(
1332: new IllegalArgumentException(
1333: "MBean "
1334: + name
1335: + " does not implement NotificationListener"));
1336: }
1337: }
1338:
1339: if (!(lobject instanceof NotificationListener)) {
1340: throw new RuntimeOperationsException(
1341: new IllegalArgumentException(
1342: "MBean "
1343: + name
1344: + " does not implement NotificationListener"));
1345: }
1346:
1347: synchronized (object) {
1348: ((NotificationBroadcaster) object).addNotificationListener(
1349: (NotificationListener) lobject, filter, handback);
1350: }
1351: }
1352:
1353: /**
1354: * Instantiates and registers a MBean with the MBeanServer.
1355: * The MBean server will use the default loader repository to load the
1356: * class of the MBean. An object name is associated to the MBean. If the
1357: * object name given is null, the MBean can automatically provide its own
1358: * name by implementing the MBeanRegistration interface. The call returns a
1359: * reference to the new instance and its object name.
1360: *
1361: * @param className The class name of the MBean to be instantiated.
1362: *
1363: * @param name The object name of the MBean. May be null.
1364: *
1365: * @return An ObjectInstance, containing the ObjectName and the Java class
1366: * name of the newly instantiated MBean.
1367: *
1368: * @exception javax.management.ReflectionException Wraps Wraps a
1369: * ClassNotFoundException or a java.lang.Exception that
1370: * occured trying to invoke the MBean's constructor.
1371: *
1372: * @exception javax.management.InstanceAlreadyExistsException The MBean is
1373: * already under the control of the MBeanServer.
1374: *
1375: * @exception javax.management.MBeanRegistrationException The preRegister
1376: * (MBeanRegistration interface) method of the MBean has
1377: * thrown an exception. The MBean will not be registered.
1378: *
1379: * @exception javax.management.MBeanException The constructor of the
1380: * MBean has thrown an exception
1381: *
1382: * @exception javax.management.NotCompliantMBeanException This class is
1383: * not an JMX compliant MBean
1384: **/
1385: public ObjectInstance createMBean(String className, ObjectName name)
1386: throws ReflectionException, InstanceAlreadyExistsException,
1387: MBeanRegistrationException, MBeanException,
1388: NotCompliantMBeanException {
1389: log.trace("createMBean called with className and ObjectName");
1390:
1391: Object obj = null;
1392:
1393: if (className == null)
1394: throw new RuntimeOperationsException(
1395: new IllegalArgumentException(
1396: "ClassName or ObjectName cannot be null"));
1397:
1398: Class class_name = null;
1399:
1400: try {
1401: class_name = Thread.currentThread().getContextClassLoader()
1402: .loadClass(className);
1403: } catch (ClassNotFoundException cne) {
1404: try {
1405: class_name = DefaultLoaderRepositoryExt
1406: .loadClass(className);
1407: } catch (ClassNotFoundException cnee) {
1408: throw new ReflectionException(cnee,
1409: "The MBean class could not be loaded by the default loader repository");
1410: }
1411: }
1412:
1413: try {
1414: obj = class_name.newInstance();
1415: } catch (InstantiationException ite) {
1416: if (Modifier.isAbstract(class_name.getModifiers())) {
1417: throw new NotCompliantMBeanException();
1418: }
1419: try {
1420: Constructor cons = class_name
1421: .getConstructor(new Class[0]);
1422: } catch (NoSuchMethodException e) {
1423: throw new ReflectionException(e);
1424: }
1425:
1426: throw new MBeanException(ite,
1427: "Exception occured while invoking the constructor of the MBean!!!");
1428: } catch (IllegalAccessException iae) {
1429: throw new MBeanException(iae,
1430: "Exception occured while invoking the constructor of the MBean!!!");
1431: } catch (Exception e) {
1432: if (e instanceof NotCompliantMBeanException) {
1433: throw (NotCompliantMBeanException) e;
1434: }
1435:
1436: throw new MBeanException(
1437: e,
1438: "General Exception occured. It might"
1439: + " be runtime errors while invoking the constructor");
1440: }
1441:
1442: return registerMBean(obj, name);
1443: }
1444:
1445: /**
1446: * Instantiates and registers a MBean with the MBeanServer.
1447: * The MBean server will use the default loader repository to load the
1448: * class of the MBean. An object name is associated to the MBean. If the
1449: * object name given is null, the MBean can automatically provide its own
1450: * name by implementing the MBeanRegistration interface. The call returns
1451: * a reference to the new instance and its object name.
1452: *
1453: * @param className The class name of the MBean to be instantiated.
1454: *
1455: * @param name The object name of the MBean. May be null.
1456: *
1457: * @param params An array containing the parameters of the constructor
1458: * to be invoked.
1459: *
1460: * @param signature An array containing the signature of the constructor
1461: * to be invoked.
1462: *
1463: * @return An ObjectInstance, containing the ObjectName and the Java class
1464: * name of the newly instantiated MBean.
1465: *
1466: * @exception javax.management.ReflectionException Wraps Wraps a
1467: * ClassNotFoundException or a java.lang.Exception that
1468: * occured trying to invoke the MBean's constructor.
1469: *
1470: * @exception javax.management.InstanceAlreadyExistsException The MBean is
1471: * already under the control of the MBeanServer.
1472: *
1473: * @exception javax.management.MBeanRegistrationException The preRegister
1474: * (MBeanRegistration interface) method of the MBean has
1475: * thrown an exception. The MBean will not be registered.
1476: *
1477: * @exception javax.management.MBeanException The constructor of the MBean
1478: * has thrown an exception
1479: **/
1480: public ObjectInstance createMBean(String className,
1481: ObjectName name, Object[] params, String[] signature)
1482: throws ReflectionException, InstanceAlreadyExistsException,
1483: MBeanRegistrationException, MBeanException,
1484: NotCompliantMBeanException {
1485: log
1486: .trace("createMBean called with className, ObjectName, "
1487: + "parameters as Object array and signature as String array");
1488:
1489: Object obj = null;
1490:
1491: if (className == null) {
1492: throw new RuntimeOperationsException(
1493: new IllegalArgumentException(
1494: "ClassName or ObjectName cannot be null"));
1495: }
1496:
1497: Class class_name = null;
1498:
1499: try {
1500: class_name = DefaultLoaderRepositoryExt
1501: .loadClass(className);
1502:
1503: if (params == null || signature == null) {
1504: return createMBean(className, name);
1505: }
1506:
1507: Class[] sig = new Class[signature.length];
1508:
1509: for (int i = 0; i < signature.length; i++) {
1510: if ((sig[i] = getThePrimitiveClassObject(signature[i])) == null) {
1511: try {
1512: sig[i] = Thread.currentThread()
1513: .getContextClassLoader().loadClass(
1514: signature[i]);
1515: } catch (ClassNotFoundException ee) {
1516: throw new ReflectionException(
1517: ee,
1518: "The MBean class "
1519: + "could not be loaded by the default loader repository");
1520: }
1521:
1522: try {
1523: if (obj instanceof DefaultDynamicMBean
1524: && ((DefaultDynamicMBean) obj)
1525: .getStandardMBeanObject() instanceof ClassLoader) {
1526: sig[i] = ((ClassLoader) ((DefaultDynamicMBean) obj)
1527: .getStandardMBeanObject())
1528: .loadClass(signature[i]);
1529: }
1530: } catch (ClassNotFoundException ce) {
1531: try {
1532: sig[i] = Thread.currentThread()
1533: .getContextClassLoader().loadClass(
1534: signature[i]);
1535: } catch (ClassNotFoundException ee) {
1536: throw new ReflectionException(ee,
1537: "The MBean class could not be loaded by the default loader repository");
1538: }
1539: }
1540:
1541: }
1542: }
1543:
1544: Constructor constr = null;
1545:
1546: constr = class_name.getConstructor(sig);
1547: obj = constr.newInstance(params);
1548: } catch (ClassNotFoundException cne) {
1549: throw new ReflectionException(cne,
1550: "The MBean class could not be loaded by the default loader repository");
1551: } catch (NoSuchMethodException ne) {
1552: throw new ReflectionException(ne);
1553: } catch (IllegalArgumentException iae) {
1554: throw new ReflectionException(iae);
1555: } catch (InstantiationException ie) {
1556: try {
1557: Class[] classParams = new Class[params.length];
1558:
1559: for (int i = 0; i < params.length; i++) {
1560: classParams[i] = params[i].getClass();
1561: }
1562:
1563: Constructor cons = class_name
1564: .getConstructor(classParams);
1565: } catch (NoSuchMethodException ne) {
1566: throw new ReflectionException(ne);
1567: }
1568:
1569: throw new MBeanException(ie);
1570: } catch (Exception e) {
1571: if (e instanceof ReflectionException) {
1572: throw (ReflectionException) e;
1573: }
1574:
1575: throw new MBeanException(e,
1576: "Exception occured while calling the constructor of the MBean!!!");
1577: }
1578:
1579: return registerMBean(obj, name);
1580: }
1581:
1582: /**
1583: * Instantiates and registers a MBean with the MBeanServer.
1584: * The class loader to be used is identified by its object name. An object
1585: * name is associated to the MBean. If the object name of the loader is null,
1586: * the system ClassLoader will be used.If the MBean's object name given is
1587: * null, the MBean can automatically provide its own name by implementing
1588: * the MBeanRegistration interface. The call returns a reference to the new
1589: * instance and its object name.
1590: *
1591: * @param className The class name of the MBean to be instantiated.
1592: *
1593: * @param name The object name of the MBean. May be null.
1594: *
1595: * @param loaderName The object name of the class loader to be used.
1596: *
1597: * @return An ObjectInstance, containing the ObjectName and the Java class
1598: * name of the newly instantiated MBean.
1599: *
1600: * @exception javax.management.InstanceAlreadyExistsException The MBean is
1601: * already under the control of the MBeanServer.
1602: *
1603: * @exception javax.management.MBeanRegistrationException The preRegister
1604: * (MBeanRegistration interface) method of the MBean has
1605: * thrown an exception. The MBean will not be registered.
1606: *
1607: * @exception javax.management.MBeanException The constructor of the MBean
1608: * has thrown an exception
1609: *
1610: * @exception javax.management.NotCompliantMBeanException This class is not
1611: * an JMX compliant MBean
1612: *
1613: * @exception javax.management.InstanceNotFoundException The specified class
1614: * loader is not registered in the MBeanServer.
1615: **/
1616: public ObjectInstance createMBean(String className,
1617: ObjectName name, ObjectName loaderName)
1618: throws ReflectionException, InstanceAlreadyExistsException,
1619: MBeanRegistrationException, MBeanException,
1620: NotCompliantMBeanException, InstanceNotFoundException {
1621: log.trace("createMBean called with className, ObjectName and "
1622: + "loaderName as ObjectName");
1623:
1624: Object object = null;
1625: Class clazz = null;
1626: Object obj = null;
1627:
1628: if (className == null) {
1629: throw new RuntimeOperationsException(
1630: new IllegalArgumentException(
1631: "ClassName or ObjectName cannot be null"));
1632: }
1633:
1634: try {
1635: if (loaderName == null) {
1636: //clazz = this.getClass().getClassLoader().loadClass(className);
1637: clazz = getClassLoader(this ).loadClass(className);
1638: } else if ((object = serverTable.get(loaderName)) == null) {
1639: //clazz = this.getClass().getClassLoader().loadClass(className);
1640: //clazz = getClassLoader(this).loadClass(className);
1641: throw new InstanceNotFoundException(loaderName
1642: .toString());
1643: } else if (object instanceof DefaultDynamicMBean
1644: && ((DefaultDynamicMBean) object)
1645: .getStandardMBeanObject() instanceof ClassLoader) {
1646: clazz = ((ClassLoader) ((DefaultDynamicMBean) object)
1647: .getStandardMBeanObject()).loadClass(className);
1648: } else if (!(object instanceof ClassLoader)) {
1649: throw new MBeanException(new Exception(
1650: "Invalid ClassLoader"));
1651: } else {
1652: clazz = ((ClassLoader) object).loadClass(className);
1653: }
1654:
1655: obj = clazz.newInstance();
1656: } catch (ClassNotFoundException cne) {
1657: throw new ReflectionException(cne,
1658: "The MBean class could not be loaded by the available class Loaders");
1659: } catch (InstantiationException ite) {
1660: try {
1661: Constructor cons = clazz.getConstructor(new Class[0]);
1662: } catch (NoSuchMethodException ne) {
1663: throw new ReflectionException(ne);
1664: }
1665:
1666: throw new MBeanException(ite);
1667: } catch (IllegalAccessException iae) {
1668: throw new MBeanException(iae,
1669: "Exception occured while invoking the constructor of the MBean!!!");
1670: } catch (Exception e) {
1671: throw new MBeanException(
1672: e,
1673: "General Exception occured .It might be runtime errors while invoking the constructor");
1674: }
1675:
1676: return registerMBean(obj, name);
1677: }
1678:
1679: /**
1680: * Instantiates and registers a MBean with the MBeanServer. The class
1681: * loader to be used is identified by its object name. An object name
1682: * is associated to the MBean. If the object name of the loader is not
1683: * specified, the system ClassLoader will be used.If the MBean object
1684: * name given is null, the MBean can automatically provide its own name
1685: * by implementing the MBeanRegistration interface. The call returns a
1686: * reference to the new instance and its object name.
1687: *
1688: * @param className The class name of the MBean to be instantiated.
1689: *
1690: * @param name The object name of the MBean. May be null.
1691: *
1692: * @param params An array containing the parameters of the constructor
1693: * to be invoked.
1694: *
1695: * @param signature An array containing the signature of the constructor
1696: * to be invoked.
1697: *
1698: * @param loaderName The object name of the class loader to be used.
1699: *
1700: * @return An ObjectInstance, containing the ObjectName and the Java class
1701: * name of the newly instantiated MBean.
1702: *
1703: * @exception javax.management.ReflectionException Wraps Wraps a
1704: * ClassNotFoundException or a java.lang.Exception that
1705: * occured trying to invoke the MBean's constructor.
1706: *
1707: * @exception javax.management.InstanceAlreadyExistsException The MBean is
1708: * already under the control of the MBeanServer.
1709: *
1710: * @exception javax.management.MBeanRegistrationException The preRegister
1711: * (MBeanRegistration interface) method of the MBean has thrown
1712: * an exception. The MBean will not be registered.
1713: *
1714: * @exception javax.management.MBeanException The constructor of the MBean
1715: * has thrown an exception
1716: **/
1717: public ObjectInstance createMBean(String className,
1718: ObjectName name, ObjectName loaderName, Object[] params,
1719: String[] signature) throws ReflectionException,
1720: InstanceAlreadyExistsException, MBeanRegistrationException,
1721: MBeanException, RuntimeMBeanException,
1722: NotCompliantMBeanException, InstanceNotFoundException {
1723: log
1724: .trace("createMBean called with className, ObjectName, loaderName as "
1725: + "ObjectName, parameters as Object array and signature as String array");
1726:
1727: Object object = null;
1728: Class clazz = null;
1729: Object obj = null;
1730:
1731: if (className == null) {
1732: throw new RuntimeOperationsException(
1733: new IllegalArgumentException(
1734: "ClassName or ObjectName cannot be null"));
1735: }
1736:
1737: try {
1738: if (loaderName == null) {
1739: //clazz = this.getClass().getClassLoader().loadClass(className);
1740: clazz = getClassLoader(this ).loadClass(className);
1741: } else if ((object = serverTable.get(loaderName)) == null) {
1742: //clazz = this.getClass().getClassLoader().loadClass(className);
1743: //clazz = getClassLoader(this).loadClass(className);
1744: throw new InstanceNotFoundException(loaderName
1745: .toString());
1746: } else if (object instanceof DefaultDynamicMBean
1747: && ((DefaultDynamicMBean) object)
1748: .getStandardMBeanObject() instanceof ClassLoader) {
1749: clazz = ((ClassLoader) ((DefaultDynamicMBean) object)
1750: .getStandardMBeanObject()).loadClass(className);
1751: } else if (!(object instanceof ClassLoader)) {
1752: throw new MBeanException(new Exception(
1753: "Invalid ClassLoader "));
1754: } else {
1755: clazz = ((ClassLoader) object).loadClass(className);
1756: }
1757: } catch (ClassNotFoundException cne) {
1758: throw new ReflectionException(cne,
1759: "The MBean class could not be "
1760: + "loaded by the available class Loaders");
1761: }
1762:
1763: if (params == null || signature == null) {
1764: return createMBean(className, name, loaderName);
1765: }
1766:
1767: try {
1768: Class[] sig = new Class[signature.length];
1769: for (int i = 0; i < signature.length; i++) {
1770: try {
1771: if ((sig[i] = getThePrimitiveClassObject(signature[i])) == null)
1772: sig[i] = Thread.currentThread()
1773: .getContextClassLoader().loadClass(
1774: signature[i]);
1775: } catch (ClassNotFoundException eee) {
1776: try {
1777: DefaultLoaderRepositoryExt
1778: .loadClass(signature[i]);
1779: } catch (ClassNotFoundException ee) {
1780: throw new ReflectionException(ee,
1781: "The MBean class could not be loaded by the default loader repository");
1782: }
1783: }
1784: }
1785:
1786: Constructor constr = null;
1787: constr = clazz.getConstructor(sig);
1788: obj = constr.newInstance(params);
1789: } catch (InstantiationException ie) {
1790: try {
1791: Class[] classParams = new Class[params.length];
1792: for (int i = 0; i < params.length; i++) {
1793: classParams[i] = params[i].getClass();
1794: }
1795: Constructor cons = clazz.getConstructor(classParams);
1796: } catch (NoSuchMethodException ne) {
1797: throw new ReflectionException(ne);
1798: }
1799: } catch (NoSuchMethodException nsme) {
1800: try {
1801: Class[] classParams = new Class[params.length];
1802: for (int i = 0; i < params.length; i++) {
1803: classParams[i] = params[i].getClass();
1804: }
1805:
1806: Constructor cons = clazz.getConstructor(classParams);
1807: obj = cons.newInstance(params);
1808: } catch (NoSuchMethodException ne) {
1809: throw new ReflectionException(ne);
1810: } catch (Exception ene) {
1811: throw new MBeanException(ene);
1812: }
1813: } catch (IllegalAccessException iae) {
1814: throw new MBeanException(iae,
1815: "Exception occured while invoking the constructor of the MBean!!!");
1816: } catch (Exception e) {
1817: if (e instanceof ReflectionException) {
1818: throw (ReflectionException) e;
1819: }
1820:
1821: if (e instanceof RuntimeException) {
1822: throw new RuntimeMBeanException((RuntimeException) e);
1823: }
1824:
1825: if (e instanceof MBeanException) {
1826: throw (MBeanException) e;
1827: }
1828:
1829: if (e instanceof InvocationTargetException) {
1830: Exception ee = (Exception) ((InvocationTargetException) e)
1831: .getTargetException();
1832: if (ee instanceof RuntimeException) {
1833: throw new RuntimeMBeanException(
1834: (RuntimeException) ee);
1835: } else {
1836: throw new MBeanException(ee);
1837: }
1838: }
1839:
1840: throw new ReflectionException(
1841: e,
1842: "General Exception occured. "
1843: + "It might be runtime errors while invoking the constructor");
1844: }
1845:
1846: return registerMBean(obj, name);
1847: }
1848:
1849: /**
1850: * De-serializes a byte array in the context of the class loader of an MBean.
1851: *
1852: * @param name The name of the MBean whose class loader should be used
1853: * for the de-serialization.
1854: *
1855: * @param data The byte array to be de-sererialized.
1856: *
1857: * @return The de-serialized object stream.
1858: *
1859: * @exception javax.management.InstanceNotFoundException The MBean
1860: * specified is not found.
1861: *
1862: * @exception javax.management.OperationsException Any of the usual
1863: * Input/Output related exceptions.
1864: */
1865: public java.io.ObjectInputStream deserialize(ObjectName name,
1866: byte[] data) throws InstanceNotFoundException,
1867: OperationsException {
1868: Object obj = serverTable.get(name);
1869: if (obj == null)
1870: throw new InstanceNotFoundException(name.toString());
1871:
1872: try {
1873: return deserialize(obj.getClass().getName(), data);
1874: } catch (ReflectionException re) {
1875: throw new OperationsException(
1876: "Exception while de-serializing the byte array data");
1877: }
1878: }
1879:
1880: /**
1881: * De-serializes a byte array in the context of a given MBean class loader.
1882: * The class loader is the one that loaded the class with name "className".
1883: *
1884: * @param name The name of the class whose class loader should be used
1885: * for the de-serialization.
1886: *
1887: * @param data The byte array to be de-sererialized.
1888: *
1889: * @return The de-serialized object stream.
1890: *
1891: * @exception javax.management.OperationsException Any of the usual
1892: * Input/Output related exceptions.
1893: *
1894: * @exception javax.management.ReflectionException The specified class could
1895: * not be loaded by the default loader repository
1896: */
1897: public java.io.ObjectInputStream deserialize(String className,
1898: byte[] data) throws OperationsException,
1899: ReflectionException {
1900: return deserialize(className, null, data);
1901: }
1902:
1903: /**
1904: * De-serializes a byte array in the context of a given MBean class loader.
1905: * The class loader is the one that loaded the class with name "className".
1906: * The name of the class loader to be used for loading the specified class
1907: * is specified. If null, the MBean Server's class loader will be used.
1908: *
1909: * @param name The name of the class whose class loader should be used
1910: * for the de-serialization.
1911: *
1912: * @param data The byte array to be de-sererialized.
1913: *
1914: * @param loaderName The name of the class loader to be used for loading
1915: * the specified class. If null, the MBean Server's
1916: * class loader will be used.
1917: *
1918: * @return The de-serialized object stream.
1919: *
1920: * @exception javax.management.InstanceNotFoundException The specified
1921: * class loader MBean is not found.
1922: *
1923: * @exception javax.management.OperationsException Any of the usual
1924: * Input/Output related exceptions.
1925: *
1926: * @exception javax.management.ReflectionException The specified class
1927: * could not be loaded by the specified class loader.
1928: */
1929: public java.io.ObjectInputStream deserialize(String className,
1930: ObjectName loaderName, byte[] data)
1931: throws InstanceNotFoundException, OperationsException,
1932: ReflectionException {
1933: Class clazz = null;
1934:
1935: if (data == null || data.length == 0)
1936: throw new RuntimeOperationsException(
1937: new IllegalArgumentException(), "invalid data");
1938:
1939: if (className == null)
1940: throw new RuntimeOperationsException(
1941: new IllegalArgumentException(), "invalid className");
1942:
1943: if (loaderName != null) {
1944: Object obj = serverTable.get(loaderName);
1945: try {
1946: if (obj == null) {
1947: throw new RuntimeOperationsException(
1948: new IllegalArgumentException(
1949: "Invalid classloader " + loaderName));
1950: } else if (obj instanceof DefaultDynamicMBean
1951: && ((DefaultDynamicMBean) obj)
1952: .getStandardMBeanObject() instanceof ClassLoader) {
1953: clazz = ((ClassLoader) ((DefaultDynamicMBean) obj)
1954: .getStandardMBeanObject())
1955: .loadClass(className);
1956: } else if (!(obj instanceof ClassLoader)) {
1957: throw new RuntimeOperationsException(
1958: new IllegalArgumentException(
1959: "Invalid classloader " + loaderName));
1960: } else {
1961: clazz = ((ClassLoader) obj).loadClass(className);
1962: }
1963: } catch (ClassNotFoundException cnfe) {
1964: throw new ReflectionException(cnfe,
1965: "Loading failed by the loader " + loaderName);
1966: }
1967: } else {
1968: try {
1969: //clazz = this.getClass().getClassLoader().loadClass(className);
1970: clazz = getClassLoader(this ).loadClass(className);
1971: } catch (ClassNotFoundException cnfe) {
1972: throw new ReflectionException(cnfe, className
1973: + " MBean could not be loaded");
1974: }
1975: }
1976:
1977: ByteArrayInputStream bis = new ByteArrayInputStream(data);
1978:
1979: ObjectInputStreamSupport oiss = null;
1980: try {
1981: //oiss = new ObjectInputStreamSupport(bis, clazz.getClassLoader());
1982: oiss = new ObjectInputStreamSupport(bis,
1983: getClassLoader(clazz));
1984: } catch (Exception e) {
1985: throw new OperationsException(
1986: "Exception while de-serializing the byte array data");
1987: }
1988:
1989: return oiss;
1990: }
1991:
1992: /**
1993: * Returns the number of MBeans controlled by the MBeanServer.
1994: **/
1995: public Integer getMBeanCount() {
1996: return new Integer(serverTable.size());
1997: }
1998:
1999: /**
2000: * This method de-serializes an object in the context of a given MBean
2001: * class loader.
2002: *
2003: * @param name The name of the MBean which defines the class loader
2004: *
2005: * @param entityBody The byte array to be de-sererialized.
2006: *
2007: * @return The de-serializaed object. Null can be returned if entityBody
2008: * is null or has no element(length to zero).
2009: *
2010: * @exception javax.management.InstanceNotFoundException The specified
2011: * MBean is not found.
2012: *
2013: * @exception java.lang.ClassNotFoundException The object which has to be
2014: * deserialized is not found the context the the MBean class loader
2015: *
2016: * @exception java.io.OptionalDataException Primitive data was found in
2017: * the stream instead of objects.
2018: *
2019: * @exception java.io.IOException Any of the usual Input/Output related
2020: * exceptions.
2021: **/
2022: public Object getObjectInClassLoader(ObjectName name,
2023: byte[] entityBody) throws InstanceNotFoundException,
2024: ClassNotFoundException, java.io.OptionalDataException,
2025: java.io.IOException
2026: //throws Exception
2027: {
2028: Object object = null;
2029: Class clazz = null;
2030:
2031: if (entityBody == null)
2032: throw new IOException("entityBody is null");
2033:
2034: if (entityBody.length == 0)
2035: throw new IOException("entityBody length is zero");
2036:
2037: return null;
2038: }
2039:
2040: /**
2041: * Instantiates an object using the list of all class loaders registered
2042: * in the MBeanServer. It returns a reference to the newly created object.
2043: *
2044: * @param className The class name of the object to be instantiated.
2045: *
2046: * @return The newly instantiated object.
2047: *
2048: * @exception javax.management.ReflectionException Wraps a
2049: * ClassNotFoundException or the java.lang.Exception that
2050: * occured trying to invoke the object's constructor.
2051: *
2052: * @exception javax.management.MBeanException The constructor of the object
2053: * has thrown an exception
2054: **/
2055: public Object instantiate(String className)
2056: throws ReflectionException, MBeanException {
2057: if (className == null)
2058: throw new RuntimeOperationsException(
2059: new IllegalArgumentException(
2060: "className cannot be null"));
2061:
2062: Class class_name = null;
2063:
2064: try {
2065: class_name = DefaultLoaderRepositoryExt
2066: .loadClass(className);
2067: return class_name.newInstance();
2068: } catch (ClassNotFoundException e) {
2069: throw new ReflectionException(e);
2070: } catch (IllegalAccessException ie) {
2071: throw new ReflectionException(ie);
2072: } catch (InstantiationException ie) {
2073: try {
2074: Constructor cons = class_name
2075: .getConstructor(new Class[0]);
2076: } catch (NoSuchMethodException e) {
2077: throw new ReflectionException(e);
2078: }
2079:
2080: throw new MBeanException(ie);
2081: } catch (Exception e1) {
2082: if (e1 instanceof ReflectionException) {
2083: throw (ReflectionException) e1;
2084: }
2085:
2086: throw new MBeanException(e1);
2087: }
2088: }
2089:
2090: /**
2091: * Instantiates an object using the list of all class loaders registered
2092: * in the MBeanServer. The call returns a reference to the newly created
2093: * object.
2094: *
2095: * @param className The class name of the object to be instantiated.
2096: *
2097: * @param params An array containing the parameters of the constructor
2098: * to be invoked.
2099: *
2100: * @param signature An array containing the signature of the constructor
2101: * to be invoked.
2102: *
2103: * @return The newly instantiated object.
2104: *
2105: * @exception javax.management.ReflectionException Wraps a
2106: * ClassNotFoundException or the java.lang.Exception that
2107: * occured trying to invoke the object's constructor.
2108: *
2109: * @exception javax.management.MBeanException The constructor of the object
2110: * has thrown an exception
2111: **/
2112: public Object instantiate(String className, Object[] params,
2113: String[] signature) throws ReflectionException,
2114: MBeanException {
2115: if (className == null)
2116: throw new RuntimeOperationsException(
2117: new IllegalArgumentException(
2118: "className cannot be null"));
2119:
2120: try {
2121: Class class_name = DefaultLoaderRepositoryExt
2122: .loadClass(className);
2123:
2124: if (params == null || signature == null) {
2125: return instantiate(className);
2126: }
2127:
2128: Class[] sig = new Class[signature.length];
2129: for (int i = 0; i < signature.length; i++) {
2130: if ((sig[i] = getThePrimitiveClassObject(signature[i])) == null)
2131: sig[i] = Thread.currentThread()
2132: .getContextClassLoader().loadClass(
2133: signature[i]);
2134: }
2135:
2136: Constructor constr = class_name.getConstructor(sig);
2137:
2138: return constr.newInstance(params);
2139: } catch (ClassNotFoundException e) {
2140: throw new ReflectionException(e);
2141: } catch (IllegalArgumentException ie) {
2142: throw new ReflectionException(ie);
2143: } catch (NoSuchMethodException ne) {
2144: throw new ReflectionException(ne);
2145: } catch (Exception e1) {
2146: throw new MBeanException(e1);
2147: }
2148: }
2149:
2150: /**
2151: * Instantiates an object using the class Loader specified by its ObjectName.
2152: * If the loader name is null, the system ClassLoader will be used. It
2153: * returns a reference to the new created object.
2154: *
2155: * @param className The class name of the MBean to be instantiated.
2156: *
2157: * @param loaderName The object name of the class loader to be used.
2158: *
2159: * @return The newly instantiated object.
2160: *
2161: * @exception javax.management.ReflectionException Wraps a
2162: * ClassNotFoundException or the java.lang.Exception
2163: * that occured trying to invoke the object's constructor.
2164: *
2165: * @exception javax.management.MBeanException The constructor of the
2166: * object has thrown an exception.
2167: *
2168: * @exception javax.management.InstanceNotFoundException The specified
2169: * class loader is not registered in the MBeanServer.
2170: **/
2171: public Object instantiate(String className, ObjectName loaderName)
2172: throws ReflectionException, MBeanException,
2173: InstanceNotFoundException {
2174: if (className == null)
2175: throw new RuntimeOperationsException(
2176: new IllegalArgumentException(
2177: "className cannot be null"));
2178:
2179: Object object = null;
2180: Object loaderObject = null;
2181: Class clazz = null;
2182:
2183: try {
2184: object = serverTable.get(loaderName);
2185: if ((object instanceof DefaultDynamicMBean)) {
2186: loaderObject = ((DefaultDynamicMBean) (object))
2187: .getStandardMBeanObject();
2188:
2189: } else if (!(object instanceof ClassLoader)) {
2190: throw new MBeanException(new Exception(
2191: "Invalid ClassLoader "));
2192: }
2193:
2194: if (loaderObject instanceof ClassLoader) {
2195: clazz = ((ClassLoader) loaderObject)
2196: .loadClass(className);
2197: } else {
2198: throw new MBeanException(new Exception(
2199: "Invalid ClassLoader "));
2200: }
2201: } catch (ClassNotFoundException cnfe) {
2202: throw new ReflectionException(cnfe);
2203: } catch (Exception e) {
2204: try {
2205: clazz = getClassLoader(this ).loadClass(className);
2206: } catch (ClassNotFoundException cnfe) {
2207: throw new ReflectionException(cnfe);
2208: }
2209: }
2210:
2211: try {
2212: return clazz.newInstance();
2213: } catch (IllegalAccessException ie) {
2214: throw new ReflectionException(ie);
2215: } catch (Exception e1) {
2216: throw new MBeanException(e1,
2217: "Exception occured while invoking the constructor of the MBean");
2218: }
2219: }
2220:
2221: /**
2222: * Instantiates an object. The class loader to be used is identified by its
2223: * object name. If the object name of the loader is null, the system ClassLoader
2224: * will be used. The call returns a reference to the newly created object.
2225: *
2226: * @param className The class name of the object to be instantiated.
2227: *
2228: * @param params An array containing the parameters of the constructor
2229: * to be invoked.
2230: *
2231: * @param signature An array containing the signature of the constructor
2232: * to be invoked.
2233: *
2234: * @param loaderName The object name of the class loader to be used.
2235: *
2236: * @return The newly instantiated object.
2237: *
2238: * @exception javax.management.ReflectionException Wraps a
2239: * ClassNotFoundException or the java.lang.Exception that
2240: * occured trying to invoke the object's constructor.
2241: *
2242: * @exception javax.management.MBeanException The constructor of the object has
2243: * thrown an exception
2244: *
2245: * @exception javax.management.InstanceNotFoundException The specified class
2246: * loader is not registered in the MBeanServer.
2247: **/
2248: public Object instantiate(String className, ObjectName loaderName,
2249: Object[] params, String[] signature)
2250: throws ReflectionException, MBeanException,
2251: InstanceNotFoundException {
2252: if (className == null)
2253: throw new RuntimeOperationsException(
2254: new IllegalArgumentException(
2255: "className cannot be null"));
2256:
2257: Object object = null;
2258: Class clazz = null;
2259: Object loaderObject = null;
2260:
2261: try {
2262: object = serverTable.get(loaderName);
2263:
2264: if ((object instanceof DefaultDynamicMBean)) {
2265: loaderObject = ((DefaultDynamicMBean) (object))
2266: .getStandardMBeanObject();
2267: } else if (!(object instanceof ClassLoader)) {
2268: throw new MBeanException(new Exception(
2269: "Invalid ClassLoader "));
2270: }
2271:
2272: if (loaderObject instanceof ClassLoader) {
2273: clazz = ((ClassLoader) loaderObject)
2274: .loadClass(className);
2275: } else {
2276: throw new MBeanException(new Exception(
2277: "Invalid ClassLoader "));
2278: }
2279: } catch (ClassNotFoundException cnfe) {
2280: throw new ReflectionException(cnfe);
2281: } catch (Exception e) {
2282: try {
2283: //clazz = this.getClass().getClassLoader().loadClass(className);
2284: clazz = getClassLoader(this ).loadClass(className);
2285: } catch (ClassNotFoundException cnfe) {
2286: throw new ReflectionException(cnfe);
2287: }
2288: }
2289:
2290: if (params == null || signature == null) {
2291: return instantiate(className, loaderName);
2292: }
2293:
2294: try {
2295: Class[] sig = new Class[signature.length];
2296:
2297: for (int i = 0; i < signature.length; i++) {
2298: if ((sig[i] = getThePrimitiveClassObject(signature[i])) == null)
2299: sig[i] = Thread.currentThread()
2300: .getContextClassLoader().loadClass(
2301: signature[i]);
2302: }
2303:
2304: Constructor constr = clazz.getConstructor(sig);
2305: return constr.newInstance(params);
2306: } catch (ClassNotFoundException cnfe) {
2307: throw new ReflectionException(cnfe);
2308: } catch (IllegalAccessException ie) {
2309: throw new ReflectionException(ie);
2310: } catch (Exception e1) {
2311: throw new MBeanException(e1);
2312: }
2313: }
2314:
2315: /**
2316: * This method checks if an object has been loaded in the context of a
2317: * MBean class loader.
2318: *
2319: * @param name The name of the MBean which defines the class loader
2320: *
2321: * @param object The object to check.
2322: *
2323: * @return True if the object as been loaded in the context of the MBean
2324: * class loader ; false if not. of this MBean.
2325: *
2326: * @exception javax.management.InstanceNotFoundException The specified
2327: * MBean is not found.
2328: **/
2329: public boolean isInSameClassLoader(ObjectName name, Object object)
2330: throws InstanceNotFoundException {
2331: return false;
2332: }
2333:
2334: /**
2335: * @exception java.lang.IllegalArgumentException The object with name "name"
2336: * doesn't implements NotificationBroadcaster
2337: **/
2338: public void removeNotificationListener(ObjectName name,
2339: NotificationListener listener)
2340: throws InstanceNotFoundException, ListenerNotFoundException {
2341: Object object = null;
2342:
2343: if (name == null) {
2344: throw new RuntimeOperationsException(
2345: new IllegalArgumentException(
2346: "ObjectName cannot be null!!!"));
2347: }
2348:
2349: object = serverTable.get(name);
2350:
2351: if (object == null) {
2352: throw new InstanceNotFoundException(
2353: "No MBean with the Object Name " + listener
2354: + " registered with MBeanServer");
2355: }
2356:
2357: if (!(object instanceof NotificationBroadcaster)) {
2358: throw new RuntimeOperationsException(
2359: new IllegalArgumentException(
2360: "The MBean"
2361: + name
2362: + " does not implement NotificationBroadCaster"));
2363: }
2364:
2365: synchronized (object) {
2366: ((NotificationBroadcaster) object)
2367: .removeNotificationListener(listener);
2368: }
2369: }
2370:
2371: /**
2372: * Enables a listener for an MBean to be removed.
2373: *
2374: * @param name The name of the MBean on which the listener should be removed
2375: *
2376: * @param listener The listener name which will handles notifications
2377: * emitted by the registered MBean. This method will remove
2378: * all information related to this listener.
2379: *
2380: * @exception javax.management.InstanceNotFoundException The MBean name or
2381: * the listener name doesn't correspond to a registered MBean
2382: *
2383: * @exception javax.management.ListenerNotFoundException The couple
2384: * (listener,handback) is not registered in the MBean. The
2385: * exception message contains either "listener", "handback" or
2386: * the object name depending on which object cannot be found.
2387: *
2388: * @exception java.lang.IllegalArgumentException The object with name "name"
2389: * doesn't implements NotificationBroadcaster or the object
2390: * with name "listener" doesn't implements NotificationListener.
2391: * The faulty ObjectName string representation can be obtained
2392: * by means of the getMessage() method of the
2393: * IllegalArgumentException object.
2394: **/
2395: public void removeNotificationListener(ObjectName name,
2396: ObjectName listener) throws InstanceNotFoundException,
2397: ListenerNotFoundException {
2398: Object object = null;
2399:
2400: if (name == null) {
2401: throw new RuntimeOperationsException(
2402: new IllegalArgumentException(
2403: "ObjectName cannot be null!!!"));
2404: }
2405: object = serverTable.get(name);
2406:
2407: if (object == null) {
2408: throw new InstanceNotFoundException(
2409: "No MBean with the Object Name " + listener
2410: + " registered with MBeanServer");
2411: }
2412:
2413: Object lobject = null;
2414: lobject = serverTable.get(listener);
2415:
2416: if (lobject == null) {
2417: throw new InstanceNotFoundException(
2418: "No listener with the name " + listener
2419: + " present in the JMXServer!!!");
2420: }
2421:
2422: if (!(lobject instanceof NotificationListener)) {
2423: throw new RuntimeOperationsException(
2424: new IllegalArgumentException(
2425: "The MBean"
2426: + name
2427: + " does not implement NotificationListener"));
2428: }
2429:
2430: synchronized (object) {
2431: ((NotificationBroadcaster) object)
2432: .removeNotificationListener((NotificationListener) lobject);
2433: }
2434: }
2435:
2436: /**
2437: * Returns the table that has the loader references os the mbeans.
2438: * Used by the package access class.
2439: */
2440: public Hashtable getLoaderTable() {
2441: return loaderTable;
2442: }
2443:
2444: //---------------------------- Private methods --------------------------//
2445:
2446: private Class getThePrimitiveClassObject(String str) {
2447: if (str.indexOf("boolean") != -1)
2448: return Boolean.TYPE;
2449: else if (str.indexOf("int") != -1)
2450: return Integer.TYPE;
2451: else if (str.indexOf("double") != -1)
2452: return Double.TYPE;
2453: else if (str.indexOf("float") != -1)
2454: return Float.TYPE;
2455: else if (str.indexOf("short") != -1)
2456: return Short.TYPE;
2457: else if (str.indexOf("long") != -1)
2458: return Long.TYPE;
2459: else if (str.indexOf("byte") != -1)
2460: return Byte.TYPE;
2461: else if (str.indexOf("char") != -1)
2462: return Character.TYPE;
2463: else
2464: return null;
2465: }
2466:
2467: private void testForCompliance(String className, ClassLoader l)
2468: throws NotCompliantMBeanException {
2469: Class cl = null;
2470:
2471: try {
2472: cl = Class.forName(className, true, l);
2473: int i = cl.getModifiers();
2474: if (Modifier.isAbstract(i))
2475: throw new NotCompliantMBeanException(
2476: "The MBean cannot be a abstract class");
2477:
2478: java.lang.reflect.Constructor constructors[] = cl
2479: .getConstructors();
2480:
2481: if (constructors.length == 0)
2482: throw new NotCompliantMBeanException(
2483: "The MBean does not have a public constructor");
2484: } catch (ClassNotFoundException ee) {
2485: log.warn("ClassNotFoundException", ee);
2486: }
2487:
2488: Class super Class = null;
2489: super Class = cl;
2490: Class class1 = null;
2491: Class class2 = null;
2492: boolean flag = false;
2493: for (; super Class != null; super Class = super Class
2494: .getSuperclass()) {
2495: Class[] interfaces = super Class.getInterfaces();
2496: boolean checkFlag = false;
2497: for (int j = 0; j < interfaces.length; j++) {
2498: Class tempClass = checkForMBeanInterface(interfaces[j],
2499: super Class.getName());
2500: if (tempClass != null) {
2501: flag = true;
2502: class1 = tempClass;
2503: class2 = super Class;
2504: }
2505:
2506: if (checkForDynamicMBeanInterface(interfaces[j])) {
2507: flag = true;
2508: checkFlag = true;
2509: }
2510:
2511: if (class1 != null && super Class.equals(class2)
2512: && checkFlag)
2513: throw new NotCompliantMBeanException(
2514: className
2515: + " implements javax.management.DynamicMBean and MBean interface");
2516: }
2517: if (flag)
2518: break;
2519: }
2520:
2521: if (!flag)
2522: throw new NotCompliantMBeanException(className
2523: + " does not implement the " + className + "MBean"
2524: + " interface or the DynamicMBean interface");
2525: }
2526:
2527: private Class checkForMBeanInterface(Class class1, String s) {
2528: if (class1.getName().compareTo(s + "MBean") == 0)
2529: return class1;
2530:
2531: Class aclass[] = class1.getInterfaces();
2532: for (int i = 0; i < aclass.length; i++) {
2533: if (aclass[i].getName().compareTo(s + "MBean") == 0)
2534: return aclass[i];
2535: }
2536:
2537: return null;
2538: }
2539:
2540: private boolean checkForDynamicMBeanInterface(Class class1) {
2541: if (class1.getName().compareTo("javax.management.DynamicMBean") == 0)
2542: return true;
2543: Class aclass[] = class1.getInterfaces();
2544: for (int i = 0; i < aclass.length; i++)
2545: if (aclass[i].getName().compareTo(
2546: "javax.management.DynamicMBean") == 0)
2547: return true;
2548:
2549: return false;
2550: }
2551:
2552: private boolean isValidMBeanInfo(MBeanInfo mbInfo) {
2553: if (mbInfo.getClassName() == null) {
2554: return false;
2555: }
2556:
2557: /*
2558: MBeanConstructorInfo[] consInfo = mbInfo.getConstructors();
2559: if(consInfo == null){
2560: return false;
2561: }
2562: else if(consInfo.length == 0){
2563: return false;
2564: }*/
2565:
2566: return true;
2567: }
2568:
2569: private ClassLoader getClassLoader(Object o) {
2570: return getClassLoader(o.getClass());
2571: }
2572:
2573: private ClassLoader getClassLoader(Class clazz) {
2574: ClassLoader cl = clazz.getClassLoader();
2575: if (cl == null) {
2576: cl = Thread.currentThread().getContextClassLoader();
2577: }
2578: return cl;
2579: }
2580:
2581: private void createLogger() {
2582: try {
2583: log = LogFactory.getInstance("JMX");
2584: } catch (Exception e) {
2585: System.out.println("FATAL:Log Creation Failed");
2586: }
2587: }
2588: }
|