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 javax.management.modelmbean;
0027:
0028: import java.lang.reflect.Constructor;
0029: import java.lang.reflect.Method;
0030: import java.lang.reflect.InvocationTargetException;
0031: import java.io.PrintStream;
0032: import java.io.InputStream;
0033: import java.io.OutputStream;
0034: import java.io.File;
0035: import java.io.FileInputStream;
0036: import java.io.FileOutputStream;
0037: import java.io.ObjectInputStream;
0038: import java.io.ObjectOutputStream;
0039: import java.util.Date;
0040: import java.util.Enumeration;
0041: import java.util.Hashtable;
0042: import java.util.Vector;
0043:
0044: import javax.management.ObjectName;
0045: import javax.management.Attribute;
0046: import javax.management.AttributeList;
0047: import javax.management.AttributeChangeNotification;
0048: import javax.management.AttributeChangeNotificationFilter;
0049: import javax.management.Descriptor;
0050: import javax.management.MBeanInfo;
0051: import javax.management.MBeanOperationInfo;
0052: import javax.management.MBeanParameterInfo;
0053: import javax.management.MBeanNotificationInfo;
0054: import javax.management.Notification;
0055: import javax.management.NotificationBroadcasterSupport;
0056: import javax.management.NotificationFilter;
0057: import javax.management.NotificationListener;
0058: import javax.management.MBeanException;
0059: import javax.management.AttributeNotFoundException;
0060: import javax.management.InstanceNotFoundException;
0061: import javax.management.InvalidAttributeValueException;
0062: import javax.management.ListenerNotFoundException;
0063: import javax.management.ReflectionException;
0064: import javax.management.RuntimeOperationsException;
0065: import javax.management.ServiceNotFoundException;
0066:
0067: import javax.jmx.openmbean.CompositeData;
0068: import javax.jmx.openmbean.TabularData;
0069: import javax.jmx.openmbean.TabularParameterInfo;
0070:
0071: import com.adventnet.agent.utilities.scheduler.Scheduler;
0072: import com.adventnet.agent.logging.Log;
0073: import com.adventnet.agent.logging.LogFactory;
0074:
0075: /**
0076: * This class is the implementation of a ModelMBean. An appropriate
0077: * implementation of a ModelMBean must be shipped with every JMX Agent and
0078: * the class must be named RequiredModelMBean.
0079: * <p>
0080: * Java resources wishing to be manageable instatiate the RequiredModelMBean
0081: * using the MBeanServer's createMBean method. The resource then sets the
0082: * MBeanInfo and Descriptors for the RequiredModelMBean instance. The
0083: * attributes and operations exposed via the ModelMBeanInfo for the ModelMBean
0084: * are accessible from Mbeans, connectors/adapters like other MBeans. Through
0085: * the Descriptors, values and methods in the managed application can be
0086: * defined and mapped to attributes and operations of the ModelMBean. This
0087: * mapping can be defined in an XML formatted file or dynamically and
0088: * programmatically at runtime.
0089: * <p>
0090: * Every RequiredModelMBean which is instantiated in the MBeanServer becomes
0091: * manageable: its attributes and operations become remotely accessible through
0092: * the connectors/adaptors connected to that MBeanServer. A Java object cannot
0093: * be registered in the MBeanServer unless it is a JMX compliant MBean. By
0094: * instantiating a RequiredModelMBean, resources are guaranteed that the MBean
0095: * is valid. MBeanException and RuntimeOperatiosException must be thrown on
0096: * every public method. This allows for wrappering exceptions from distributed
0097: * communications (RMI, EJB, etc.)
0098: **/
0099: public class RequiredModelMBean implements ModelMBean {
0100: /** The mbean information of this mbean **/
0101: ModelMBeanInfo mbeanInfo = null;
0102:
0103: /** The actual instrumentaton object. **/
0104: Object object = null;
0105:
0106: /** The actual instrumentaton object type. **/
0107: String objectType = null;
0108:
0109: /** The Class object of the instrumentation **/
0110: Class clazz = null;
0111:
0112: /** The object name of the mbean **/
0113: private ObjectName objectName = null;
0114:
0115: private Log log;
0116:
0117: private static final String OBJECT_REFERENCE = "ObjectReference";
0118: private static final String HANDLE = "Handle";
0119: private static final String IOR = "IOR";
0120: private static final String EJB_HANDLE = "EJBHandle";
0121: private static final String RMI_REFERENCE = "RMIReference";
0122:
0123: private final static String direcName = "." + File.separator
0124: + "jmxstore";
0125:
0126: /**
0127: * The configuration file name which has the list of persistent
0128: * information of mbeans.
0129: */
0130: String confFileName = "JMXObjects.conf";
0131:
0132: /** The directory specifying where the conf file resides **/
0133: String confDirName = direcName;
0134:
0135: /** The complete path ...**/
0136: String confFile = confDirName + File.separator + confFileName;
0137:
0138: private String storageDirName = null;
0139: private String storageFileName = null;
0140:
0141: private String persistLocation = null;
0142: private String persistName = null;
0143:
0144: private Vector nameVec = null;
0145: private Vector valueVec = null;
0146:
0147: /** The enable storage flag **/
0148: private boolean enableStorage = true;
0149:
0150: /** indicated whether the storage is allowed **/
0151: private boolean storageAllowed = true;
0152:
0153: //added for use in store method..
0154: private ModelMBeanAttributeInfo currentads = null;
0155:
0156: private Date nomoreoftentimer = null;
0157:
0158: /** The first packet flag **/
0159: private boolean firstPacket = true;
0160:
0161: /** field separator used in the conf file **/
0162: private String fieldSeparator = " \t\n\r";
0163:
0164: /**
0165: * The table that hols the persistent mbeans info as specified in
0166: * the conf file
0167: */
0168: Hashtable confFileTable = new Hashtable();
0169:
0170: /**
0171: * The scheduler object used for timer persistent suppport for different
0172: * attributes. Each OnTimer attribute gets scheduled by this scheduler.
0173: */
0174: private static Scheduler scheduler = null;
0175:
0176: Hashtable adsValueTable = new Hashtable();
0177:
0178: PrintStream prints = null;
0179:
0180: //Implementing ModelMBeanNotificationBroadcaster
0181: NotificationBroadcasterSupport notifbroadcaster = null;
0182: NotificationBroadcasterSupport attrbroadcaster = null;
0183: Hashtable attrFilterMapTable = new Hashtable();
0184:
0185: String[] notiffields1 = new String[] { "name=General",
0186: "descriptorType=notification", "severity=5", "log=T",
0187: "displayName=jmx general" };
0188:
0189: String[] notiffields2 = new String[] { "name=AttributeChange",
0190: "descriptorType=notification", "severity=5", "log=T",
0191: "displayName=jmx attribute change" };
0192:
0193: private static int MAX_THREADS = 4;
0194:
0195: private static boolean modelMBeansStorage = true;
0196:
0197: private static boolean modelMBeansCaching = true;
0198:
0199: /**
0200: * Constructs an RequiredModelMBean with an empty ModelMBeanInfo. After the
0201: * RequiredModelMBean's MBeanInfo and Descriptors are customized, the
0202: * RequiredModelMBean should be registered with the MBeanServer.
0203: *
0204: * @exception MBeanException The constructor of the object has thrown an exception.
0205: *
0206: * @exception RuntimeOperationsException Wraps an IllegalArgumentException
0207: */
0208: public RequiredModelMBean() throws MBeanException,
0209: RuntimeOperationsException {
0210: try {
0211: createLogger();
0212: log.trace("Before calling load method");
0213: load();
0214: log.trace("load called successfully");
0215: } catch (Exception e) {
0216: e.printStackTrace();
0217: }
0218: }
0219:
0220: /**
0221: * Constructs a RequiredModelMBean object using ModelMBeanInfo passed in.
0222: * The RequiredModelMBean must be instantiated, but not registered with the
0223: * MBeanServer. After the RequiredModelMBean's MBeanInfo and Descriptors
0224: * are customized, the RequiredModelMBean should be registered with the
0225: * MBeanServer.
0226: *
0227: * @param mbi The ModelMBeanInfo object to be used by the RequiredModelMBean.
0228: *
0229: * @exception MBeanException The constructor of the object has thrown an exception.
0230: *
0231: * @exception RuntimeOperationsException Wraps an IllegalArgumentException :
0232: * The MBeanInfo passed in parameter is null or invalid.
0233: */
0234: public RequiredModelMBean(ModelMBeanInfo mbi)
0235: throws MBeanException, RuntimeOperationsException {
0236: createLogger();
0237:
0238: if (mbi == null)
0239: throw new RuntimeOperationsException(
0240: new IllegalArgumentException("mbi"));
0241:
0242: this .mbeanInfo = mbi;
0243:
0244: try {
0245: log.trace("Before calling load method");
0246: load();
0247: log.trace("load called successfully");
0248: } catch (Exception e) {
0249: e.printStackTrace();
0250: }
0251: }
0252:
0253: /**
0254: * Sets the instance handle of the object against which to execute all
0255: * methods in this RequiredModelMBean management interface (ModelMBeanInfo
0256: * and Descriptors). This setting can be overridden by setting the
0257: * 'targetObject' field of the ModelMBeanOperationInfo's descriptor.
0258: *
0259: * @param mr Object that is the managed resource
0260: *
0261: * @param mr_type The type of reference for the managed resource. Can be:
0262: * ObjectReference, Handle, IOR, EJBHandle, RMIReference.
0263: * If the MBeanServer cannot process the mr_type passed in,
0264: * an exception will be thrown. Current Implementation of
0265: * RequiredModelMBean supports only ObjectReference.
0266: *
0267: * @exception MBeanException The initializer of the object has thrown an exception.
0268: *
0269: * @exception RuntimeOperationsException Wraps an IllegalArgumentException:
0270: * The managed resource or managed resoure type passed in
0271: * parameter is null or invalid.
0272: *
0273: * @exception InstanceNotFoundException The managed resource object
0274: * could not be found
0275: *
0276: * @exception InvalidTargetObjectType The managed resource type cannot
0277: * be processed by the RequiredModelMBean or JMX Agent.
0278: */
0279: public void setManagedResource(Object mr, String mr_type)
0280: throws MBeanException, RuntimeOperationsException,
0281: InstanceNotFoundException, InvalidTargetObjectTypeException {
0282: if (mr == null || mr_type == null)
0283: throw new RuntimeOperationsException(
0284: new IllegalArgumentException(
0285: "Invalid mr or mr_type"));
0286:
0287: if (mr_type.equalsIgnoreCase(OBJECT_REFERENCE) == false)
0288: throw new InvalidTargetObjectTypeException(
0289: ("Unsupported mr_tpe"));
0290:
0291: this .object = mr;
0292: this .objectType = mr_type;
0293:
0294: clazz = object.getClass();
0295:
0296: if (scheduler == null) {
0297: scheduler = Scheduler.createScheduler("JMX", MAX_THREADS);
0298: scheduler.start();
0299: log.trace("JMX Scheduler instantiated successfully");
0300: }
0301:
0302: //set the "class" field in ModelMBeanOperationInfo's & ModelMBeanConstructorInfo's
0303: //if it is not already set
0304:
0305: if (this .mbeanInfo != null) {
0306: ModelMBeanConstructorInfo[] constrs = (ModelMBeanConstructorInfo[]) this .mbeanInfo
0307: .getConstructors();
0308: ModelMBeanOperationInfo[] opers = (ModelMBeanOperationInfo[]) this .mbeanInfo
0309: .getOperations();
0310:
0311: if (constrs != null) {
0312: for (int i = 0; i < constrs.length; i++)
0313: if (constrs[i].getDescriptor().getFieldValue(
0314: "class") == null)
0315: constrs[i].getDescriptor().setField("class",
0316: clazz.getName());
0317: }
0318:
0319: if (opers != null) {
0320: for (int i = 0; i < opers.length; i++)
0321: if (opers[i].getDescriptor().getFieldValue("class") == null)
0322: opers[i].getDescriptor().setField("class",
0323: clazz.getName());
0324: }
0325: }
0326:
0327: //to be deleted
0328: initializeAttributeDescriptorsField((ModelMBeanAttributeInfo[]) mbeanInfo
0329: .getAttributes());
0330: log.trace("Attribute Descriptors Initialized");
0331: }
0332:
0333: /**
0334: * Initializes a RequiredModelMBean object using ModelMBeanInfo passed in.
0335: * The RequiredModelMBean should be instantiated, but not registered with
0336: * the MBeanServer. After the RequiredModelMBean's MBeanInfo and
0337: * Descriptors are customized, the RequiredModelMBean should be registered
0338: * with the MBeanServer.
0339: * <P>
0340: * @param mbi The MBeanInfo object to be used by the RequiredModelMBean.
0341: *
0342: * @exception MBeanException The constructor of the MBeanInfo has return null
0343: * or thrown an exception.
0344: *
0345: * @exception RuntimeOperationsException Wraps an IllegalArgumentException:
0346: * The MBeanInfo passed in parameter is null or invalid.
0347: *
0348: */
0349: public void setModelMBeanInfo(ModelMBeanInfo mbi)
0350: throws MBeanException, RuntimeOperationsException {
0351: if (mbi == null)
0352: throw new RuntimeOperationsException(
0353: new IllegalArgumentException("mbi"));
0354:
0355: this .mbeanInfo = mbi;
0356: }
0357:
0358: /**
0359: * Locates the MBean in a persistent store and primes this instance of the
0360: * MBean with the stored values.Any currently set values are overwritten.
0361: *
0362: * @throws MBeanException Wraps another exception or persistence is not supported
0363: *
0364: * @throws RuntimeOperationsException Wraps exceptions from the persistence mechanism
0365: *
0366: * @throws InstanceNotFoundException Could not find or load this MBean
0367: * from persistent storage
0368: */
0369: public void load() throws MBeanException,
0370: RuntimeOperationsException, InstanceNotFoundException {
0371: // Check for Persistence else return
0372: if (!findPersistent()) {
0373: log.info("NO PERSISTENT DATA FOUND");
0374: return;
0375: }
0376:
0377: // Read the MbeanInfo and initialize attributes
0378: try {
0379: InputStream in = new FileInputStream(persistLocation
0380: + File.separator + persistName);
0381: ObjectInputStream data = new ObjectInputStream(in);
0382:
0383: mbeanInfo = (ModelMBeanInfo) data.readObject();
0384: initializeAttributeDescriptorsField((ModelMBeanAttributeInfo[]) mbeanInfo
0385: .getAttributes());
0386:
0387: while (true) {
0388: try {
0389: String name = (String) data.readObject();
0390: Object value = data.readObject();
0391:
0392: if (value instanceof TabularData && value != null) {
0393: TabularData tdata = (TabularData) value;
0394: for (Enumeration e = tdata.enumerate(); e
0395: .hasMoreElements();) {
0396: Object[] index = (Object[]) e.nextElement();
0397: CompositeData comp = tdata.getRow(index);
0398: comp
0399: .setOperationType(CompositeData.CREATED);
0400:
0401: for (Enumeration ce = comp.enumerate(); ce
0402: .hasMoreElements();) {
0403: String key = (String) ce.nextElement();
0404: comp.setModified(key, true);
0405: }
0406: }
0407: }
0408:
0409: log.debug("load(): Going to set " + value);
0410: setAttribute(new Attribute(name, value));
0411: } catch (Exception e1) {
0412: break;
0413: }
0414: }
0415: } catch (Exception e) {
0416: throw new MBeanException(e);
0417: }
0418: }
0419:
0420: /**
0421: * Writes the MBean in a persistent store.Should only be called by
0422: * the <i> RequiredModelMBean</i> class to store itself according to
0423: * persistence policy for the MBean.When used,it may be called with every
0424: * setAttribute or on a periodic basis. Currently the store supports only
0425: * the flat file persistence.
0426: *
0427: * @throws MBeanException Wraps another exception or persistence is not supported
0428: *
0429: * @throws RuntimeOperationsException Wraps exceptions from the persistence mechanism
0430: *
0431: * @throws InstanceNotFoundException Could not find or load this MBean
0432: * from persistent storage
0433: */
0434: public void store() throws MBeanException,
0435: RuntimeOperationsException, InstanceNotFoundException {
0436: try {
0437: Descriptor mmbDesc = mbeanInfo.getMBeanDescriptor();
0438:
0439: persistLocation = (String) mmbDesc
0440: .getFieldValue("persistLocation");
0441: persistName = (String) mmbDesc.getFieldValue("persistName");
0442:
0443: log.trace("Location=" + persistLocation + " persistName="
0444: + persistName);
0445:
0446: if (!modelMBeansStorage)
0447: storageAllowed = modelMBeansStorage;
0448:
0449: if (!storageAllowed)
0450: return;
0451:
0452: ModelMBeanAttributeInfo[] attrs = (ModelMBeanAttributeInfo[]) mbeanInfo
0453: .getAttributes();
0454: Descriptor attributeDescriptor = currentads.getDescriptor();
0455: String persistPolicy = (String) attributeDescriptor
0456: .getFieldValue(DescriptorSupport.PERSIST_POLICY);
0457:
0458: //log.debug("Grrrrrr"+attributeDescriptor.getFieldValue(DescriptorSupport.LAST_UPDATED_TIMESTAMP));
0459:
0460: //if the attribute is not writable or persist policy is null
0461: if (!(currentads.isWritable()) || persistPolicy == null) {
0462: log
0463: .trace("attribute is not writable or persist policy is null");
0464: return;
0465: }
0466:
0467: //if persist policy is never
0468: if (persistPolicy.equalsIgnoreCase("Never")) {
0469: log.trace(" persist policy is never");
0470: return;
0471: }
0472: //if persist policy is OnTimer
0473: else if (persistPolicy.equalsIgnoreCase("OnTimer")) {
0474: log.trace(" persist policy is OnTimer");
0475:
0476: //get The Persist Period
0477: String period = (String) attributeDescriptor
0478: .getFieldValue(DescriptorSupport.PERSIST_PERIOD);
0479: long persistPeriod = Long.parseLong(period);
0480:
0481: //get The CurrentTime
0482: long currTime = new Date().getTime();
0483:
0484: Date when = new Date(currTime + persistPeriod);
0485: //schedule Task
0486: scheduler.scheduleTask(new UpdateTimer(this ,
0487: attributeDescriptor, when.getTime(),
0488: persistPeriod), when);
0489: }
0490: //if persist policy is OnUpdate
0491: else if (persistPolicy.equalsIgnoreCase("OnUpdate")) {
0492: log.trace(" persist policy is OnUpdate");
0493: OnUpdate(attributeDescriptor);
0494: }
0495: //if persist policy is NoMoreOftenThan
0496: else if (persistPolicy.equalsIgnoreCase("NoMoreOftenThan")) {
0497: log.trace(" persist policy is NoMoreOftenThan");
0498: //get the time period
0499: String period = (String) attributeDescriptor
0500: .getFieldValue(DescriptorSupport.PERSIST_PERIOD);
0501: long persistPeriod = Long.parseLong(period);
0502: log.trace("Persist Period=" + persistPeriod);
0503:
0504: //get the last updated time stamp
0505: String lastTime = (String) attributeDescriptor
0506: .getFieldValue(DescriptorSupport.LAST_UPDATED_TIMESTAMP);
0507: long lastUpdatedTime = new Date(Long
0508: .parseLong(lastTime)).getTime();
0509: log.trace("Last Updated Time=" + lastUpdatedTime);
0510:
0511: //get the current Time Period
0512: long currTime = new Date().getTime();
0513: log.trace("Current Time=" + currTime);
0514:
0515: if (currTime >= (lastUpdatedTime + persistPeriod)) {
0516: log
0517: .trace("currTime is greater than (lastUpdatedTime+persistPeriod)");
0518: OnUpdate(attributeDescriptor);
0519: }
0520: }
0521: } catch (Exception e) {
0522: log.error(e.getMessage());
0523: throw new MBeanException(e);
0524: }
0525: } //store
0526:
0527: /**
0528: * This method discovers the attributes and operations this MBean
0529: * exposes for management.
0530: *
0531: * @return An instance of MBeanInfo allowing to retrieve all attributes
0532: * and operations of this MBean.
0533: */
0534: public MBeanInfo getMBeanInfo() {
0535: if (mbeanInfo == null) {
0536: log.debug("MBeanInfo is null");
0537: mbeanInfo = createDefaultModelMBeanInfo();
0538: log.debug("Default Model Mbean Created");
0539: }
0540:
0541: return (MBeanInfo) mbeanInfo;
0542: }
0543:
0544: /**
0545: * Gets the value of a specific attribute of this MBean.
0546: *
0547: * @param attribute A String specifying the name of the attribute to be retrieved.
0548: *
0549: * @return The value of the retrieved attribute.
0550: */
0551: public Object getAttribute(String attribute)
0552: throws AttributeNotFoundException, MBeanException,
0553: ReflectionException {
0554: return getAttribute(attribute, true);
0555: }
0556:
0557: /**
0558: * Enables the values of several attributes of this MBean.
0559: *
0560: * @param attributes A list of attributes to be retrieved.
0561: *
0562: * @return The value of the retrieved attributes as attributeList.
0563: */
0564: public AttributeList getAttributes(String[] attributes) {
0565: AttributeList toRet = null;
0566: if (attributes == null)
0567: throw new RuntimeOperationsException(new RuntimeException(
0568: "invalid method param"));
0569:
0570: toRet = new AttributeList();
0571: for (int i = 0; i < attributes.length; i++) {
0572: Attribute attr = null;
0573:
0574: try {
0575: attr = new Attribute(attributes[i],
0576: getAttribute(attributes[i]));
0577: } catch (Exception e) {
0578: }
0579: toRet.add(attr);
0580: }
0581:
0582: return toRet;
0583: }
0584:
0585: /**
0586: * Invokes an action on this MBean.
0587: *
0588: * @param actionName The name of the action to be invoked.
0589: *
0590: * @param params An array containing the parameters to be set when
0591: * the action is invoked
0592: *
0593: * @param signature An array containing the signature of the action.
0594: * The class objects will be loaded using the same
0595: * class loader as the one used for loading the MBean on
0596: * which the action was invoked.
0597: *
0598: * @return The object returned by the action, which represents the result
0599: * of invoking the action on the specified MBean.
0600: *
0601: * @throws MBeanException Wraps an exception thrown by the MBean's invoked method.
0602: *
0603: * @throws ReflectionException Wraps an java.lang.Exception thrown while
0604: * trying to invoke the method.
0605: */
0606: public Object invoke(String actionName, Object[] params,
0607: String[] signature) throws MBeanException,
0608: ReflectionException {
0609: if (signature == null)
0610: signature = new String[0];
0611:
0612: boolean methodFound = true;
0613:
0614: try {
0615: Class[] sigClass = null;
0616: //if(signature != null)
0617: //{
0618: sigClass = new Class[signature.length];
0619: for (int i = 0; i < signature.length; i++) {
0620: //sigClass[i] = Class.forName(signature[i]);
0621: try {
0622: sigClass[i] = Thread.currentThread()
0623: .getContextClassLoader().loadClass(
0624: signature[i]);
0625: } catch (Exception ee) {
0626: sigClass[i] = getProperClass(signature[i]);
0627: if (sigClass[i] == null && params[i] != null) {
0628: sigClass[i] = params[i].getClass()
0629: .getClassLoader().loadClass(
0630: signature[i]);
0631: }
0632: }
0633: //System.out.println(" The class object "+sigClass[i]);
0634: }
0635: //}
0636: if (this .getClass().getMethod(actionName, sigClass) == null) {
0637: methodFound = false;
0638: }
0639: } catch (Exception e) {
0640: methodFound = false;
0641: }
0642:
0643: /*if(methodFound && (actionName.equals("setManagedResource") ||
0644: actionName.equals("setModelMBeanInfo") ||
0645: actionName.equals("load") ||
0646: actionName.equals("store") ||
0647: actionName.equals("addAttributeChangeNotificationListener") ||
0648: actionName.equals("removeAttributeChangeNotificationListener") ||
0649: actionName.equals("sendAttributeChangeNotification") ||
0650: actionName.equals("sendNotification") ||
0651: actionName.equals("addNotificationListener") ||
0652: actionName.equals("getNotificationInfo") ||
0653: actionName.equals("removeNotificationListener")))*/
0654: if (methodFound) {
0655: return invoke(actionName, params, signature, true);
0656: }
0657:
0658: //if(signature != null && signature.length == 1)
0659: if (signature.length == 1) {
0660: ModelMBeanAttributeInfo[] adss = (ModelMBeanAttributeInfo[]) mbeanInfo
0661: .getAttributes();
0662: ModelMBeanAttributeInfo attr = null;
0663: for (int i = 0; i < adss.length; i++) {
0664: attr = adss[i];
0665:
0666: if (("set" + attr.getName()).equals(actionName)
0667: && attr.getType().equals(signature[0])) {
0668: try {
0669: setAttribute(new Attribute(attr.getName(),
0670: params[0]));
0671: } catch (javax.management.AttributeNotFoundException ae) {
0672: throw new MBeanException(ae);
0673: } catch (javax.management.InvalidAttributeValueException ie) {
0674: throw new MBeanException(ie);
0675: }
0676: //throw new MBeanException(new ServiceNotFoundException("Exception during invokation of operation : "+actionName));
0677: return null;
0678: }
0679: }
0680: }
0681:
0682: //here check for operation match with managed resource
0683: log.debug("action name=" + actionName);
0684: /*
0685: ModelMBeanOperationInfo operInfo = this.mbeanInfo.getOperation(actionName);
0686: if(operInfo == null)
0687: throw new MBeanException(new ServiceNotFoundException("Exception during invokation of operation : "+actionName));
0688:
0689: MBeanParameterInfo[] paramsInfo = operInfo.getSignature();
0690: //if(signature == null || (paramsInfo.length != signature.length))
0691: if(paramsInfo.length!=signature.length)
0692: throw new MBeanException(new ServiceNotFoundException("Exception during invocation of operation : "+actionName));
0693:
0694: for(int i=0;i<paramsInfo.length;i++)
0695: {
0696: if(!paramsInfo[i].getType().equals(signature[i]))
0697: throw new MBeanException(new ServiceNotFoundException("Exception during invokation of operation : "+actionName));
0698: }
0699: */
0700:
0701: // To Support Method Overloading - Ramesh
0702: boolean operfound = false;
0703: ModelMBeanOperationInfo operInfo = null;
0704: MBeanOperationInfo[] operInfos = this .mbeanInfo.getOperations();
0705: for (int k = 0; k < operInfos.length; k++) {
0706: operInfo = (ModelMBeanOperationInfo) operInfos[k];
0707: if (!operInfo.getName().equals(actionName)) {
0708: continue;
0709: }
0710: MBeanParameterInfo[] paramsInfo = operInfo.getSignature();
0711: if (paramsInfo.length != signature.length) {
0712: continue;
0713: }
0714: int tmpcount = 0;
0715: for (int i = 0; i < paramsInfo.length; i++) {
0716: if (!paramsInfo[i].getType().equals(signature[i])) {
0717: break;
0718: } else {
0719: tmpcount++;
0720: }
0721: }
0722: if (tmpcount == paramsInfo.length) {
0723: operfound = true;
0724: break;
0725: }
0726: }
0727:
0728: if (!operfound) {
0729: throw new MBeanException(new ServiceNotFoundException(
0730: "Exception during invokation of operation : "
0731: + actionName));
0732: }
0733: // Fix for Method overloading support ends
0734:
0735: Descriptor desc = operInfo.getDescriptor();
0736:
0737: if (desc.getFieldValue(DescriptorSupport.TARGET_OBJECT) != null
0738: && desc.getFieldValue(DescriptorSupport.TARGET_TYPE) != null
0739: && ((String) desc
0740: .getFieldValue(DescriptorSupport.TARGET_TYPE))
0741: .equalsIgnoreCase("objectReference")) {
0742:
0743: return invokeFriend(actionName, params, signature, desc
0744: .getFieldValue(DescriptorSupport.TARGET_OBJECT));
0745: }
0746:
0747: return invoke(actionName, params, signature, false);
0748: }
0749:
0750: /**
0751: * Sets the value of a specific attribute of this MBean.
0752: *
0753: * @param attribute The identification of the attribute to be set and
0754: * the value it is to be set to.
0755: *
0756: * @throws AttributeNotFoundException - The specified attribute is
0757: * not accessible in the MBean.
0758: *
0759: * @throws InvalidAttributeValueException - The specified value for
0760: * the attribute is not valid.
0761: *
0762: * @throws MBeanException - Wraps an exception thrown by the MBean's setter.
0763: *
0764: * @throws ReflectionException - Wraps an java.lang.Exception thrown
0765: * while trying to invoke the setter.
0766: *
0767: * @throws RuntimeOperationsException - Wraps an IllegalArgumentException:
0768: * The object name in parameter is null or the attribute
0769: * in parameter is null.
0770: */
0771: public void setAttribute(Attribute attribute)
0772: throws AttributeNotFoundException,
0773: InvalidAttributeValueException, MBeanException,
0774: ReflectionException {
0775: log.trace("setAttribute() entering");
0776:
0777: ModelMBeanAttributeInfo[] adss = (ModelMBeanAttributeInfo[]) mbeanInfo
0778: .getAttributes();
0779: ModelMBeanOperationInfo[] odss = (ModelMBeanOperationInfo[]) mbeanInfo
0780: .getOperations();
0781:
0782: ModelMBeanAttributeInfo attr = null;
0783: ModelMBeanOperationInfo oper = null;
0784:
0785: Descriptor ads = null;
0786: Descriptor ods = null;
0787:
0788: for (int i = 0; i < adss.length; i++) {
0789: attr = adss[i];
0790: ads = attr.getDescriptor();
0791: if (ads.getFieldValue(DescriptorSupport.NAME).equals(
0792: attribute.getName())) {
0793:
0794: if (ads.getFieldValue(DescriptorSupport.SET_METHOD) != null)
0795: ;
0796: {
0797: for (int j = 0; j < odss.length; j++) {
0798: oper = odss[j];
0799: ods = oper.getDescriptor();
0800: if (((String) ods
0801: .getFieldValue(DescriptorSupport.NAME))
0802: .equals(ads
0803: .getFieldValue(DescriptorSupport.SET_METHOD)))
0804: break;
0805: else {
0806: oper = null;
0807: ods = null;
0808: }
0809: }
0810: }
0811:
0812: break;
0813: } else {
0814: attr = null;
0815: ads = null;
0816: }
0817: }
0818:
0819: Object value = null;
0820: if (ods != null) {
0821: value = attribute.getValue();
0822:
0823: Attribute oldattr = new Attribute(attribute.getName(),
0824: adsValueTable.get(ads));
0825:
0826: Object tempValue = adsValueTable.get(ads);
0827: log.trace("before value");
0828: log.trace("DD" + DescriptorSupport.VALUE);
0829: log.trace("ads=" + ads);
0830:
0831: Object o = ads.getFieldValue(DescriptorSupport.VALUE);//.toString();
0832: String tempValueString = null;
0833: if (o != null)
0834: tempValueString = o.toString();
0835: //String tempValueString = ads.getFieldValue(DescriptorSupport.VALUE).toString();
0836: log.trace("after value");
0837:
0838: adsValueTable.put(ads, value);
0839: ads.setField(DescriptorSupport.VALUE, value.toString());
0840:
0841: Hashtable tempTable = new Hashtable();
0842: boolean isTabularCreate = false;
0843: //Added for sending only changed rows in TabularData and changed columns in the
0844: //CompositeData
0845: if (value instanceof TabularData) {
0846: TabularData data = (TabularData) value;
0847: TabularParameterInfo info = data
0848: .getTabularParameterInfo();
0849: String[] indexNames = info.getIndexNames();
0850:
0851: for (Enumeration e = data.enumerate(); e
0852: .hasMoreElements();) {
0853: Object[] index = (Object[]) e.nextElement();
0854: CompositeData comp = data.getRow(index);
0855: if (comp.getOperationType().equals(
0856: CompositeData.NOCHANGES)) {
0857: tempTable.put(index, data.deleteRow(index));
0858: } else if (comp.getOperationType().equals(
0859: CompositeData.MODIFIED)
0860: || comp.getOperationType().equals(
0861: CompositeData.CREATED)) {
0862: if (comp.getOperationType().equals(
0863: CompositeData.CREATED))
0864: isTabularCreate = true;
0865: for (Enumeration ce = comp.enumerate(); ce
0866: .hasMoreElements();) {
0867: String key = (String) ce.nextElement();
0868: if (!comp.isModified(key)) {
0869: if (isInArray(indexNames, key))
0870: continue;
0871:
0872: Hashtable ttt = (Hashtable) tempTable
0873: .get(index);
0874: if (ttt == null)
0875: ttt = new Hashtable();
0876: ttt.put(key, comp.deleteDataItem(key));
0877: tempTable.put(index, ttt);
0878: }
0879: }
0880: }
0881: }
0882: }//end
0883:
0884: try {
0885: invoke((String) ods
0886: .getFieldValue(DescriptorSupport.NAME),
0887: new Object[] { value }, new String[] { oper
0888: .getSignature()[0].getType() }, false);
0889:
0890: Attribute newattr = new Attribute(attribute.getName(),
0891: value);
0892: sendAttributeChangeNotification(oldattr, newattr);
0893:
0894: } catch (IllegalArgumentException iaex) {
0895: throw new InvalidAttributeValueException(iaex
0896: .getMessage());
0897: } catch (javax.management.MBeanException me) {
0898: //me.printStackTrace();
0899: restoreTabularData(value, tempTable);
0900:
0901: if (tempValue != null)
0902: adsValueTable.put(ads, tempValue);
0903: if (tempValueString != null)
0904: ads.setField(DescriptorSupport.VALUE,
0905: tempValueString);
0906:
0907: throw me;
0908: } catch (javax.management.ReflectionException re) {
0909: //re.printStackTrace();
0910: restoreTabularData(value, tempTable);
0911:
0912: if (re.getTargetException() instanceof java.lang.NoSuchMethodException)
0913: throw new AttributeNotFoundException(attribute
0914: .getName());
0915:
0916: if (tempValue != null)
0917: adsValueTable.put(ads, tempValue);
0918: if (tempValueString != null)
0919: ads.setField(DescriptorSupport.VALUE,
0920: tempValueString);
0921:
0922: throw re;
0923: }
0924:
0925: restoreTabularData(value, tempTable);
0926:
0927: String lutime = null;
0928:
0929: if (enableStorage) {
0930:
0931: currentads = attr;
0932: try {
0933: store();
0934: lutime = (String) ads
0935: .getFieldValue(DescriptorSupport.LAST_UPDATED_TIMESTAMP);
0936: } catch (Exception ex) {
0937: throw new MBeanException(ex);
0938: }
0939: }
0940:
0941: //if(isTabularCreate)
0942: if (value instanceof TabularData) {
0943: String ctl = (String) ads
0944: .getFieldValue(DescriptorSupport.CURRENCY_TIME_LIMIT)
0945: + "000";
0946: try {
0947: long longCtl = Long.parseLong(ctl);
0948: ads.setField(
0949: DescriptorSupport.LAST_UPDATED_TIMESTAMP,
0950: "" + (new Date().getTime() - longCtl - 1));
0951: } catch (Exception e) {
0952: e.printStackTrace();
0953: }
0954: getAttribute(attribute.getName(), false);
0955: if (lutime != null)
0956: ads.setField(
0957: DescriptorSupport.LAST_UPDATED_TIMESTAMP,
0958: "" + lutime);
0959: }
0960:
0961: return;
0962: } else if (ads == null) {
0963: throw new AttributeNotFoundException("Attribute \""
0964: + attribute.getName() + "\" not found");
0965: } else if ((value = ads.getFieldValue(DescriptorSupport.VALUE)) != null) {
0966: //value = adsValueTable.get(ads);
0967:
0968: adsValueTable.put(ads, value);
0969: ads.setField(DescriptorSupport.VALUE, value);
0970: return;
0971: }
0972:
0973: String defaultValue = (String) ads
0974: .getFieldValue(DescriptorSupport.DEFAULT);
0975: if (defaultValue == null)
0976: throw new AttributeNotFoundException(
0977: "Attribute has no default value"
0978: + ads.getFieldValue(DescriptorSupport.NAME));
0979:
0980: ads.setField(DescriptorSupport.DEFAULT, attribute.getValue()
0981: .toString());
0982:
0983: /*
0984: try
0985: {
0986: Class e = object.getClass();
0987:
0988: Class parameterTypes[]=new Class[1];
0989: parameterTypes[0] = attribute.getValue().getClass();
0990:
0991: Method attrMethod = e.getMethod("set"+attribute.getName(),parameterTypes);
0992:
0993: attrMethod.invoke(object, new Object[]{attribute.getValue()} );
0994: }
0995: catch(Exception e)
0996: {
0997: e.printStackTrace();
0998: }
0999: */
1000: }
1001:
1002: /**
1003: * Sets the values of several attributes of this MBean.
1004: *
1005: * @param attributes A list of attributes: The identification of the
1006: * attributes to be set and the values they are to be set to.
1007: *
1008: * @return The list of attributes that were set, with their new values.
1009: */
1010: public AttributeList setAttributes(AttributeList attributes) {
1011: log.trace("setAttributes");
1012: if (attributes == null)
1013: return null;
1014:
1015: Object[] array = attributes.toArray();
1016:
1017: if (array == null)
1018: return attributes;
1019:
1020: for (int i = 0; i < array.length; i++) {
1021: Attribute attr = null;
1022: try {
1023: attr = (Attribute) array[i];
1024: } catch (ClassCastException ce) {
1025: continue;
1026: }
1027:
1028: try {
1029: setAttribute(attr);
1030: } catch (Exception e) {
1031: }
1032: }
1033:
1034: return attributes;
1035: }
1036:
1037: /**
1038: * Enables a couple (listener,handback) for a registered MBean to be added.
1039: *
1040: * Specified by :
1041: * addNotificationListener in interface NotificationBroadcaster
1042: *
1043: * @param listener The listener object which will handles notifications
1044: * emitted by the registered MBean.
1045: *
1046: * @param filter The filter object. If not specified, no filtering will be
1047: * performed before handling notifications.
1048: *
1049: * @param handback The context to be sent to the listener when a
1050: * notification is emitted.
1051: *
1052: * @throws java.lang.IllegalArgumentException Listener parameter is null.
1053: */
1054: public void addNotificationListener(NotificationListener listener,
1055: NotificationFilter filter, Object handback)
1056: throws IllegalArgumentException
1057:
1058: {
1059: log.trace("add Notification Listener");
1060:
1061: if (listener == null)
1062: throw new IllegalArgumentException("Notif Listener is null");
1063:
1064: if (notifbroadcaster == null) {
1065: notifbroadcaster = new NotificationBroadcasterSupport();
1066: }
1067:
1068: notifbroadcaster.addNotificationListener(listener, filter,
1069: handback);
1070: }
1071:
1072: /**
1073: * Enables a listener for an MBean to be removed. All couple
1074: * (listener, handback) are removed.
1075: *
1076: * Specified by:
1077: * removeNotificationListener in interface NotificationBroadcaster
1078: *
1079: * @param listener The listener object which will handles notifications
1080: * emitted by the registered MBean.
1081: *
1082: * @throws ListenerNotFoundException The listener is not registered in the MBean.
1083: */
1084: public void removeNotificationListener(NotificationListener listener)
1085: throws ListenerNotFoundException {
1086: log.trace("remove Notification Listener");
1087:
1088: if (listener == null || notifbroadcaster == null)
1089: throw new ListenerNotFoundException(
1090: "Notif Listener is null");
1091:
1092: notifbroadcaster.removeNotificationListener(listener);
1093: }
1094:
1095: /**
1096: * Returns a NotificationInfo object containing the name of the Java class
1097: * of the notification and the notification types sent.
1098: * This getNotificationInfo method will inturn call the original
1099: * instrumentation object's getNotificationInfo method if the object is
1100: * an instanceof NotificationBroadcaster.
1101: */
1102: public MBeanNotificationInfo[] getNotificationInfo() {
1103: MBeanNotificationInfo[] notifications = mbeanInfo
1104: .getNotifications();
1105: ModelMBeanNotificationInfo[] toRet = null;
1106:
1107: if (notifications == null)
1108: toRet = new ModelMBeanNotificationInfo[2];
1109: else
1110: toRet = new ModelMBeanNotificationInfo[notifications.length + 2];
1111:
1112: Descriptor d1 = new DescriptorSupport(notiffields2);
1113: Descriptor d2 = new DescriptorSupport(notiffields2);
1114:
1115: Descriptor descr = null;
1116: try {
1117: descr = mbeanInfo.getMBeanDescriptor();
1118: } catch (Exception e) {
1119: }
1120:
1121: if (d1.getFieldValue(DescriptorSupport.LOG_FILE) == null
1122: && descr.getFieldValue(DescriptorSupport.LOG_FILE) != null)
1123: d1.setField(DescriptorSupport.LOG_FILE, descr
1124: .getFieldValue(DescriptorSupport.LOG_FILE));
1125:
1126: if (d2.getFieldValue(DescriptorSupport.LOG_FILE) == null
1127: && descr.getFieldValue(DescriptorSupport.LOG_FILE) != null)
1128: d2.setField(DescriptorSupport.LOG_FILE, descr
1129: .getFieldValue(DescriptorSupport.LOG_FILE));
1130:
1131: if (descr.getFieldValue(DescriptorSupport.LOG) != null)
1132: d1.setField(DescriptorSupport.LOG, descr
1133: .getFieldValue(DescriptorSupport.LOG));
1134:
1135: if (descr.getFieldValue(DescriptorSupport.LOG) != null)
1136: d2.setField(DescriptorSupport.LOG, descr
1137: .getFieldValue(DescriptorSupport.LOG));
1138:
1139: toRet[0] = new ModelMBeanNotificationInfo(
1140: new String[] { "jmx.modelmbean.general" }, "General",
1141: "general notification", d1);
1142:
1143: toRet[1] = new ModelMBeanNotificationInfo(
1144: new String[] { "jmx.attribute.change" },
1145: "AttributeChange", "attribute change notification", d2);
1146:
1147: if (notifications != null) {
1148: for (int i = 0, j = 2; i < notifications.length; i++, j++) {
1149: toRet[j] = (ModelMBeanNotificationInfo) (notifications[i]);
1150: }
1151: }
1152:
1153: return toRet;
1154:
1155: /*
1156: if( !(object instanceof NotificationBroadcaster))
1157: return null;
1158:
1159: return ((NotificationBroadcaster)object).getNotificationInfo();
1160: */
1161: }
1162:
1163: /**
1164: * Register3s an object which implements the NotificationListener interface
1165: * as a listener for AttributeChangeNotifications. This object's
1166: * 'handleNotification()' method will be invoked when any
1167: * attributeChangeNotification is issued through or by the MBean. This
1168: * does not include other Notifications. They must be registered for
1169: * independently. An AttributeChangeNotification will be generated for this
1170: * attributeName.
1171: *
1172: * @param inlistener The listener object which will handles notifications
1173: * emitted by the registered MBean.
1174: *
1175: * @param inAttributeName The name of the MBean attribute for which to
1176: * receive change notifications. If null, then all attribute
1177: * changes will cause an attributeChangeNotification to be issued.
1178: *
1179: * @param inhandback The context to be sent to the listener with the
1180: * notification when a notification is emitted.
1181: *
1182: * @throws MBeanException Wraps an exception thrown by this method
1183: *
1184: * @throws RuntimeOperationsException To wrap the Run time Exceptions
1185: *
1186: * @throws IllegalArgumentException Listener is null or attributeName is null.
1187: */
1188: public void addAttributeChangeNotificationListener(
1189: NotificationListener inlistener, String inAttributeName,
1190: Object inhandback) throws MBeanException,
1191: RuntimeOperationsException, IllegalArgumentException {
1192: log.trace("addAttributeChangeNotificationListener");
1193:
1194: if (inlistener == null)
1195: throw new IllegalArgumentException(
1196: "Attr Notif Listener is null");
1197:
1198: if (attrbroadcaster == null)
1199: attrbroadcaster = new NotificationBroadcasterSupport();
1200:
1201: AttributeChangeNotificationFilter infilter = (AttributeChangeNotificationFilter) attrFilterMapTable
1202: .get(inlistener);
1203: if (infilter == null) {
1204: infilter = new AttributeChangeNotificationFilter();
1205: attrFilterMapTable.put(inlistener, infilter);
1206: }
1207: infilter.enableAttribute(inAttributeName);
1208:
1209: attrbroadcaster.addNotificationListener(inlistener, infilter,
1210: inhandback);
1211: }
1212:
1213: /**
1214: * Removes a listener for attributeChangeNotifications from the MBean.
1215: *
1216: * @param inlistener The listener name which was handling notifications
1217: * emitted by the registered MBean. This method will remove
1218: * all information related to this listener.
1219: *
1220: * @param inAttributeName The attribute for which the listener no longer
1221: * wants to receive attributeChangeNotifications.
1222: *
1223: * @throws MBeanException Wraps an exception thrown while
1224: * removing AttributeChangeNotificationListener
1225: *
1226: * @throws RuntimeOperationsException Wraps the exceptions thrown in the
1227: * run time
1228: *
1229: * @exception ListenerNotFoundException The couple (listener,handback) is
1230: * not registered in the MBean. The exception message contains
1231: * either "listener", "handback" or the object name
1232: * depending on which object cannot be found.
1233: */
1234: public void removeAttributeChangeNotificationListener(
1235: NotificationListener inlistener, String inAttributeName)
1236: throws MBeanException, RuntimeOperationsException,
1237: ListenerNotFoundException {
1238: log.trace("removeAttributeChangeNotificationListener");
1239:
1240: if (inlistener == null || notifbroadcaster == null)
1241: throw new ListenerNotFoundException(
1242: "Notif Listener is null");
1243:
1244: AttributeChangeNotificationFilter infilter = (AttributeChangeNotificationFilter) attrFilterMapTable
1245: .get(inlistener);
1246: infilter.disableAttribute(inAttributeName);
1247:
1248: //attrbroadcaster.removeNotificationListener(inlistener);
1249: }
1250:
1251: /**
1252: * Sends a Notification which is passed in to the registered Notification
1253: * listeners on the ModelMBean as a jmx.modelmbean.general notification.
1254: *
1255: * @param ntfyObj The notification which is to be passed to the
1256: * 'handleNotification' method of the listener object.
1257: *
1258: * @exception MBeanException The initializer of the object has thrown an exception.
1259: *
1260: * @exception RuntimeOperationsException Wraps an IllegalArgumentException:
1261: * The Notification object passed in parameter is null or invalid.
1262: */
1263: public void sendNotification(Notification ntfyObj)
1264: throws MBeanException, RuntimeOperationsException {
1265: log.trace("Sending Notification [" + ntfyObj + "]");
1266:
1267: if (ntfyObj == null)
1268: throw new RuntimeOperationsException(
1269: new IllegalArgumentException("notif object is null"));
1270:
1271: if (notifbroadcaster == null) {
1272: log
1273: .trace("Notification broadcaster is NULL and hence not sending notification ["
1274: + ntfyObj + "]");
1275: return;
1276: }
1277:
1278: MBeanNotificationInfo notifinfo = null;
1279: if ((notifinfo = getNotifInfo(ntfyObj)) != null) {
1280: //throw new RuntimeOperationsException(new IllegalArgumentException(
1281: // "Notification undefined in the model mbean")); */
1282: logTheNotification(ntfyObj, notifinfo);
1283: }
1284:
1285: log.trace("$$$ Before sending [" + ntfyObj
1286: + "] using notification broadcaster");
1287: notifbroadcaster.sendNotification(ntfyObj);
1288: log.trace("$$$$ AFTER sent [" + ntfyObj
1289: + "] using notification broadcaster");
1290: }
1291:
1292: /**
1293: * Sends a Notification which contains the text string that is passed in
1294: * to the registered Notification listeners on the ModelMBean.
1295: *
1296: * @param ntfyText The text which is to be passed in the Notification to the
1297: * 'handleNotification' method of the listener object.
1298: * the constructed Notification will be:
1299: * type "jmx.modelmbean.general"
1300: * source this ModelMBean instance
1301: * sequence 1
1302: *
1303: * @exception MBeanException The initializer of the object has thrown an exception.
1304: *
1305: * @exception RuntimeOperationsException Wraps an IllegalArgumentException:
1306: * The Notification text string passed in parameter is null or invalid.
1307: */
1308: public void sendNotification(String ntfyText)
1309: throws MBeanException, RuntimeOperationsException {
1310: //to do :: log the notification sent.
1311: if (ntfyText == null) {
1312: throw new RuntimeOperationsException(
1313: new IllegalArgumentException(
1314: "Notification message should not be empty or null"),
1315: "Exception occured trying to send a text notification from a ModelMBean");
1316: }
1317:
1318: Notification ntfyObj = new Notification(
1319: "jmx.modelmbean.general", this , 1,
1320: new Date().getTime(), ntfyText);
1321: log.trace("$$$$ Notification is " + ntfyObj);
1322: sendNotification(ntfyObj);
1323: log.trace("$$$ Sent Notification is " + ntfyObj);
1324: }
1325:
1326: /**
1327: * Sends an attributeChangeNotification which is passed in to the registered
1328: * attributeChangeNotification listeners on the ModelMBean.
1329: *
1330: * @param ntfyObj The notification which is to be passed to the
1331: * 'handleNotification' method of the listener object.
1332: *
1333: * @exception MBeanException The initializer of the object has thrown an exception.
1334: *
1335: * @exception RuntimeOperationsException Wraps an IllegalArgumentException:
1336: * The Notification object passed in parameter is null or invalid.
1337: */
1338: public void sendAttributeChangeNotification(
1339: AttributeChangeNotification ntfyObj) throws MBeanException,
1340: RuntimeOperationsException {
1341: //to do :: log the notification sent.
1342:
1343: if (ntfyObj == null)
1344: throw new RuntimeOperationsException(
1345: new IllegalArgumentException("notif object is null"));
1346:
1347: if (attrbroadcaster == null)
1348: return;
1349:
1350: MBeanNotificationInfo notifinfo = null;
1351: if ((notifinfo = getNotifInfo(ntfyObj)) == null)
1352: throw new RuntimeOperationsException(
1353: new IllegalArgumentException(
1354: "Notification undefined in the model mbean"));
1355: logTheNotification(ntfyObj, notifinfo);
1356:
1357: attrbroadcaster.sendNotification(ntfyObj);
1358: }
1359:
1360: /*
1361: * To get the number of threads that this scheduler controls. Default value is 4
1362: * @return Maximum number of threads controlled by this scheduler
1363: */
1364: /*
1365: public static int getMaxThreads()
1366: {
1367: return MAX_THREADS ;
1368: }
1369: */
1370:
1371: /*
1372: * To set the number of threads that this scheduler controls
1373: * @param maxThreads The number of threads that this scheduler controls
1374: */
1375: /*
1376: public static void setMaxThreads(int maxThreads)
1377: {
1378: MAX_THREADS = maxThreads;
1379: }
1380: */
1381:
1382: /**
1383: * Sends an attributeChangeNotification which contains the old value and
1384: * new value for the attribute to the registered
1385: * AttributeChangeNotification listeners on the ModelMBean.
1386: *
1387: * @param inOldVal The origional value for the Attribute
1388: *
1389: * @param inNewVal The current value for the Attribute
1390: * <p>
1391: * <p>
1392: * <PRE>
1393: * The constructed attributeChangeNotification will be:
1394: * type "jmx.attribute.change"
1395: * source this ModelMBean instance
1396: * sequence 1
1397: * attributeName oldValue.getName()
1398: * attributeType oldValue's class
1399: * attributeOldValue oldValue.getValue()
1400: * attributeNewValue newValue.getValue()
1401: * </PRE>
1402: * <p>
1403: *
1404: * @exception MBeanException to wrapper implementation exceptions
1405: *
1406: * @exception RuntimeOperationsException to wrapper IllegalArgumentExceptions.
1407: */
1408: public void sendAttributeChangeNotification(Attribute inOldVal,
1409: Attribute inNewVal) throws MBeanException,
1410: RuntimeOperationsException {
1411: //to do :: log the notification sent.
1412:
1413: if (inOldVal == null || inNewVal == null)
1414: throw new RuntimeOperationsException(
1415: new IllegalArgumentException("param is null"));
1416:
1417: String attrName = inOldVal.getName();
1418: String attrType = null;
1419:
1420: if (inOldVal.getValue() != null)
1421: attrType = inOldVal.getValue().getClass().getName();
1422:
1423: AttributeChangeNotification ntfyObj = new AttributeChangeNotification(
1424: this , 1, new Date().getTime(), "Attribute changed...",
1425: attrName, attrType, inOldVal.getValue(), inNewVal
1426: .getValue());
1427:
1428: sendAttributeChangeNotification(ntfyObj);
1429: }
1430:
1431: //--------------------------- Inner class -----------------------------//
1432:
1433: // Call Back
1434: class UpdateTimer implements Runnable {
1435: RequiredModelMBean rmm = null;
1436: Descriptor attributeDescriptor = null;
1437: long when = 0l;
1438: long persistPeriod = 0l;
1439:
1440: UpdateTimer(RequiredModelMBean rmm,
1441: Descriptor attributeDescriptor, long when,
1442: long persistPeriod) {
1443: this .rmm = rmm;
1444: this .attributeDescriptor = attributeDescriptor;
1445: this .when = when;
1446: this .persistPeriod = persistPeriod;
1447: }
1448:
1449: public void run() {
1450: log.trace(Thread.currentThread().getName());
1451:
1452: try {
1453: rmm.OnUpdate(attributeDescriptor);
1454:
1455: scheduler.scheduleTask(new UpdateTimer(rmm,
1456: attributeDescriptor, new Date(when
1457: + persistPeriod).getTime(),
1458: persistPeriod), new Date(when + persistPeriod));
1459: log.info("SCHEDULER scheduled Task at"
1460: + new Date(when + persistPeriod));
1461: } catch (Exception e) {
1462: log.error(e.getMessage());
1463: }
1464: }
1465: }
1466:
1467: //--------------------------- Package methods ---------------------------//
1468:
1469: /**
1470: * Gets the value of a specific attribute of this MBean.
1471: *
1472: * @param attribute A String specifying the name of the attribute to be retrieved.
1473: *
1474: * @return The value of the retrieved attribute.
1475: */
1476: Object getAttribute(String attribute, boolean isFromServer)
1477: throws AttributeNotFoundException, MBeanException,
1478: ReflectionException {
1479: log.trace("getAttribute called in RMB ******");
1480:
1481: ModelMBeanAttributeInfo[] adss = (ModelMBeanAttributeInfo[]) mbeanInfo
1482: .getAttributes();
1483: ModelMBeanOperationInfo[] odss = (ModelMBeanOperationInfo[]) mbeanInfo
1484: .getOperations();
1485:
1486: ModelMBeanAttributeInfo attr = null;
1487: ModelMBeanOperationInfo oper = null;
1488:
1489: Descriptor ads = null;
1490: Descriptor ods = null;
1491:
1492: for (int i = 0; i < adss.length; i++) {
1493: attr = adss[i];
1494: ads = attr.getDescriptor();
1495: if (((String) ads.getFieldValue(DescriptorSupport.NAME))
1496: .equalsIgnoreCase(attribute)) {
1497: if (ads.getFieldValue(DescriptorSupport.GET_METHOD) != null)
1498: ;
1499: {
1500: for (int j = 0; j < odss.length; j++) {
1501: oper = odss[j];
1502: ods = oper.getDescriptor();
1503: if (((String) ods
1504: .getFieldValue(DescriptorSupport.NAME))
1505: .equalsIgnoreCase((String) ads
1506: .getFieldValue(DescriptorSupport.GET_METHOD)))
1507: break;
1508: else {
1509: oper = null;
1510: ods = null;
1511: }
1512: }
1513: }
1514:
1515: break;
1516: } else {
1517: attr = null;
1518: ads = null;
1519: }
1520: }
1521:
1522: if (ads == null)
1523: throw new AttributeNotFoundException(attribute);
1524:
1525: Object value = null;
1526:
1527: try {
1528: if (ods != null) {
1529: String ctl = (String) ads
1530: .getFieldValue(DescriptorSupport.CURRENCY_TIME_LIMIT)
1531: + "000";
1532: String lut = (String) ads
1533: .getFieldValue(DescriptorSupport.LAST_UPDATED_TIMESTAMP);
1534:
1535: long ctlValue = -1;
1536: if (ctl != null)
1537: ctlValue = Long.parseLong(ctl);
1538:
1539: long lutValue = -1;
1540: try {
1541: if (lut != null)
1542: lutValue = Long.parseLong(lut);
1543: } catch (NumberFormatException nfe) {
1544: //added by hyther
1545: //just throw the nfe
1546: //throw nfe;
1547: //nfe.printStackTrace();
1548: log.error("NumberFormatException:", nfe);
1549: }
1550:
1551: if ((ctlValue > 0 || ctlValue == -1) && lutValue != -1) {
1552: long currTime = new Date().getTime();
1553: log.debug("currTime = " + currTime);
1554:
1555: if ((currTime < (lutValue + ctlValue))
1556: || ctlValue == -1) {
1557: value = adsValueTable.get(ads);
1558: String strVal = null;
1559: if (value != null)
1560: strVal = value.toString();
1561:
1562: try {
1563: if (strVal != null)
1564: value = returnDefaultValue(value, attr
1565: .getType());
1566: } catch (Exception ex) {
1567: throw new MBeanException(ex);
1568: }
1569:
1570: if (value == null
1571: || (attr.getType().startsWith("[L") && attr
1572: .getType().endsWith(";"))) {
1573: value = invoke(
1574: (String) ods
1575: .getFieldValue(DescriptorSupport.NAME),
1576: new Object[0], null, false);
1577: if (value == null)
1578: throw new MBeanException(new Exception(
1579: "value is null"));
1580:
1581: ads.setField(DescriptorSupport.VALUE, value
1582: .toString());
1583: adsValueTable.put(ads, value);
1584: log.debug("# Value = " + value.toString());
1585: }
1586: //ads.setField("lastUpdatedTimeStamp", ""+ (new Date().getTime()));
1587: return value;
1588: }
1589: }
1590:
1591: value = invoke((String) ods
1592: .getFieldValue(DescriptorSupport.NAME),
1593: new Object[0], null, false);
1594: if (value == null) {
1595: return null;
1596: }
1597: adsValueTable.put(ads, value);
1598: ads.setField(DescriptorSupport.VALUE, value.toString());
1599: if (isFromServer)
1600: ads.setField(
1601: DescriptorSupport.LAST_UPDATED_TIMESTAMP,
1602: "" + (new Date().getTime()));
1603: return value;
1604: } else if ((value = ads
1605: .getFieldValue(DescriptorSupport.VALUE)) != null) {
1606: adsValueTable.put(ads, value);
1607: //value = adsValueTable.get(ads);
1608: String strVal = value.toString();
1609: //String strVal = ads.getField("value");
1610:
1611: try {
1612: //ads.setField("lastUpdatedTimeStamp", ""+ (new Date().getTime()));
1613: return returnDefaultValue(value, attr.getType());
1614: } catch (Exception ex) {
1615: throw new MBeanException(ex);
1616: }
1617: }
1618:
1619: } catch (NullPointerException npe) {
1620: log.debug("Attribute = " + attribute);
1621: log.debug("Object = " + object.getClass().getName());
1622: throw npe;
1623: }
1624:
1625: String defaultValue = (String) ads
1626: .getFieldValue(DescriptorSupport.DEFAULT);
1627:
1628: if (defaultValue == null)
1629: throw new AttributeNotFoundException(
1630: "Attribute has no default value");
1631:
1632: try {
1633: //ads.setField(DescriptorSupport.LAST_UPDATED_TIMESTAMP, ""+ (new Date().getTime()));
1634: return returnDefaultValue(defaultValue, attr.getType());
1635: } catch (Exception e) {
1636: throw new MBeanException(e);
1637: }
1638: }
1639:
1640: //--------------------------- Private methods ---------------------------//
1641:
1642: /**
1643: * This allows all the Model MBeans storage control.
1644: *
1645: * @param modelMBeansStorage The Model MBeans storage control
1646: */
1647: private static void setModelMBeansStorage(boolean modelMBeansStorage) {
1648: RequiredModelMBean.modelMBeansStorage = modelMBeansStorage;
1649: }
1650:
1651: /**
1652: * This allows all the Model MBeans caching control.
1653: *
1654: * @param modelMBeansCaching The Model MBeans caching control
1655: */
1656: private static void setModelMBeansCaching(boolean modelMBeansCaching) {
1657: RequiredModelMBean.modelMBeansCaching = modelMBeansCaching;
1658: }
1659:
1660: private void initializeAttributeDescriptorsField(
1661: ModelMBeanAttributeInfo[] attrs) {
1662: for (int i = 0; i < attrs.length; i++) {
1663: Descriptor ads = attrs[i].getDescriptor();
1664:
1665: String perStr = (String) ads
1666: .getFieldValue(DescriptorSupport.PERSIST_PERIOD);
1667:
1668: if (perStr == null)
1669: ads.setField(DescriptorSupport.LAST_UPDATED_TIMESTAMP,
1670: "" + (new Date().getTime()));
1671: else {
1672: long persistPeriod = Long.parseLong(perStr);
1673: ads
1674: .setField(
1675: DescriptorSupport.LAST_UPDATED_TIMESTAMP,
1676: ""
1677: + (new Date().getTime()
1678: - persistPeriod - 1));
1679: }
1680:
1681: if (!modelMBeansCaching)
1682: ads.setField(DescriptorSupport.CURRENCY_TIME_LIMIT,
1683: "" + 0);
1684: }
1685: }
1686:
1687: private synchronized void OnUpdate(Descriptor attributeDescriptor)
1688: throws Exception {
1689: //check if its for the first time
1690: boolean alreadyPersisted = findPersistent();
1691:
1692: //if persistent data already present
1693: if (alreadyPersisted) {
1694: log.trace("//readDataIntoCache");
1695: readDataIntoCache();
1696:
1697: log.trace("//removeUpdatedValueFromCache");
1698: updateCache(attributeDescriptor);
1699:
1700: log.trace("//write Data Back into file");
1701: writeBackIntoFile(attributeDescriptor);
1702: } else {
1703: //else
1704: log.trace("//write Data into file");
1705: writeIntoFile(attributeDescriptor);
1706: }
1707: }//OnUpdate
1708:
1709: private boolean findPersistent() {
1710: FileInputStream f = null;
1711:
1712: try {
1713: f = new FileInputStream(persistLocation + File.separator
1714: + persistName);
1715: return true;
1716: } catch (Exception e) {
1717: return false;
1718: } finally {
1719: try {
1720: if (f != null)
1721: f.close();
1722: } catch (Exception ee) {
1723: log.error("Couldnt Close the File", ee);
1724: }
1725: }
1726: }
1727:
1728: //readDataIntoCache
1729: private void readDataIntoCache() throws Exception {
1730: try {
1731: InputStream istr = new FileInputStream(persistLocation
1732: + File.separator + persistName);
1733: ObjectInputStream data = new ObjectInputStream(istr);
1734:
1735: //chumma read the mbean info
1736: log.trace("Read MBeanInfo");
1737: data.readObject();
1738:
1739: nameVec = new Vector();
1740: valueVec = new Vector();
1741:
1742: while (true) {
1743: try {
1744: String s = (String) data.readObject();
1745: log.debug("add " + s + "to Name Vector");
1746: nameVec.addElement(s);
1747: Object o = data.readObject();
1748: log.debug("add " + o + " to Value Vector");
1749: valueVec.addElement(o);
1750:
1751: } catch (Exception e1) {
1752: break;
1753: }
1754: }//read from old file over..
1755:
1756: log.debug("read from file over");
1757: data.close();
1758: istr.close();
1759: } catch (Exception e) {
1760: log.error(e.getMessage(), e);
1761: throw e;
1762: }
1763: }
1764:
1765: //update Cache
1766: private void updateCache(Descriptor attributeDescriptor)
1767: throws Exception {
1768: try {
1769: String name = (String) attributeDescriptor
1770: .getFieldValue(DescriptorSupport.NAME);
1771: Object value = getAttribute(name, false);
1772:
1773: log.debug("Size=" + nameVec.size() + ":" + valueVec.size());
1774: if (nameVec != null) {
1775: for (int j = 0; j < nameVec.size(); j++) {
1776: if (((String) nameVec.elementAt(j)).equals(name)) {
1777: log.debug("Gonna Remove" + valueVec.get(j));
1778: nameVec.remove(j);
1779: valueVec.remove(j);
1780: break;
1781: }
1782: }
1783:
1784: log.debug("adding " + name + " and " + value
1785: + "to Vector");
1786: nameVec.addElement(name);
1787: valueVec.addElement(value);
1788: log.debug("Size=" + nameVec.size() + ":"
1789: + valueVec.size());
1790: }
1791: } catch (Exception e) {
1792: log.error(e.getMessage(), e);
1793: throw e;
1794: }
1795: }
1796:
1797: //write back to file
1798: private void writeBackIntoFile(Descriptor attributeDescriptor)
1799: throws Exception {
1800: try {
1801: OutputStream ostr = new FileOutputStream(persistLocation
1802: + File.separator + persistName);
1803: ObjectOutputStream data = new ObjectOutputStream(ostr);
1804:
1805: data.writeObject(mbeanInfo);
1806:
1807: if (nameVec != null) {
1808: for (int i = 0; i < nameVec.size(); i++) {
1809: log.debug("writeObject " + nameVec.elementAt(i)
1810: + "into file");
1811: data.writeObject(nameVec.elementAt(i));
1812: log.debug("writeObject " + valueVec.elementAt(i)
1813: + "into file");
1814: data.writeObject(valueVec.elementAt(i));
1815: }
1816: }
1817:
1818: data.close();
1819: ostr.close();
1820: log.debug("SET LAST UPDATED TIME");
1821: attributeDescriptor.setField(
1822: DescriptorSupport.LAST_UPDATED_TIMESTAMP, ""
1823: + (new Date().getTime()));
1824: } catch (Exception e) {
1825: log.error(e.getMessage(), e);
1826: throw e;
1827: }
1828: }
1829:
1830: private void writeIntoFile(Descriptor attributeDescriptor)
1831: throws Exception {
1832: try {
1833: String name = (String) attributeDescriptor
1834: .getFieldValue(DescriptorSupport.NAME);
1835: Object value = getAttribute(name, false);
1836:
1837: if (persistLocation != null) {
1838: File dir = new File(persistLocation);
1839: if (!dir.exists()) {
1840: dir.mkdir();
1841: }
1842:
1843: FileOutputStream out = new FileOutputStream(dir
1844: + File.separator + persistName);
1845: ObjectOutputStream data = new ObjectOutputStream(out);
1846:
1847: data.writeObject(mbeanInfo);
1848: data.writeObject(name);
1849: data.writeObject(value);
1850:
1851: data.close();
1852: out.close();
1853: }
1854: //Set Last Updated and Last Persisted Time
1855:
1856: attributeDescriptor.setField(
1857: DescriptorSupport.LAST_UPDATED_TIMESTAMP, ""
1858: + (new Date().getTime()));
1859: //attributeDescriptor.setField(DescriptorSupport.LAST_PERSISTED_TIMESTAMP,""+(new Date().getTime()));
1860: } catch (Exception e) {
1861: log.error(e.getMessage(), e);
1862: throw e;
1863: }
1864: }
1865:
1866: private ModelMBeanInfo createDefaultModelMBeanInfo() {
1867: Constructor[] cons = this .getClass().getConstructors();
1868: ModelMBeanConstructorInfo[] cinfo = new ModelMBeanConstructorInfo[cons.length];
1869:
1870: for (int i = 0; i < cons.length; i++) {
1871: cinfo[i] = new ModelMBeanConstructorInfo("Constructors",
1872: cons[i]);
1873: }
1874:
1875: return (new ModelMBeanInfoSupport((this .getClass().getName()),
1876: "Default ModelMBean", null, cinfo, null, null));
1877: }
1878:
1879: /**
1880: * returns the object of type as per given in the parameter string for the
1881: * given value.
1882: */
1883: private Object returnDefaultValue(Object objvalue, String type)
1884: throws Exception {
1885: if (type.indexOf("TabularData") != -1)
1886: return objvalue;
1887:
1888: String value = objvalue.toString();
1889: if (type.indexOf("String") != -1) {
1890: if (type.startsWith("[L") && type.endsWith(";"))
1891: return (Object) (new String[] { value });
1892: else
1893: return value;
1894: }
1895:
1896: if (type.indexOf("Integer") != -1) {
1897: if (type.startsWith("[L") && type.endsWith(";"))
1898: return (Object) (new Integer[] { (new Integer(value)) });
1899: else
1900: return new Integer(value);
1901: }
1902: if (type.indexOf("Long") != -1) {
1903: if (type.startsWith("[L") && type.endsWith(";"))
1904: return (Object) (new Long[] { (new Long(value)) });
1905: else
1906: return new Long(value);
1907: }
1908: if (type.indexOf("Boolean") != -1) {
1909: if (type.startsWith("[L") && type.endsWith(";"))
1910: return (Object) (new Boolean[] { (new Boolean(value)) });
1911: else
1912: return new Boolean(value);
1913: }
1914: if (type.indexOf("Float") != -1) {
1915: if (type.startsWith("[L") && type.endsWith(";"))
1916: return (Object) (new Float[] { (new Float(value)) });
1917: else
1918: return new Float(value);
1919: }
1920: if (type.indexOf("Double") != -1) {
1921: if (type.startsWith("[L") && type.endsWith(";"))
1922: return (Object) (new Double[] { (new Double(value)) });
1923: else
1924: return new Double(value);
1925: }
1926: if (type.indexOf("Byte") != -1) {
1927: if (type.startsWith("[L") && type.endsWith(";"))
1928: return (Object) (new Byte[] { (new Byte(value)) });
1929: else
1930: return new Byte(value);
1931: }
1932: if (type.indexOf("Character") != -1) {
1933: if (type.startsWith("[L") && type.endsWith(";"))
1934: return (Object) (new Character[] { (new Character(value
1935: .charAt(0))) });
1936: else
1937: return new Character(value.charAt(0));
1938: }
1939: if (type.indexOf("Short") != -1) {
1940: if (type.startsWith("[L") && type.endsWith(";"))
1941: return (Object) (new Short[] { (new Short(value)) });
1942: else
1943: return new Short(value);
1944: }
1945: //if(type.indexOf("TabularData"))
1946: // return new TabularData(value);
1947: //if(type.indexOf("CompositeData"))
1948: // return new CompositeData(value);
1949:
1950: //throw new Exception("Composite/Tabular Data cannot be formed");
1951: return null;
1952: }
1953:
1954: private Object invokeFriend(String actionName, Object[] params,
1955: String[] signature, Object source) throws MBeanException,
1956: ReflectionException {
1957: try {
1958:
1959: Class e = source.getClass();
1960:
1961: if (signature == null)
1962: signature = new String[0];
1963:
1964: Class parameterTypes[] = new Class[signature.length];
1965: for (int i = 0; i < signature.length; i++)
1966: //parameterTypes[i] = Class.forName(signature[i]);
1967: parameterTypes[i] = Thread.currentThread()
1968: .getContextClassLoader()
1969: .loadClass(signature[i]);
1970:
1971: Method attrMethod = e.getMethod(actionName, parameterTypes);
1972:
1973: return attrMethod.invoke(source, params);
1974:
1975: } catch (Exception e) {
1976: if (e instanceof InvocationTargetException) {
1977: Exception e1 = (Exception) ((InvocationTargetException) e)
1978: .getTargetException();
1979: throw new ReflectionException(e1);
1980: }
1981:
1982: throw new ReflectionException(e);
1983: }
1984: }
1985:
1986: private Object invoke(String actionName, Object[] params,
1987: String[] signature, boolean flag)
1988: //throws Exception
1989: throws javax.management.MBeanException,
1990: javax.management.ReflectionException {
1991: try {
1992: Object source = null;
1993:
1994: if (flag)
1995: source = this ;
1996: else
1997: source = object;
1998:
1999: Class e = source.getClass();
2000:
2001: if (signature == null)
2002: // throw new MBeanException(new ServiceNotFoundException("Could Not Invoke the operation"+actionName));
2003: signature = new String[0];
2004:
2005: Class parameterTypes[] = new Class[signature.length];
2006: for (int i = 0; i < signature.length; i++) {
2007: //parameterTypes[i] = params[i].getClass();
2008: try {
2009: parameterTypes[i] = Thread.currentThread()
2010: .getContextClassLoader().loadClass(
2011: signature[i]);
2012: } catch (Exception ee) {
2013: parameterTypes[i] = getProperClass(signature[i]);
2014: if (parameterTypes[i] == null && params[i] != null) {
2015: parameterTypes[i] = params[i].getClass()
2016: .getClassLoader().loadClass(
2017: signature[i]);
2018: }
2019: }
2020:
2021: }
2022:
2023: Method attrMethod = e.getMethod(actionName, parameterTypes);
2024: return attrMethod.invoke(source, params);
2025: } catch (Exception e) {
2026: //e.printStackTrace();
2027: if (e instanceof InvocationTargetException) {
2028: Exception e1 = (Exception) ((InvocationTargetException) e)
2029: .getTargetException();
2030: throw new ReflectionException(e1);
2031: }
2032:
2033: throw new ReflectionException(e);
2034: }
2035: }
2036:
2037: private void restoreTabularData(Object value, Hashtable tempTable) {
2038: if (value instanceof TabularData) {
2039: TabularData data = (TabularData) value;
2040: for (Enumeration en = tempTable.keys(); en
2041: .hasMoreElements();) {
2042: Object[] index = (Object[]) en.nextElement();
2043: Object indexValue = tempTable.get(index);
2044: if (indexValue instanceof CompositeData) {
2045: data.addRow(index, (CompositeData) indexValue);
2046: } else if (indexValue instanceof Hashtable) {
2047: CompositeData comp = data.getRow(index);
2048: Hashtable ttt = (Hashtable) indexValue;
2049: for (Enumeration ex = ttt.keys(); ex
2050: .hasMoreElements();) {
2051: String key = (String) ex.nextElement();
2052: comp.setDataItem(key, ttt.get(key));
2053: }
2054: }
2055: }
2056: }
2057: }
2058:
2059: private boolean isInArray(String[] indexNames, String key) {
2060: for (int i = 0; i < indexNames.length; i++)
2061: if (indexNames[i].equals(key))
2062: return true;
2063:
2064: return false;
2065: }
2066:
2067: private MBeanNotificationInfo getNotifInfo(Notification notif) {
2068: MBeanNotificationInfo[] notifinfo = getNotificationInfo();
2069:
2070: for (int i = 0; i < notifinfo.length; i++) {
2071: String[] notifTypes = notifinfo[i].getNotifTypes();
2072: for (int j = 0; j < notifTypes.length; j++)
2073: if (notifTypes[j].equals(notif.getType()))
2074: return notifinfo[i];
2075: }
2076:
2077: return null;
2078: }
2079:
2080: /** Create Logger*/
2081: private void createLogger() {
2082: try {
2083: LogFactory
2084: .setLoggingLevel(com.adventnet.agent.logging.Level.TRACE);
2085: log = LogFactory.getInstance("JMX");
2086: log
2087: .setLoggingLevel(com.adventnet.agent.logging.Level.TRACE);
2088: } catch (Exception e) {
2089: e.printStackTrace();
2090: }
2091: }
2092:
2093: /**
2094: * This method returns the appropriate Wrapper Class
2095: * if the string value of the primitive data type is given.
2096: * @param type The String representation of the primitive data type
2097: * @return The appropriate wrapper class.
2098: */
2099: private static Class getProperClass(String type) {
2100: if (type.endsWith("int[]") || type.endsWith("[I"))
2101: return (new int[0]).getClass();
2102:
2103: else if (type.endsWith("long[]") || type.endsWith("[J"))
2104: return (new long[0]).getClass();
2105: else if (type.endsWith("byte[]") || type.endsWith("[B"))
2106: return (new byte[0]).getClass();
2107: else if (type.endsWith("float[]") || type.endsWith("[F"))
2108: return (new float[0]).getClass();
2109: else if (type.endsWith("char[]") || type.endsWith("[C"))
2110: return (new char[0]).getClass();
2111: else if (type.endsWith("short[]") || type.endsWith("[S"))
2112: return (new short[0]).getClass();
2113: else if (type.endsWith("double[]") || type.endsWith("[D"))
2114: return (new double[0]).getClass();
2115: else if (type.endsWith("boolean[]") || type.endsWith("[Z"))
2116: return (new boolean[0]).getClass();
2117:
2118: if (type.endsWith("int"))
2119: return int.class;
2120: else if (type.endsWith("long"))
2121: return long.class;
2122: else if (type.endsWith("byte"))
2123: return byte.class;
2124: else if (type.endsWith("float"))
2125: return float.class;
2126: else if (type.endsWith("char"))
2127: return char.class;
2128: else if (type.endsWith("short"))
2129: return short.class;
2130: else if (type.endsWith("double"))
2131: return double.class;
2132: else if (type.endsWith("boolean"))
2133: return boolean.class;
2134:
2135: return null;
2136: }
2137:
2138: private void logTheNotification(Notification ntfyObj,
2139: MBeanNotificationInfo notifinfo) {
2140: Descriptor descr = ((ModelMBeanNotificationInfo) notifinfo)
2141: .getDescriptor();
2142:
2143: if ((descr.getFieldValue(DescriptorSupport.LOG) != null)
2144: && !((String) descr
2145: .getFieldValue(DescriptorSupport.LOG))
2146: .toLowerCase().equals("t"))
2147: return;
2148:
2149: String notifFileName = (String) descr
2150: .getFieldValue(DescriptorSupport.LOG_FILE);
2151:
2152: try {
2153: if (prints == null && (notifFileName != null)) {
2154: FileOutputStream fileos = new FileOutputStream(
2155: notifFileName, true);
2156: prints = new PrintStream(fileos);
2157: }
2158: } catch (Exception e) {
2159: //e.printStackTrace();
2160: }
2161:
2162: if (prints != null) {
2163: prints.println("*** NOTIFICATION ***");
2164: prints.println("time = "
2165: + new Date(ntfyObj.getTimeStamp()).toString());
2166: prints.println("type = " + ntfyObj.getType());
2167: prints.println("message = " + ntfyObj.getMessage());
2168:
2169: if (ntfyObj instanceof AttributeChangeNotification) {
2170: prints.println("oldvalue = "
2171: + ((AttributeChangeNotification) ntfyObj)
2172: .getOldValue());
2173: prints.println("newvalue = "
2174: + ((AttributeChangeNotification) ntfyObj)
2175: .getNewValue());
2176: }
2177:
2178: prints.println("");
2179: }
2180: }
2181: }
|