Search in sources :

Example 26 with Descriptor

use of javax.management.Descriptor in project jdk8u_jdk by JetBrains.

the class ModelMBeanOperationInfo method validDescriptor.

/**
         * Clones the passed in Descriptor, sets default values, and checks for validity.
         * If the Descriptor is invalid (for instance by having the wrong "name"),
         * this indicates programming error and a RuntimeOperationsException will be thrown.
         *
         * The following fields will be defaulted if they are not already set:
         * displayName=this.getName(),name=this.getName(),
         * descriptorType="operation", role="operation"
         *
         *
         * @param in Descriptor to be checked, or null which is equivalent to
         * an empty Descriptor.
         * @exception RuntimeOperationsException if Descriptor is invalid
         */
private Descriptor validDescriptor(final Descriptor in) throws RuntimeOperationsException {
    Descriptor clone;
    boolean defaulted = (in == null);
    if (defaulted) {
        clone = new DescriptorSupport();
        MODELMBEAN_LOGGER.finer("Null Descriptor, creating new.");
    } else {
        clone = (Descriptor) in.clone();
    }
    //Setting defaults.
    if (defaulted && clone.getFieldValue("name") == null) {
        clone.setField("name", this.getName());
        MODELMBEAN_LOGGER.finer("Defaulting Descriptor name to " + this.getName());
    }
    if (defaulted && clone.getFieldValue("descriptorType") == null) {
        clone.setField("descriptorType", "operation");
        MODELMBEAN_LOGGER.finer("Defaulting descriptorType to \"operation\"");
    }
    if (clone.getFieldValue("displayName") == null) {
        clone.setField("displayName", this.getName());
        MODELMBEAN_LOGGER.finer("Defaulting Descriptor displayName to " + this.getName());
    }
    if (clone.getFieldValue("role") == null) {
        clone.setField("role", "operation");
        MODELMBEAN_LOGGER.finer("Defaulting Descriptor role field to \"operation\"");
    }
    //Checking validity
    if (!clone.isValid()) {
        throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), "The isValid() method of the Descriptor object itself returned false," + "one or more required fields are invalid. Descriptor:" + clone.toString());
    }
    if (!getName().equalsIgnoreCase((String) clone.getFieldValue("name"))) {
        throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), "The Descriptor \"name\" field does not match the object described. " + " Expected: " + this.getName() + " , was: " + clone.getFieldValue("name"));
    }
    if (!"operation".equalsIgnoreCase((String) clone.getFieldValue("descriptorType"))) {
        throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), "The Descriptor \"descriptorType\" field does not match the object described. " + " Expected: \"operation\" ," + " was: " + clone.getFieldValue("descriptorType"));
    }
    final String role = (String) clone.getFieldValue("role");
    if (!(role.equalsIgnoreCase("operation") || role.equalsIgnoreCase("setter") || role.equalsIgnoreCase("getter"))) {
        throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), "The Descriptor \"role\" field does not match the object described. " + " Expected: \"operation\", \"setter\", or \"getter\" ," + " was: " + clone.getFieldValue("role"));
    }
    final Object targetValue = clone.getFieldValue("targetType");
    if (targetValue != null) {
        if (!(targetValue instanceof java.lang.String)) {
            throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), "The Descriptor field \"targetValue\" is invalid class. " + " Expected: java.lang.String, " + " was: " + targetValue.getClass().getName());
        }
    }
    return clone;
}
Also used : Descriptor(javax.management.Descriptor) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 27 with Descriptor

use of javax.management.Descriptor in project jdk8u_jdk by JetBrains.

the class RequiredModelMBean method cacheResult.

/*
     * Cache the result of an operation in the descriptor, if that is
     * called for by the descriptor's configuration.  Note that we
     * don't remember operation parameters when caching the result, so
     * this is unlikely to be useful if there are any.
     */
