Search in sources :

Example 1 with MBeanRegistrationException

use of org.superbiz.resource.jmx.factory.MBeanRegistrationException in project tomee by apache.

the class Alternative method postConstruct.

@PostConstruct
public <T> void postConstruct() throws MBeanRegistrationException {
    final String name = properties.remove("name").toString();
    final String iface = properties.remove("interface").toString();
    final String prefix = properties.remove("prefix").toString();
    requireNotNull(name);
    requireNotNull(iface);
    try {
        final Class<T> ifaceCls = (Class<T>) Class.forName(iface, true, Thread.currentThread().getContextClassLoader());
        final StandardMBean mBean = new StandardMBean((T) this, ifaceCls);
        for (String attributeName : properties.stringPropertyNames()) {
            final Object value = properties.remove(attributeName);
            if (prefix != null) {
                if (!attributeName.startsWith(prefix + ".")) {
                    continue;
                } else {
                    attributeName = attributeName.substring(prefix.length() + 1);
                }
            }
            final Class<?> targetType = findAttributeType(mBean.getMBeanInfo(), attributeName);
            final Object targetValue = Converter.convert(value, targetType, null);
            final Attribute attribute = new Attribute(attributeName, targetValue);
            mBean.setAttribute(attribute);
        }
        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        final ObjectName objectName = new ObjectName(name);
        mbs.registerMBean(this, objectName);
    } catch (final Exception e) {
        LOGGER.severe("Unable to register mbean " + e.getMessage());
        throw new MBeanRegistrationException(e);
    }
}
Also used : StandardMBean(javax.management.StandardMBean) Attribute(javax.management.Attribute) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanRegistrationException(org.superbiz.resource.jmx.factory.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanRegistrationException(org.superbiz.resource.jmx.factory.MBeanRegistrationException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) PostConstruct(javax.annotation.PostConstruct)

Example 2 with MBeanRegistrationException

use of org.superbiz.resource.jmx.factory.MBeanRegistrationException in project tomee by apache.

the class Alternative method preDestroy.

@PreDestroy
public void preDestroy() throws MBeanRegistrationException {
    final String name = properties.getProperty("name");
    requireNotNull(name);
    try {
        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        final ObjectName objectName = new ObjectName(name);
        mbs.unregisterMBean(objectName);
    } catch (final MalformedObjectNameException e) {
        LOGGER.severe("Malformed MBean name: " + name);
        throw new MBeanRegistrationException(e);
    } catch (final javax.management.MBeanRegistrationException e) {
        LOGGER.severe("Error unregistering " + name);
        throw new MBeanRegistrationException(e);
    } catch (InstanceNotFoundException e) {
        LOGGER.severe("Error unregistering " + name);
        throw new MBeanRegistrationException(e);
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanRegistrationException(org.superbiz.resource.jmx.factory.MBeanRegistrationException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) PreDestroy(javax.annotation.PreDestroy)

Aggregations

InstanceNotFoundException (javax.management.InstanceNotFoundException)2 MBeanServer (javax.management.MBeanServer)2 MalformedObjectNameException (javax.management.MalformedObjectNameException)2 ObjectName (javax.management.ObjectName)2 MBeanRegistrationException (org.superbiz.resource.jmx.factory.MBeanRegistrationException)2 PostConstruct (javax.annotation.PostConstruct)1 PreDestroy (javax.annotation.PreDestroy)1 Attribute (javax.management.Attribute)1 StandardMBean (javax.management.StandardMBean)1