Search in sources :

Example 1 with Logger

use of org.apache.felix.mosgi.jmx.agent.mx4j.log.Logger in project felix by apache.

the class MBeanIntrospector method createMBeanAttributeInfo.

private MBeanAttributeInfo[] createMBeanAttributeInfo(MBeanMetaData metadata, MBeanDescription description) {
    Logger logger = getLogger();
    HashMap attributes = new HashMap();
    HashMap getterNames = new HashMap();
    Method[] methods = metadata.management.getMethods();
    for (int j = 0; j < methods.length; ++j) {
        Method method = methods[j];
        if (Utils.isAttributeGetter(method)) {
            String name = method.getName();
            boolean isIs = name.startsWith("is");
            String attribute = null;
            if (isIs)
                attribute = name.substring(2);
            else
                attribute = name.substring(3);
            String descr = description == null ? null : description.getAttributeDescription(attribute);
            MBeanAttributeInfo info = (MBeanAttributeInfo) attributes.get(attribute);
            if (info != null) {
                // If an attribute with the same name already exists the MBean is not compliant
                if (!info.getType().equals(method.getReturnType().getName())) {
                    if (logger.isEnabledFor(Logger.DEBUG))
                        logger.debug("MBean is not compliant: has overloaded attribute " + attribute);
                    return null;
                } else {
                    // They return the same value,
                    if (getterNames.get(name) != null) {
                        // Ignore all but the first, since they resolve to the same method anyways
                        continue;
                    }
                    // for a boolean attribute. In this case, the MBean is not compliant.
                    if (info.isReadable()) {
                        if (logger.isEnabledFor(Logger.DEBUG))
                            logger.debug("MBean is not compliant: has overloaded attribute " + attribute);
                        return null;
                    }
                    // MBeanAttributeInfo is already present due to a setter method, just update its readability
                    info = new MBeanAttributeInfo(attribute, info.getType(), info.getDescription(), true, info.isWritable(), isIs);
                }
            } else {
                info = new MBeanAttributeInfo(attribute, method.getReturnType().getName(), descr, true, false, isIs);
            }
            // Replace if exists
            attributes.put(attribute, info);
            getterNames.put(name, method);
        } else if (Utils.isAttributeSetter(method)) {
            String name = method.getName();
            String attribute = name.substring(3);
            String descr = description == null ? null : description.getAttributeDescription(attribute);
            MBeanAttributeInfo info = (MBeanAttributeInfo) attributes.get(attribute);
            if (info != null) {
                // If an attribute with the same name already exists the MBean is not compliant
                if (!info.getType().equals(method.getParameterTypes()[0].getName())) {
                    if (logger.isEnabledFor(Logger.DEBUG))
                        logger.debug("MBean is not compliant: has overloaded attribute " + attribute);
                    return null;
                } else {
                    // MBeanAttributeInfo is already present due to a getter method, just update its writability
                    info = new MBeanAttributeInfo(info.getName(), info.getType(), info.getDescription(), info.isReadable(), true, info.isIs());
                }
            } else {
                info = new MBeanAttributeInfo(attribute, method.getParameterTypes()[0].getName(), descr, false, true, false);
            }
            // Replace if exists
            attributes.put(attribute, info);
        }
    }
    return (MBeanAttributeInfo[]) attributes.values().toArray(new MBeanAttributeInfo[attributes.size()]);
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) Logger(org.apache.felix.mosgi.jmx.agent.mx4j.log.Logger) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 2 with Logger

use of org.apache.felix.mosgi.jmx.agent.mx4j.log.Logger in project felix by apache.

the class MBeanIntrospector method createMBeanDescription.

