Search in sources :

Example 31 with SynapseLog

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

the class IterateMediator method mediate.

public boolean mediate(MessageContext synCtx, ContinuationState continuationState) {
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Iterate mediator : Mediating from ContinuationState");
    }
    boolean result;
    SequenceMediator branchSequence = target.getSequence();
    boolean isStatisticsEnabled = RuntimeStatisticCollector.isStatisticsEnabled();
    if (!continuationState.hasChild()) {
        result = branchSequence.mediate(synCtx, continuationState.getPosition() + 1);
    } else {
        FlowContinuableMediator mediator = (FlowContinuableMediator) branchSequence.getChild(continuationState.getPosition());
        result = mediator.mediate(synCtx, continuationState.getChildContState());
        if (isStatisticsEnabled) {
            ((Mediator) mediator).reportCloseStatistics(synCtx, null);
        }
    }
    if (isStatisticsEnabled) {
        branchSequence.reportCloseStatistics(synCtx, null);
    }
    return result;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 32 with SynapseLog

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

the class EnrichMediator method mediate.

public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Enrich mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    ArrayList<OMNode> sourceNodeList;
    try {
        sourceNodeList = source.evaluate(synCtx, synLog);
        if (sourceNodeList == null) {
            handleException("Failed to get the source for Enriching : ", synCtx);
        } else {
            target.insert(synCtx, sourceNodeList, synLog);
        }
    } catch (JaxenException e) {
        handleException("Failed to get the source for Enriching", e, synCtx);
    }
    // If enrich mediator modifies JSON payload update JSON stream in the axis2MessageContext
    org.apache.axis2.context.MessageContext axis2MsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    if (JsonUtil.hasAJsonPayload(axis2MsgCtx)) {
        JsonUtil.setJsonStream(axis2MsgCtx, JsonUtil.toJsonStream(axis2MsgCtx.getEnvelope().getBody().getFirstElement()));
    }
    synLog.traceOrDebug("End : Enrich mediator");
    return true;
}
Also used : OMNode(org.apache.axiom.om.OMNode) SynapseLog(org.apache.synapse.SynapseLog) JaxenException(org.jaxen.JaxenException) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 33 with SynapseLog

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

the class AnnotatedCommandMediator method mediate.

