Search in sources :

Example 41 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class BeanMediatorFactoryTest method testCreateSpecificMediator3.

/**
 * Getting the property of BeanMediator and asserting the action.
 *
 * @throws XMLStreamException - XMLStreamException.
 */
@Test
public void testCreateSpecificMediator3() throws XMLStreamException {
    BeanMediatorFactory beanMediatorFactory = new BeanMediatorFactory();
    String inputXML = "<bean action=\"GET_PROPERTY\" property=\"latitude\" value=\"{//latitude}\" var=\"loc\" " + "target=\"target\"   xmlns:ns3=\"http://org.apache.synapse/xsd\"             " + "xmlns:ns=\"http://org.apache.synapse/xsd\"></bean>\n";
    OMElement element = AXIOMUtil.stringToOM(inputXML);
    Mediator mediator = beanMediatorFactory.createSpecificMediator(element, null);
    Assert.assertTrue("BeanMediator is not created successfully.", mediator instanceof BeanMediator);
    BeanMediator beanMediator = (BeanMediator) mediator;
    Assert.assertEquals("GET_PROPERTY action is not set", BeanMediator.Action.GET_PROPERTY, beanMediator.getAction());
}
Also used : OMElement(org.apache.axiom.om.OMElement) Mediator(org.apache.synapse.Mediator) BeanMediator(org.apache.synapse.mediators.bean.BeanMediator) BeanMediator(org.apache.synapse.mediators.bean.BeanMediator) Test(org.junit.Test)

Example 42 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class AbstractListMediatorFactory method addChildren.

protected static void addChildren(OMElement el, ListMediator m, Properties properties) {
    Iterator it = el.getChildren();
    while (it.hasNext()) {
        OMNode child = (OMNode) it.next();
        if (child instanceof OMElement) {
            if (!DESCRIPTION_Q.equals(((OMElement) child).getQName())) {
                // neglect the description tag
                Mediator med = MediatorFactoryFinder.getInstance().getMediator((OMElement) child, properties);
                if (med != null) {
                    m.addChild(med);
                } else {
                    String msg = "Unknown mediator : " + ((OMElement) child).getLocalName();
                    log.error(msg);
                    throw new SynapseException(msg);
                }
            }
        } else if (child instanceof OMComment) {
            CommentMediator commendMediator = new CommentMediator();
            commendMediator.setCommentText(((OMComment) child).getValue());
            m.addChild(commendMediator);
        }
    }
}
Also used : OMNode(org.apache.axiom.om.OMNode) SynapseException(org.apache.synapse.SynapseException) OMComment(org.apache.axiom.om.OMComment) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) Mediator(org.apache.synapse.Mediator) ListMediator(org.apache.synapse.mediators.ListMediator) CommentMediator(org.apache.synapse.mediators.builtin.CommentMediator) CommentMediator(org.apache.synapse.mediators.builtin.CommentMediator)

Example 43 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class AbstractMediatorFactory method createMediator.

/**
 * Creates the mediator by looking at the given XML element. This method handles
 * extracting the common information from the respective element. It delegates the mediator
 * specific building to the {@link #createSpecificMediator(org.apache.axiom.om.OMElement,
 * java.util.Properties)} method, which has tobe implemented by the respective mediators</p>
 *
 * <p>This method has been marked as <code>final</code> to avoid mistakenly overwriting
 * this method instead of the {@link #createSpecificMediator(org.apache.axiom.om.OMElement,
 * java.util.Properties)} by the sub classes
 *
 * @param elem configuration element of the mediator to be built
 * @return built mediator using the above element
 */