private void cacheResult(ModelMBeanOperationInfo opInfo, Descriptor opDescr, Object result) throws MBeanException {
    Descriptor mmbDesc = modelMBeanInfo.getMBeanDescriptor();
    Object objctl = opDescr.getFieldValue("currencyTimeLimit");
    String ctl;
    if (objctl != null) {
        ctl = objctl.toString();
    } else {
        ctl = null;
    }
    if ((ctl == null) && (mmbDesc != null)) {
        objctl = mmbDesc.getFieldValue("currencyTimeLimit");
        if (objctl != null) {
            ctl = objctl.toString();
        } else {
            ctl = null;
        }
    }
    if ((ctl != null) && !(ctl.equals("-1"))) {
        opDescr.setField("value", result);
        opDescr.setField("lastUpdatedTimeStamp", String.valueOf((new Date()).getTime()));
        modelMBeanInfo.setDescriptor(opDescr, "operation");
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
            MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), "invoke(String,Object[],Object[])", "new descriptor is " + opDescr);
        }
    }
}
Also used : Descriptor(javax.management.Descriptor) Date(java.util.Date)

Example 28 with Descriptor

use of javax.management.Descriptor in project jdk8u_jdk by JetBrains.

the class RequiredModelMBean method resolveForCacheValue.

/*************************************/
/* DynamicMBean Interface            */
/*************************************/
/**
     * The resolveForCacheValue method checks the descriptor passed in to
     * see if there is a valid cached value in the descriptor.
     * The valid value will be in the 'value' field if there is one.
     * If the 'currencyTimeLimit' field in the descriptor is:
     * <ul>
     *   <li><b>&lt;0</b> Then the value is not cached and is never valid.
     *         Null is returned. The 'value' and 'lastUpdatedTimeStamp'
     *         fields are cleared.</li>
     *   <li><b>=0</b> Then the value is always cached and always valid.
     *         The 'value' field is returned.
     *         The 'lastUpdatedTimeStamp' field is not checked.</li>
     *   <li><b>&gt;0</b> Represents the number of seconds that the
     *         'value' field is valid.
     *         The 'value' field is no longer valid when
     *         'lastUpdatedTimeStamp' + 'currencyTimeLimit' &gt; Now.
     *       <ul>
     *       <li>When 'value' is valid, 'valid' is returned.</li>
     *       <li>When 'value' is no longer valid then null is returned and
     *           'value' and 'lastUpdatedTimeStamp' fields are cleared.</li>
     *       </ul>
     *   </li>
     * </ul>
     *
     **/