@Override
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : POJOCommand mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Creating a new instance of POJO class : " + getCommand().getClass());
    }
    Object commandObject = null;
    try {
        // instantiate a new command object each time
        commandObject = getCommand().newInstance();
    } catch (Exception e) {
        handleException("Error creating an instance of the POJO command class : " + getCommand().getClass(), e, synCtx);
    }
    synLog.traceOrDebug("Instance created, setting static and dynamic properties");
    // then set the static/constant properties first
    for (Iterator iter = getStaticSetterProperties().keySet().iterator(); iter.hasNext(); ) {
        String name = (String) iter.next();
        PropertyHelper.setInstanceProperty(name, getStaticSetterProperties().get(name), commandObject);
    }
    for (Field f : beforeFields.keySet()) {
        SynapseXPath xpath = beforeFields.get(f);
        Object v;
        if (f.getType().equals(String.class)) {
            v = xpath.stringValueOf(synCtx);
        } else {
            throw new UnsupportedOperationException("non-String types not supportted yet");
        }
        try {
            f.set(commandObject, v);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    for (Method m : beforeMethods.keySet()) {
        SynapseXPath xpath = beforeMethods.get(m);
        Object v;
        if (m.getParameterTypes().length == 1 && m.getParameterTypes()[0].equals(String.class)) {
            v = xpath.stringValueOf(synCtx);
        } else {
            throw new UnsupportedOperationException("non-String types not supportted yet");
        }
        try {
            m.invoke(commandObject, v);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    synLog.traceOrDebug("POJO initialized successfully, invoking the execute() method");
    // then call the execute method if the Command interface is implemented
    if (commandObject instanceof Command) {
        try {
            ((Command) commandObject).execute();
        } catch (Exception e) {
            handleException("Error invoking POJO command class : " + getCommand().getClass(), e, synCtx);
        }
    } else {
        Method exeMethod = null;
        try {
            exeMethod = getCommand().getMethod("execute", new Class[] {});
            exeMethod.invoke(commandObject, new Object[] {});
        } catch (NoSuchMethodException e) {
            handleException("Cannot locate an execute() method on POJO class : " + getCommand().getClass(), e, synCtx);
        } catch (Exception e) {
            handleException("Error invoking the execute() method on POJO class : " + getCommand().getClass(), e, synCtx);
        }
    }
    // TODO: now update the MessageContext from the commandObject
    synLog.traceOrDebug("End : POJOCommand mediator");
    return true;
}
Also used : Field(java.lang.reflect.Field) SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) SynapseLog(org.apache.synapse.SynapseLog) Command(org.apache.synapse.Command) Iterator(java.util.Iterator) Method(java.lang.reflect.Method) JaxenException(org.jaxen.JaxenException)

Example 34 with SynapseLog

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

the class ClassMediator method mediate.

/**
 * Don't use a new instance... do one instance of the object per instance of
 * this mediator
 *
 * @param synCtx
 *            the message context
 * @return as per standard semantics
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Class mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("invoking : " + mediator.getClass() + ".mediate()");
    }
    boolean result;
    try {
        for (MediatorProperty property : properties) {
            String propertyValue = property.getValue() != null ? property.getValue() : property.getEvaluatedExpression(synCtx);
            PropertyHelper.setInstanceProperty(property.getName(), propertyValue, mediator);
        }
        result = mediator.mediate(synCtx);
    } catch (Exception e) {
        // so that the fault handler will be invoked
        throw new SynapseException("Error occured in the mediation of the class mediator", e);
    }
    synLog.traceOrDebug("End : Class mediator");
    return result;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) SynapseException(org.apache.synapse.SynapseException) SynapseException(org.apache.synapse.SynapseException)

Example 35 with SynapseLog

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

the class POJOCommandMediator method mediate.

/**
 * Implements the mediate method of the Mediator interface. This method will instantiate
 * a new instance of the POJO class, set all specified properties from the current runtime
 * state (and message context) and call the execute method of the Command object.
 *
 * @param synCtx - Synapse MessageContext to be mediated
 * @return boolean true since this will not stop exection chain
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : POJOCommand mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Creating a new instance of POJO class : " + command.getClass());
    }
    Object commandObject = null;
    try {
        // instantiate a new command object each time
        commandObject = command.newInstance();
    } catch (Exception e) {
        handleException("Error creating an instance of the POJO command class : " + command.getClass(), e, synCtx);
    }
    synLog.traceOrDebug("Instance created, setting static and dynamic properties");
    // then set the static/constant properties first
    for (String name : staticSetterProperties.keySet()) {
        PropertyHelper.setInstanceProperty(name, staticSetterProperties.get(name), commandObject);
    }
    // now set the any dynamic properties from the message context properties
    for (String name : contextSetterProperties.keySet()) {
        PropertyHelper.setInstanceProperty(name, synCtx.getProperty(contextSetterProperties.get(name)), commandObject);
    }
    // now set the any dynamic properties evaluating XPath's on the current message
    for (String name : messageSetterProperties.keySet()) {
        SynapseXPath xpath = messageSetterProperties.get(name);
        String value = xpath.stringValueOf(synCtx);
        PropertyHelper.setInstanceProperty(name, value, commandObject);
    }
    synLog.traceOrDebug("POJO initialized successfully, invoking the execute() method");
    // then call the execute method if the Command interface is implemented
    if (commandObject instanceof Command) {
        try {
            ((Command) commandObject).execute();
        } catch (Exception e) {
            handleException("Error invoking POJO command class : " + command.getClass(), e, synCtx);
        }
    } else {
        try {
            Method exeMethod = command.getMethod("execute");
            exeMethod.invoke(commandObject);
        } catch (NoSuchMethodException e) {
            handleException("Cannot locate an execute() method on POJO class : " + command.getClass(), e, synCtx);
        } catch (Exception e) {
            handleException("Error invoking the execute() method on POJO class : " + command.getClass(), e, synCtx);
        }
    }
    // then set the context properties back to the messageContext from the command
    for (String name : contextGetterProperties.keySet()) {
        synCtx.setProperty(contextGetterProperties.get(name), getInstanceProperty(name, commandObject, synCtx));
    }
    // to the message from the command
    for (String name : messageGetterProperties.keySet()) {
        SynapseXPath xpath = messageGetterProperties.get(name);
        Object resultValue = getInstanceProperty(name, commandObject, synCtx);
        try {
            List list = EIPUtils.getMatchingElements(synCtx.getEnvelope(), xpath);
            if (list.size() > 0) {
                Object o = list.get(0);
                if (resultValue instanceof String) {
                    OMAbstractFactory.getOMFactory().createOMText(((OMNode) o).getParent(), (String) resultValue);
                    ((OMNode) o).detach();
                } else if (resultValue instanceof OMNode) {
                    ((OMNode) o).insertSiblingAfter((OMNode) resultValue);
                    ((OMNode) o).detach();
                }
            } else {
                if (synLog.isTraceOrDebugEnabled()) {
                    synLog.traceOrDebug("Unable to set the message property " + resultValue + "back to the message : Specified element by the xpath " + xpath + " can not be found");
                }
            }
        } catch (JaxenException e) {
            handleException("Unable to set the command property " + name + " back to the message", e, synCtx);
        }
    }
    synLog.traceOrDebug("End : POJOCommand mediator");
    return true;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) OMNode(org.apache.axiom.om.OMNode) SynapseLog(org.apache.synapse.SynapseLog) Command(org.apache.synapse.Command) JaxenException(org.jaxen.JaxenException) List(java.util.List) Method(java.lang.reflect.Method) JaxenException(org.jaxen.JaxenException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

SynapseLog (org.apache.synapse.SynapseLog)53 Mediator (org.apache.synapse.Mediator)16 FlowContinuableMediator (org.apache.synapse.mediators.FlowContinuableMediator)13 SynapseException (org.apache.synapse.SynapseException)10 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)9 MessageContext (org.apache.synapse.MessageContext)8 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)8 OMNode (org.apache.axiom.om.OMNode)7 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)6 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)6 JaxenException (org.jaxen.JaxenException)5 PreparedStatement (java.sql.PreparedStatement)4 Set (java.util.Set)4 AxisFault (org.apache.axis2.AxisFault)4 OperationContext (org.apache.axis2.context.OperationContext)4 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)3 ReliantContinuationState (org.apache.synapse.continuation.ReliantContinuationState)3 Endpoint (org.apache.synapse.endpoints.Endpoint)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2