Search in sources :

Example 21 with InvalidAttributeValueException

use of javax.management.InvalidAttributeValueException in project hive by apache.

the class MetricsMBeanImpl method setAttributes.

@Override
public AttributeList setAttributes(AttributeList arg0) {
    AttributeList attributesSet = new AttributeList();
    for (Attribute attr : arg0.asList()) {
        try {
            setAttribute(attr);
            attributesSet.add(attr);
        } catch (AttributeNotFoundException e) {
        // ignore exception - we simply don't add this attribute
        // back in to the resultant set.
        } catch (InvalidAttributeValueException e) {
        // ditto
        } catch (MBeanException e) {
        // likewise
        } catch (ReflectionException e) {
        // and again, one last time.
        }
    }
    return attributesSet;
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) MBeanException(javax.management.MBeanException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException)

Example 22 with InvalidAttributeValueException

use of javax.management.InvalidAttributeValueException in project tomcat by apache.

the class BaseModelMBean method setAttribute.

/**
 * Set the value of a specific attribute of this MBean.
 *
 * @param attribute The identification of the attribute to be set
 *  and the new value
 *
 * @exception AttributeNotFoundException if this attribute is not
 *  supported by this MBean
 * @exception MBeanException if the initializer of an object
 *  throws an exception
 * @exception ReflectionException if a Java reflection exception
 *  occurs when invoking the getter
 */
@Override
public void setAttribute(Attribute attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
    if (log.isDebugEnabled()) {
        log.debug("Setting attribute " + this + " " + attribute);
    }
    if ((resource instanceof DynamicMBean) && !(resource instanceof BaseModelMBean)) {
        try {
            ((DynamicMBean) resource).setAttribute(attribute);
        } catch (InvalidAttributeValueException e) {
            throw new MBeanException(e);
        }
        return;
    }
    // Validate the input parameters
    if (attribute == null) {
        throw new RuntimeOperationsException(new IllegalArgumentException(sm.getString("baseModelMBean.nullAttribute")), sm.getString("baseModelMBean.nullAttribute"));
    }
    String name = attribute.getName();
    Object value = attribute.getValue();
    if (name == null) {
        throw new RuntimeOperationsException(new IllegalArgumentException(sm.getString("baseModelMBean.nullAttributeName")), sm.getString("baseModelMBean.nullAttributeName"));
    }
    Object oldValue = null;
    // if( getAttMap.get(name) != null )
    // oldValue=getAttribute( name );
    Method m = managedBean.getSetter(name, this, resource);
    try {
        if (m.getDeclaringClass().isAssignableFrom(this.getClass())) {
            m.invoke(this, new Object[] { value });
        } else {
            m.invoke(resource, new Object[] { value });
        }
    } catch (InvocationTargetException e) {
        Throwable t = e.getTargetException();
        if (t == null) {
            t = e;
        }
        if (t instanceof RuntimeException) {
            throw new RuntimeOperationsException((RuntimeException) t, sm.getString("baseModelMBean.invokeError", name));
        } else if (t instanceof Error) {
            throw new RuntimeErrorException((Error) t, sm.getString("baseModelMBean.invokeError", name));
        } else {
            throw new MBeanException(e, sm.getString("baseModelMBean.invokeError", name));
        }
    } catch (Exception e) {
        log.error(sm.getString("baseModelMBean.invokeError", name), e);
        throw new MBeanException(e, sm.getString("baseModelMBean.invokeError", name));
    }
    try {
        sendAttributeChangeNotification(new Attribute(name, oldValue), attribute);
    } catch (Exception ex) {
        log.error(sm.getString("baseModelMBean.notificationError", name), ex);
    }
// attributes.put( name, value );
// if( source != null ) {
// // this mbean is associated with a source - maybe we want to persist
// source.updateField(oname, name, value);
// }
}
Also used : DynamicMBean(javax.management.DynamicMBean) RuntimeErrorException(javax.management.RuntimeErrorException) Attribute(javax.management.Attribute) Method(java.lang.reflect.Method) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) 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) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 23 with InvalidAttributeValueException

use of javax.management.InvalidAttributeValueException in project jetty.project by eclipse.

the class ObjectMBean method setAttribute.

