Pattern-type (Part 21): How to call a java method from your topology template

In this article, I will explain how to call a java method from your topology template. This is achieved by creating a class which extends the TemplateTransformer class and declaring it as the implementation class for your topology transformer.
In this example, we will create a method which retrieves an attribute from the component and insert it in the template. I know this can be reach by using $attributes.xxx directly in the template but it is just an example.

Here are the steps:

1) Create the Java class..
2) Create the OSGi service.
3) Update the Manifest.inf
4) Update the vm template.

1. Create the Java class:

You must first create a class in the src directory of your plugin, this class must extend the TemplateTransformer.

public class MyTemplateTransformer extends TemplateTransformer {

	public JSONObject getParms(RequiredMap attributes, String attributeName) {
		JSONObject parms = null;
		if (attributes != null && attributes.get(attributeName) != null
				&& !"".equals(attributes.get(attributeName).toString())) {
			parms = new JSONObject();
			parms.put("MyString", attributes.get(attributeName));
		}
		return parms;
	}

The method getParms will take as parameters the attributes and the name of the attribute to insert. These parameter will be provided during the method call in the vm template.

2. Create the OSGi service:

In the OSGI file you have to specify your own implementation instead of the one provided by maestro “TemplateTransformer”.

xml version=”1.0″ encoding=”UTF-8″?>
xmlns:scr=”http://www.osgi.org/xmlns/scr/v1.1.0″ name=”Master”>
<implementation />
<service>
<provide interface=”com.ibm.maestro.model.transform.TopologyProvider” />
</service>
<property name=”component.template” type=”String” value=”templates/master.vm” />
</scr:component>

3. Expose the Java class:

In is important to export the package containing this class otherwise the deployer engine will be not able to find your class.

Open the manifest.inf file and add the line (or via the Manifest editor):

Export-Package: com.itdove.maestro.model.transform.template

Check also if your OSGi service is defined in the Manifest.inf:

Service-Component: OSGI-INF/master.xml

4. Update the topology template:

Here my master.vm file:

{
    "vm-templates": [
        {
            "persistent":false,                 
            "name": "JavaTemplateTransformerPlugin",
            "roles": [
                {
#set( $parms = $provider.getParms($attribute, "myString"))
					#if ($parms)
					"parms": $parms,
					#end
                    "type": "Master",
                    "name": "Master"
                } 
            ],
            "packages": [
                "JAVATEMPLATETRANSFORMERPLUGIN" 
            ] 
        } 
    ]
}

You can see that the $attributes is sent as parameter as well as the name of the attribute defined in the application model we would like to insert as parms in the template..

Test:

Now, I can test it look at the generated topology in the storehouse and you can see that the parms “MyString” has been added with the value defined in the pattern.

roles”: [

  • {
    • “parms”: {
      • “MyString”: “MyStringValue”

      },

    • “type”: “Master”,
    • “name”: “Master”

    },