Search in sources :

Example 16 with JMException

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

the class HttpAdaptor method start.

/**
 * Starts the server
 */
public void start() throws IOException {
    if (server != null) {
        serverSocket = createServerSocket();
        if (serverSocket == null) {
            HttpAdaptor.log(LogService.LOG_ERROR, "Server socket is null", null);
            return;
        }
        if (processorClass != null && processorName != null) {
            HttpAdaptor.log(LogService.LOG_INFO, "Building processor class of type " + processorClass + " and name " + processorName, null);
            try {
                server.createMBean(processorClass, processorName, null);
            } catch (JMException e) {
                HttpAdaptor.log(LogService.LOG_INFO, "Exception creating processor class", e);
            }
        }
        Iterator i = commands.values().iterator();
        while (i.hasNext()) {
            HttpCommandProcessor processor = (HttpCommandProcessor) i.next();
            processor.setMBeanServer(server);
            processor.setDocumentBuilder(builder);
        }
        HttpAdaptor.log(LogService.LOG_INFO, "HttpAdaptor server listening on port " + port, null);
        alive = true;
        Thread serverThread = new Thread(new Runnable() {

            public void run() {
                HttpAdaptor.log(LogService.LOG_INFO, "HttpAdaptor version " + VERSION + " started", null);
                startDate = new Date();
                requestsCount = 0;
                while (alive) {
                    try {
                        Socket client = null;
                        client = serverSocket.accept();
                        if (!alive) {
                            break;
                        }
                        requestsCount++;
                        new HttpClient(client).start();
                    } catch (InterruptedIOException e) {
                        continue;
                    } catch (IOException e) {
                        continue;
                    } catch (Exception e) {
                        HttpAdaptor.log(LogService.LOG_WARNING, "Exception during request processing", e);
                        continue;
                    } catch (Error e) {
                        HttpAdaptor.log(LogService.LOG_ERROR, "Error during request processing", e);
                        continue;
                    }
                }
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    HttpAdaptor.log(LogService.LOG_WARNING, "Exception closing the server", e);
                }
                serverSocket = null;
                alive = false;
                HttpAdaptor.log(LogService.LOG_INFO, "Server stopped", null);
            }
        });
        serverThread.start();
    } else {
        HttpAdaptor.log(LogService.LOG_INFO, "Start failed, no server target server has been set", null);
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) JMException(javax.management.JMException) Iterator(java.util.Iterator) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Date(java.util.Date) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) MalformedObjectNameException(javax.management.MalformedObjectNameException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) JMException(javax.management.JMException)

Example 17 with JMException

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

the class MBeanCommandProcessor method createMBeanElement.

