You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@synapse.apache.org by an...@apache.org on 2007/04/17 13:12:41 UTC

svn commit: r529566 - in /webservices/synapse/trunk/java/modules/extensions/src: main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java

Author: antelder
Date: Tue Apr 17 04:12:40 2007
New Revision: 529566

URL: http://svn.apache.org/viewvc?view=rev&rev=529566
Log:
Apply patch from Kasun Samarasinghe that formatted the ScriptMediator and added a script thread saftey test

Modified:
    webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java
    webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java

Modified: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java?view=diff&rev=529566&r1=529565&r2=529566
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java (original)
+++ webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java Tue Apr 17 04:12:40 2007
@@ -59,31 +59,52 @@
     private static final Log log = LogFactory.getLog(ScriptMediator.class);
     private static final Log trace = LogFactory.getLog(Constants.TRACE_LOGGER);
 
-    /** The name of the variable made available to the scripting language to access the message */
+    /**
+     * The name of the variable made available to the scripting language to access the message
+     */
     private static final String MC_VAR_NAME = "mc";
 
-    /** The registry entry key for a script loaded from the registry */
+    /**
+     * The registry entry key for a script loaded from the registry
+     */
     private String key;
-    /** The language of the script code */
+    /**
+     * The language of the script code
+     */
     private String language;
-    /** The optional name of the function to be invoked, defaults to mediate */
+    /**
+     * The optional name of the function to be invoked, defaults to mediate
+     */
     private String function = "mediate";
-    /** The source code of the script */
+    /**
+     * The source code of the script
+     */
     private String scriptSourceCode;
-    /** The BSF engine created to process each message through the script */
+    /**
+     * The BSF engine created to process each message through the script
+     */
     protected ScriptEngine scriptEngine;
-    /** Does the ScriptEngine support multi-threading */
+    /**
+     * Does the ScriptEngine support multi-threading
+     */
     private boolean multiThreadedEngine;
-    /** The compiled script. Only used for inline scripts */
+    /**
+     * The compiled script. Only used for inline scripts
+     */
     private CompiledScript compiledScript;
-    /** The Invocable script. Only used for external scripts */
+    /**
+     * The Invocable script. Only used for external scripts
+     */
     private Invocable invocableScript;
-    /** The BSF helper to convert between the XML representations used by Java and the scripting language */
+    /**
+     * The BSF helper to convert between the XML representations used by Java and the scripting language
+     */
     private XMLHelper xmlHelper;
 
     /**
      * Create a script mediator for the given language and given script source
-     * @param language the BSF language
+     *
+     * @param language         the BSF language
      * @param scriptSourceCode the source code of the script
      */
     public ScriptMediator(String language, String scriptSourceCode) {
@@ -94,8 +115,9 @@
 
     /**
      * Create a script mediator for the given language and given script entry key and function
+     *
      * @param language the BSF language
-     * @param key the registry entry key to load the script
+     * @param key      the registry entry key to load the script
      * @param function the function to be invoked
      */
     public ScriptMediator(String language, String key, String function) {
@@ -109,27 +131,28 @@
         if (!(scriptEngine instanceof Invocable)) {
             throw new SynapseException("Script engine is not an Invocable engine for language: " + language);
         }
-        invocableScript = (Invocable)scriptEngine;
+        invocableScript = (Invocable) scriptEngine;
     }
 
     /**
      * Perform Script mediation
+     *
      * @param synCtx the Synapse message context
      * @return the boolean result from the script invocation
      */
     public boolean mediate(MessageContext synCtx) {
         if (log.isDebugEnabled()) {
             log.debug("Script Mediator - mediate() # Language : " + language +
-                      (key == null ? " inline script" : " script with key : " + key) +
-                      " function : " + function);
+                    (key == null ? " inline script" : " script with key : " + key) +
+                    " function : " + function);
         }
 
         boolean shouldTrace = shouldTrace(synCtx.getTracingState());
 
         if (shouldTrace) {
             trace.trace("Start : Script mediator # Language : " + language +
-                (key == null ? " inline script" : " script with key : " + key) +
-                " function : " + function);
+                    (key == null ? " inline script" : " script with key : " + key) +
+                    " function : " + function);
             trace.trace("Invoking inline script for current message : " + synCtx);
         }
 
