Search in sources :

Example 1 with Command

use of org.apache.synapse.Command 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 2 with Command

use of org.apache.synapse.Command 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

Method (java.lang.reflect.Method)2 Command (org.apache.synapse.Command)2 SynapseLog (org.apache.synapse.SynapseLog)2 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)2 JaxenException (org.jaxen.JaxenException)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 OMNode (org.apache.axiom.om.OMNode)1