Search in sources :

Example 76 with JMException

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

the class ScanManager method addDirectoryScanner.

// Creates and registers a new directory scanner.
// Called by applyConfiguration.
// throws IllegalStateException if state is not STOPPED or COMPLETED
// (you cannot change the config while scanning is scheduled or running).
//
private DirectoryScannerMXBean addDirectoryScanner(DirectoryScannerConfig bean) throws JMException {
    try {
        final DirectoryScannerMXBean scanner;
        final ObjectName scanName;
        synchronized (this) {
            if (state != STOPPED && state != COMPLETED)
                throw new IllegalStateException(state.toString());
            scanner = createDirectoryScanner(bean);
            scanName = makeDirectoryScannerName(bean.getName());
        }
        LOG.fine("server: " + mbeanServer);
        LOG.fine("scanner: " + scanner);
        LOG.fine("scanName: " + scanName);
        final ObjectInstance moi = mbeanServer.registerMBean(scanner, scanName);
        final ObjectName moiName = moi.getObjectName();
        final DirectoryScannerMXBean proxy = JMX.newMXBeanProxy(mbeanServer, moiName, DirectoryScannerMXBean.class, true);
        scanmap.put(moiName, proxy);
        return proxy;
    } catch (RuntimeException x) {
        final String msg = "Operation failed: " + x;
        if (LOG.isLoggable(Level.FINEST))
            LOG.log(Level.FINEST, msg, x);
        else
            LOG.fine(msg);
        throw x;
    } catch (JMException x) {
        final String msg = "Operation failed: " + x;
        if (LOG.isLoggable(Level.FINEST))
            LOG.log(Level.FINEST, msg, x);
        else
            LOG.fine(msg);
        throw x;
    }
}
Also used : ObjectInstance(javax.management.ObjectInstance) JMException(javax.management.JMException) ObjectName(javax.management.ObjectName)

Example 77 with JMException

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

the class XMBeanAttributes method doLoadAttributes.

