Search in sources :

Example 16 with ShellException

use of org.neo4j.shell.ShellException in project neo4j by neo4j.

the class SimpleAppServer method welcome.

@Override
public Welcome welcome(Map<String, Serializable> initialSession) throws RemoteException, ShellException {
    Serializable clientId = newClientId();
    if (clientSessions.containsKey(clientId)) {
        throw new IllegalStateException("Client " + clientId + " already initialized");
    }
    Session session = newSession(clientId, initialSession);
    clientSessions.put(clientId, session);
    try {
        String message = noWelcome(initialSession) ? "" : getWelcomeMessage();
        return new Welcome(message, clientId, getPrompt(session));
    } catch (ShellException e) {
        throw new RemoteException(e.getMessage());
    }
}
Also used : Serializable(java.io.Serializable) Welcome(org.neo4j.shell.Welcome) RemoteException(java.rmi.RemoteException) ShellException(org.neo4j.shell.ShellException) Session(org.neo4j.shell.Session)

Example 17 with ShellException

use of org.neo4j.shell.ShellException in project neo4j by neo4j.

the class GraphDatabaseShellServer method bindTransaction.

public void bindTransaction(Serializable clientId) throws ShellException {
    KernelTransaction tx = clients.get(clientId);
    if (tx != null) {
        try {
            ThreadToStatementContextBridge threadToStatementContextBridge = getThreadToStatementContextBridge();
            threadToStatementContextBridge.bindTransactionToCurrentThread(tx);
        } catch (Exception e) {
            throw wrapException(e);
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) RemoteException(java.rmi.RemoteException) ShellException(org.neo4j.shell.ShellException)

Example 18 with ShellException

use of org.neo4j.shell.ShellException in project neo4j by neo4j.

the class Cd method completionCandidatesInTx.

@Override
protected List<String> completionCandidatesInTx(String partOfLine, Session session) throws ShellException {
    String lastWord = lastWordOrQuoteOf(partOfLine, false);
    if (lastWord.startsWith("-")) {
        return super.completionCandidates(partOfLine, session);
    }
    NodeOrRelationship current;
    try {
        current = getCurrent(session);
    } catch (ShellException e) {
        return Collections.emptyList();
    }
    TreeSet<String> result = new TreeSet<>();
    if (current.isNode()) {
        // TODO Check if -r is supplied
        Node node = current.asNode();
        for (Node otherNode : RelationshipToNodeIterable.wrap(node.getRelationships(), node)) {
            long otherNodeId = otherNode.getId();
            String title = findTitle(session, otherNode);
            if (title != null) {
                if (!result.contains(title)) {
                    maybeAddCompletionCandidate(result, title + "," + otherNodeId, lastWord);
                }
            }
            maybeAddCompletionCandidate(result, "" + otherNodeId, lastWord);
        }
    } else {
        maybeAddCompletionCandidate(result, START_ALIAS, lastWord);
        maybeAddCompletionCandidate(result, END_ALIAS, lastWord);
        Relationship rel = current.asRelationship();
        maybeAddCompletionCandidate(result, "" + rel.getStartNode().getId(), lastWord);
        maybeAddCompletionCandidate(result, "" + rel.getEndNode().getId(), lastWord);
    }
    return new ArrayList<>(result);
}
Also used : TreeSet(java.util.TreeSet) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) ArrayList(java.util.ArrayList) ShellException(org.neo4j.shell.ShellException)

Example 19 with ShellException

use of org.neo4j.shell.ShellException in project neo4j by neo4j.

the class Cd method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    List<TypedId> paths = readCurrentWorkingDir(session);
    NodeOrRelationship newThing = null;
    if (parser.arguments().isEmpty()) {
        clearCurrent(session);
        writeCurrentWorkingDir(paths, session);
        return Continuation.INPUT_COMPLETE;
    } else {
        NodeOrRelationship current = null;
        try {
            current = getCurrent(session);
        } catch (ShellException e) {
        // Ok, didn't exist
        }
        String arg = parser.arguments().get(0);
        TypedId newId = null;
        if (arg.equals("..")) {
            if (paths.size() > 0) {
                newId = paths.remove(paths.size() - 1);
            }
        } else if (arg.equals(".")) {
        // Do nothing
        } else if (arg.equals(START_ALIAS) || arg.equals(END_ALIAS)) {
            if (current == null) {
                throw new ShellException("Can't do " + START_ALIAS + " or " + END_ALIAS + " on a non-existent relationship");
            }
            newId = getStartOrEnd(current, arg);
            paths.add(current.getTypedId());
        } else {
            long suppliedId = -1;
            try {
                suppliedId = Long.parseLong(arg);
            } catch (NumberFormatException e) {
                if (current != null) {
                    suppliedId = findNodeWithTitle(current.asNode(), arg, session);
                }
                if (suppliedId == -1) {
                    throw new ShellException("No connected node with title '" + arg + "'");
                }
            }
            newId = parser.options().containsKey("r") ? new TypedId(NodeOrRelationship.TYPE_RELATIONSHIP, suppliedId) : new TypedId(NodeOrRelationship.TYPE_NODE, suppliedId);
            if (current != null && newId.equals(current.getTypedId())) {
                throw new ShellException("Can't cd to where you stand");
            }
            boolean absolute = parser.options().containsKey("a");
            if (!absolute && current != null && !isConnected(current, newId)) {
                throw new ShellException(getDisplayName(getServer(), session, newId, false) + " isn't connected to the current primitive," + " use -a to force it to go there anyway");
            }
            if (current != null) {
                paths.add(current.getTypedId());
            }
        }
        newThing = newId != null ? getThingById(newId) : current;
    }
    if (newThing != null) {
        setCurrent(session, newThing);
    } else {
        clearCurrent(session);
    }
    writeCurrentWorkingDir(paths, session);
    return Continuation.INPUT_COMPLETE;
}
Also used : ShellException(org.neo4j.shell.ShellException)

Example 20 with ShellException

use of org.neo4j.shell.ShellException in project neo4j by neo4j.

the class Commit method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    if (parser.getLineWithoutApp().trim().length() > 0) {
        out.println("Error: COMMIT should  be run without trailing arguments");
        return Continuation.INPUT_COMPLETE;
    }
    Integer txCount = session.getCommitCount();
    KernelTransaction tx = Begin.currentTransaction(getServer());
    if (txCount == null || txCount.equals(0)) {
        if (tx != null) {
            out.println("Warning: committing a transaction not started by the shell");
            txCount = 1;
        } else {
            throw new ShellException("Not in a transaction");
        }
    }
    if (txCount.equals(1)) {
        if (tx == null) {
            throw fail(session, "Not in a transaction");
        }
        try {
            tx.success();
            tx.close();
            session.remove(Variables.TX_COUNT);
            out.println("Transaction committed");
            return Continuation.INPUT_COMPLETE;
        } catch (Exception e) {
            throw fail(session, e.getMessage());
        }
    } else {
        session.set(Variables.TX_COUNT, --txCount);
        out.println(String.format("Nested transaction committed (Tx count: %d)", txCount));
        return Continuation.INPUT_COMPLETE;
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ShellException(org.neo4j.shell.ShellException) ShellException(org.neo4j.shell.ShellException) RemoteException(java.rmi.RemoteException)

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