private Object resolveForCacheValue(Descriptor descr) throws MBeanException, RuntimeOperationsException {
    final boolean tracing = MODELMBEAN_LOGGER.isLoggable(Level.FINER);
    final String mth = "resolveForCacheValue(Descriptor)";
    if (tracing) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Entry");
    }
    Object response = null;
    boolean resetValue = false, returnCachedValue = true;
    long currencyPeriod = 0;
    if (descr == null) {
        if (tracing) {
            MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Input Descriptor is null");
        }
        return response;
    }
    if (tracing) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "descriptor is " + descr);
    }
    final Descriptor mmbDescr = modelMBeanInfo.getMBeanDescriptor();
    if (mmbDescr == null) {
        if (tracing) {
            MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "MBean Descriptor is null");
        }
    //return response;
    }
    Object objExpTime = descr.getFieldValue("currencyTimeLimit");
    String expTime;
    if (objExpTime != null) {
        expTime = objExpTime.toString();
    } else {
        expTime = null;
    }
    if ((expTime == null) && (mmbDescr != null)) {
        objExpTime = mmbDescr.getFieldValue("currencyTimeLimit");
        if (objExpTime != null) {
            expTime = objExpTime.toString();
        } else {
            expTime = null;
        }
    }
    if (expTime != null) {
        if (tracing) {
            MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "currencyTimeLimit: " + expTime);
        }
        // convert seconds to milliseconds for time comparison
        currencyPeriod = ((new Long(expTime)).longValue()) * 1000;
        if (currencyPeriod < 0) {
            /* if currencyTimeLimit is -1 then value is never cached */
            returnCachedValue = false;
            resetValue = true;
            if (tracing) {
                MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, currencyPeriod + ": never Cached");
            }
        } else if (currencyPeriod == 0) {
            /* if currencyTimeLimit is 0 then value is always cached */
            returnCachedValue = true;
            resetValue = false;
            if (tracing) {
                MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "always valid Cache");
            }
        } else {
            Object objtStamp = descr.getFieldValue("lastUpdatedTimeStamp");
            String tStamp;
            if (objtStamp != null)
                tStamp = objtStamp.toString();
            else
                tStamp = null;
            if (tracing) {
                MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "lastUpdatedTimeStamp: " + tStamp);
            }
            if (tStamp == null)
                tStamp = "0";
            long lastTime = (new Long(tStamp)).longValue();
            if (tracing) {
                MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "currencyPeriod:" + currencyPeriod + " lastUpdatedTimeStamp:" + lastTime);
            }
            long now = (new Date()).getTime();
            if (now < (lastTime + currencyPeriod)) {
                returnCachedValue = true;
                resetValue = false;
                if (tracing) {
                    MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, " timed valid Cache for " + now + " < " + (lastTime + currencyPeriod));
                }
            } else {
                /* value is expired */
                returnCachedValue = false;
                resetValue = true;
                if (tracing) {
                    MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "timed expired cache for " + now + " > " + (lastTime + currencyPeriod));
                }
            }
        }
        if (tracing) {
            MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "returnCachedValue:" + returnCachedValue + " resetValue: " + resetValue);
        }
        if (returnCachedValue == true) {
            Object currValue = descr.getFieldValue("value");
            if (currValue != null) {
                /* error/validity check return value here */
                response = currValue;
                /* need to cast string cached value to type */
                if (tracing) {
                    MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "valid Cache value: " + currValue);
                }
            } else {
                response = null;
                if (tracing) {
                    MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "no Cached value");
                }
            }
        }
        if (resetValue == true) {
            /* value is not current, so remove it */
            descr.removeField("lastUpdatedTimeStamp");
            descr.removeField("value");
            response = null;
            modelMBeanInfo.setDescriptor(descr, null);
            if (tracing) {
                MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "reset cached value to null");
            }
        }
    }
    if (tracing) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Exit");
    }
    return response;
}
Also used : Descriptor(javax.management.Descriptor) Date(java.util.Date)

Example 29 with Descriptor

use of javax.management.Descriptor in project jdk8u_jdk by JetBrains.

the class RequiredModelMBean method sendAttributeChangeNotification.

