Search in sources :

Example 1 with EJBMediator

use of org.apache.synapse.mediators.bean.enterprise.EJBMediator in project wso2-synapse by wso2.

the class EJBMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    EJBMediator mediator = new EJBMediator();
    processAuditStatus(mediator, elem);
    String attributeValue;
    attributeValue = elem.getAttributeValue(new QName(EJBConstants.BEANSTALK));
    if (attributeValue != null) {
        mediator.setBeanstalkName(attributeValue.trim());
    } else {
        handleException("'beanstalk' attribute of callEjb mediator is required");
    }
    attributeValue = elem.getAttributeValue(new QName(BeanConstants.CLASS));
    if (attributeValue != null) {
        mediator.setClassName(attributeValue.trim());
    } else {
        handleException("'class' attribute of callEjb mediator is required");
    }
    attributeValue = elem.getAttributeValue(new QName(EJBConstants.STATEFUL));
    if (Boolean.valueOf(attributeValue)) {
        attributeValue = elem.getAttributeValue(new QName(EJBConstants.BEAN_ID));
        if (attributeValue != null) {
            mediator.setBeanId(new ValueFactory().createValue(EJBConstants.BEAN_ID, elem));
        } else {
            handleException("'id' attribute is required for stateful session bean " + "invocations.");
        }
    }
    boolean remove;
    attributeValue = elem.getAttributeValue(new QName(EJBConstants.REMOVE));
    remove = Boolean.valueOf(attributeValue);
    if (remove) {
        mediator.setRemove(true);
    }
    if (elem.getAttributeValue(new QName(BeanConstants.TARGET)) != null) {
        mediator.setTarget(new Target(BeanConstants.TARGET, elem));
    }
    attributeValue = elem.getAttributeValue(new QName(EJBConstants.JNDI_NAME));
    if (attributeValue != null) {
        mediator.setJndiName(attributeValue);
    }
    OMElement argumentsElem = elem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, EJBConstants.ARGS));
    if (argumentsElem != null) {
        Iterator itr = argumentsElem.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, EJBConstants.ARG));
        while (itr.hasNext()) {
            OMElement argElem = (OMElement) itr.next();
            if (argElem.getAttributeValue(ATT_VALUE) != null) {
                mediator.addArgument(new ValueFactory().createValue(BeanConstants.VALUE, argElem));
            } else {
                handleException("'value' attribute of 'arg' element is required.");
            }
        }
    }
    attributeValue = elem.getAttributeValue(new QName(EJBConstants.METHOD));
    if (attributeValue != null) {
        Method method = null;
        try {
            method = BeanUtils.resolveMethod(Class.forName(mediator.getClassName()), attributeValue, mediator.getArgumentList().size());
        } catch (ClassNotFoundException e) {
            handleException("Could not load '" + mediator.getClassName() + "' class.", e);
        }
        mediator.setMethod(method);
    } else if (!remove) {
        handleException("'method' attribute of EJB mediator is optional only when it's a " + "bean removal.");
    }
    return mediator;
}
Also used : Target(org.apache.synapse.mediators.bean.Target) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) Method(java.lang.reflect.Method) EJBMediator(org.apache.synapse.mediators.bean.enterprise.EJBMediator)

Example 2 with EJBMediator

use of org.apache.synapse.mediators.bean.enterprise.EJBMediator in project wso2-synapse by wso2.

