Previous Topic

Next Topic

Book Contents

Book Index

Creating a Command

Extend the com.prosyst.mbs.services.ham.Command interface and then implement it.

Extending the Command Interface

The following example shows the interface of a command type called DemoCommand whose configuration consists of a single integer property:

package demo.command.serv;

import com.prosyst.mbs.services.ham.Command;

public interface DemoCommandInt extends Command {

        // Command type constant:

  public static final String TYPE = "DemoCommand";

        // Fields and methods for property handling:

  public static final String PROP_1 = "property";

  public int getProp();

  public void setProp(int value);

}  

Implementing the Command Interface

Your implementation of a command must perform the following tasks:

The basic implementation of command provider performs the first two of these tasks for you. If you are using it, proceed in the following way:

  1. Extend the com.prosyst.mbs.services.ham.util.BaseCommand class.
  2. Implement a constructor calling the first constructor of the super class. This constructor assigns an ID to each command and sets up an empty config node for its configuration.
  3. Implement a constructor calling the second or third constructor of the super class. These constructors create a command object from an existing config node.
  4. Implement the methods for setting and getting the command's configuration properties from its config node as you defined them in the command interface. You can retrieve the node with the getConfig method.

If you are using base command, you do not need to implement the start(Map) method of the command but runCommand(BaseCommand, Map) of the associated base command provider.

The following example shows an implementation of the DemoCommand interface which extends BaseCommand:

package demo.command.impl;

import com.prosyst.mbs.services.configtree.CftException;

import com.prosyst.mbs.services.configtree.ConfigNode;

import com.prosyst.mbs.services.ham.util.BaseCommand;

import demo.command.serv.*;

public class DemoCommand extends BaseCommand implements DemoCommandInt {

  // First constructor - gives each object a unique ID and creates a config node for it:

  protected DemoCommand(DemoCommandProvider provider) {

    super(provider);}

  // Second constructor - creates a  new command object from an existing Config Tree node

  protected DemoCommand(DemoCommandProvider provider, ConfigNode cmdNode) throws CftException {

    super(provider, cmdNode);

  }

  // Properties are written directly into the command's node:

  public void setProp(int value) {

    getConfig().setProperty(PROP_1, value);

  }

  public int getProp() {

    return (Integer) getConfig().getProperty(PROP_1);

  }

}