Author: ruwan
Date: Tue May 11 10:31:52 2010
New Revision: 943076
URL: http://svn.apache.org/viewvc?rev=943076&view=rev
Log:
Adding a conditional router mediator
Added:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ConditionalRouterMediatorFactory.java
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ConditionalRouterMediatorSerializer.java
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/router/
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/router/ConditionalRouterMediator.java
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/router/Route.java
Modified:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java
Added:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ConditionalRouterMediatorFactory.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ConditionalRouterMediatorFactory.java?rev=943076&view=auto
==============================================================================
---
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ConditionalRouterMediatorFactory.java
(added)
+++
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ConditionalRouterMediatorFactory.java
Tue May 11 10:31:52 2010
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.synapse.config.xml;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axis2.util.JavaUtils;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.commons.evaluators.Evaluator;
+import org.apache.synapse.commons.evaluators.EvaluatorException;
+import org.apache.synapse.commons.evaluators.config.EvaluatorFactoryFinder;
+import org.apache.synapse.mediators.eip.Target;
+import org.apache.synapse.mediators.filters.router.ConditionalRouterMediator;
+import org.apache.synapse.mediators.filters.router.Route;
+
+import javax.xml.namespace.QName;
+import java.util.Iterator;
+
+/**
+ * <pre>
+ * <conditionalRouter continueAfter="(true|false)">
+ * <route breakRoute="(true|false)">
+ * <condition ../>
+ * <target ../>
+ * </route>
+ * </conditionalRouter>
+ * </pre>
+ */
+public class ConditionalRouterMediatorFactory extends AbstractMediatorFactory {
+
+ private static final QName CONDITIONAL_ROUTER_Q
+ = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE,
"conditionalRouter");
+ private static final QName ROUTE_Q
+ = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "route");
+ private static final QName CONDITION_Q
+ = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "condition");
+ private static final QName TARGET_Q
+ = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "target");
+
+ private static final QName CONTINUE_AFTER_ATTR
+ = new QName(XMLConfigConstants.NULL_NAMESPACE, "continueAfter");
+ private static final QName BREAK_ROUTE_ATTR
+ = new QName(XMLConfigConstants.NULL_NAMESPACE, "breakRoute");
+ private static final QName ASYNCHRONOUS_ATTR
+ = new QName(XMLConfigConstants.NULL_NAMESPACE, "asynchronous");
+
+ public Mediator createMediator(OMElement elem) {
+
+ ConditionalRouterMediator conditionalRouterMediator = new
ConditionalRouterMediator();
+ processAuditStatus(conditionalRouterMediator, elem);
+
+ if (elem.getAttribute(CONTINUE_AFTER_ATTR) != null) {
+ if
(JavaUtils.isTrueExplicitly(elem.getAttributeValue(CONTINUE_AFTER_ATTR).trim()))
{
+ conditionalRouterMediator.setContinueAfter(true);
+ } else if
(JavaUtils.isFalseExplicitly(elem.getAttributeValue(CONTINUE_AFTER_ATTR).trim()))
{
+ conditionalRouterMediator.setContinueAfter(false);
+ } else {
+ handleException("continueAfter attribute value of the
conditionalRouter must " +
+ "be either 'true' or 'false', the value found is : "
+ + elem.getAttributeValue(CONTINUE_AFTER_ATTR).trim());
+ }
+ }
+
+ Iterator itr = elem.getChildrenWithName(ROUTE_Q);
+ while (itr.hasNext()) {
+
+ OMElement routeElem = (OMElement) itr.next();
+ if (ROUTE_Q.equals(routeElem.getQName())) {
+
+ Route route = new Route();
+
+ if (routeElem.getAttribute(BREAK_ROUTE_ATTR) != null) {
+ if
(JavaUtils.isTrueExplicitly(routeElem.getAttributeValue(BREAK_ROUTE_ATTR).trim()))
{
+ route.setBreakRoute(true);
+ } else if
(JavaUtils.isFalseExplicitly(routeElem.getAttributeValue(BREAK_ROUTE_ATTR).trim()))
{
+ route.setBreakRoute(false);
+ } else {
+ handleException("breakRoute attribute value of the
route element must " +
+ "be either 'true' or 'false', the value found
is : "
+ +
routeElem.getAttributeValue(BREAK_ROUTE_ATTR).trim());
+ }
+ }
+
+ OMElement conditionElem =
routeElem.getFirstChildWithName(CONDITION_Q);
+ try {
+ Evaluator evaluator =
EvaluatorFactoryFinder.getInstance().getEvaluator(
+ conditionElem.getFirstElement());
+ route.setEvaluator(evaluator);
+ } catch (EvaluatorException ee) {
+ throw new SynapseException("Couldn't build the condition
of the conditional router");
+ }
+ OMElement targetElem =
routeElem.getFirstChildWithName(TARGET_Q);
+ Target target = TargetFactory.createTarget(targetElem);
+ if
(JavaUtils.isTrueExplicitly(routeElem.getAttributeValue(ASYNCHRONOUS_ATTR))) {
+ target.setAsynchronous(true);
+ } else {
+ target.setAsynchronous(false);
+ }
+ route.setTarget(target);
+ conditionalRouterMediator.addRoute(route);
+ }
+ }
+ return conditionalRouterMediator;
+ }
+
+ public QName getTagQName() {
+ return CONDITIONAL_ROUTER_Q;
+ }
+}
Added:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ConditionalRouterMediatorSerializer.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ConditionalRouterMediatorSerializer.java?rev=943076&view=auto
==============================================================================
---
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ConditionalRouterMediatorSerializer.java
(added)
+++
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ConditionalRouterMediatorSerializer.java
Tue May 11 10:31:52 2010
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.synapse.config.xml;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.mediators.filters.router.ConditionalRouterMediator;
+import org.apache.synapse.mediators.filters.router.Route;
+
+/**
+ * <pre>
+ * <conditionalRouter continueAfter="(true|false)">
+ * <route breakRoute="(true|false)">
+ * <condition ../>
+ * <target ../>
+ * </route>
+ * </conditionalRouter>
+ * </pre>
+ */
+public class ConditionalRouterMediatorSerializer extends
AbstractMediatorSerializer {
+
+ public OMElement serializeMediator(OMElement parent, Mediator m) {
+ OMElement conditionalRouterElem =
fac.createOMElement("conditionalRouter", synNS);
+ saveTracingState(conditionalRouterElem, m);
+
+ ConditionalRouterMediator conditionalRouterMediator =
(ConditionalRouterMediator) m;
+ if (conditionalRouterMediator.isContinueAfterExplicitlySet()) {
+ conditionalRouterElem.addAttribute("continueAfter",
+
Boolean.toString(conditionalRouterMediator.isContinueAfter()), nullNS);
+ }
+
+ for (Route route : conditionalRouterMediator.getRoutes()) {
+ OMElement routeElem = fac.createOMElement("route", synNS);
+
+ if (route.isBreakRouteExplicitlySet()) {
+ routeElem.addAttribute("breakRoute",
Boolean.toString(route.isBreakRoute()), nullNS);
+ }
+
+ if (route.getTarget() != null) {
+
routeElem.addChild(TargetSerializer.serializeTarget(route.getTarget()));
+ } else {
+ handleException("Route in a conditional router has to have a
target");
+ }
+
+ if (route.getEvaluator() != null) {
+ // todo serialize the route evaluator
+ }
+ conditionalRouterElem.addChild(routeElem);
+ }
+
+ if (parent != null) {
+ parent.addChild(conditionalRouterElem);
+ }
+
+ return conditionalRouterElem;
+ }
+
+ public String getMediatorClassName() {
+ return ConditionalRouterMediator.class.getName();
+ }
+}
Modified:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java?rev=943076&r1=943075&r2=943076&view=diff
==============================================================================
---
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
(original)
+++
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
Tue May 11 10:31:52 2010
@@ -74,7 +74,8 @@ public class MediatorFactoryFinder imple
CalloutMediatorFactory.class,
EventPublisherMediatorFactory.class,
TransactionMediatorFactory.class,
- EnqueueMediatorFactory.class
+ EnqueueMediatorFactory.class,
+ ConditionalRouterMediatorFactory.class
};
private final static MediatorFactoryFinder instance = new
MediatorFactoryFinder();
Modified:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java?rev=943076&r1=943075&r2=943076&view=diff
==============================================================================
---
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java
(original)
+++
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java
Tue May 11 10:31:52 2010
@@ -61,7 +61,8 @@ public class MediatorSerializerFinder {
CalloutMediatorSerializer.class,
EventPublisherMediatorSerializer.class,
TransactionMediatorSerializer.class,
- EnqueueMediatorSerializer.class
+ EnqueueMediatorSerializer.class,
+ ConditionalRouterMediatorSerializer.class
};
private final static MediatorSerializerFinder instance = new
MediatorSerializerFinder();
Added:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/router/ConditionalRouterMediator.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/router/ConditionalRouterMediator.java?rev=943076&view=auto
==============================================================================
---
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/router/ConditionalRouterMediator.java
(added)
+++
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/router/ConditionalRouterMediator.java
Tue May 11 10:31:52 2010
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.synapse.mediators.filters.router;
+
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.commons.evaluators.EvaluatorContext;
+import org.apache.synapse.commons.evaluators.EvaluatorException;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.mediators.AbstractMediator;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Checks whether the route condition evaluates to true and mediates using the
target if it evaluates to true.
+ * Matching route will break the router if the <code>breakRoute</code> is set
to true on the evaluated route
+ *
+ * @see org.apache.synapse.Mediator
+ */
+public class ConditionalRouterMediator extends AbstractMediator {
+
+ private List<Route> routes = new ArrayList<Route>();
+
+ private boolean continueAfter;
+
+ private boolean continueAfterExplicitlySet;
+
+ public boolean mediate(MessageContext synCtx) {
+
+ Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
+ org.apache.axis2.context.MessageContext axis2MessageCtx =
+ axis2smc.getAxis2MessageContext();
+ Object headers = axis2MessageCtx.getProperty(
+ org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
+ Map<String, String> evaluatorHeaders = new HashMap<String, String>();
+
+ if (headers != null && headers instanceof Map) {
+ Map headersMap = (Map) headers;
+ for (Object key : headersMap.keySet()) {
+ if (key instanceof String && headersMap.get(key) instanceof
String) {
+ evaluatorHeaders.put((String) key, (String)
headersMap.get(key));
+ }
+ }
+ }
+
+ EvaluatorContext context = new
EvaluatorContext(synCtx.getTo().getAddress(),
+ evaluatorHeaders);
+
+ try {
+ for (Route route : routes) {
+ if (route.getEvaluator().evaluate(context)) {
+ route.getTarget().mediate(synCtx);
+ if (route.isBreakRoute()) {
+ break;
+ }
+ }
+ }
+ } catch (EvaluatorException ee) {
+ handleException("Couldn't evaluate the route condition", ee,
synCtx);
+ }
+ return continueAfter;
+ }
+
+ public List<Route> getRoutes() {
+ return routes;
+ }
+
+ public void addRoute(Route route) {
+ routes.add(route);
+ }
+
+ public boolean isContinueAfter() {
+ return continueAfter;
+ }
+
+ public void setContinueAfter(boolean continueAfter) {
+ this.continueAfterExplicitlySet = true;
+ this.continueAfter = continueAfter;
+ }
+
+ public boolean isContinueAfterExplicitlySet() {
+ return continueAfterExplicitlySet;
+ }
+}
Added:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/router/Route.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/router/Route.java?rev=943076&view=auto
==============================================================================
---
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/router/Route.java
(added)
+++
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/router/Route.java
Tue May 11 10:31:52 2010
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.synapse.mediators.filters.router;
+
+import org.apache.synapse.commons.evaluators.Evaluator;
+import org.apache.synapse.mediators.eip.Target;
+
+/**
+ *
+ */
+public class Route {
+
+ private Evaluator evaluator;
+
+ private Target target;
+
+ private boolean breakRoute = true;
+
+ private boolean breakRouteExplicitlySet;
+
+ public Evaluator getEvaluator() {
+ return evaluator;
+ }
+
+ public void setEvaluator(Evaluator evaluator) {
+ this.evaluator = evaluator;
+ }
+
+ public Target getTarget() {
+ return target;
+ }
+
+ public void setTarget(Target target) {
+ this.target = target;
+ }
+
+ public boolean isBreakRoute() {
+ return breakRoute;
+ }
+
+ public void setBreakRoute(boolean breakRoute) {
+ this.breakRouteExplicitlySet = true;
+ this.breakRoute = breakRoute;
+ }
+
+ public boolean isBreakRouteExplicitlySet() {
+ return breakRouteExplicitlySet;
+ }
+}