Search in sources :

Example 11 with JMException

use of javax.management.JMException in project nhin-d by DirectProject.

the class DNSSocketServer method registerMBean.

protected void registerMBean(Class<?> clazz) {
    final StringBuilder objectNameBuilder = new StringBuilder(clazz.getPackage().getName());
    objectNameBuilder.append(":type=").append(clazz.getSimpleName());
    objectNameBuilder.append(",name=").append(UUID.randomUUID());
    try {
        final StandardMBean mbean = new StandardMBean(this, DNSSocketServerMBean.class);
        final MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
        mbeanServer.registerMBean(mbean, new ObjectName(objectNameBuilder.toString()));
    } catch (JMException e) {
        LOGGER.error("Unable to register the DNSSocketServer MBean", e);
    }
}
Also used : StandardMBean(javax.management.StandardMBean) JMException(javax.management.JMException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 12 with JMException

use of javax.management.JMException in project camel by apache.

the class SpringManagementMBeanAssembler method assemble.

public ModelMBean assemble(MBeanServer mBeanServer, Object obj, ObjectName name) throws JMException {
    ModelMBeanInfo mbi = null;
    // prefer to use the managed instance if it has been annotated with Spring JMX annotations
    if (obj instanceof ManagedInstance) {
        Object custom = ((ManagedInstance) obj).getInstance();
        if (custom != null && ObjectHelper.hasAnnotation(custom.getClass().getAnnotations(), ManagedResource.class)) {
            LOG.trace("Assembling MBeanInfo for: {} from custom @ManagedResource object: {}", name, custom);
            // get the mbean info from the custom managed object
            mbi = springAssembler.getMBeanInfo(custom, name.toString());
            // and let the custom object be registered in JMX
            obj = custom;
        }
    }
    if (mbi == null) {
        if (ObjectHelper.hasAnnotation(obj.getClass().getAnnotations(), ManagedResource.class)) {
            // the object has a Spring ManagedResource annotations so assemble the MBeanInfo
            LOG.trace("Assembling MBeanInfo for: {} from @ManagedResource object: {}", name, obj);
            mbi = springAssembler.getMBeanInfo(obj, name.toString());
        } else {
            // fallback and let the default mbean assembler handle this instead
            return super.assemble(mBeanServer, obj, name);
        }
    }
    LOG.trace("Assembled MBeanInfo {}", mbi);
    RequiredModelMBean mbean = (RequiredModelMBean) mBeanServer.instantiate(RequiredModelMBean.class.getName());
    mbean.setModelMBeanInfo(mbi);
    try {
        mbean.setManagedResource(obj, "ObjectReference");
    } catch (InvalidTargetObjectTypeException e) {
        throw new JMException(e.getMessage());
    }
    // Allows the managed object to send notifications
    if (obj instanceof NotificationSenderAware) {
        ((NotificationSenderAware) obj).setNotificationSender(new NotificationSenderAdapter(mbean));
    }
    return mbean;
}
Also used : JMException(javax.management.JMException) NotificationSenderAware(org.apache.camel.api.management.NotificationSenderAware) ManagedInstance(org.apache.camel.api.management.ManagedInstance) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) NotificationSenderAdapter(org.apache.camel.management.NotificationSenderAdapter) ManagedResource(org.springframework.jmx.export.annotation.ManagedResource) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean)

Example 13 with JMException

use of javax.management.JMException in project camel by apache.

the class DefaultManagementMBeanAssembler method assemble.