public void sendAttributeChangeNotification(AttributeChangeNotification ntfyObj) throws MBeanException, RuntimeOperationsException {
    final String mth = "sendAttributeChangeNotification(" + "AttributeChangeNotification)";
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Entry");
    }
    if (ntfyObj == null)
        throw new RuntimeOperationsException(new IllegalArgumentException("attribute change notification object must not be null"), "Exception occurred trying to send " + "attribute change notification of a ModelMBean");
    Object oldv = ntfyObj.getOldValue();
    Object newv = ntfyObj.getNewValue();
    if (oldv == null)
        oldv = "null";
    if (newv == null)
        newv = "null";
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Sending AttributeChangeNotification with " + ntfyObj.getAttributeName() + ntfyObj.getAttributeType() + ntfyObj.getNewValue() + ntfyObj.getOldValue());
    }
    // log notification if specified in descriptor
    Descriptor ntfyDesc = modelMBeanInfo.getDescriptor(ntfyObj.getType(), "notification");
    Descriptor mmbDesc = modelMBeanInfo.getMBeanDescriptor();
    String logging, logfile;
    if (ntfyDesc != null) {
        logging = (String) ntfyDesc.getFieldValue("log");
        if (logging == null) {
            if (mmbDesc != null)
                logging = (String) mmbDesc.getFieldValue("log");
        }
        if ((logging != null) && (logging.equalsIgnoreCase("t") || logging.equalsIgnoreCase("true"))) {
            logfile = (String) ntfyDesc.getFieldValue("logfile");
            if (logfile == null) {
                if (mmbDesc != null)
                    logfile = (String) mmbDesc.getFieldValue("logfile");
            }
            if (logfile != null) {
                try {
                    writeToLog(logfile, "LogMsg: " + ((new Date(ntfyObj.getTimeStamp())).toString()) + " " + ntfyObj.getType() + " " + ntfyObj.getMessage() + " Name = " + ntfyObj.getAttributeName() + " Old value = " + oldv + " New value = " + newv);
                } catch (Exception e) {
                    if (MODELMBEAN_LOGGER.isLoggable(Level.FINE)) {
                        MODELMBEAN_LOGGER.logp(Level.FINE, RequiredModelMBean.class.getName(), mth, "Failed to log " + ntfyObj.getType() + " notification: ", e);
                    }
                }
            }
        }
    } else if (mmbDesc != null) {
        logging = (String) mmbDesc.getFieldValue("log");
        if ((logging != null) && (logging.equalsIgnoreCase("t") || logging.equalsIgnoreCase("true"))) {
            logfile = (String) mmbDesc.getFieldValue("logfile");
            if (logfile != null) {
                try {
                    writeToLog(logfile, "LogMsg: " + ((new Date(ntfyObj.getTimeStamp())).toString()) + " " + ntfyObj.getType() + " " + ntfyObj.getMessage() + " Name = " + ntfyObj.getAttributeName() + " Old value = " + oldv + " New value = " + newv);
                } catch (Exception e) {
                    if (MODELMBEAN_LOGGER.isLoggable(Level.FINE)) {
                        MODELMBEAN_LOGGER.logp(Level.FINE, RequiredModelMBean.class.getName(), mth, "Failed to log " + ntfyObj.getType() + " notification: ", e);
                    }
                }
            }
        }
    }
    if (attributeBroadcaster != null) {
        attributeBroadcaster.sendNotification(ntfyObj);
    }
    //
    if (generalBroadcaster != null) {
        generalBroadcaster.sendNotification(ntfyObj);
    }
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "sent notification");
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Exit");
    }
}
Also used : Descriptor(javax.management.Descriptor) Date(java.util.Date) AttributeNotFoundException(javax.management.AttributeNotFoundException) ServiceNotFoundException(javax.management.ServiceNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 30 with Descriptor

use of javax.management.Descriptor in project jdk8u_jdk by JetBrains.

the class ModelMBeanInfoSupport method getDescriptors.

public Descriptor[] getDescriptors(String inDescriptorType) throws MBeanException, RuntimeOperationsException {
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
        MODELMBEAN_LOGGER.logp(Level.FINER, ModelMBeanInfoSupport.class.getName(), "getDescriptors(String)", "Entry");
    }
    if ((inDescriptorType == null) || (inDescriptorType.equals(""))) {
        inDescriptorType = "all";
    }
    // if no descriptors of that type, will return empty array
    //
    final Descriptor[] retList;
    if (inDescriptorType.equalsIgnoreCase(MMB)) {
        retList = new Descriptor[] { modelMBeanDescriptor };
    } else if (inDescriptorType.equalsIgnoreCase(ATTR)) {
        final MBeanAttributeInfo[] attrList = modelMBeanAttributes;
        int numAttrs = 0;
        if (attrList != null)
            numAttrs = attrList.length;
        retList = new Descriptor[numAttrs];
        for (int i = 0; i < numAttrs; i++) {
            retList[i] = (((ModelMBeanAttributeInfo) attrList[i]).getDescriptor());
        }
    } else if (inDescriptorType.equalsIgnoreCase(OPER)) {
        final MBeanOperationInfo[] operList = modelMBeanOperations;
        int numOpers = 0;
        if (operList != null)
            numOpers = operList.length;
        retList = new Descriptor[numOpers];
        for (int i = 0; i < numOpers; i++) {
            retList[i] = (((ModelMBeanOperationInfo) operList[i]).getDescriptor());
        }
    } else if (inDescriptorType.equalsIgnoreCase(CONS)) {
        final MBeanConstructorInfo[] consList = modelMBeanConstructors;
        int numCons = 0;
        if (consList != null)
            numCons = consList.length;
        retList = new Descriptor[numCons];
        for (int i = 0; i < numCons; i++) {
            retList[i] = (((ModelMBeanConstructorInfo) consList[i]).getDescriptor());
        }
    } else if (inDescriptorType.equalsIgnoreCase(NOTF)) {
        final MBeanNotificationInfo[] notifList = modelMBeanNotifications;
        int numNotifs = 0;
        if (notifList != null)
            numNotifs = notifList.length;
        retList = new Descriptor[numNotifs];
        for (int i = 0; i < numNotifs; i++) {
            retList[i] = (((ModelMBeanNotificationInfo) notifList[i]).getDescriptor());
        }
    } else if (inDescriptorType.equalsIgnoreCase(ALL)) {
        final MBeanAttributeInfo[] attrList = modelMBeanAttributes;
        int numAttrs = 0;
        if (attrList != null)
            numAttrs = attrList.length;
        final MBeanOperationInfo[] operList = modelMBeanOperations;
        int numOpers = 0;
        if (operList != null)
            numOpers = operList.length;
        final MBeanConstructorInfo[] consList = modelMBeanConstructors;
        int numCons = 0;
        if (consList != null)
            numCons = consList.length;
        final MBeanNotificationInfo[] notifList = modelMBeanNotifications;
        int numNotifs = 0;
        if (notifList != null)
            numNotifs = notifList.length;
        int count = numAttrs + numCons + numOpers + numNotifs + 1;
        retList = new Descriptor[count];
        retList[count - 1] = modelMBeanDescriptor;
        int j = 0;
        for (int i = 0; i < numAttrs; i++) {
            retList[j] = (((ModelMBeanAttributeInfo) attrList[i]).getDescriptor());
            j++;
        }
        for (int i = 0; i < numCons; i++) {
            retList[j] = (((ModelMBeanConstructorInfo) consList[i]).getDescriptor());
            j++;
        }
        for (int i = 0; i < numOpers; i++) {
            retList[j] = (((ModelMBeanOperationInfo) operList[i]).getDescriptor());
            j++;
        }
        for (int i = 0; i < numNotifs; i++) {
            retList[j] = (((ModelMBeanNotificationInfo) notifList[i]).getDescriptor());
            j++;
        }
    } else {
        final IllegalArgumentException iae = new IllegalArgumentException("Descriptor Type is invalid");
        final String msg = "Exception occurred trying to find" + " the descriptors of the MBean";
        throw new RuntimeOperationsException(iae, msg);
    }
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
        MODELMBEAN_LOGGER.logp(Level.FINER, ModelMBeanInfoSupport.class.getName(), "getDescriptors(String)", "Exit");
    }
    return retList;
}
Also used : MBeanOperationInfo(javax.management.MBeanOperationInfo) MBeanNotificationInfo(javax.management.MBeanNotificationInfo) Descriptor(javax.management.Descriptor) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Aggregations

Descriptor (javax.management.Descriptor)60 RuntimeOperationsException (javax.management.RuntimeOperationsException)18 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)16 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)15 ImmutableDescriptor (javax.management.ImmutableDescriptor)9 MBeanException (javax.management.MBeanException)9 ModelMBeanOperationInfo (javax.management.modelmbean.ModelMBeanOperationInfo)9 AttributeNotFoundException (javax.management.AttributeNotFoundException)8 MBeanServer (javax.management.MBeanServer)8 ObjectName (javax.management.ObjectName)8 ServiceNotFoundException (javax.management.ServiceNotFoundException)8 Method (java.lang.reflect.Method)7 InstanceNotFoundException (javax.management.InstanceNotFoundException)7 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)7 FileLogger (mx4j.log.FileLogger)7 Logger (mx4j.log.Logger)7 MBeanLogger (mx4j.log.MBeanLogger)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 Date (java.util.Date)6 ListenerNotFoundException (javax.management.ListenerNotFoundException)6