private Element createMBeanElement(Document document, ObjectName objectName, HttpInputStream in) throws JMException {
    Element root = document.createElement("MBean");
    MBeanInfo info = server.getMBeanInfo(objectName);
    root.setAttribute("description", info.getDescription());
    root.setAttribute("classname", info.getClassName());
    root.setAttribute("objectname", objectName.toString());
    /*
		if (info instanceof ModelMBeanInfo)
		{
			root.setAttribute("model", "true");
		}
*/
    if (HttpUtil.booleanVariableValue(in, "attributes", true)) {
        MBeanAttributeInfo[] attributes = info.getAttributes();
        if (attributes != null) {
            SortedMap sortedAttributes = new TreeMap();
            for (int i = 0; i < attributes.length; i++) {
                Element attribute = document.createElement("Attribute");
                attribute.setAttribute("name", attributes[i].getName());
                attribute.setAttribute("type", attributes[i].getType());
                attribute.setAttribute("description", attributes[i].getDescription());
                attribute.setAttribute("strinit", String.valueOf(CommandProcessorUtil.canCreateParameterValue(attributes[i].getType())));
                if (attributes[i].isReadable() && attributes[i].isWritable()) {
                    attribute.setAttribute("availability", "RW");
                }
                if (attributes[i].isReadable() && !attributes[i].isWritable()) {
                    attribute.setAttribute("availability", "RO");
                }
                if (!attributes[i].isReadable() && attributes[i].isWritable()) {
                    attribute.setAttribute("availability", "WO");
                }
                try {
                    Object attributeValue = server.getAttribute(objectName, attributes[i].getName());
                    attribute.setAttribute("isnull", (attributeValue == null) ? "true" : "false");
                    if (attributeValue != null) {
                        attribute.setAttribute("value", attributeValue.toString());
                        if (attributeValue.getClass().isArray()) {
                            attribute.setAttribute("aggregation", "array");
                        }
                        if (attributeValue instanceof java.util.Collection) {
                            attribute.setAttribute("aggregation", "collection");
                        }
                        if (attributeValue instanceof java.util.Map) {
                            attribute.setAttribute("aggregation", "map");
                        }
                    } else {
                        attribute.setAttribute("value", "null");
                    }
                } catch (JMException e) {
                    attribute.setAttribute("value", e.getMessage());
                }
                sortedAttributes.put(attributes[i].getName(), attribute);
            }
            Iterator keys = sortedAttributes.keySet().iterator();
            while (keys.hasNext()) {
                root.appendChild((Element) sortedAttributes.get(keys.next()));
            }
        }
    }
    if (HttpUtil.booleanVariableValue(in, "constructors", true)) {
        MBeanConstructorInfo[] constructors = info.getConstructors();
        if (constructors != null) {
            // How to order contructors?
            for (int i = 0; i < constructors.length; i++) {
                Element constructor = document.createElement("Constructor");
                constructor.setAttribute("name", constructors[i].getName());
                constructor.setAttribute("description", constructors[i].getDescription());
                addParameters(constructor, document, constructors[i].getSignature());
                root.appendChild(constructor);
            }
        }
    }
    if (HttpUtil.booleanVariableValue(in, "operations", true)) {
        MBeanOperationInfo[] operations = info.getOperations();
        if (operations != null) {
            for (int i = 0; i < operations.length; i++) {
                Element operation = document.createElement("Operation");
                operation.setAttribute("name", operations[i].getName());
                operation.setAttribute("description", operations[i].getDescription());
                operation.setAttribute("return", operations[i].getReturnType());
                switch(operations[i].getImpact()) {
                    case MBeanOperationInfo.UNKNOWN:
                        operation.setAttribute("impact", "unknown");
                        break;
                    case MBeanOperationInfo.ACTION:
                        operation.setAttribute("impact", "action");
                        break;
                    case MBeanOperationInfo.INFO:
                        operation.setAttribute("impact", "info");
                        break;
                    case MBeanOperationInfo.ACTION_INFO:
                        operation.setAttribute("impact", "action_info");
                        break;
                }
                addParameters(operation, document, operations[i].getSignature());
                root.appendChild(operation);
            }
        }
    }
    if (HttpUtil.booleanVariableValue(in, "notifications", true)) {
        MBeanNotificationInfo[] notifications = info.getNotifications();
        if (notifications != null) {
            for (int i = 0; i < notifications.length; i++) {
                Element notification = document.createElement("Notification");
                notification.setAttribute("name", notifications[i].getName());
                notification.setAttribute("description", notifications[i].getDescription());
                String[] types = notifications[i].getNotifTypes();
                for (int j = 0; j < types.length; j++) {
                    Element type = document.createElement("Type");
                    type.setAttribute("name", types[j]);
                    notification.appendChild(type);
                }
                root.appendChild(notification);
            }
        }
    }
    return root;
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) Element(org.w3c.dom.Element) TreeMap(java.util.TreeMap) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) MBeanConstructorInfo(javax.management.MBeanConstructorInfo) MBeanNotificationInfo(javax.management.MBeanNotificationInfo) SortedMap(java.util.SortedMap) JMException(javax.management.JMException) Iterator(java.util.Iterator) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 18 with JMException

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

the class SetAttributeCommandProcessor 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", "setattribute");
    root.appendChild(operationElement);
    String objectVariable = in.getVariable("objectname");
    String attributeVariable = in.getVariable("attribute");
    String valueVariable = in.getVariable("value");
    if (objectVariable == null || objectVariable.equals("") || attributeVariable == null || attributeVariable.equals("") || valueVariable == null) {
        operationElement.setAttribute("result", "error");
        operationElement.setAttribute("errorMsg", "Incorrect parameters in the request");
        return document;
    }
    operationElement.setAttribute("objectname", objectVariable);
    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)) {
        MBeanInfo info = server.getMBeanInfo(name);
        MBeanAttributeInfo[] attributes = info.getAttributes();
        MBeanAttributeInfo targetAttribute = null;
        if (attributes != null) {
            for (int i = 0; i < attributes.length; i++) {
                if (attributes[i].getName().equals(attributeVariable)) {
                    targetAttribute = attributes[i];
                    break;
                }
            }
        }
        if (targetAttribute != null) {
            String type = targetAttribute.getType();
            Object value = null;
            if (valueVariable != null) {
                try {
                    value = CommandProcessorUtil.createParameterValue(type, valueVariable);
                } catch (Exception e) {
                    operationElement.setAttribute("result", "error");
                    operationElement.setAttribute("errorMsg", "Value: " + valueVariable + " could not be converted to " + type);
                }
                if (value != null) {
                    try {
                        server.setAttribute(name, new Attribute(attributeVariable, value));
                        operationElement.setAttribute("result", "success");
                    } catch (Exception e) {
                        operationElement.setAttribute("result", "error");
                        operationElement.setAttribute("errorMsg", e.getMessage());
                    }
                }
            }
        } else {
            operationElement.setAttribute("result", "error");
            operationElement.setAttribute("errorMsg", "Attribute " + attributeVariable + " not found");
        }
    } else {
        if (name != null) {
            operationElement.setAttribute("result", "error");
            operationElement.setAttribute("errorMsg", "MBean " + name + " not registered");
        }
    }
    return document;
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) MalformedObjectNameException(javax.management.MalformedObjectNameException) JMException(javax.management.JMException) IOException(java.io.IOException) ObjectName(javax.management.ObjectName)

