Writing a WSO2 ESB Mediator
Mediators provide an easy way of extending the ESB functionalities.Â
There are two ways of writing an ESB mediator:
- Using the Class Mediator - This does not allow mediator specific XML configurations. See Writing Custom Mediator Implementations for more information.
- Writing the mediator with factory and serialize methods - This allows mediator to have its own XML configuration. See Writing Custom Configuration Implementations for Mediators for more information.
The easiest way to write a mediator is to extend your mediator class from the org.apache.synapse.mediators.AbstractMediator
class. For example, you can see the following articles in the WSO2 library:
- Writing a Mediator in WSO2 ESB - Part 1Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
- Writing a Mediator in WSO2 ESB - Part 2
You can use the Class mediator and custom mediators for user-specific custom developments when there is no built-in mediator that already provides the required functionality. However, class and custom mediators incur a high maintenance overhead. Custom mediators in particular might introduce version migration complications when upgrading the ESB. Therefore, avoid using them unless  the scenario is frequently re-used and heavily user-specific. For best results, use WSO2 Developer Studio for debugging Class and custom mediators.
Building the mediator
After you write the mediator, you must build it with WSO2 ESB and make it an OSGI bundle so that it will work with the ESB.
Basic approach
Create a regular JAR that links to the Synpase core JAR and place it in the <ESB_HOME>/
repository/components/lib
directory. The platform will automatically make it an OSGI bundle and deploy it to the server.
Advanced approach
If you want to control the way your mediator is created as an OSGI bundle, you must write the POM files so that you can export and import the packages you need, as shown in the examples below.
Following is a POM file that creates the mediator using Class Mediator.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.test</groupId> <artifactId>org.test</artifactId> <version>1.0.0</version> <packaging>bundle</packaging> <name>My Samples - Test mediator</name> <url>http://www.test.com</url> <repositories> <repository> <id>wso2-maven2-repository</id> <url>http://dist.wso2.org/maven2</url> </repository> <repository> <id>apache-Incubating-repo</id> <name>Maven Incubating Repository</name> <url>http://people.apache.org/repo/m2-incubating-repository</url> </repository> <repository> <id>apache-maven2-repo</id> <name>Apache Maven2 Repository</name> <url>http://repo1.maven.org/maven2/</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>1.4.0</version> <extensions>true</extensions> <configuration> <instructions> <Bundle-SymbolicName>org.test</Bundle-SymbolicName> <Bundle-Name>org.test</Bundle-Name> <Export-Package> org.test.mediator.*, </Export-Package> <Import-Package> *; resolution:=optional </Import-Package> </instructions> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.synapse</groupId> <artifactId>synapse-core</artifactId> <version>2.1.1-wso2v5</version> </dependency> </dependencies> </project>
The Maven bundle plug-in was used for creating the OSGI bundle here. Make sure you export the correct package that contains the mediator code. Otherwise, your mediator will not work.
Following is a POM file that creates the mediator with its own XML configuration using the Serialize
and Factory
classes.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.test</groupId> <artifactId>org.test</artifactId> <version>1.0.0</version> <packaging>bundle</packaging> <name>My Samples - Test mediator</name> <url>http://www.test.com</url> <repositories> <repository> <id>wso2-maven2-repository</id> <url>http://dist.wso2.org/maven2</url> </repository> <repository> <id>apache-Incubating-repo</id> <name>Maven Incubating Repository</name> <url>http://people.apache.org/repo/m2-incubating-repository</url> </repository> <repository> <id>apache-maven2-repo</id> <name>Apache Maven2 Repository</name> <url>http://repo1.maven.org/maven2/</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>1.4.0</version> <extensions>true</extensions> <configuration> <instructions> <Bundle-SymbolicName>org.test</Bundle-SymbolicName> <Bundle-Name>org.test</Bundle-Name> <Export-Package> org.test.mediator.*, </Export-Package> <Import-Package> org.osgi.framework, *; resolution:=optional </Import-Package> <Fragment-Host>synapse-core</Fragment-Host> </instructions> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.synapse</groupId> <artifactId>synapse-core</artifactId> <version>2.1.1-wso2v5</version> </dependency> </dependencies> </project>
In this case, it is necessary to make the mediator an OSGI fragment of the synapse-core bundler. To achieve this, use the <Fragment-Host>synapse-core</Fragment-Host>
.
Create <PROJECT_HOME>/main/resources/META-INF.services/org.apache.synapse.config.xml.MediatorFactory
 and <PROJECT_HOME>/main/resources/META-INF.services/org.apache.synapse.config.xml.MediatorSerializer
 files with the following content as shown below, to add service provider definitions to your Maven project.
org.test.mediator.YourMediatorFactory
org.test.mediator.YourMediatorSerializer
After you create the mediator, place the JAR file in the <ESB_HOME>\repository\components\dropins
 directory.
If you deploy a mediator through a Carbon application (CAR file), it can only be accessed from the sequences and proxy services within the same CAR file.