Search in sources :

Example 61 with Descriptor

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

the class RequiredModelMBean method invoke.

/**
     * Invokes a method on or through a RequiredModelMBean and returns
     * the result of the method execution.
     * <P>
     * If the given method to be invoked, together with the provided
     * signature, matches one of RequiredModelMbean
     * accessible methods, this one will be call. Otherwise the call to
     * the given method will be tried on the managed resource.
     * <P>
     * The last value returned by an operation may be cached in
     * the operation's descriptor which
     * is in the ModelMBeanOperationInfo's 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.
     *      The operation method is invoked.
     *      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. If there is no 'value' field
     *      then the operation method is invoked for the attribute.
     *      The 'lastUpdatedTimeStamp' field and `value' fields are set to
     *      the operation's return value and the current time stamp.</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, 'value' is returned.</LI>
     *         <LI>When 'value' is no longer valid then the operation
     *             method is invoked. The 'lastUpdatedTimeStamp' field
     *             and `value' fields are updated.</lI>
     *      </UL>
     * </LI>
     * </UL>
     *
     * <p><b>Note:</b> because of inconsistencies in previous versions of
     * this specification, it is recommended not to use negative or zero
     * values for <code>currencyTimeLimit</code>.  To indicate that a
     * cached value is never valid, omit the
     * <code>currencyTimeLimit</code> field.  To indicate that it is
     * always valid, use a very large number for this field.</p>
     *
     * @param opName The name of the method to be invoked. The
     *     name can be the fully qualified method name including the
     *     classname, or just the method name if the classname is
     *     defined in the 'class' field of the operation descriptor.
     * @param opArgs An array containing the parameters to be set
     *     when the operation is invoked
     * @param sig An array containing the signature of the
     *     operation. The class objects will be loaded using the same
     *     class loader as the one used for loading the MBean on which
     *     the operation was invoked.
     *
     * @return  The object returned by the method, which represents the
     *     result of invoking the method on the specified managed resource.
     *
     * @exception MBeanException  Wraps one of the following Exceptions:
     * <UL>
     * <LI> An Exception thrown by the managed object's invoked method.</LI>
     * <LI> {@link ServiceNotFoundException}: No ModelMBeanOperationInfo or
     *      no descriptor defined for the specified operation or the managed
     *      resource is null.</LI>
     * <LI> {@link InvalidTargetObjectTypeException}: The 'targetType'
     *      field value is not 'objectReference'.</LI>
     * </UL>
     * @exception ReflectionException  Wraps an {@link java.lang.Exception}
     *      thrown while trying to invoke the method.
     * @exception RuntimeOperationsException Wraps an
     *      {@link IllegalArgumentException} Method name is null.
     *
     **/
/*
      The requirement to be able to invoke methods on the
      RequiredModelMBean class itself makes this method considerably
      more complicated than it might otherwise be.  Note that, unlike
      earlier versions, we do not allow you to invoke such methods if
      they are not explicitly mentioned in the ModelMBeanInfo.  Doing
      so was potentially a security problem, and certainly very
      surprising.

      We do not look for the method in the RequiredModelMBean class
      itself if:
      (a) there is a "targetObject" field in the Descriptor for the
      operation; or
      (b) there is a "class" field in the Descriptor for the operation
      and the named class is not RequiredModelMBean or one of its
      superinterfaces; or
      (c) the name of the operation is not the name of a method in
      RequiredModelMBean (this is just an optimization).

      In cases (a) and (b), if you have gone to the trouble of adding
      those fields specifically for this operation then presumably you
      do not want RequiredModelMBean's methods to be called.

      We have to pay attention to class loading issues.  If the
      "class" field is present, the named class has to be resolved
      relative to RequiredModelMBean's class loader to test the
      condition (b) above, and relative to the managed resource's
      class loader to ensure that the managed resource is in fact of
      the named class (or a subclass).  The class names in the sig
      array likewise have to be resolved, first against
      RequiredModelMBean's class loader, then against the managed
      resource's class loader.  There is no point in using any other
      loader because when we call Method.invoke we must call it on
      a Method that is implemented by the target object.
     */
