如何使用自己的XML配置实现自定义中介器? [英] How to implement a custom mediator with its own XML configuration?

查看:103
本文介绍了如何使用自己的XML配置实现自定义中介器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自己的XML配置为WSO2 ESB(4.5.1)实现自定义介体.我可以通过以下配置将调解器用作类调解器:

I'm trying to implement a custom mediator for WSO2 ESB (4.5.1) using its own XML configuration. I'm able to use the mediator just fine as a class mediator with the following config:

<class name="test.synapse.mediator.TestMediator"/>

但是,我要实现的目标是能够使用如下语法调用调解器:

However, what I'm trying to achieve is being able to call the mediator with a syntax like this:

<t:TestMediator xmlns:t="test:mediator" />

遵循有关WSO2 ESB的可用帮助之后,在尝试使用具有自己的XML配置的中介程序创建代理时,出现以下错误:

Having followed the available help on the matter for WSO2 ESB to the letter, I'm getting the following error as I try to create a proxy using the mediator with its own XML config:

ERROR - MediatorFactoryFinder Unknown mediator referenced by configuration element : {test:mediator}TestMediator

不用说,我已经编写了两个文本文件,分别包含调解器工厂和序列化器类的完全合格的类名,并将它们放在捆绑jar文件的META-INF/services目录中.

Needless to say, I've written the two text files containing the fully qualified class names of the mediator factory and serializer classes respectively and placed them in the META-INF/services directory in the bundle jar file.

这是我的调解器类的源代码:

This is the source code for my mediator class:

package test.synapse.mediator;

import org.apache.synapse.MessageContext; 
import org.apache.synapse.mediators.AbstractMediator;

public class TestMediator extends AbstractMediator { 

    public boolean mediate(MessageContext context) { 
        System.out.println("TestMediator mediating!"); 
        return true;
    }
}

这是我的调解员工厂的代码:

Here's the code for my mediator factory:

package test.synapse.mediator;

import java.util.Properties;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMElement;
import org.apache.synapse.Mediator;
import org.apache.synapse.config.xml.MediatorFactory;

public class TestMediatorFactory implements MediatorFactory {

    public static final QName QNAME = new QName("test:mediator", "TestMediator");

    @Override
    public Mediator createMediator(OMElement omElement, Properties properties) {
        return new TestMediator();
    }

    @Override
    public QName getTagQName() {
        return QNAME;
    }
}

以下是我的调解员序列化器的代码:

And the following is the code for my mediator serializer:

package test.synapse.mediator;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.synapse.Mediator;
import org.apache.synapse.config.xml.MediatorSerializer;

public class TestMediatorSerializer implements MediatorSerializer {

    public static final String MEDIATOR_CLASS_NAME = TestMediator.class.getName(); 

    @Override
    public String getMediatorClassName() {
        return MEDIATOR_CLASS_NAME;
    }

    @Override
    public OMElement serializeMediator(OMElement parent, Mediator mediator) {
        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMElement element = factory.createOMElement(TestMediatorFactory.QNAME);
        parent.addChild(element);
        return element;
    }
}

最后,该项目的pom.xml文件的内容有些冗长:

