use of org.neo4j.shell.ShellException in project neo4j by neo4j.
the class IndexProviderShellApp method exec.
@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
boolean doCd = parser.options().containsKey("cd");
boolean doLs = parser.options().containsKey("ls");
boolean query = parser.options().containsKey("q");
boolean get = parser.options().containsKey("g") || query || doCd || doLs;
boolean index = parser.options().containsKey("i");
boolean remove = parser.options().containsKey("r");
boolean getConfig = parser.options().containsKey("get-config");
boolean create = parser.options().containsKey("create");
boolean setConfig = parser.options().containsKey("set-config");
boolean delete = parser.options().containsKey("delete");
boolean indexes = parser.options().containsKey("indexes");
int count = boolCount(get, index, remove, getConfig, create, setConfig, delete, indexes);
if (count != 1) {
throw new ShellException("Supply one of: -g, -i, -r, --get-config, --set-config, --create, --delete, --indexes");
}
if (get) {
String commandToRun = parser.options().get("c");
Collection<String> commandsToRun = new ArrayList<String>();
boolean specialCommand = false;
if (doCd || doLs) {
specialCommand = true;
if (doCd) {
commandsToRun.add("cd -a $i");
} else if (doLs) {
commandsToRun.add("ls $i");
}
} else if (commandToRun != null) {
commandsToRun.addAll(Arrays.asList(commandToRun.split(Pattern.quote("&&"))));
}
if (getIndex(getIndexName(parser), getEntityType(parser), out) == null) {
return Continuation.INPUT_COMPLETE;
}
IndexHits<PropertyContainer> result = query ? query(parser, out) : get(parser, out);
try {
for (PropertyContainer hit : result) {
printAndInterpretTemplateLines(commandsToRun, false, !specialCommand, NodeOrRelationship.wrap(hit), getServer(), session, out);
}
} finally {
result.close();
}
} else if (index) {
index(parser, session, out);
} else if (remove) {
if (getIndex(getIndexName(parser), Node.class, out) == null) {
return null;
}
remove(parser, session, out);
} else if (getConfig) {
displayConfig(parser, out);
} else if (create) {
createIndex(parser, out);
} else if (setConfig) {
setConfig(parser, out);
} else if (delete) {
deleteIndex(parser, out);
}
if (indexes) {
listIndexes(out);
}
return Continuation.INPUT_COMPLETE;
}
use of org.neo4j.shell.ShellException in project neo4j by neo4j.
the class Rm method exec.
@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws Exception {
NodeOrRelationship thing = getCurrent(session);
boolean forProperty = parser.options().containsKey("p");
boolean forLabel = parser.options().containsKey("l");
if (forProperty || !forLabel) {
// Property
if (parser.arguments().isEmpty()) {
throw new ShellException("Must supply the property key or label name to " + "remove, like: rm title");
}
String key = parser.arguments().get(0);
if (thing.removeProperty(key) == null) {
out.println("Property '" + key + "' not found");
}
} else {
Node node = thing.asNode();
for (Label label : parseLabels(parser)) {
node.removeLabel(label);
}
}
return Continuation.INPUT_COMPLETE;
}
use of org.neo4j.shell.ShellException in project neo4j by neo4j.
the class Dbinfo method getKernel.
private Kernel getKernel() throws ShellException {
GraphDatabaseAPI graphDb = getServer().getDb();
Kernel kernel = null;
if (graphDb instanceof GraphDatabaseAPI) {
try {
kernel = graphDb.getDependencyResolver().resolveDependency(JmxKernelExtension.class).getSingleManagementBean(Kernel.class);
} catch (Exception e) {
// Ignore - the null check does the work
}
}
if (kernel == null) {
throw new ShellException(getName() + " is not available for this graph database.");
}
return kernel;
}
use of org.neo4j.shell.ShellException 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.ShellException in project neo4j by neo4j.
the class ShellTabCompleter method complete.
public int complete(String buffer, int cursor, List candidates) {
if (buffer == null || buffer.length() == 0) {
return cursor;
}
try {
if (buffer.contains(" ")) {
TabCompletion completion = client.getServer().tabComplete(client.getId(), buffer.trim());
cursor = completion.getCursor();
//noinspection unchecked
candidates.addAll(completion.getCandidates());
} else {
// Complete the app name
return getAppNameCompleter().complete(buffer, cursor, candidates);
}
} catch (RemoteException e) {
// TODO Throw something?
e.printStackTrace();
} catch (ShellException e) {
// TODO Throw something?
e.printStackTrace();
}
return cursor;
}
Aggregations