Search in sources :

Example 16 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 17 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 18 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 19 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)

Example 20 with Descriptor

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

the class OpenMBeanAttributeInfoSupport method equal.

static boolean equal(OpenMBeanParameterInfo x1, OpenMBeanParameterInfo x2) {
    if (x1 instanceof DescriptorRead) {
        if (!(x2 instanceof DescriptorRead))
            return false;
        Descriptor d1 = ((DescriptorRead) x1).getDescriptor();
        Descriptor d2 = ((DescriptorRead) x2).getDescriptor();
        if (!d1.equals(d2))
            return false;
    } else if (x2 instanceof DescriptorRead)
        return false;
    return x1.getName().equals(x2.getName()) && x1.getOpenType().equals(x2.getOpenType()) && (x1.hasDefaultValue() ? x1.getDefaultValue().equals(x2.getDefaultValue()) : !x2.hasDefaultValue()) && (x1.hasMinValue() ? x1.getMinValue().equals(x2.getMinValue()) : !x2.hasMinValue()) && (x1.hasMaxValue() ? x1.getMaxValue().equals(x2.getMaxValue()) : !x2.hasMaxValue()) && (x1.hasLegalValues() ? x1.getLegalValues().equals(x2.getLegalValues()) : !x2.hasLegalValues());
}
Also used : DescriptorRead(javax.management.DescriptorRead) Descriptor(javax.management.Descriptor) ImmutableDescriptor(javax.management.ImmutableDescriptor)

Aggregations

Descriptor (javax.management.Descriptor)73 RuntimeOperationsException (javax.management.RuntimeOperationsException)18 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)16 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)16 DescriptorSupport (javax.management.modelmbean.DescriptorSupport)11 ModelMBeanOperationInfo (javax.management.modelmbean.ModelMBeanOperationInfo)11 Method (java.lang.reflect.Method)10 ImmutableDescriptor (javax.management.ImmutableDescriptor)9 MBeanException (javax.management.MBeanException)9 AttributeNotFoundException (javax.management.AttributeNotFoundException)8 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)8 MBeanInfo (javax.management.MBeanInfo)8 MBeanServer (javax.management.MBeanServer)8 ObjectName (javax.management.ObjectName)8 ServiceNotFoundException (javax.management.ServiceNotFoundException)8 ArrayList (java.util.ArrayList)7 InstanceNotFoundException (javax.management.InstanceNotFoundException)7 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)7 MBeanOperationInfo (javax.management.MBeanOperationInfo)7 MBeanParameterInfo (javax.management.MBeanParameterInfo)7