Search in sources :

Example 1 with JSONObject

use of org.neo4j.shell.util.json.JSONObject in project neo4j by neo4j.

the class Dbinfo method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws Exception {
    Kernel kernel = getKernel();
    boolean list = parser.options().containsKey("l"), get = parser.options().containsKey("g");
    if ((list && get) || (!list && !get)) {
        StringBuilder usage = new StringBuilder();
        getUsage(usage);
        usage.append(".\n");
        out.print(usage.toString());
        return Continuation.INPUT_COMPLETE;
    }
    MBeanServer mbeans = getPlatformMBeanServer();
    String bean = null;
    String[] attributes = null;
    if (list) {
        bean = parser.options().get("l");
    } else if (get) {
        bean = parser.options().get("g");
        attributes = parser.arguments().toArray(new String[parser.arguments().size()]);
    }
    if (// list beans
    bean == null) {
        StringBuilder result = new StringBuilder();
        availableBeans(mbeans, kernel, result);
        out.print(result.toString());
        return Continuation.INPUT_COMPLETE;
    }
    ObjectName mbean;
    {
        mbean = kernel.getMBeanQuery();
        Hashtable<String, String> properties = new Hashtable<String, String>(mbean.getKeyPropertyList());
        properties.put("name", bean);
        try {
            Iterator<ObjectName> names = mbeans.queryNames(new ObjectName(mbean.getDomain(), properties), null).iterator();
            if (names.hasNext()) {
                mbean = names.next();
                if (names.hasNext()) {
                    mbean = null;
                }
            } else {
                mbean = null;
            }
        } catch (Exception e) {
            mbean = null;
        }
    }
    if (mbean == null) {
        throw new ShellException("No such management bean \"" + bean + "\".");
    }
    if (// list attributes
    attributes == null) {
        for (MBeanAttributeInfo attr : mbeans.getMBeanInfo(mbean).getAttributes()) {
            out.println(attr.getName() + " - " + attr.getDescription());
        }
    } else {
        if (// specify all attributes
        attributes.length == 0) {
            MBeanAttributeInfo[] allAttributes = mbeans.getMBeanInfo(mbean).getAttributes();
            attributes = new String[allAttributes.length];
            for (int i = 0; i < allAttributes.length; i++) {
                attributes[i] = allAttributes[i].getName();
            }
        }
        JSONObject json = new JSONObject();
        for (Object value : mbeans.getAttributes(mbean, attributes)) {
            printAttribute(json, value);
        }
        out.println(json.toString(2));
    }
    return Continuation.INPUT_COMPLETE;
}
Also used : Hashtable(java.util.Hashtable) ShellException(org.neo4j.shell.ShellException) JSONException(org.neo4j.shell.util.json.JSONException) RemoteException(java.rmi.RemoteException) ShellException(org.neo4j.shell.ShellException) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) JSONObject(org.neo4j.shell.util.json.JSONObject) Iterator(java.util.Iterator) JSONObject(org.neo4j.shell.util.json.JSONObject) Kernel(org.neo4j.jmx.Kernel) ManagementFactory.getPlatformMBeanServer(java.lang.management.ManagementFactory.getPlatformMBeanServer) MBeanServer(javax.management.MBeanServer)

Example 2 with JSONObject

use of org.neo4j.shell.util.json.JSONObject in project neo4j by neo4j.

the class Dbinfo method printAttribute.

private void printAttribute(JSONObject json, Object value) throws RemoteException, ShellException {
    try {
        Attribute attribute = (Attribute) value;
        Object attributeValue = attribute.getValue();
        if (attributeValue != null && attributeValue.getClass().isArray()) {
            Object[] arrayValue = (Object[]) attributeValue;
            JSONArray array = new JSONArray();
            for (Object item : (Object[]) arrayValue) {
                if (item instanceof CompositeData) {
                    array.put(compositeDataAsMap((CompositeData) item));
                } else {
                    array.put(item.toString());
                }
            }
            json.put(attribute.getName(), array);
        } else {
            json.put(attribute.getName(), attributeValue);
        }
    } catch (JSONException e) {
        throw ShellException.wrapCause(e);
    }
}
Also used : Attribute(javax.management.Attribute) CompositeData(javax.management.openmbean.CompositeData) JSONArray(org.neo4j.shell.util.json.JSONArray) JSONException(org.neo4j.shell.util.json.JSONException) JSONObject(org.neo4j.shell.util.json.JSONObject)

Example 3 with JSONObject

use of org.neo4j.shell.util.json.JSONObject in project neo4j by neo4j.

the class AbstractApp method parseJSONMap.

protected static Map<String, Object> parseJSONMap(String jsonString) throws JSONException {
    JSONObject object = new JSONObject(jsonString);
    Map<String, Object> result = new HashMap<>();
    for (String name : JSONObject.getNames(object)) {
        Object value = object.get(name);
        if (value != null && value instanceof String && ((String) value).length() == 0) {
            value = null;
        }
        result.put(name, value);
    }
    return result;
}
Also used : JSONObject(org.neo4j.shell.util.json.JSONObject) HashMap(java.util.HashMap) JSONObject(org.neo4j.shell.util.json.JSONObject)

Aggregations

JSONObject (org.neo4j.shell.util.json.JSONObject)3 JSONException (org.neo4j.shell.util.json.JSONException)2 ManagementFactory.getPlatformMBeanServer (java.lang.management.ManagementFactory.getPlatformMBeanServer)1 RemoteException (java.rmi.RemoteException)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 Attribute (javax.management.Attribute)1 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 CompositeData (javax.management.openmbean.CompositeData)1 Kernel (org.neo4j.jmx.Kernel)1 ShellException (org.neo4j.shell.ShellException)1 JSONArray (org.neo4j.shell.util.json.JSONArray)1