public Object invoke(String opName, Object[] opArgs, String[] sig) throws MBeanException, ReflectionException {
    final boolean tracing = MODELMBEAN_LOGGER.isLoggable(Level.FINER);
    final String mth = "invoke(String, Object[], String[])";
    if (tracing) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Entry");
    }
    if (opName == null) {
        final RuntimeException x = new IllegalArgumentException("Method name must not be null");
        throw new RuntimeOperationsException(x, "An exception occurred while trying to " + "invoke a method on a RequiredModelMBean");
    }
    String opClassName = null;
    String opMethodName;
    // Parse for class name and method
    int opSplitter = opName.lastIndexOf(".");
    if (opSplitter > 0) {
        opClassName = opName.substring(0, opSplitter);
        opMethodName = opName.substring(opSplitter + 1);
    } else
        opMethodName = opName;
    /* Ignore anything after a left paren.  We keep this for
           compatibility but it isn't specified.  */
    opSplitter = opMethodName.indexOf("(");
    if (opSplitter > 0)
        opMethodName = opMethodName.substring(0, opSplitter);
    if (tracing) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Finding operation " + opName + " as " + opMethodName);
    }
    ModelMBeanOperationInfo opInfo = modelMBeanInfo.getOperation(opMethodName);
    if (opInfo == null) {
        final String msg = "Operation " + opName + " not in ModelMBeanInfo";
        throw new MBeanException(new ServiceNotFoundException(msg), msg);
    }
    final Descriptor opDescr = opInfo.getDescriptor();
    if (opDescr == null) {
        final String msg = "Operation descriptor null";
        throw new MBeanException(new ServiceNotFoundException(msg), msg);
    }
    final Object cached = resolveForCacheValue(opDescr);
    if (cached != null) {
        if (tracing) {
            MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Returning cached value");
        }
        return cached;
    }
    if (opClassName == null)
        opClassName = (String) opDescr.getFieldValue("class");
    // may still be null now
    opMethodName = (String) opDescr.getFieldValue("name");
    if (opMethodName == null) {
        final String msg = "Method descriptor must include `name' field";
        throw new MBeanException(new ServiceNotFoundException(msg), msg);
    }
    final String targetTypeField = (String) opDescr.getFieldValue("targetType");
    if (targetTypeField != null && !targetTypeField.equalsIgnoreCase("objectReference")) {
        final String msg = "Target type must be objectReference: " + targetTypeField;
        throw new MBeanException(new InvalidTargetObjectTypeException(msg), msg);
    }
    final Object targetObjectField = opDescr.getFieldValue("targetObject");
    if (tracing && targetObjectField != null)
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Found target object in descriptor");
    /* Now look for the method, either in RequiredModelMBean itself
           or in the target object.  Set "method" and "targetObject"
           appropriately.  */
    Method method;
    Object targetObject;
    method = findRMMBMethod(opMethodName, targetObjectField, opClassName, sig);
    if (method != null)
        targetObject = this;
    else {
        if (tracing) {
            MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "looking for method in managedResource class");
        }
        if (targetObjectField != null)
            targetObject = targetObjectField;
        else {
            targetObject = managedResource;
            if (targetObject == null) {
                final String msg = "managedResource for invoke " + opName + " is null";
                Exception snfe = new ServiceNotFoundException(msg);
                throw new MBeanException(snfe);
            }
        }
        final Class<?> targetClass;
        if (opClassName != null) {
            try {
                AccessControlContext stack = AccessController.getContext();
                final Object obj = targetObject;
                final String className = opClassName;
                final ClassNotFoundException[] caughtException = new ClassNotFoundException[1];
                targetClass = javaSecurityAccess.doIntersectionPrivilege(new PrivilegedAction<Class<?>>() {

                    @Override
                    public Class<?> run() {
                        try {
                            ReflectUtil.checkPackageAccess(className);
                            final ClassLoader targetClassLoader = obj.getClass().getClassLoader();
                            return Class.forName(className, false, targetClassLoader);
                        } catch (ClassNotFoundException e) {
                            caughtException[0] = e;
                        }
                        return null;
                    }
                }, stack, acc);
                if (caughtException[0] != null) {
                    throw caughtException[0];
                }
            } catch (ClassNotFoundException e) {
                final String msg = "class for invoke " + opName + " not found";
                throw new ReflectionException(e, msg);
            }
        } else
            targetClass = targetObject.getClass();
        method = resolveMethod(targetClass, opMethodName, sig);
    }
    if (tracing) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "found " + opMethodName + ", now invoking");
    }
    final Object result = invokeMethod(opName, method, targetObject, opArgs);
    if (tracing) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "successfully invoked method");
    }
    if (result != null)
        cacheResult(opInfo, opDescr, result);
    return result;
}
Also used : ReflectionException(javax.management.ReflectionException) Method(java.lang.reflect.Method) 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) AccessControlContext(java.security.AccessControlContext) PrivilegedAction(java.security.PrivilegedAction) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanException(javax.management.MBeanException) Descriptor(javax.management.Descriptor) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 62 with Descriptor

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

