7.7. Extending the shell: Adding your own commands

Of course the shell is extendable and has a generic core which has nothing to do with Neo4j… only some of the commands do.

So you say you’d like to start a Neo4j graph database, enable the remote shell and add your own apps to it so that your apps and the standard Neo4j apps co-exist side by side? Well, here’s an example of how an app could look like:

public class LsRelTypes extends GraphDatabaseApp
{
    @Override
    protected String exec( AppCommandParser parser, Session session, Output out )
            throws ShellException, RemoteException
    {
        GraphDatabaseService graphDb = getServer().getDb();
        out.println( "Types:" );
        for ( RelationshipType type : graphDb.getRelationshipTypes() )
        {
            out.println( type.name() );
        }
        return null;
    }
}

To start up a shell with some additional apps in it you can do like this:

GraphDatabaseService graphDb = new EmbeddedGraphDatabase( "my/path" );

// IMPORTANT: Don't enable the remote shell in the normal way
// graphDb.enableRemoteShell();

// Instead instantiate your own and add apps to it
GraphDatabaseShellServer shellServer = new GraphDatabaseShellServer( graphDb );
shellServer.addApp( LsRelTypes.class );
shellServer.makeRemotelyAvailable( AbstractServer.DEFAULT_PORT,
        AbstractServer.DEFAULT_NAME );

And you could now use it in the shell by typing lsreltypes (its name is based on the class name).

If you’d like it to display some nice help information when using the help (or man) app, override the getDescription method for a general description and use addValueType method to add descriptions about (and logic to) the options you can supply when using your app.

Know that the apps reside server-side so if you have a running server and starts a remote client to it from another JVM you can’t add your apps on the client.