// Don't call this in EDT, but execute returned Runnable inside
// EDT - typically in the done() method of a SwingWorker
// This method can return null.
private Runnable doLoadAttributes(final XMBean mbean, MBeanInfo infoOrNull) throws JMException, IOException {
    if (mbean == null)
        return null;
    final MBeanInfo curMBeanInfo = (infoOrNull == null) ? mbean.getMBeanInfo() : infoOrNull;
    final MBeanAttributeInfo[] attrsInfo = curMBeanInfo.getAttributes();
    final HashMap<String, Object> attrs = new HashMap<String, Object>(attrsInfo.length);
    final HashMap<String, Object> unavailableAttrs = new HashMap<String, Object>(attrsInfo.length);
    final HashMap<String, Object> viewableAttrs = new HashMap<String, Object>(attrsInfo.length);
    AttributeList list = null;
    try {
        list = mbean.getAttributes(attrsInfo);
    } catch (Exception e) {
        if (JConsole.isDebug()) {
            System.err.println("Error calling getAttributes() on MBean \"" + mbean.getObjectName() + "\". JConsole will " + "try to get them individually calling " + "getAttribute() instead. Exception:");
            e.printStackTrace(System.err);
        }
        list = new AttributeList();
        //Can't load all attributes, do it one after each other.
        for (int i = 0; i < attrsInfo.length; i++) {
            String name = null;
            try {
                name = attrsInfo[i].getName();
                Object value = mbean.getMBeanServerConnection().getAttribute(mbean.getObjectName(), name);
                list.add(new Attribute(name, value));
            } catch (Exception ex) {
                if (attrsInfo[i].isReadable()) {
                    unavailableAttrs.put(name, Utils.getActualException(ex).toString());
                }
            }
        }
    }
    try {
        int att_length = list.size();
        for (int i = 0; i < att_length; i++) {
            Attribute attribute = (Attribute) list.get(i);
            if (isViewable(attribute)) {
                viewableAttrs.put(attribute.getName(), attribute.getValue());
            } else
                attrs.put(attribute.getName(), attribute.getValue());
        }
        // check them one after the other.
        if (att_length < attrsInfo.length) {
            for (int i = 0; i < attrsInfo.length; i++) {
                MBeanAttributeInfo attributeInfo = attrsInfo[i];
                if (!attrs.containsKey(attributeInfo.getName()) && !viewableAttrs.containsKey(attributeInfo.getName()) && !unavailableAttrs.containsKey(attributeInfo.getName())) {
                    if (attributeInfo.isReadable()) {
                        // went wrong.
                        try {
                            Object v = mbean.getMBeanServerConnection().getAttribute(mbean.getObjectName(), attributeInfo.getName());
                            //What happens if now it is ok?
                            // Be pragmatic, add it to readable...
                            attrs.put(attributeInfo.getName(), v);
                        } catch (Exception e) {
                            //Put the exception that will be displayed
                            // in tooltip
                            unavailableAttrs.put(attributeInfo.getName(), Utils.getActualException(e).toString());
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        //sets all attributes unavailable except the writable ones
        for (int i = 0; i < attrsInfo.length; i++) {
            MBeanAttributeInfo attributeInfo = attrsInfo[i];
            if (attributeInfo.isReadable()) {
                unavailableAttrs.put(attributeInfo.getName(), Utils.getActualException(e).toString());
            }
        }
    }
    //one update at a time
    return new Runnable() {

        public void run() {
            synchronized (XMBeanAttributes.this) {
                XMBeanAttributes.this.mbean = mbean;
                XMBeanAttributes.this.mbeanInfo = curMBeanInfo;
                XMBeanAttributes.this.attributesInfo = attrsInfo;
                XMBeanAttributes.this.attributes = attrs;
                XMBeanAttributes.this.unavailableAttributes = unavailableAttrs;
                XMBeanAttributes.this.viewableAttributes = viewableAttrs;
                DefaultTableModel tableModel = (DefaultTableModel) getModel();
                // add attribute information
                emptyTable(tableModel);
                addTableData(tableModel, mbean, attrsInfo, attrs, unavailableAttrs, viewableAttrs);
                // update the model with the new data
                tableModel.newDataAvailable(new TableModelEvent(tableModel));
                // re-register for change events
                tableModel.addTableModelListener(attributesListener);
            }
        }
    };
}
Also used : MBeanInfo(javax.management.MBeanInfo) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) TableModelEvent(javax.swing.event.TableModelEvent) DefaultTableModel(javax.swing.table.DefaultTableModel) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JMException(javax.management.JMException) EventObject(java.util.EventObject)

Example 78 with JMException

use of javax.management.JMException in project tdi-studio-se by Talend.

the class MBeanServer method setAttribute.

/*
     * @see IMBeanServer#setAttribute(ObjectName, Attribute)
     */
@Override
public void setAttribute(ObjectName objectName, Attribute attribute) throws JvmCoreException {
    Assert.isNotNull(objectName);
    Assert.isNotNull(attribute);
    if (!checkReachability()) {
        return;
    }
    try {
        connection.setAttribute(objectName, attribute);
    } catch (JMException e) {
        throw new JvmCoreException(IStatus.ERROR, NLS.bind(Messages.setAttributeFailedMsg, attribute.getName()), e);
    } catch (IOException e) {
        throw new JvmCoreException(IStatus.ERROR, NLS.bind(Messages.setAttributeFailedMsg, attribute.getName()), e);
    }
}
Also used : JMException(javax.management.JMException) IOException(java.io.IOException) JvmCoreException(org.talend.designer.runtime.visualization.JvmCoreException)

Example 79 with JMException

use of javax.management.JMException in project tdi-studio-se by Talend.

the class MBeanServer method invoke.

/*
     * @see IMBeanServer#invoke(ObjectName, String, String[], String[])
     */
@Override
public Object invoke(ObjectName objectName, String method, Object[] params, String[] signatures) throws JvmCoreException {
    Assert.isNotNull(objectName);
    Assert.isNotNull(method);
    if (!checkReachability()) {
        return null;
    }
    try {
        return connection.invoke(objectName, method, params, signatures);
    } catch (JMException e) {
        throw new JvmCoreException(IStatus.ERROR, NLS.bind(Messages.mBeanOperationFailedMsg, method), e);
    } catch (IOException e) {
        throw new JvmCoreException(IStatus.ERROR, NLS.bind(Messages.mBeanOperationFailedMsg, method), e);
    }
}
Also used : JMException(javax.management.JMException) IOException(java.io.IOException) JvmCoreException(org.talend.designer.runtime.visualization.JvmCoreException)

Example 80 with JMException

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

the class InvokeOperationCommandProcessor 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", "invoke");
    root.appendChild(operationElement);
    String objectVariable = in.getVariable("objectname");
    String operationVariable = in.getVariable("operation");
    if (objectVariable == null || objectVariable.equals("") || operationVariable == null || operationVariable.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("") || operationVariable == null || operationVariable.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)) {
        MBeanInfo info = server.getMBeanInfo(name);
        MBeanOperationInfo[] operations = info.getOperations();
        boolean match = false;
        if (operations != null) {
            for (int j = 0; j < operations.length; j++) {
                if (operations[j].getName().equals(operationVariable)) {
                    MBeanParameterInfo[] parameters = operations[j].getSignature();
                    if (parameters.length != types.size()) {
                        continue;
                    }
                    Iterator k = types.iterator();
                    boolean signatureMatch = true;
                    for (int p = 0; p < types.size(); p++) {
                        if (!parameters[p].getType().equals(k.next())) {
                            signatureMatch = false;
                            break;
                        }
                    }
                    match = signatureMatch;
                }
                if (match) {
                    break;
                }
            }
        }
        if (!match) {
            operationElement.setAttribute("result", "error");
            operationElement.setAttribute("errorMsg", "Operation singature has no match in the MBean");
        } else {
            try {
                Object[] params = values.toArray();
                String[] signature = new String[types.size()];
                types.toArray(signature);
                Object returnValue = server.invoke(name, operationVariable, params, signature);
                operationElement.setAttribute("result", "success");
                if (returnValue != null) {
                    operationElement.setAttribute("returnclass", returnValue.getClass().getName());
                    operationElement.setAttribute("return", returnValue.toString());
                } else {
                    operationElement.setAttribute("returnclass", null);
                    operationElement.setAttribute("return", null);
                }
            } catch (Exception e) {
                operationElement.setAttribute("result", "error");
                operationElement.setAttribute("errorMsg", e.getMessage());
            }
        }
    } else {
        if (name != null) {
            operationElement.setAttribute("result", "error");
            operationElement.setAttribute("errorMsg", new StringBuffer("MBean ").append(name).append(" not registered").toString());
        }
    }
    return document;
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) 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) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) MBeanParameterInfo(javax.management.MBeanParameterInfo)

Aggregations

JMException (javax.management.JMException)116 ObjectName (javax.management.ObjectName)66 MBeanServer (javax.management.MBeanServer)33 IOException (java.io.IOException)22 InstrumentationManager (org.apache.cxf.management.InstrumentationManager)13 MBeanInfo (javax.management.MBeanInfo)11 MalformedObjectNameException (javax.management.MalformedObjectNameException)11 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)9 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 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)4