You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by ru...@apache.org on 2009/03/03 20:11:43 UTC

svn commit: r749702 - in /synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler: HandlerConstants.java SynapseInHandler.java SynapseModule.java SynapseOutHandler.java util/HandlerUtil.java

Author: ruwan
Date: Tue Mar  3 19:11:42 2009
New Revision: 749702

URL: http://svn.apache.org/viewvc?rev=749702&view=rev
Log:
Synapse-handler module improvements, so that synapse can be engaged to an existing axis2 service and specify the mediation sequences through the service parameters

Added:
    synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/HandlerConstants.java
Modified:
    synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseInHandler.java
    synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseModule.java
    synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseOutHandler.java
    synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/util/HandlerUtil.java

Added: synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/HandlerConstants.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/HandlerConstants.java?rev=749702&view=auto
==============================================================================
--- synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/HandlerConstants.java (added)
+++ synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/HandlerConstants.java Tue Mar  3 19:11:42 2009
@@ -0,0 +1,33 @@
+/*
+ *  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.handler;
+
+/**
+ * This class contains a set of contstants used in the synapse-handler module
+ */
+public final class HandlerConstants {
+
+    /** incoming mediation sequence parameter name of the service parameter */
+    public static final String IN_SEQUENCE_PARAM_NAME = "mediation-in-sequence";
+    /** ioutgoing mediation sequence parameter name of the service parameter */
+    public static final String OUT_SEQUENCE_PARAM_NAME = "mediation-out-sequence";
+    /** fault flow mediation sequence parameter name of the service parameter */
+    public static final String FAULT_SEQUENCE_PARAM_NAME = "mediation-fault-sequence";
+}