the class ModelMBeanAttributeInfo 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 = "attribute"
         *
         * @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", "attribute");
        MODELMBEAN_LOGGER.finer("Defaulting descriptorType to \"attribute\"");
    }
    if (clone.getFieldValue("displayName") == null) {
        clone.setField("displayName", this.getName());
        MODELMBEAN_LOGGER.finer("Defaulting Descriptor displayName to " + this.getName());
    }
    //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 (!"attribute".equalsIgnoreCase((String) clone.getFieldValue("descriptorType"))) {
        throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), "The Descriptor \"descriptorType\" field does not match the object described. " + " Expected: \"attribute\" ," + " was: " + clone.getFieldValue("descriptorType"));
    }
    return clone;
}
Also used : Descriptor(javax.management.Descriptor) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 63 with Descriptor

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

the class ModelMBeanConstructorInfo 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="constructor"
         *
         *
         * @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", "constructor");
        MODELMBEAN_LOGGER.finer("Defaulting Descriptor role field to \"constructor\"");
    }
    //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"));
    }
    if (!((String) clone.getFieldValue("role")).equalsIgnoreCase("constructor")) {
        throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), "The Descriptor \"role\" field does not match the object described. " + " Expected: \"constructor\" ," + " was: " + clone.getFieldValue("role"));
    }
    return clone;
}
Also used : Descriptor(javax.management.Descriptor) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 64 with Descriptor

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

the class ModelMBeanInfoSupport 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=className,name=className,descriptorType="mbean",
     * persistPolicy="never", log="F", visibility="1"
     *
     * @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.getClassName());
        MODELMBEAN_LOGGER.finer("Defaulting Descriptor name to " + this.getClassName());
    }
    if (defaulted && clone.getFieldValue("descriptorType") == null) {
        clone.setField("descriptorType", MMB);
        MODELMBEAN_LOGGER.finer("Defaulting descriptorType to \"" + MMB + "\"");
    }
    if (clone.getFieldValue("displayName") == null) {
        clone.setField("displayName", this.getClassName());
        MODELMBEAN_LOGGER.finer("Defaulting Descriptor displayName to " + this.getClassName());
    }
    if (clone.getFieldValue("persistPolicy") == null) {
        clone.setField("persistPolicy", "never");
        MODELMBEAN_LOGGER.finer("Defaulting Descriptor persistPolicy to \"never\"");
    }
    if (clone.getFieldValue("log") == null) {
        clone.setField("log", "F");
        MODELMBEAN_LOGGER.finer("Defaulting Descriptor \"log\" field to \"F\"");
    }
    if (clone.getFieldValue("visibility") == null) {
        clone.setField("visibility", "1");
        MODELMBEAN_LOGGER.finer("Defaulting Descriptor visibility to 1");
    }
    //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 (!((String) clone.getFieldValue("descriptorType")).equalsIgnoreCase(MMB)) {
        throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), "The Descriptor \"descriptorType\" field does not match the object described. " + " Expected: " + MMB + " , was: " + clone.getFieldValue("descriptorType"));
    }
    return clone;
}
Also used : Descriptor(javax.management.Descriptor) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 65 with Descriptor

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

the class ModelMBeanNotificationInfo 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:
     * descriptorType="notification",displayName=this.getName(),
     * name=this.getName(),severity="6"
     *
     *
     * @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", "notification");
        MODELMBEAN_LOGGER.finer("Defaulting descriptorType to \"notification\"");
    }
    if (clone.getFieldValue("displayName") == null) {
        clone.setField("displayName", this.getName());
        MODELMBEAN_LOGGER.finer("Defaulting Descriptor displayName to " + this.getName());
    }
    if (clone.getFieldValue("severity") == null) {
        clone.setField("severity", "6");
        MODELMBEAN_LOGGER.finer("Defaulting Descriptor severity field to 6");
    }
    //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 (!"notification".equalsIgnoreCase((String) clone.getFieldValue("descriptorType"))) {
        throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), "The Descriptor \"descriptorType\" field does not match the object described. " + " Expected: \"notification\" ," + " was: " + clone.getFieldValue("descriptorType"));
    }
    return clone;
}
Also used : Descriptor(javax.management.Descriptor) RuntimeOperationsException(javax.management.RuntimeOperationsException)

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