Prev |  | Next

## 10.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;
        }
    }

You make your app discoverable via the Java Service API, so in a file e.g. src/main/resources/META-INF/services/org.neo4j.shell.App include: `org.my.domain.MyShellApp`

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.

Copyright © 2011 Neo Technology

Prev | Up | Next
| Home |