Modified: synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseInHandler.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseInHandler.java?rev=749702&r1=749701&r2=749702&view=diff
==============================================================================
--- synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseInHandler.java (original)
+++ synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseInHandler.java Tue Mar  3 19:11:42 2009
@@ -62,27 +62,32 @@
         if (!messageContext.isServerSide()) {
             synCtx.setProperty(SynapseConstants.RESPONSE, Boolean.TRUE);
             synCtx.setResponse(true);
+            try {
+                // if synapse says ok let the message to flow through
+                if (HandlerUtil.mediateOutMessage(log, messageContext, synCtx)) {
+                    return InvocationResponse.CONTINUE;
+                } else {
+                    // if not abort the further processings
+                    log.debug("Synapse has decided to abort the message:\n" + synCtx);
+                    return InvocationResponse.ABORT;
+                }
+            } catch (SynapseException syne) {
+                // todo : invoke the fault sequence
+            }
         } else {
             synCtx.setProperty(SynapseConstants.RESPONSE, Boolean.FALSE);
             synCtx.setResponse(false);
-        }
-
-        try {
-            // if synapse says ok let the message to flow through
-            if (synCtx.getEnvironment().injectMessage(synCtx)) {
-                return InvocationResponse.CONTINUE;
-            } else {
-                // if not abort the further processings
-                log.debug("Synapse has decided to abort the message:\n" + synCtx.getEnvelope());
-                return InvocationResponse.ABORT;
-            }
-        } catch (SynapseException syne) {
-            if (!synCtx.getFaultStack().isEmpty()) {
-                ((FaultHandler) synCtx.getFaultStack().pop()).handleFault(synCtx, syne);
-            } else {
-                log.error("Synapse encountered an exception, " +
-                        "No error handlers found.\n" + syne.getMessage());
-                throw new AxisFault("Synapse encountered an error." + syne);
+            try {
+                // if synapse says ok let the message to flow through
+                if (HandlerUtil.mediateInMessage(log, messageContext, synCtx)) {
+                    return InvocationResponse.CONTINUE;
+                } else {
+                    // if not abort the further processings
+                    log.debug("Synapse has decided to abort the message:\n" + synCtx);
+                    return InvocationResponse.ABORT;
+                }
+            } catch (SynapseException syne) {
+                // todo : invoke the fault sequence
             }
         }
 

Modified: synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseModule.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseModule.java?rev=749702&r1=749701&r2=749702&view=diff
==============================================================================
--- synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseModule.java (original)
+++ synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseModule.java Tue Mar  3 19:11:42 2009
@@ -23,10 +23,16 @@
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.description.AxisModule;
 import org.apache.axis2.description.AxisDescription;
+import org.apache.axis2.description.Parameter;
 import org.apache.axis2.AxisFault;
 import org.apache.neethi.Assertion;
 import org.apache.neethi.Policy;
-import org.apache.synapse.*;
+import org.apache.synapse.SynapseConstants;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.ServerManager;
+import org.apache.synapse.config.SynapseConfiguration;
+import org.apache.synapse.core.axis2.SynapseInitializationModule;
+import org.apache.synapse.core.SynapseEnvironment;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -41,55 +47,129 @@
  */
 public class SynapseModule implements Module {
 
-    private final ServerManager serverManager = ServerManager.getInstance();
     /**
      * Log variable to be used as the logging appender
      */
     private static final Log log = LogFactory.getLog(SynapseModule.class);
 
     /**
+     * Normal SynapseInitializationModule which initiates the Synapse
+     */
+    private SynapseInitializationModule initializationModule = null;
+
+    /**
      * This method will call the normal initiation after setting the SYNAPSE_XML file to get from
      * the axis2 respository/conf folder
      *
      * @param configurationContext - ConfigurationContext of the Axis2 env
-     * @param axisModule           - AxisModule describing handler initializationModule of Synapse
+     * @param axisModule - AxisModule describing handler initializationModule of Synapse
      * @throws AxisFault - incase of a failure in initiation
      */
     public void init(ConfigurationContext configurationContext,
                      AxisModule axisModule) throws AxisFault {
-        ServerConfigurationInformation information = new ServerConfigurationInformation();
-        String synapseHome = System.getProperty(SynapseConstants.SYNAPSE_HOME);
-        if (synapseHome == null) {
-            synapseHome = new File(".").getAbsolutePath();
-        }
-        information.setSynapseHome(synapseHome);
-        String synapseXMl = System.getProperty(SynapseConstants.SYNAPSE_XML);
-        if (synapseXMl == null) {
-            synapseXMl = configurationContext.
-                    getAxisConfiguration().getRepository().getPath() + "/conf/synapse.xml";
-        }
-        information.setSynapseXMLLocation(synapseXMl);
-
-        ServerContextInformation contextInformation =
-                new ServerContextInformation(configurationContext);
-        serverManager.init(information, contextInformation);
-    }
 
+        if (System.getProperty(SynapseConstants.SYNAPSE_XML) == null) {
+            Parameter synapseHome = configurationContext.getAxisConfiguration().getParameter(
+                    SynapseConstants.Axis2Param.SYNAPSE_HOME);
+            Parameter synapseXml = configurationContext.getAxisConfiguration().getParameter(
+                    SynapseConstants.Axis2Param.SYNAPSE_CONFIG_LOCATION);
+            if (synapseHome != null && synapseHome.getValue() != null) {
+                ServerManager.getInstance().getInformation().setSynapseHome(synapseHome.getValue().toString());
+            }
+            if (synapseXml != null && synapseXml.getValue() != null) {
+                ServerManager.getInstance().getInformation().setSynapseXMLLocation(synapseXml.getValue().toString());
+            }
+        }
 
+        if (new File(ServerManager.getInstance().getInformation().getSynapseXMLLocation()).exists()) {
+            initializationModule = new org.apache.synapse.core.axis2.SynapseInitializationModule();
+            initializationModule.init(configurationContext, axisModule);
+
+            // now initialize SynapseConfig
+            Parameter synEnv = configurationContext
+                .getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_ENV);
+            Parameter synCfg = configurationContext
+                .getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_CONFIG);
+            String message = "Unable to initialize the Synapse Configuration : Can not find the ";
+            if (synCfg == null || synCfg.getValue() == null
+                || !(synCfg.getValue() instanceof SynapseConfiguration)) {
+                log.fatal(message + "Synapse Configuration");
+                throw new SynapseException(message + "Synapse Configuration");
+            }
+
+            if (synEnv == null || synEnv.getValue() == null
+                || !(synEnv.getValue() instanceof SynapseEnvironment)) {
+                log.fatal(message + "Synapse Environment");
+                throw new SynapseException(message + "Synapse Environment");
+            }
+
+            ((SynapseConfiguration) synCfg.getValue()).init((SynapseEnvironment) synEnv.getValue());
+        } else {
+            handleException("Unable to initialize the Synapse initializationModule. Couldn't " +
+                    "find the configuration file in the location "
+                    + ServerManager.getInstance().getInformation().getSynapseXMLLocation());
+        }
+    }
+
+    /**
+     * Just do what the main SynapseInitializationModule tells you to do
+     *
+     * @param axisDescription
+     * @throws AxisFault
+     */
     public void engageNotify(AxisDescription axisDescription) throws AxisFault {
-        serverManager.start();
+        if (initializationModule != null) {
+            initializationModule.engageNotify(axisDescription);
+        } else {
+            handleException("Couldn't find the initializationModule");
+        }
     }
 
+    /**
+     * Just do what the main SynapseInitializationModule tells you to do
+     *
+     * @param assertion
+     * @return
+     */
     public boolean canSupportAssertion(Assertion assertion) {
-        return false;
+        if (initializationModule != null) {
+            return initializationModule.canSupportAssertion(assertion);
+        } else {
+            return false;
+        }
     }
 