public final Mediator createMediator(OMElement elem, Properties properties) {
    Mediator mediator = createSpecificMediator(elem, properties);
    OMElement descElem = elem.getFirstChildWithName(DESCRIPTION_Q);
    if (descElem != null) {
        mediator.setDescription(descElem.getText());
    }
    OMAttribute attDescription = elem.getAttribute(ATT_DESCRIPTION);
    if (attDescription != null) {
        mediator.setShortDescription(attDescription.getAttributeValue());
    }
    return mediator;
}
Also used : Mediator(org.apache.synapse.Mediator) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 44 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class ClassMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    ClassMediator classMediator = new ClassMediator();
    OMAttribute name = elem.getAttribute(ATT_NAME);
    if (name == null) {
        String msg = "The name of the actual mediator class is a required attribute";
        log.error(msg);
        throw new SynapseException(msg);
    }
    Class clazz = null;
    Mediator mediator;
    if (properties != null) {
        // load from synapse libs or dynamic class mediators
        ClassLoader libLoader = (ClassLoader) properties.get(SynapseConstants.SYNAPSE_LIB_LOADER);
        if (libLoader != null) {
            // load from synapse lib
            try {
                clazz = libLoader.loadClass(name.getAttributeValue());
            } catch (ClassNotFoundException e) {
                String msg = "Error loading class : " + name.getAttributeValue() + " from Synapse library";
                log.error(msg, e);
                throw new SynapseException(msg, e);
            }
        } else {
            // load from dynamic class mediators
            Map<String, ClassLoader> dynamicClassMediatorLoaderMap = (Map<String, ClassLoader>) properties.get(SynapseConstants.CLASS_MEDIATOR_LOADERS);
            if (dynamicClassMediatorLoaderMap != null) {
                // Has registered dynamic class mediator loaders in the deployment store.
                // Try to load class from them.
                Iterator<ClassLoader> dynamicClassMediatorLoaders = dynamicClassMediatorLoaderMap.values().iterator();
                while (dynamicClassMediatorLoaders.hasNext()) {
                    try {
                        clazz = dynamicClassMediatorLoaders.next().loadClass(name.getAttributeValue());
                        break;
                    } catch (Exception ignore) {
                    }
                }
            }
        }
    }
    if (clazz == null) {
        try {
            clazz = getClass().getClassLoader().loadClass(name.getAttributeValue());
        } catch (ClassNotFoundException e) {
            String msg = "Error loading class : " + name.getAttributeValue() + " - Class not found";
            log.error(msg, e);
            throw new SynapseException(msg, e);
        }
    }
    try {
        mediator = (Mediator) clazz.newInstance();
    } catch (Throwable e) {
        String msg = "Error in instantiating class : " + name.getAttributeValue();
        log.error(msg, e);
        throw new SynapseException(msg, e);
    }
    classMediator.addAllProperties(MediatorPropertyFactory.getMediatorProperties(elem));
    // after successfully creating the mediator
    // set its common attributes such as tracing etc
    classMediator.setMediator(mediator);
    processAuditStatus(classMediator, elem);
    return classMediator;
}
Also used : ClassMediator(org.apache.synapse.mediators.ext.ClassMediator) SynapseException(org.apache.synapse.SynapseException) SynapseException(org.apache.synapse.SynapseException) Mediator(org.apache.synapse.Mediator) ClassMediator(org.apache.synapse.mediators.ext.ClassMediator) OMAttribute(org.apache.axiom.om.OMAttribute) Map(java.util.Map)

Example 45 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class HandlerUtil method mediateInMessage.

public static boolean mediateInMessage(Log log, MessageContext messageContext, org.apache.synapse.MessageContext synCtx) throws AxisFault {
    AxisService service = messageContext.getAxisService();
    if (service != null) {
        Parameter inMediationParam = service.getParameter(HandlerConstants.IN_SEQUENCE_PARAM_NAME);
        if (inMediationParam != null && inMediationParam.getValue() != null) {
            if (inMediationParam.getValue() instanceof Mediator) {
                Mediator inMessageSequence = (Mediator) inMediationParam.getValue();
                return inMessageSequence.mediate(synCtx);
            } else if (inMediationParam.getValue() instanceof String) {
                Mediator inMessageSequence = synCtx.getConfiguration().getSequence((String) inMediationParam.getValue());
                return inMessageSequence.mediate(synCtx);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("The provided in message mediation " + "sequence is not a proper mediator");
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Couldn't find the incoming mediation for the service " + service.getName());
            }
        }
    } else {
        String message = "Couldn't find the Service for the associated message with id " + messageContext.getMessageID();
        log.error(message);
        throw new AxisFault(message);
    }
    return true;
}
Also used : AxisFault(org.apache.axis2.AxisFault) AxisService(org.apache.axis2.description.AxisService) Parameter(org.apache.axis2.description.Parameter) Mediator(org.apache.synapse.Mediator)

Aggregations

Mediator (org.apache.synapse.Mediator)108 Properties (java.util.Properties)30 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)24 OMElement (org.apache.axiom.om.OMElement)22 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)22 MessageContext (org.apache.synapse.MessageContext)16 SynapseLog (org.apache.synapse.SynapseLog)16 FlowContinuableMediator (org.apache.synapse.mediators.FlowContinuableMediator)13 SynapseException (org.apache.synapse.SynapseException)12 TestMessageContext (org.apache.synapse.TestMessageContext)12 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)12 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)12 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)12 Test (org.junit.Test)10 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)9 SynapseSequenceType (org.apache.synapse.debug.constructs.SynapseSequenceType)8 SequenceMediationFlowPoint (org.apache.synapse.debug.constructs.SequenceMediationFlowPoint)6 ForEachMediatorFactory (org.apache.synapse.config.xml.ForEachMediatorFactory)5 MediatorFactory (org.apache.synapse.config.xml.MediatorFactory)5 MediatorFaultHandler (org.apache.synapse.mediators.MediatorFaultHandler)5