private MBeanDescription createMBeanDescription(MBeanMetaData metadata) {
    // This is a non-standard extension
    Logger logger = getLogger();
    if (logger.isEnabledFor(Logger.TRACE))
        logger.trace("Looking for standard MBean description...");
    Class mbeanClass = metadata.mbean.getClass();
    for (Class cls = mbeanClass; cls != null; cls = cls.getSuperclass()) {
        String clsName = cls.getName();
        if (clsName.startsWith("java."))
            break;
        // Use full qualified name only
        String descrClassName = clsName + "MBeanDescription";
        // Try to load the class
        try {
            Class descrClass = null;
            ClassLoader loader = metadata.classloader;
            if (loader != null) {
                // only in the classloader of the mbean, not in the whole CLR (since MLets delegates to the CLR)
                if (loader.getClass() == MLet.class)
                    descrClass = ((MLet) loader).loadClass(descrClassName, null);
                else
                    descrClass = loader.loadClass(descrClassName);
            } else {
                descrClass = Class.forName(descrClassName, false, null);
            }
            Object descrInstance = descrClass.newInstance();
            if (descrInstance instanceof MBeanDescription) {
                MBeanDescription description = (MBeanDescription) descrInstance;
                if (logger.isEnabledFor(Logger.TRACE))
                    logger.trace("Found provided standard MBean description: " + description);
                return description;
            }
        } catch (ClassNotFoundException ignored) {
        } catch (InstantiationException ignored) {
        } catch (IllegalAccessException ignored) {
        }
    }
    MBeanDescription description = DEFAULT_DESCRIPTION;
    if (logger.isEnabledFor(Logger.TRACE))
        logger.trace("Cannot find standard MBean description, using default: " + description);
    return description;
}
Also used : MBeanDescription(org.apache.felix.mosgi.jmx.agent.mx4j.MBeanDescription) MLet(javax.management.loading.MLet) Logger(org.apache.felix.mosgi.jmx.agent.mx4j.log.Logger)

Example 3 with Logger

use of org.apache.felix.mosgi.jmx.agent.mx4j.log.Logger in project felix by apache.

the class MBeanIntrospector method createInvoker.

private MBeanInvoker createInvoker(MBeanMetaData metadata) {
    Logger logger = getLogger();
    if (m_customMBeanInvoker != null) {
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("Custom MBeanInvoker class is: " + m_customMBeanInvoker);
        try {
            MBeanInvoker mbeanInvoker = (MBeanInvoker) Thread.currentThread().getContextClassLoader().loadClass(m_customMBeanInvoker).newInstance();
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("Using custom MBeanInvoker: " + mbeanInvoker);
            return mbeanInvoker;
        } catch (Exception x) {
            if (logger.isEnabledFor(Logger.DEBUG))
                logger.debug("Cannot instantiate custom MBeanInvoker, using default", x);
        }
    }
    /* SFR
      if (m_bcelClassesAvailable)
      {
         MBeanInvoker mbeanInvoker = BCELMBeanInvoker.create(metadata);
         if (logger.isEnabledFor(Logger.TRACE)) logger.trace("Using default BCEL MBeanInvoker for MBean " + metadata.name + ", " + mbeanInvoker);
         return mbeanInvoker;
      }
      else
      {
*/
    MBeanInvoker mbeanInvoker = new ReflectedMBeanInvoker();
    if (logger.isEnabledFor(Logger.TRACE))
        logger.trace("Using default Reflection MBeanInvoker for MBean " + metadata.name + ", " + mbeanInvoker);
    return mbeanInvoker;
/* SFR     } */
}
Also used : Logger(org.apache.felix.mosgi.jmx.agent.mx4j.log.Logger)

Example 4 with Logger

use of org.apache.felix.mosgi.jmx.agent.mx4j.log.Logger in project felix by apache.

the class MX4JMBeanServer method createClassLoaderRepository.

/**
 * Creates a new ClassLoaderRepository for ClassLoader MBeans.
 * The system property {@link mx4j.MX4JSystemKeys#MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY}
 * is tested for a full qualified name of a class
 * extending the {@link ModifiableClassLoaderRepository} class.
 * In case the system property is not defined or the class is not loadable or instantiable, a default
 * implementation is returned.
 */
private ModifiableClassLoaderRepository createClassLoaderRepository() {
    Logger logger = getLogger();
    if (logger.isEnabledFor(Logger.TRACE))
        logger.trace("Checking for system property " + MX4JSystemKeys.MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY);
    String value = (String) AccessController.doPrivileged(new PrivilegedAction() {

        public Object run() {
            return System.getProperty(MX4JSystemKeys.MX4J_MBEANSERVER_CLASSLOADER_REPOSITORY);
        }
    });
    if (value != null) {
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Property found for custom ClassLoaderRepository; class is: " + value);
        try {
            ModifiableClassLoaderRepository repository = (ModifiableClassLoaderRepository) Thread.currentThread().getContextClassLoader().loadClass(value).newInstance();
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("Custom ClassLoaderRepository created successfully " + repository);
            return repository;
        } catch (Exception x) {
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("Custom ClassLoaderRepository could not be created", x);
        }
    }
    return new DefaultClassLoaderRepository();
}
Also used : PrivilegedAction(java.security.PrivilegedAction) Logger(org.apache.felix.mosgi.jmx.agent.mx4j.log.Logger) IntrospectionException(javax.management.IntrospectionException) OperationsException(javax.management.OperationsException) BadBinaryOpValueExpException(javax.management.BadBinaryOpValueExpException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) BadStringOperationException(javax.management.BadStringOperationException) ReflectionException(javax.management.ReflectionException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) RuntimeErrorException(javax.management.RuntimeErrorException) MalformedObjectNameException(javax.management.MalformedObjectNameException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) JMRuntimeException(javax.management.JMRuntimeException) PrivilegedActionException(java.security.PrivilegedActionException) BadAttributeValueExpException(javax.management.BadAttributeValueExpException) IOException(java.io.IOException) ImplementationException(org.apache.felix.mosgi.jmx.agent.mx4j.ImplementationException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) InvalidApplicationException(javax.management.InvalidApplicationException)