public ModelMBean assemble(MBeanServer mBeanServer, Object obj, ObjectName name) throws JMException {
    ModelMBeanInfo mbi = null;
    ModelMBeanInfo standardMbi = null;
    Object custom = null;
    // prefer to use the managed instance if it has been annotated with JMX annotations
    if (obj instanceof ManagedInstance) {
        // there may be a custom embedded instance which have additional methods
        custom = ((ManagedInstance) obj).getInstance();
        if (custom != null && ObjectHelper.hasAnnotation(custom.getClass().getAnnotations(), ManagedResource.class)) {
            LOG.trace("Assembling MBeanInfo for: {} from custom @ManagedResource object: {}", name, custom);
            // get the mbean info into different groups (mbi = both, standard = standard out of the box mbi)
            mbi = assembler.getMBeanInfo(obj, custom, name.toString());
            standardMbi = assembler.getMBeanInfo(obj, null, name.toString());
        }
    }
    if (mbi == null) {
        // use the default provided mbean which has been annotated with JMX annotations
        LOG.trace("Assembling MBeanInfo for: {} from @ManagedResource object: {}", name, obj);
        mbi = assembler.getMBeanInfo(obj, null, name.toString());
    }
    if (mbi == null) {
        return null;
    }
    RequiredModelMBean mbean;
    RequiredModelMBean mixinMBean = null;
    boolean sanitize = camelContext.getManagementStrategy().getManagementAgent().getMask() != null && camelContext.getManagementStrategy().getManagementAgent().getMask();
    // as we want a combined mbean that has both the custom and the standard
    if (standardMbi != null) {
        mixinMBean = (RequiredModelMBean) mBeanServer.instantiate(RequiredModelMBean.class.getName());
        mixinMBean.setModelMBeanInfo(standardMbi);
        try {
            mixinMBean.setManagedResource(obj, "ObjectReference");
        } catch (InvalidTargetObjectTypeException e) {
            throw new JMException(e.getMessage());
        }
        // use custom as the object to call
        obj = custom;
    }
    // use a mixin mbean model to combine the custom and standard (custom is optional)
    mbean = new MixinRequiredModelMBean(mbi, sanitize, standardMbi, mixinMBean);
    try {
        mbean.setManagedResource(obj, "ObjectReference");
    } catch (InvalidTargetObjectTypeException e) {
        throw new JMException(e.getMessage());
    }
    // Allows the managed object to send notifications
    if (obj instanceof NotificationSenderAware) {
        ((NotificationSenderAware) obj).setNotificationSender(new NotificationSenderAdapter(mbean));
    }
    return mbean;
}
Also used : JMException(javax.management.JMException) NotificationSenderAware(org.apache.camel.api.management.NotificationSenderAware) ManagedInstance(org.apache.camel.api.management.ManagedInstance) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ManagedResource(org.apache.camel.api.management.ManagedResource) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean)

Example 14 with JMException

use of javax.management.JMException in project felix by apache.

the class CreateMBeanCommandProcessor method executeRequest.

public Document executeRequest(HttpInputStream in) throws IOException, JMException {
    Document document = builder.newDocument();
    Element root = document.createElement("MBeanOperation");
    document.appendChild(root);
    Element operationElement = document.createElement("Operation");
    operationElement.setAttribute("name", "create");
    root.appendChild(operationElement);
    String objectVariable = in.getVariable("objectname");
    String classVariable = in.getVariable("class");
    if (objectVariable == null || objectVariable.equals("") || classVariable == null || classVariable.equals("")) {
        operationElement.setAttribute("result", "error");
        operationElement.setAttribute("errorMsg", "Incorrect parameters in the request");
        return document;
    }
    operationElement.setAttribute("objectname", objectVariable);
    List types = new ArrayList();
    List values = new ArrayList();
    int i = 0;
    boolean unmatchedParameters = false;
    boolean valid = false;
    do {
        String parameterType = in.getVariable("type" + i);
        String parameterValue = in.getVariable("value" + i);
        valid = (parameterType != null && parameterValue != null);
        if (valid) {
            types.add(parameterType);
            Object value = null;
            try {
                value = CommandProcessorUtil.createParameterValue(parameterType, parameterValue);
            } catch (Exception e) {
                operationElement.setAttribute("result", "error");
                operationElement.setAttribute("errorMsg", "Parameter " + i + ": " + parameterValue + " cannot be converted to type " + parameterType);
                return document;
            }
            if (value != null) {
                values.add(value);
            }
        }
        if (parameterType == null ^ parameterValue == null) {
            unmatchedParameters = true;
            break;
        }
        i++;
    } while (valid);
    if (objectVariable == null || objectVariable.equals("")) {
        operationElement.setAttribute("result", "error");
        operationElement.setAttribute("errorMsg", "Incorrect parameters in the request");
        return document;
    }
    if (unmatchedParameters) {
        operationElement.setAttribute("result", "error");
        operationElement.setAttribute("errorMsg", "count of parameter types doesn't match count of parameter values");
        return document;
    }
    ObjectName name = null;
    try {
        name = new ObjectName(objectVariable);
    } catch (MalformedObjectNameException e) {
        operationElement.setAttribute("result", "error");
        operationElement.setAttribute("errorMsg", "Malformed object name");
        return document;
    }
    if (server.isRegistered(name)) {
        operationElement.setAttribute("result", "error");
        operationElement.setAttribute("errorMsg", "A MBean with name " + name + " is already registered");
        return document;
    } else {
        try {
            if (types.size() > 0) {
                Object[] params = values.toArray();
                String[] signature = new String[types.size()];
                types.toArray(signature);
                server.createMBean(classVariable, name, null, params, signature);
            } else {
                server.createMBean(classVariable, name, null);
            }
            operationElement.setAttribute("result", "success");
        } catch (Exception e) {
            operationElement.setAttribute("result", "error");
            operationElement.setAttribute("errorMsg", e.getMessage());
        }
    }
    return document;
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) MalformedObjectNameException(javax.management.MalformedObjectNameException) JMException(javax.management.JMException) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) List(java.util.List) ArrayList(java.util.ArrayList)

