Search in sources :

Example 6 with ShellException

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;
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) ShellException(org.neo4j.shell.ShellException)

Example 7 with ShellException

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;
}
Also used : Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) ShellException(org.neo4j.shell.ShellException)

Example 8 with ShellException

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;
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Kernel(org.neo4j.jmx.Kernel) ShellException(org.neo4j.shell.ShellException) JSONException(org.neo4j.shell.util.json.JSONException) RemoteException(java.rmi.RemoteException) ShellException(org.neo4j.shell.ShellException)

Example 9 with ShellException

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;
}
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 10 with ShellException

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;
}
Also used : TabCompletion(org.neo4j.shell.TabCompletion) RemoteException(java.rmi.RemoteException) ShellException(org.neo4j.shell.ShellException)

Aggregations

ShellException (org.neo4j.shell.ShellException)33 RemoteException (java.rmi.RemoteException)12 Node (org.neo4j.graphdb.Node)6 Test (org.junit.Test)5 Relationship (org.neo4j.graphdb.Relationship)4 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)4 GraphDatabaseShellServer (org.neo4j.shell.kernel.GraphDatabaseShellServer)4 Serializable (java.io.Serializable)3 Kernel (org.neo4j.jmx.Kernel)3 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)3 Response (org.neo4j.shell.Response)3 Session (org.neo4j.shell.Session)3 ShellServer (org.neo4j.shell.ShellServer)3 JSONException (org.neo4j.shell.util.json.JSONException)3 ManagementFactory.getPlatformMBeanServer (java.lang.management.ManagementFactory.getPlatformMBeanServer)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 MBeanServer (javax.management.MBeanServer)2 Label (org.neo4j.graphdb.Label)2 ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)2