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;
}
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);
}
}
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;
}
Aggregations