/* ------------------------------------------------------------ */
public void setAttribute(Attribute attr) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
    if (attr == null)
        return;
    if (LOG.isDebugEnabled())
        LOG.debug("setAttribute " + _managed + ":" + attr.getName() + "=" + attr.getValue());
    Method setter = (Method) _setters.get(attr.getName());
    if (setter == null)
        throw new AttributeNotFoundException(attr.getName());
    try {
        Object o = _managed;
        if (setter.getDeclaringClass().isInstance(this))
            o = this;
        // get the value
        Object value = attr.getValue();
        // convert from ObjectName if need be
        if (value != null && _convert.contains(attr.getName())) {
            if (value.getClass().isArray()) {
                Class<?> t = setter.getParameterTypes()[0].getComponentType();
                Object na = Array.newInstance(t, Array.getLength(value));
                for (int i = Array.getLength(value); i-- > 0; ) Array.set(na, i, _mbeanContainer.findBean((ObjectName) Array.get(value, i)));
                value = na;
            } else
                value = _mbeanContainer.findBean((ObjectName) value);
        }
        // do the setting
        setter.invoke(o, new Object[] { value });
    } catch (IllegalAccessException e) {
        LOG.warn(Log.EXCEPTION, e);
        throw new AttributeNotFoundException(e.toString());
    } catch (InvocationTargetException e) {
        LOG.warn(Log.EXCEPTION, e);
        throw new ReflectionException(new Exception(e.getCause()));
    }
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) AttributeNotFoundException(javax.management.AttributeNotFoundException) ReflectionException(javax.management.ReflectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException)

Example 24 with InvalidAttributeValueException

use of javax.management.InvalidAttributeValueException in project geode by apache.

the class MX4JModelMBean method checkAssignability.

private void checkAssignability(Class parameter, Class declared) throws MBeanException {
    Logger logger = getLogger();
    if (logger.isEnabledFor(Logger.DEBUG)) {
        logger.debug("The class of the parameter is: " + parameter);
        if (parameter != null)
            logger.debug("The classloder of the parameter's class is: " + parameter.getClassLoader());
        logger.debug("The class declared as type of the attribute is: " + declared);
        if (declared != null)
            logger.debug("The classloader of the declared parameter's class is: " + declared.getClassLoader());
    }
    boolean assignable = false;
    if (declared == null || parameter == null)
        assignable = false;
    else if (declared == boolean.class && parameter == Boolean.class)
        assignable = true;
    else if (declared == byte.class && parameter == Byte.class)
        assignable = true;
    else if (declared == char.class && parameter == Character.class)
        assignable = true;
    else if (declared == short.class && parameter == Short.class)
        assignable = true;
    else if (declared == int.class && parameter == Integer.class)
        assignable = true;
    else if (declared == long.class && parameter == Long.class)
        assignable = true;
    else if (declared == float.class && parameter == Float.class)
        assignable = true;
    else if (declared == double.class && parameter == Double.class)
        assignable = true;
    else
        assignable = declared.isAssignableFrom(parameter);
    if (!assignable) {
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("Parameter value's class and attribute's declared return class are not assignable");
        throw new MBeanException(new InvalidAttributeValueException(LocalizedStrings.MX4JModelMBean_RETURNED_TYPE_AND_DECLARED_TYPE_ARE_NOT_ASSIGNABLE.toLocalizedString()));
    }
}
Also used : MBeanException(javax.management.MBeanException) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) InvalidAttributeValueException(javax.management.InvalidAttributeValueException)

Example 25 with InvalidAttributeValueException

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

the class RequiredModelMBean method setAttribute.