Example 19 with JMException

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

the class SetAttributesCommandProcessor method setAttribute.

private Element setAttribute(Document document, String attributeVariable, String valueVariable, ObjectName name) throws JMException {
    Element attributeElement = document.createElement("Attribute");
    attributeElement.setAttribute("attribute", attributeVariable);
    MBeanInfo info = server.getMBeanInfo(name);
    MBeanAttributeInfo[] attributes = info.getAttributes();
    MBeanAttributeInfo targetAttribute = null;
    if (attributes != null) {
        for (int i = 0; i < attributes.length; i++) {
            if (attributes[i].getName().equals(attributeVariable)) {
                targetAttribute = attributes[i];
                break;
            }
        }
    }
    if (targetAttribute != null) {
        String type = targetAttribute.getType();
        Object value = null;
        if (valueVariable != null) {
            try {
                value = CommandProcessorUtil.createParameterValue(type, valueVariable);
            } catch (Exception e) {
                attributeElement.setAttribute("result", "error");
                attributeElement.setAttribute("errorMsg", "Value: " + valueVariable + " could not be converted to " + type);
            }
            if (value != null) {
                try {
                    server.setAttribute(name, new Attribute(attributeVariable, value));
                    attributeElement.setAttribute("result", "success");
                    attributeElement.setAttribute("value", valueVariable);
                } catch (Exception e) {
                    attributeElement.setAttribute("result", "error");
                    attributeElement.setAttribute("errorMsg", e.getMessage());
                }
            }
        }
    } else {
        attributeElement.setAttribute("result", "error");
        attributeElement.setAttribute("errorMsg", "Attribute " + attributeVariable + " not found");
    }
    return attributeElement;
}
Also used : MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) Element(org.w3c.dom.Element) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) IOException(java.io.IOException) MalformedObjectNameException(javax.management.MalformedObjectNameException) JMException(javax.management.JMException)

Example 20 with JMException

use of javax.management.JMException in project opennms by OpenNMS.

the class DetectMBeansJob method execute.

@Override
public JmxDatacollectionConfig execute() throws JobManager.TaskRunException {
    final JmxConnectionConfig connectionConfig = new JmxConnectionConfigBuilder().withUrl(config.getConnection()).withUsername(config.getUser()).withPassword(config.getPassword()).build();
    try (JmxServerConnectionWrapper connector = new DefaultJmxConnector().createConnection(connectionConfig)) {
        final JmxDatacollectionConfiggenerator jmxConfigGenerator = new JmxDatacollectionConfiggenerator(new Slf4jLogAdapter(JmxDatacollectionConfiggenerator.class));
        final JmxDatacollectionConfig generatedJmxConfigModel = jmxConfigGenerator.generateJmxConfigModel(connector.getMBeanServerConnection(), "anyservice", !config.isSkipDefaultVM(), config.isSkipNonNumber(), JmxHelper.loadInternalDictionary());
        applyFilters(generatedJmxConfigModel);
        return generatedJmxConfigModel;
    } catch (IOException | MBeanServerQueryException | JMException | JmxServerConnectionException e) {
        if (e instanceof UnknownHostException || e.getCause() instanceof UnknownHostException) {
            throw new JobManager.TaskRunException(String.format("Unknown host: %s", config.getConnection()), e);
        }
        if (e instanceof MalformedURLException || e.getCause() instanceof MalformedURLException) {
            throw new JobManager.TaskRunException(String.format("Cannot create valid JMX Connection URL. Connection: '%s'", config.getConnection()), e);
        }
        throw new JobManager.TaskRunException("Error while retrieving MBeans from server.", e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) Slf4jLogAdapter(org.opennms.features.jmxconfiggenerator.log.Slf4jLogAdapter) MBeanServerQueryException(org.opennms.features.jmxconfiggenerator.jmxconfig.query.MBeanServerQueryException) JmxDatacollectionConfig(org.opennms.netmgt.config.collectd.jmx.JmxDatacollectionConfig) IOException(java.io.IOException) JmxConnectionConfig(org.opennms.netmgt.jmx.connection.JmxConnectionConfig) JmxServerConnectionException(org.opennms.netmgt.jmx.connection.JmxServerConnectionException) JmxDatacollectionConfiggenerator(org.opennms.features.jmxconfiggenerator.jmxconfig.JmxDatacollectionConfiggenerator) DefaultJmxConnector(org.opennms.netmgt.jmx.impl.connection.connectors.DefaultJmxConnector) JMException(javax.management.JMException) JmxServerConnectionWrapper(org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper) JmxConnectionConfigBuilder(org.opennms.netmgt.jmx.connection.JmxConnectionConfigBuilder)

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