Database Tutorial Command Provider

From RifidiWiki

Jump to: navigation, search

In order to control the database application, you can use a CommandProvider, which is a class that plugs into the eclipse osgi console and allows you to provide custom commands.

Implement the CommandProvider interface

The org.eclipse.osgi.framework.console.CommandProvider interface allows you to write methods which are exposed as commands in the eclipse console. By convention, any methods that you want to expose should start with an underscore (i.e. '_'), should have a return type of Object and should take in a CommandInterpreter (which allows you to get input and write output to the eclipse console ).

	/**
	 * Start the application
	 * 
	 * @param intp
	 * @return
	 */
	public Object _dbstart(CommandInterpreter intp) {
		app.start();
		return null;
	}

	/**
	 * Stop the application
	 * 
	 * @param intp
	 * @return
	 */
	public Object _dbstop(CommandInterpreter intp) {
		app.stop();
		return null;
	}

Dependency Injection

Like the other objects we have create so far, we are going to use dependency injection to give the CommandProvider access to the DBApp object.

	/**
	 * Called by spring
	 * 
	 * @param app
	 *            the app to set
	 */
	public void setApp(DBApp app) {
		this.app = app;
	}

Extra Credit

Here are some extra things that could be added to this class to extend functionality.

A command that lists the last five tag reads

Right now the console only allows you to start and stop the application. However, it could provide richer functionality. For example, suppose you wrote a method like this:

public Object _lastFiveTagReads(CommandInterpreter intp)

This method might print out the last five tag reads seen by the reader.

Control the DBApp with RMI instead of the console

The Eclipse Console is one way to control the DB Application, but it has its pitfalls. For example, if the edge server is started up as a background service, you won't have access to the console. In this case, you may want a more robust way to control the application. You could do this any number of ways, one of which would be to use RMI (Remote Method Invocation). This would allow clients in the network to issue commands to your application.

Personal tools