* @param synCtx - Synapse MessageContext to be mediated
* @return boolean true since this will not stop exection chain
*/
public boolean mediate(MessageContext synCtx) {
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));
}
// now set the any message properties evaluating XPath's on the current message back
// 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;
}