Consuming OSGi services from a rule agent

To access an OSGi service from rules, you must create a business object model (BOM) that maps to the Java™ code of the service. You first create a project to contain the BOM, and then you reference the project in the rule agent.

Before you begin

Create an OSGi service in a plug-in project and import the project into the Eclipse workspace that contains your rule agent.
Restriction: OSGi services to be packaged in a solution must be implemented in a plug-in project that is created in the Plug-in Development Environment (PDE). OSGi projects from the Eclipse Web Tools Platform (WTP) are not supported.

About this task

The OSGi service is represented by a verbalized BOM class that corresponds to the Java interface of the OSGi service. This BOM class must contain a custom property named OSGi.service. The value of this property is the fully qualified name of the Java interface. The BOM class can also contain a property to specify the version of the package as defined in the Export-Package header property in the manifest file of the solution.

The BOM class for the OSGi service must be created in a separate rule project because it cannot be hosted by the solution BOM project. This rule project must have a reference to the plug-in project that contains the Java interface. This plug-in project serves as the Execution Object Model (XOM).

To be accessible in rules, a method of the service must have a corresponding method in the BOM class. This corresponding method must be static, and have the same name and the same number of parameters. Each parameter must have a type that corresponds to the Java type of the parameter in the Java interface.

Procedure

  1. Create a rule project:
    1. Click File > New > Other > Insight Designer > Rule Project.
    2. Follow the wizard to create a standard rule project.
    3. On the Rule Project References page, do not select references to other projects.
    4. On the Rule Project XOM Settings page, select the Java project that contains your service, and click Finish. This setting is used at export time to check the validity of the OSGi service API.
    5. In the Solution Explorer, right-click the new rule project and click Properties.
    6. Click Rule Engine, select Decision Engine, and click OK.
      Restriction: The Classic Rule Engine is not supported in Decision Server Insights.
  2. Create a BOM entry:
    1. Right-click the new rule project, and click New > Other > Insight Designer > BOM Entry, and then click Next.
    2. Enter a name for the BOM entry, select Create an empty BOM entry, and click Finish. A BOM entry with a type that represents the OSGi service is generated and displayed in the Solution Explorer.
  3. Edit the new BOM entry:
    1. In the Solution Explorer, double-click the empty BOM type that you just created to open it the BOM editor.
    2. Click New Class to create a new class and enter a name. This class is used to hold properties to map an OSGi service with method definitions.
    3. In the Members section of the Class tab, add a member for each method that is defined in the interface of the OSGi service. The members must have the same name, type, and arguments as defined in the OSGi service.
    4. In the Member tab, create a default verbalization for each member. You might edit the default verbalizations to make them easier to use and read in rules.
    5. Select the Static check-box for each member.
    6. When you select the Static check-box, an error is displayed in the verbalization. To remove the error, delete the {this} placeholder in the member verbalization in the BOM editor.
    7. In the Class tab, expand the Custom Properties section and click Add. You might have to scroll down in the BOM editor to view the Customer properties section.
    8. Enter OSGi.service as the name of the property, and the fully qualified name of the Java type for your OSGi service as the value. You can also add an OSGi.service.version property to refer to a specific version of the service. If you don't specify a version, the most recent version of the service available on the server is used.
      Important: The OSGi.service property is case-sensitive and must be written as shown.

      The OSGi.service and OSGi.service.version properties appear in the Import-Package header in the manifest file of the solution when you export an agent or the solution.

      You might have to scroll down in the BOM editor to view the Custom Properties section.

      In the following example, the fully qualified name of the type is service.ageservice.AgeHelper, and its version is 1.0.0.
      Custom properties section with an OSGi.service property.
  4. Save the changes.
  5. In the rule agent project, add a reference to the rule project that contains the service BOM:
    1. Right-click the agent project, and click Properties.
    2. Click Project References, and select the rule project where you created the service BOM.
    3. Click OK.

Results

A separate business object model is created for the OSGi service along with the vocabulary to use in the rules.

What to do next

In your rule agent, you can write rules that use the OSGi service.