+    /**
+     * Just do what the main SynapseInitializationModule tells you to do
+     *
+     * @param policy
+     * @param axisDescription
+     * @throws AxisFault
+     */
     public void applyPolicy(Policy policy, AxisDescription axisDescription) throws AxisFault {
-
+        if (initializationModule != null) {
+            initializationModule.applyPolicy(policy, axisDescription);
+        } else {
+            handleException("Couldn't find the initializationModule");
+        }
     }
 
+    /**
+     * Just do what the main SynapseInitializationModule tells you to do
+     *
+     * @param configurationContext
+     * @throws AxisFault
+     */
     public void shutdown(ConfigurationContext configurationContext) throws AxisFault {
-        serverManager.stop();
+        if (initializationModule != null) {
+            initializationModule.shutdown(configurationContext);
+        } else {
+            handleException("Couldn't find the initializationModule");
+        }
     }
 
+    private void handleException(String message) throws AxisFault {
+        log.error(message);
+        throw new AxisFault(message);
+    }
 }

Modified: synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseOutHandler.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseOutHandler.java?rev=749702&r1=749701&r2=749702&view=diff
==============================================================================
--- synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseOutHandler.java (original)
+++ synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/SynapseOutHandler.java Tue Mar  3 19:11:42 2009
@@ -62,9 +62,35 @@
         if (messageContext.isServerSide()) {
             synCtx.setProperty(SynapseConstants.RESPONSE, Boolean.TRUE);
             synCtx.setResponse(true);
+            synCtx.setProperty(SynapseConstants.RESPONSE, Boolean.TRUE);
+            synCtx.setResponse(true);
+            try {
+                // if synapse says ok let the message to flow through
+                if (HandlerUtil.mediateOutMessage(log, messageContext, synCtx)) {
+                    return InvocationResponse.CONTINUE;
+                } else {
+                    // if not abort the further processings
+                    log.debug("Synapse has decided to abort the message:\n" + synCtx);
+                    return InvocationResponse.ABORT;
+                }
+            } catch (SynapseException syne) {
+                // todo : invoke the fault sequence
+            }
         } else {
             synCtx.setProperty(SynapseConstants.RESPONSE, Boolean.FALSE);
             synCtx.setResponse(false);
+            try {
+                // if synapse says ok let the message to flow through
+                if (HandlerUtil.mediateInMessage(log, messageContext, synCtx)) {
+                    return InvocationResponse.CONTINUE;
+                } else {
+                    // if not abort the further processings
+                    log.debug("Synapse has decided to abort the message:\n" + synCtx);
+                    return InvocationResponse.ABORT;
+                }
+            } catch (SynapseException syne) {
+                // todo : invoke the fault sequence
+            }
         }
 
         try {
@@ -73,7 +99,7 @@
                 return InvocationResponse.CONTINUE;
             } else {
                 // if not abort the further processings
-                log.debug("Synapse has decided to abort the message:\n" + synCtx.getEnvelope());                
+                log.debug("Synapse has decided to abort the message:\n" + synCtx.getEnvelope());
                 return InvocationResponse.ABORT;
             }
         } catch (SynapseException syne) {

Modified: synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/util/HandlerUtil.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/util/HandlerUtil.java?rev=749702&r1=749701&r2=749702&view=diff
==============================================================================
--- synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/util/HandlerUtil.java (original)
+++ synapse/trunk/java/modules/handler/src/main/java/org/apache/synapse/handler/util/HandlerUtil.java Tue Mar  3 19:11:42 2009
@@ -19,8 +19,13 @@
 
 package org.apache.synapse.handler.util;
 
-import org.apache.commons.logging.Log;
+import org.apache.axis2.AxisFault;
 import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.AxisService;
+import org.apache.axis2.description.Parameter;
+import org.apache.commons.logging.Log;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.handler.HandlerConstants;
 
 /**
  * This is a helper class to get the loggin done in both in and out handlers
@@ -29,7 +34,7 @@
 
     /**
      * Helper util method to get the logging done whenever injecting the message into synapse
-     * 
+     *
      * @param log - Log appender to be used to append logs
      * @param messageContext - MessageContext which will be logged
      */
@@ -51,4 +56,70 @@
             log.debug("Body : \n" + messageContext.getEnvelope());
         }
     }
+
+    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;
+    }
+
+    public static boolean mediateOutMessage(Log log, MessageContext messageContext,
+                                           org.apache.synapse.MessageContext synCtx) throws AxisFault {
+
+        AxisService service = messageContext.getAxisService();
+        if (service != null) {
+            Parameter inMediationParam = service.getParameter(HandlerConstants.OUT_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 out message mediation sequence is not a proper mediator");
+                    }
+                }
+            } else {
+                if (log.isDebugEnabled()) {
+                    log.debug("Couldn't find the outgoing 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;
+    }
 }