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