1
votes

I'm trying to implement a custom mediator in wso2 ESB 4.8. I used this article as a reference, and reffered to the docs as well, but couldn't get ESB to recognize my xml configuration for my mediator.

I followed all the steps mentioned in the docs, moved the mediator's project .jar to <ESB_HOME>/ repository/components/lib and restarted the server, but I keep receiving the following error during deployment:

ERROR - MediatorFactoryFinder Unknown mediator referenced by configuration element : {http://ws.apache.org/ns/synapse}currencyMediator 

Here follows the mediator code:

package org.wso2.esb.tutorial.custom;

import java.util.Iterator;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.soap.SOAPBody;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;


public class CurrencyMedXML extends AbstractMediator { 
    private String symbol = "$";

    public boolean mediate(MessageContext synCtx) { 

        // get symbol, last elements of SOAP envelope
        SOAPBody body = synCtx.getEnvelope().getBody();
        OMElement firstElement = body.getFirstElement();

        //Iterator it = firstElement.getChildren();
        Iterator it = firstElement.getChildrenWithName(new QName( "return"));
        while (it.hasNext()) {
            OMNode node = (OMNode)it.next();
            if (node.getType()==OMNode.ELEMENT_NODE) {
                OMElement ele=(OMElement)node;
                String text = ele.getText();
                ele.setText(symbol+text);
            }
        }


        return true;
    }

    public void setSymbol(String sym){
        symbol=sym;
    }

    public String getSymbol(){
        return symbol;
    }
}

The serializer implementation:

package org.wso2.esb.tutorial.custom;

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

public class CurrencyMedXMLSerializer extends AbstractMediatorSerializer {

    public String getMediatorClassName() {
        return CurrencyMedXML.class.getName();
    }

    @Override
    protected OMElement serializeSpecificMediator(Mediator m) {
        if (!(m instanceof CurrencyMedXML)) {
              handleException("Unsupported mediator passed in for serialization : "
                  + m.getType());
        }

        CurrencyMedXML mediator = (CurrencyMedXML) m;
        OMElement CurrencyMediatorElement = fac
            .createOMElement(CurrencyMedXMLFactory.CURRENCY_MEDIATOR_Q);

        saveTracingState(CurrencyMediatorElement, mediator);

        OMElement symbolElement = fac.createOMElement(CurrencyMedXMLFactory.SYMBOL_Q, CurrencyMediatorElement);
        symbolElement.setText(mediator.getSymbol());

        return CurrencyMediatorElement;
    }
}

And the Factory implementation:

package org.wso2.esb.tutorial.custom;

import java.util.Properties;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMElement;
import org.apache.synapse.Mediator;
import org.apache.synapse.SynapseException;
import org.apache.synapse.config.xml.AbstractMediatorFactory;
import org.apache.synapse.config.xml.XMLConfigConstants;

public class CurrencyMedXMLFactory extends AbstractMediatorFactory {

      static final QName CURRENCY_MEDIATOR_Q = new QName(
          XMLConfigConstants.SYNAPSE_NAMESPACE, "currencyMediator");

      static final QName SYMBOL_Q = new QName(
          XMLConfigConstants.SYNAPSE_NAMESPACE, "symbol");

      public QName getTagQName() {
        return CURRENCY_MEDIATOR_Q;
      }

    @Override
    protected Mediator createSpecificMediator(OMElement elem, Properties properties) {
        // create new mediator
          CurrencyMedXML newMediator = new CurrencyMedXML();

        // setup initial settings 
          processAuditStatus(newMediator, elem);

          OMElement symbolElement = elem.getFirstChildWithName(SYMBOL_Q);
          if (symbolElement != null) {
              String symbol = symbolElement.getText();
              newMediator.setSymbol(symbol);
          } else {
              throw new SynapseException("default percentage element missing");
          }

        return newMediator; 
    }
}

Here's a snippet from the proxy service where I reference the custom mediator:

<outSequence>
  <currencyMediator>
    <symbol>$</symbol>
  </currencyMediator>
  <send/>
</outSequence>
2

2 Answers

2
votes

I haven't tried that custom tag method. But just for your information, you can call your class mediator like this too.

<class name="samples.mediators.DiscountQuoteMediator">
    <property name="discountFactor" value="10"/>
    <property name="bonusFor" value="5"/>
</class>
1
votes

I had the same problem when I created my custom mediator with WSO2 Dev Studio. More Information can be found here.

To solve it, I had to build the mediator using maven from command line.

Hope that helps.