the class EJBMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof EJBMediator)) {
        handleException("An unsupported mediator was passed in for serialization : " + m.getType());
        return null;
    }
    EJBMediator mediator = (EJBMediator) m;
    OMElement mediatorElem = fac.createOMElement(EJB, synNS);
    saveTracingState(mediatorElem, mediator);
    if (mediator.getBeanstalkName() != null) {
        mediatorElem.addAttribute(fac.createOMAttribute(EJBConstants.BEANSTALK, nullNS, mediator.getBeanstalkName()));
    } else {
        handleException();
    }
    if (mediator.getClassName() != null) {
        mediatorElem.addAttribute(fac.createOMAttribute(BeanConstants.CLASS, nullNS, mediator.getClassName()));
    } else {
        handleException();
    }
    if (mediator.getBeanId() != null) {
        mediatorElem.addAttribute(fac.createOMAttribute(EJBConstants.STATEFUL, nullNS, Boolean.toString(true)));
        new ValueSerializer().serializeValue(mediator.getBeanId(), EJBConstants.BEAN_ID, mediatorElem);
    }
    if (mediator.getMethod() != null) {
        mediatorElem.addAttribute(fac.createOMAttribute(EJBConstants.METHOD, nullNS, mediator.getMethod().getName()));
    } else if (!mediator.isRemove()) {
        handleException();
    }
    if (mediator.getTarget() != null) {
        mediator.getTarget().serializeTarget(BeanConstants.TARGET, mediatorElem);
    }
    if (mediator.getJndiName() != null) {
        mediatorElem.addAttribute(fac.createOMAttribute(EJBConstants.JNDI_NAME, nullNS, mediator.getJndiName()));
    }
    if (mediator.isRemove()) {
        mediatorElem.addAttribute(fac.createOMAttribute(EJBConstants.REMOVE, nullNS, Boolean.toString(true)));
    }
    List<Value> argList = mediator.getArgumentList();
    if (argList != null && argList.size() > 0) {
        OMElement argumentsElem = fac.createOMElement(EJBConstants.ARGS, synNS);
        for (Value arg : argList) {
            OMElement argElem = fac.createOMElement(EJBConstants.ARG, synNS);
            new ValueSerializer().serializeValue(arg, BeanConstants.VALUE, argElem);
            argumentsElem.addChild(argElem);
        }
        mediatorElem.addChild(argumentsElem);
    }
    return mediatorElem;
}
Also used : Value(org.apache.synapse.mediators.Value) OMElement(org.apache.axiom.om.OMElement) EJBMediator(org.apache.synapse.mediators.bean.enterprise.EJBMediator)

Example 3 with EJBMediator

use of org.apache.synapse.mediators.bean.enterprise.EJBMediator in project wso2-synapse by wso2.

the class EJBMediatorFactoryTest method testCreateSpecificMediator.

/**
 * Test createSpecificMediator method and checked the returned mediator benStalkName.
 *
 * @throws XMLStreamException - XMLStreamException
 */
@Test
public void testCreateSpecificMediator() throws XMLStreamException {
    EJBMediatorFactory factory = new EJBMediatorFactory();
    String inputXML = "<ejb xmlns=\"http://ws.apache.org/ns/synapse\" beanstalk=\"jack\" class=\"org.apache.synapse" + ".mediators.bean.enterprise.EJBMediator\" method=\"setMethod\" target=\"store\" " + "jndiName=\"ejb:/EJBDemo/StoreRegisterBean!org.ejb.wso2.test.StoreRegister\">" + "<args><arg value=\"{get-property('loc_id')}\"/></args></ejb>";
    OMElement inputElement = AXIOMUtil.stringToOM(inputXML);
    Mediator mediator = factory.createSpecificMediator(inputElement, null);
    EJBMediatorSerializer serializer = new EJBMediatorSerializer();
    OMElement outputElement = serializer.serializeSpecificMediator(mediator);
    Assert.assertEquals("EJB Mediator with beanstalk name jack is not returned", "jack", ((EJBMediator) mediator).getBeanstalkName());
    Assert.assertTrue("Input XML and serialized output XMLs are not same", compare(inputElement, outputElement));
}
Also used : OMElement(org.apache.axiom.om.OMElement) Mediator(org.apache.synapse.Mediator) EJBMediator(org.apache.synapse.mediators.bean.enterprise.EJBMediator) Test(org.junit.Test)

Aggregations

OMElement (org.apache.axiom.om.OMElement)3 EJBMediator (org.apache.synapse.mediators.bean.enterprise.EJBMediator)3 Method (java.lang.reflect.Method)1 Iterator (java.util.Iterator)1 QName (javax.xml.namespace.QName)1 Mediator (org.apache.synapse.Mediator)1 Value (org.apache.synapse.mediators.Value)1 Target (org.apache.synapse.mediators.bean.Target)1 Test (org.junit.Test)1