@@ -176,22 +199,23 @@
 
     /**
      * Mediation implementation when the script to be executed should be loaded from the registry
+     *
      * @param synCtx the message context
      * @return script result
-     * @throws ScriptException 
+     * @throws ScriptException
      */
     protected Object mediateWithExternalScript(MessageContext synCtx) throws ScriptException {
         prepareExternalScript(synCtx);
         ScriptMessageContext scriptMC = new ScriptMessageContext(synCtx, xmlHelper);
-        Object response = invocableScript.invokeFunction(function, new Object[]{scriptMC});
-        return response;
+        return invocableScript.invokeFunction(function, new Object[]{scriptMC});
     }
 
     /**
      * Perform mediation with static inline script of the given scripting language
+     *
      * @param synCtx message context
      * @return true, or the script return value
-     * @throws ScriptException 
+     * @throws ScriptException
      */
     protected Object mediateForInlineScript(MessageContext synCtx) throws ScriptException {
 
@@ -199,7 +223,7 @@
 
         Bindings bindings = scriptEngine.createBindings();
         bindings.put(MC_VAR_NAME, scriptMC);
-        
+
         Object response;
         if (compiledScript != null) {
             response = compiledScript.eval(bindings);
@@ -233,14 +257,14 @@
 
     /**
      * Prepares the mediator for the invocation of an external script
-     * @throws ScriptException 
+     * @throws ScriptException
      */
     protected synchronized void prepareExternalScript(MessageContext synCtx) throws ScriptException {
-        
+
         // TODO: only need this synchronized method for dynamic registry entries. If there was a way
         // to access the registry entry during mediator initialization then for non-dynamic entries
         // this could be done just the once during mediator initialization.
-        
+
         Entry entry = synCtx.getConfiguration().getEntryDefinition(key);
         boolean needsReload = (entry != null) && entry.isDynamic() && (!entry.isCached() || entry.isExpired());
 
@@ -263,7 +287,7 @@
             throw new SynapseException("No script engine found for language: " + language);
         }
         xmlHelper = XMLHelper.getArgHelper(scriptEngine);
-        
+
         this.multiThreadedEngine = scriptEngine.getFactory().getParameter("THREADING") != null;
     }
 

Modified: webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java?view=diff&rev=529566&r1=529565&r2=529566
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java (original)
+++ webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java Tue Apr 17 04:12:40 2007
@@ -20,46 +20,51 @@
 package org.apache.synapse.mediators.bsf;
 
 import junit.framework.TestCase;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.extensions.RepeatedTest;
 
 import org.apache.synapse.MessageContext;
 import org.apache.synapse.mediators.TestUtils;
 
+import java.util.Random;
+
 public class ScriptMediatorTest extends TestCase {
 
-    private static final String inlinescript = "<x><![CDATA[ function mediate(mc) { return true;} ]]></x>";
+    private static final String inlinescript = "var state=5;";
 
-    private static final String falsescript = "<x><![CDATA[ function mediate(mc) { return false;} ]]></x>";
+    private String randomno = null;
 
-    public void testTrueMediator() throws Exception {
+    private String threadsafetyscript = "var rno = mc.getPayloadXML().toString(); rno=rno*2; mc.setPayloadXML" +
+            "(<randomNo>{rno}</randomNo>)";
 
+    public void testInlineMediator() throws Exception {
         MessageContext mc = TestUtils.getTestContext("<foo/>", null);
         ScriptMediator mediator = new ScriptMediator("js", inlinescript);
         assertTrue(mediator.mediate(mc));
     }
 
-    public void testFalseMediator() throws Exception {
-        MessageContext mc = TestUtils.getTestContext("<foo/>", null);
-        ScriptMediator mediator = new ScriptMediator("js", falsescript);
-        assertTrue(mediator.mediate(mc));
+    public void testThreadSafety() throws Exception {
+        MessageContext mc = TestUtils.getTestContext("<randomNo/>", null);
+        Random rand = new Random();
+        randomno = new Integer(rand.nextInt(200)).toString();
+        mc.getEnvelope().getBody().getFirstElement().setText(randomno);
+        ScriptMediator mediator = new ScriptMediator("js", threadsafetyscript);
+        mediator.mediate(mc);
+        assertEquals(Integer.parseInt(mc.getEnvelope().getBody().getFirstElement().getText()),
+                Integer.parseInt(randomno) * 2);
     }
 
-    public void testJSCreateOMElementConvertor() {
-        // ScriptMediator mediator = new ScriptMediator("js", "true;");
-        // ScriptEngine engine = mediator.scriptEngine;
-        // XMLHelper convertor = mediator.getOMElementConvertor(engine);
-        // assertTrue(convertor instanceof JavaScriptXMLHelper);
-    }
 
-    // public void testRBCreateOMElementConvertor() {
-    // ScriptMediator mediator = new ScriptMediator("ruby", null);
-    // OMElementConvertor convertor = mediator.getOMElementConvertor();
-    // assertTrue(convertor instanceof RBOMElementConvertor);
-    // }
-    //    
-    // public void testDefaultCreateOMElementConvertor() {
-    // ScriptMediator mediator = new ScriptMediator("foo.bar", null);
-    // OMElementConvertor convertor = mediator.getOMElementConvertor();
-    // assertTrue(convertor instanceof DefaultOMElementConvertor);
-    // }
+    public static Test suite() {
+        TestSuite suite = new TestSuite();
+        for (int i = 0; i < 10; i++) {
+            suite.addTest(new RepeatedTest(new ScriptMediatorTest("testThreadSafety"), 10));
+        }
+        return suite;
+    }
 
+    public ScriptMediatorTest(String name) {
+        super(name);
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: synapse-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: synapse-dev-help@ws.apache.org