And finally, the somewhat lengthy content of the project's pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test.synapse.mediator.TestMediator</groupId>
  <artifactId>TestMediator</artifactId>
  <version>1.0.0</version>
  <packaging>bundle</packaging>
  <name>TestMediator</name>
  <description>TestMediator</description>
  <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>2.3.4</version>
        <extensions>true</extensions>
        <configuration>
          <instructions>
            <Bundle-SymbolicName>TestMediator</Bundle-SymbolicName>
            <Bundle-Name>TestMediator</Bundle-Name>
            <Bundle-ClassPath>.</Bundle-ClassPath>
            <Export-Package>test.synapse.mediator</Export-Package>
            <Import-Package>*; resolution:=optional</Import-Package>
            <Fragment-Host>synapse-core</Fragment-Host>
          </instructions>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.9</version>
        <configuration>
          <buildcommands>
            <buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
          </buildcommands>
          <projectnatures>
            <projectnature>org.wso2.developerstudio.eclipse.artifact.mediator.project.nature</projectnature>
            <projectnature>org.eclipse.jdt.core.javanature</projectnature>
          </projectnatures>
        </configuration>
      </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources/services</directory>
            <targetPath>META-INF/services</targetPath>
        </resource>
    </resources>
  </build>
  <repositories>
    <repository>
      <releases>
        <updatePolicy>daily</updatePolicy>
        <checksumPolicy>ignore</checksumPolicy>
      </releases>
      <id>wso2-nexus</id>
      <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>daily</updatePolicy>
        <checksumPolicy>ignore</checksumPolicy>
      </releases>
      <id>wso2-nexus</id>
      <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
    </pluginRepository>
  </pluginRepositories>
  <dependencies>
    <dependency>
      <groupId>commons-httpclient.wso2</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.1.0.wso2v2</version>
    </dependency>
    <dependency>
      <groupId>commons-codec.wso2</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.4.0.wso2v1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.synapse</groupId>
      <artifactId>synapse-core</artifactId>
      <version>2.1.0-wso2v7</version>
    </dependency>
    <dependency>
      <groupId>wsdl4j.wso2</groupId>
      <artifactId>wsdl4j</artifactId>
      <version>1.6.2.wso2v4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.ws.commons.schema.wso2</groupId>
      <artifactId>XmlSchema</artifactId>
      <version>1.4.7.wso2v2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.abdera.wso2</groupId>
      <artifactId>abdera</artifactId>
      <version>1.0.0.wso2v3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.geronimo.specs.wso2</groupId>
      <artifactId>geronimo-stax-api_1.0_spec</artifactId>
      <version>1.0.1.wso2v2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents.wso2</groupId>
      <artifactId>httpcore</artifactId>
      <version>4.1.0-wso2v1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.neethi.wso2</groupId>
      <artifactId>neethi</artifactId>
      <version>2.0.4.wso2v4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.axis2.wso2</groupId>
      <artifactId>axis2</artifactId>
      <version>1.6.1.wso2v6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.woden.wso2</groupId>
      <artifactId>woden</artifactId>
      <version>1.0.0.M8-wso2v1</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.ws.commons.axiom.wso2</groupId>
      <artifactId>axiom</artifactId>
      <version>1.2.11.wso2v3</version>
    </dependency>
    <dependency>
      <groupId>commons-io.wso2</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.0.0.wso2v2</version>
    </dependency>
  </dependencies>
  <properties>
    <CApp.type>lib/synapse/mediator</CApp.type>
  </properties>
</project>

我已经尝试了很长时间,更改了pom文件和代码的各个方面.我注意到,如果省略配置的Fragment-Host部分,则可以使用class-mediator调用调解器.如果存在Fragment-Host元素,则没有任何调用调解器的方法.

I've been experimenting for a long time changing various aspects of the pom-file and the code. I've come to notice, that I can call the mediator using the class-mediator if I leave out the Fragment-Host part of the configuration. If the Fragment-Host element is present, neither way of calling the mediator works.

按预期,我正在使用apache Maven构建项目的jar文件.我将jar放到<ESB_HOME>/repository/components/dropins-目录.

As expected I'm using apache Maven to build a jar-file of the project. I'm dropping the jar to the <ESB_HOME>/repository/components/dropins-directory.

我尝试使用WSO2 ESB 4.5.1和4.7.0取得完全相同的结果.

I've tried using WSO2 ESB 4.5.1 and 4.7.0 with the exact same results.

要使自定义XML配置生效,必须更改什么?

What must I change to get the custom XML configuration to work?

任何输入将不胜感激!

附件:
Dropbox上的压缩源: TestMediator.zip
在Dropbox上使用Maven构建的jar: TestMediator-1.0.0.jar

Attachments:
Zipped source at Dropbox: TestMediator.zip
Jar built using maven at Dropbox: TestMediator-1.0.0.jar

推荐答案