Example 15 with JMException

use of javax.management.JMException in project felix by apache.

the class DeleteMBeanCommandProcessor method executeRequest.

public Document executeRequest(HttpInputStream in) throws IOException, JMException {
    Document document = builder.newDocument();
    Element root = document.createElement("MBeanOperation");
    document.appendChild(root);
    Element operationElement = document.createElement("Operation");
    operationElement.setAttribute("operation", "delete");
    root.appendChild(operationElement);
    String objectVariable = in.getVariable("objectname");
    operationElement.setAttribute("objectname", objectVariable);
    if (objectVariable == null || objectVariable.equals("")) {
        operationElement.setAttribute("result", "error");
        operationElement.setAttribute("errorMsg", "Incorrect parameters in the request");
        return document;
    }
    ObjectName name = null;
    try {
        name = new ObjectName(objectVariable);
    } catch (MalformedObjectNameException e) {
        operationElement.setAttribute("result", "error");
        operationElement.setAttribute("errorMsg", "Malformed object name");
        return document;
    }
    if (server.isRegistered(name)) {
        try {
            server.unregisterMBean(name);
            operationElement.setAttribute("result", "success");
        } catch (Exception e) {
            operationElement.setAttribute("result", "error");
            operationElement.setAttribute("errorMsg", e.getMessage());
        }
    } else {
        if (name != null) {
            operationElement.setAttribute("result", "error");
            operationElement.setAttribute("errorMsg", "MBean " + name + " not registered");
        }
    }
    return document;
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) MalformedObjectNameException(javax.management.MalformedObjectNameException) JMException(javax.management.JMException) IOException(java.io.IOException) ObjectName(javax.management.ObjectName)

Aggregations

JMException (javax.management.JMException)114 ObjectName (javax.management.ObjectName)65 MBeanServer (javax.management.MBeanServer)35 IOException (java.io.IOException)21 InstrumentationManager (org.apache.cxf.management.InstrumentationManager)13 MalformedObjectNameException (javax.management.MalformedObjectNameException)12 MBeanInfo (javax.management.MBeanInfo)10 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)8 SnmpStatusException (com.sun.management.snmp.SnmpStatusException)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 StandardMBean (javax.management.StandardMBean)7 RequiredModelMBean (javax.management.modelmbean.RequiredModelMBean)6 Date (java.util.Date)5 Attribute (javax.management.Attribute)5 InvalidTargetObjectTypeException (javax.management.modelmbean.InvalidTargetObjectTypeException)5 Element (org.w3c.dom.Element)5 PostConstruct (javax.annotation.PostConstruct)4 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)4