/**
     * Sets the value of a specific attribute of a named ModelMBean.
     *
     * If the 'setMethod' field of the attribute's descriptor
     * contains the name of a valid operation descriptor, then the
     * method described by the operation descriptor is executed.
     * In this implementation, the operation descriptor must be specified
     * correctly and assigned to the modelMBeanInfo so that the 'setMethod'
     * works correctly.
     * The response from the method is set as the value of the attribute
     * in the descriptor.
     *
     * <p>If currencyTimeLimit is &gt; 0, then the new value for the
     * attribute is cached in the attribute descriptor's
     * 'value' field and the 'lastUpdatedTimeStamp' field is set to
     * the current time stamp.
     *
     * <p>If the persist field of the attribute's descriptor is not null
     * then Persistence policy from the attribute descriptor is used to
     * guide storing the attribute in a persistent store.
     * <br>Store the MBean if 'persistPolicy' field is:
     * <UL>
     * <Li> != "never"</Li>
     * <Li> = "always"</Li>
     * <Li> = "onUpdate"</Li>
     * <Li> {@literal = "onTimer" and now > 'lastPersistTime' + 'persistPeriod'}</Li>
     * <Li> {@literal = "NoMoreOftenThan" and now > 'lastPersistTime' +
     *         'persistPeriod'}</Li>
     * </UL>
     * Do not store the MBean if 'persistPolicy' field is:
     * <UL>
     * <Li> = "never"</Li>
     * <Li> = {@literal = "onTimer" && now < 'lastPersistTime' + 'persistPeriod'}</Li>
     * <Li> = "onUnregister"</Li>
     * <Li> = {@literal = "NoMoreOftenThan" and now < 'lastPersistTime' +
     *        'persistPeriod'}</Li>
     * </UL>
     *
     * <p>The ModelMBeanInfo of the Model MBean is stored in a file.
     *
     * @param attribute The Attribute instance containing the name of
     *        the attribute to be set and the value it is to be set to.
     *
     *
     * @exception AttributeNotFoundException The specified attribute is
     *   not accessible in the MBean.
     *   <br>The following cases may result in an AttributeNotFoundException:
     *   <UL>
     *     <LI> No ModelMBeanAttributeInfo is found for the specified
     *          attribute.</LI>
     *     <LI> The ModelMBeanAttributeInfo's isWritable method returns
     *          'false'.</LI>
     *   </UL>
     * @exception InvalidAttributeValueException No descriptor is defined
     *   for the specified attribute.
     * @exception MBeanException Wraps one of the following Exceptions:
     *   <UL>
     *     <LI> An Exception thrown by the managed object's setter.</LI>
     *     <LI> A {@link ServiceNotFoundException} if a setMethod field is
     *          defined in the descriptor for the attribute and the managed
     *          resource is null; or if no setMethod field is defined and
     *          caching is not enabled for the attribute.
     *          Note that if there is no getMethod field either, then caching
     *          is automatically enabled.</LI>
     *     <LI> {@link InvalidTargetObjectTypeException} The 'targetType'
     *          field value is not 'objectReference'.</LI>
     *     <LI> An Exception thrown by the managed object's getter.</LI>
     *   </UL>
     * @exception ReflectionException  Wraps an {@link java.lang.Exception}
     *   thrown while trying to invoke the setter.
     * @exception RuntimeOperationsException Wraps an
     *   {@link IllegalArgumentException}: The attribute in parameter is
     *   null.
     *
     * @see #getAttribute(java.lang.String)
     **/