Example 5 with Logger

use of org.apache.felix.mosgi.jmx.agent.mx4j.log.Logger in project felix by apache.

the class MX4JMBeanServer method createMBeanRepository.

/**
 * Creates a new repository for MBeans.
 * The system property {@link mx4j.MX4JSystemKeys#MX4J_MBEANSERVER_REPOSITORY} is tested
 * for a full qualified name of a class implementing the {@link MBeanRepository} interface.
 * In case the system property is not defined or the class is not loadable or instantiable, a default
 * implementation is returned.
 */
private MBeanRepository createMBeanRepository() {
    Logger logger = getLogger();
    if (logger.isEnabledFor(Logger.TRACE))
        logger.trace("Checking for system property " + MX4JSystemKeys.MX4J_MBEANSERVER_REPOSITORY);
    String value = (String) AccessController.doPrivileged(new PrivilegedAction() {

        public Object run() {
            return System.getProperty(MX4JSystemKeys.MX4J_MBEANSERVER_REPOSITORY);
        }
    });
    if (value != null) {
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Property found for custom MBeanServer registry; class is: " + value);
        try {
            MBeanRepository registry = (MBeanRepository) Thread.currentThread().getContextClassLoader().loadClass(value).newInstance();
            if (logger.isEnabledFor(Logger.TRACE)) {
                logger.trace("Custom MBeanServer registry created successfully");
            }
            return registry;
        } catch (Exception x) {
            if (logger.isEnabledFor(Logger.TRACE)) {
                logger.trace("Custom MBeanServer registry could not be created", x);
            }
        }
    }
    return new DefaultMBeanRepository();
}
Also used : PrivilegedAction(java.security.PrivilegedAction) Logger(org.apache.felix.mosgi.jmx.agent.mx4j.log.Logger) IntrospectionException(javax.management.IntrospectionException) OperationsException(javax.management.OperationsException) BadBinaryOpValueExpException(javax.management.BadBinaryOpValueExpException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) BadStringOperationException(javax.management.BadStringOperationException) ReflectionException(javax.management.ReflectionException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) RuntimeErrorException(javax.management.RuntimeErrorException) MalformedObjectNameException(javax.management.MalformedObjectNameException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) JMRuntimeException(javax.management.JMRuntimeException) PrivilegedActionException(java.security.PrivilegedActionException) BadAttributeValueExpException(javax.management.BadAttributeValueExpException) IOException(java.io.IOException) ImplementationException(org.apache.felix.mosgi.jmx.agent.mx4j.ImplementationException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) InvalidApplicationException(javax.management.InvalidApplicationException)

Aggregations

Logger (org.apache.felix.mosgi.jmx.agent.mx4j.log.Logger)8 AttributeNotFoundException (javax.management.AttributeNotFoundException)4 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)4 JMRuntimeException (javax.management.JMRuntimeException)4 ListenerNotFoundException (javax.management.ListenerNotFoundException)4 MBeanException (javax.management.MBeanException)4 MBeanRegistrationException (javax.management.MBeanRegistrationException)4 ReflectionException (javax.management.ReflectionException)4 RuntimeErrorException (javax.management.RuntimeErrorException)4 ImplementationException (org.apache.felix.mosgi.jmx.agent.mx4j.ImplementationException)4 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 PrivilegedAction (java.security.PrivilegedAction)2 PrivilegedActionException (java.security.PrivilegedActionException)2 Attribute (javax.management.Attribute)2 AttributeList (javax.management.AttributeList)2 BadAttributeValueExpException (javax.management.BadAttributeValueExpException)2 BadBinaryOpValueExpException (javax.management.BadBinaryOpValueExpException)2 BadStringOperationException (javax.management.BadStringOperationException)2 DynamicMBean (javax.management.DynamicMBean)2