WSO2 ESB本身似乎存在一些错误,这导致包含介体及其工厂和序列化程序的捆绑包在以下情况下无法加载:它的清单包含一个Fragment-Host定义,我在一个稍微复杂的场景下使用自定义XML配置使介体起作用.

Seeing as there apparently is some bug in the WSO2 ESB itself, which causes the bundle containing the mediator and its factory and serializer not to get loaded in the case that its manifest contains a Fragment-Host definition I went for a slightly more complicated scenario to get my mediator to function using custom XML config.

在捆绑软件中使用了激活器类来确认它没有被加载,这使我想到我也可以使用激活器在ESB中手动注册MediatorFactoryMediatorSerializer类.

Having used an activator class in the bundle to confirm that it doesn't get loaded it occurred to me that I could also use the activator to manually register the MediatorFactory and MediatorSerializer classes in the ESB.

为此,我为OSGI捆绑包编写了以下激活器:

I did this by writing the following activator for my OSGI bundle:

package test;

import java.text.MessageFormat;
import java.util.Map;

import org.apache.synapse.config.xml.MediatorFactoryFinder;
import org.apache.synapse.config.xml.MediatorSerializer;
import org.apache.synapse.config.xml.MediatorSerializerFinder;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import test.synapse.mediator.TestMediator;
import test.synapse.mediator.TestMediatorFactory;
import test.synapse.mediator.TestMediatorSerializer;

public class Activator implements BundleActivator {

    public void start(BundleContext context) throws Exception {
        {
            Map<javax.xml.namespace.QName, java.lang.Class> mediatorFactoryMap = MediatorFactoryFinder.getInstance().getFactoryMap();
            mediatorFactoryMap.put(TestMediatorFactory.QNAME, TestMediatorFactory.class);
        }
        {
            Map<String, MediatorSerializer> mediatorSerializerMap = MediatorSerializerFinder.getInstance().getSerializerMap();
            mediatorSerializerMap.put(TestMediator.class.getName(), TestMediatorSerializer.class.newInstance());
        }
    }

    public void stop(BundleContext context) throws Exception {
        // Maybe undo what was done in the start(BundleContext) method..?
        System.out.println(this.getClass().getName() + ".stop(BundleContext) called");
    }
}

很显然,需要将Activator类定义为捆绑软件的激活器.这是通过将以下节点添加到指示"元素下的pom.xml捆绑插件配置中来完成的:

Obviously the Activator class needs to be defined to be the activator for the bundle. This is done by adding the following node to the pom.xml bundle plugin configuration under the Instructions element:

<Bundle-Activator>test.Activator</Bundle-Activator>

使用这种手动注册工厂和序列化程序类的方式,不需要org.apache.synapse.config.xml.MediatorFactoryorg.apache.synapse.config.xml.MediatorSerializer文件,可以将其从最终的jar中删除.

Using this manual way of registering the factory and serializer classes the org.apache.synapse.config.xml.MediatorFactory and org.apache.synapse.config.xml.MediatorSerializer files are not needed and can be removed from the final jar.

另外,需要从同一父节点中删除Fragment-Host元素,才能真正调用激活程序类的start方法.

Additionally the Fragment-Host element needs to be removed from the same parent node to actually have the activator class' start method get called.

还需要添加包含BundleActivator接口的osgi核心依赖项.

Also the osgi core dependency containing the BundleActivator interface needs to be added.

这样做,我们剩下了以下完整的pom.xml文件:

By doing that we're left with the following complete pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test.synapse.mediator.TestMediator</groupId>
  <artifactId>TestMediator</artifactId>
  <version>1.0.0</version>
  <packaging>bundle</packaging>
  <name>TestMediator</name>
  <description>TestMediator</description>
  <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>2.3.4</version>
        <extensions>true</extensions>
        <configuration>
          <instructions>
            <Bundle-SymbolicName>TestMediator</Bundle-SymbolicName>
            <Bundle-Name>TestMediator</Bundle-Name>
            <Bundle-ClassPath>.</Bundle-ClassPath>
            <Bundle-Activator>test.Activator</Bundle-Activator>
            <Export-Package>test.synapse.mediator</Export-Package>
            <Import-Package>*; resolution:=optional</Import-Package>
            <!-- <Fragment-Host>synapse-core</Fragment-Host> -->
          </instructions>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.9</version>
        <configuration>
          <buildcommands>
            <buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
          </buildcommands>
          <projectnatures>
            <projectnature>org.wso2.developerstudio.eclipse.artifact.mediator.project.nature</projectnature>
            <projectnature>org.eclipse.jdt.core.javanature</projectnature>
          </projectnatures>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <releases>
        <updatePolicy>daily</updatePolicy>
        <checksumPolicy>ignore</checksumPolicy>
      </releases>
      <id>wso2-nexus</id>
      <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>daily</updatePolicy>
        <checksumPolicy>ignore</checksumPolicy>
      </releases>
      <id>wso2-nexus</id>
      <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
    </pluginRepository>
  </pluginRepositories>
  <dependencies>
    <dependency>
      <groupId>commons-httpclient.wso2</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.1.0.wso2v2</version>
    </dependency>
    <dependency>
      <groupId>commons-codec.wso2</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.4.0.wso2v1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.synapse</groupId>
      <artifactId>synapse-core</artifactId>
      <version>2.1.0-wso2v7</version>
    </dependency>
    <dependency>
      <groupId>wsdl4j.wso2</groupId>
      <artifactId>wsdl4j</artifactId>
      <version>1.6.2.wso2v4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.ws.commons.schema.wso2</groupId>
      <artifactId>XmlSchema</artifactId>
      <version>1.4.7.wso2v2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.abdera.wso2</groupId>
      <artifactId>abdera</artifactId>
      <version>1.0.0.wso2v3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.geronimo.specs.wso2</groupId>
      <artifactId>geronimo-stax-api_1.0_spec</artifactId>
      <version>1.0.1.wso2v2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents.wso2</groupId>
      <artifactId>httpcore</artifactId>
      <version>4.1.0-wso2v1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.neethi.wso2</groupId>
      <artifactId>neethi</artifactId>
      <version>2.0.4.wso2v4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.axis2.wso2</groupId>
      <artifactId>axis2</artifactId>
      <version>1.6.1.wso2v6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.woden.wso2</groupId>
      <artifactId>woden</artifactId>
      <version>1.0.0.M8-wso2v1</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.ws.commons.axiom.wso2</groupId>
      <artifactId>axiom</artifactId>
      <version>1.2.11.wso2v3</version>
    </dependency>
    <dependency>
      <groupId>commons-io.wso2</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.0.0.wso2v2</version>
    </dependency>
    <dependency>
      <groupId>org.osgi</groupId>
      <artifactId>org.osgi.core</artifactId>
      <version>5.0.0</version>
    </dependency>
  </dependencies>
  <properties>
    <CApp.type>lib/synapse/mediator</CApp.type>
  </properties>
</project>

完成这些修改并将Maven内置的jar放到/repository/components/dropins目录后,调解器终于可以使用其自定义配置了.

Having done these modifications and dropping the Maven built jar to the /repository/components/dropins directory the mediator finally works with its custom configuration.

我已经压缩了完整的最终项目源代码.该归档文件也可以在Dropbox上找到: TestMediator-final.zip

I've zipped the complete final project source code. That archive also is available on Dropbox: TestMediator-final.zip

修改
经过额外的实验,很明显,以上方法在WSO2 ESB 4.5.1中不起作用,而WSO2 ESB 4.5.1是我最初试图使其工作的平台.该代码按WSO2 4.7.0的预期执行.

Edit
Upon additional experimentation it became apparent that the above approach doesn't work in WSO2 ESB 4.5.1, which is the platform I was originally trying to get this to work on. The code performs as expected in WSO2 4.7.0.

无论如何,我都无法获得WSO2 ESB 4.5.1(或4.6.0)来调用激活程序的start(BundleContext)方法.

I haven't been able to get WSO2 ESB 4.5.1 (or 4.6.0) to call the activator's start(BundleContext) method no matter what I've tried.

这篇关于如何使用自己的XML配置实现自定义中介器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