public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
    final boolean tracing = MODELMBEAN_LOGGER.isLoggable(Level.FINER);
    if (tracing) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), "setAttribute()", "Entry");
    }
    if (attribute == null)
        throw new RuntimeOperationsException(new IllegalArgumentException("attribute must not be null"), "Exception occurred trying to set an attribute of a " + "RequiredModelMBean");
    /* run setMethod if there is one */
    /* return cached value if its current */
    /* set cached value in descriptor and set date/time */
    /* send attribute change Notification */
    /* check persistence policy and persist if need be */
    String attrName = attribute.getName();
    Object attrValue = attribute.getValue();
    boolean updateDescriptor = false;
    ModelMBeanAttributeInfo attrInfo = modelMBeanInfo.getAttribute(attrName);
    if (attrInfo == null)
        throw new AttributeNotFoundException("setAttribute failed: " + attrName + " is not found ");
    Descriptor mmbDesc = modelMBeanInfo.getMBeanDescriptor();
    Descriptor attrDescr = attrInfo.getDescriptor();
    if (attrDescr != null) {
        if (!attrInfo.isWritable())
            throw new AttributeNotFoundException("setAttribute failed: " + attrName + " is not writable ");
        String attrSetMethod = (String) (attrDescr.getFieldValue("setMethod"));
        String attrGetMethod = (String) (attrDescr.getFieldValue("getMethod"));
        String attrType = attrInfo.getType();
        Object currValue = "Unknown";
        try {
            currValue = this.getAttribute(attrName);
        } catch (Throwable t) {
        // OK: Default "Unknown" value used for unknown attribute
        }
        Attribute oldAttr = new Attribute(attrName, currValue);
        /* run method from operations descriptor */
        if (attrSetMethod == null) {
            if (attrValue != null) {
                try {
                    final Class<?> clazz = loadClass(attrType);
                    if (!clazz.isInstance(attrValue))
                        throw new InvalidAttributeValueException(clazz.getName() + " expected, " + attrValue.getClass().getName() + " received.");
                } catch (ClassNotFoundException x) {
                    if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
                        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), "setAttribute(Attribute)", "Class " + attrType + " for attribute " + attrName + " not found: ", x);
                    }
                }
            }
            updateDescriptor = true;
        } else {
            invoke(attrSetMethod, (new Object[] { attrValue }), (new String[] { attrType }));
        }
        /* change cached value */
        Object objctl = attrDescr.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;
        }
        final boolean updateCache = ((ctl != null) && !(ctl.equals("-1")));
        if (attrSetMethod == null && !updateCache && attrGetMethod != null)
            throw new MBeanException(new ServiceNotFoundException("No " + "setMethod field is defined in the descriptor for " + attrName + " attribute and caching is not enabled " + "for it"));
        if (updateCache || updateDescriptor) {
            if (tracing) {
                MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), "setAttribute(Attribute)", "setting cached value of " + attrName + " to " + attrValue);
            }
            attrDescr.setField("value", attrValue);
            if (updateCache) {
                final String currtime = String.valueOf((new Date()).getTime());
                attrDescr.setField("lastUpdatedTimeStamp", currtime);
            }
            attrInfo.setDescriptor(attrDescr);
            modelMBeanInfo.setDescriptor(attrDescr, "attribute");
            if (tracing) {
                final StringBuilder strb = new StringBuilder().append("new descriptor is ").append(attrDescr).append(". AttributeInfo descriptor is ").append(attrInfo.getDescriptor()).append(". AttributeInfo descriptor is ").append(modelMBeanInfo.getDescriptor(attrName, "attribute"));
                MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), "setAttribute(Attribute)", strb.toString());
            }
        }
        if (tracing) {
            MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), "setAttribute(Attribute)", "sending sendAttributeNotification");
        }
        sendAttributeChangeNotification(oldAttr, attribute);
    } else {
        if (tracing) {
            MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), "setAttribute(Attribute)", "setMethod failed " + attrName + " not in attributeDescriptor\n");
        }
        throw new InvalidAttributeValueException("Unable to resolve attribute value, " + "no defined in descriptor for attribute");
    }
    if (tracing) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), "setAttribute(Attribute)", "Exit");
    }
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) Attribute(javax.management.Attribute) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) Date(java.util.Date) ServiceNotFoundException(javax.management.ServiceNotFoundException) Descriptor(javax.management.Descriptor) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Aggregations

InvalidAttributeValueException (javax.management.InvalidAttributeValueException)39 AttributeNotFoundException (javax.management.AttributeNotFoundException)26 ReflectionException (javax.management.ReflectionException)24 MBeanException (javax.management.MBeanException)23 Attribute (javax.management.Attribute)19 InstanceNotFoundException (javax.management.InstanceNotFoundException)13 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 RuntimeOperationsException (javax.management.RuntimeOperationsException)8 AttributeList (javax.management.AttributeList)6 IntrospectionException (javax.management.IntrospectionException)6 SCIMException (org.gluu.oxtrust.model.exception.SCIMException)6 Test (org.testng.annotations.Test)6 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 Method (java.lang.reflect.Method)5 URI (java.net.URI)5 ListenerNotFoundException (javax.management.ListenerNotFoundException)5 RuntimeErrorException (javax.management.RuntimeErrorException)5 Consumes (javax.ws.rs.Consumes)5 DefaultValue (javax.ws.rs.DefaultValue)5 HeaderParam (javax.ws.rs.HeaderParam)5