Previous Topic

Next Topic

Book Contents

Book Index

Creating a Condition

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

Extending the Condition Interface

The following example shows the interface of a condition type called DemoCondition which is triggered every time when a certain OSGi bundle starts. Its configuration consists of a single string property that represents the symbolic name of the bundle that when it is started triggers the condition:

package demo.condition.serv;

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

public interface DemoConditionInt extends Condition {

// Condition type constant:

public static final String type = "DemoCondition";

  // Fields and methods for system property handling:

public static final String BUNDLE_NAME = "Bundle symbolic name";

public String getBundleName();

public void setBundleName(String bundlename);

}

Implementing the Condition Interface

Your implementation of a condition must perform the following tasks:

The basic implementation of a condition 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.BaseCondition class.
  2. Implement a constructor calling the first constructor of the super class. This constructor assigns an ID for each condition and sets up a empty config node to hold its configuration.
  3. Implement a constructor calling the second or third constructor of the super class. These constructors create a condition object from an existing config node.
  4. Implement the methods for setting and getting the condition's configuration properties from its config node as you defined them in the condition interface. You can retrieve the node with the  getConfig  method.

The following example shows an implementation of the DemoCondition interface which extends BaseCondition:

package demo.condition.impl;

import demo.condition.serv.DemoConditionInt;

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

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

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

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

public class DemoCondition extends BaseCondition implements DemoConditionInt {

 // First constructor - gives each object a unique ID and creates a new

   config node fori t:

protected DemoCondition(BaseConditionProvider provider) {

   super(provider);

}

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

 protected DemoCondition(BaseConditionProvider provider, ConfigNode config, boolean validate) throws CftException {

  super(provider, config, validate);

}

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

public void setBundleName(String bundlename) { getConfig().setProperty(BUNDLE_NAME, bundlename); } public String getBundleName() {

   return (String) getConfig().getProperty(BUNDLE_NAME);

  }

}