27.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:

import org.neo4j.helpers.Service;
import org.neo4j.shell.kernel.apps.GraphDatabaseApp;

@Service.Implementation( App.class )
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;
    }
}

And you could now use it in the shell by typing lsreltypes (its name is based on the class name) if getName method isn’t overridden.

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.