Freitag, 9. Juli 2010

implementing a new eclipse remote control command

Introduction

eclipse remote control is an eclipse plugin which allows to execute remote commands within eclipse. Right now it's pretty limited to a very few number of commands. Currently you can open a file and launch a build command. Launching commands is done via the java client application.

Implement a command

To create a new command you have to implement a few classes within eclipse remote control. So you have to check out the eclipse remote control source from the github repository: git://github.com/marook/eclipse-remote-control.git

Implementing a new command requires the following steps:

  1. Implement a communication class. This class contains the data which is sent from the eclipse remote control client to the eclipse remote control plugin in the eclipse IDE.
  2. Extend the eclipse remote control. You need to parse the client's command line arguments and create an instance of your communication class.
  3. Implement a command runner. The command runner contains the actual work which is performed when the command is executed within eclipse.

Implement communication class

The communication classes are located in the com.github.marook.eclipse_remote_control.command project. Add a new java class to the com.github.marook.eclipse_remote_control.command.command package. Your new command class must implement the abstract Command java class from the same package.

The communication class must set a unique ID. This ID is used to identifiy commands in the eclipse remote control plugin. The unique ID is passed to the Command constructor.

The communication class contains all the information which is sent from the eclipse remote control client to the eclipse remote control plugin. So the communication class needs to contain fields for all transfered information. Also you have to add getter and setter methods for all the fields.

All communication classes implement the Serializable interface. Make sure your command class and the command class's fields implement the Serializeable requirements.

Extend eclipse remote control client

The client is implemented in the com.github.marook.eclipse_remote_control.client project. The client creates command classes from command line arguments and sends it to the eclipse remote control plugin. To create and send your command class you have to add the parse and send code to the com.github.marook.eclipse_remote_control.client.Client class's main method. The following listing is an example of the parse and send code for the open file command:

if("open_file".equals(command)){
 if(args.length < 2){
  printUsage(System.err);
    
  System.exit(1);
    
  return;
 }
   
 final OpenFileCommand cmd = new OpenFileCommand();
 cmd.setFileName(args[1]);
   
 fireCommand(cmd);
}

Implement command runner

Here comes the actual work. You have to implement a command runner which executes the command within the eclipse instance. All command runners are implemented within the com.github.marook.eclipse_remote_control.run project. Create a new command runner class in the com.github.marook.eclipse_remote_control.run.runner.impl.simple.atom package. All command runnerst must implement the ICommandRunner interface from the same project. For your convenience you should use the AbstractAtomCommandRunner superclass for your command.

The commands's work is implemented in the command runner's internalExecute(...) method. This method is specified by the ICommandRunner interface.

At last you must register the command runner in the SimpleCommandRunner class. Add a putAtomRunner method call to the static block in the SimpleCommandRunner class. Right now this static block contains only two registrations:

static {
 putAtomRunner(new OpenFileCommandRunner());
 putAtomRunner(new ExternalToolsCommandRunner());
}

Basically that's all you need to do for a new command. If you need more information check out the eclipse remote control source code. Read the source from the existing commands. I think this will be the best for getting started.

Keine Kommentare:

Kommentar veröffentlichen