Author: antelder
Date: Thu Nov 16 11:04:58 2006
New Revision: 475874

URL: http://svn.apache.org/viewvc?view=rev&rev=475874
Log:
Fix inline script mediators to work with most BSF engines

Added:
    
incubator/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/convertors/
    
incubator/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/convertors/JSOMElementConvertorTest.java
   (with props)
Modified:
    
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/InlineScriptMediator.java

Modified: 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/InlineScriptMediator.java
URL: 
http://svn.apache.org/viewvc/incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/InlineScriptMediator.java?view=diff&rev=475874&r1=475873&r2=475874
==============================================================================
--- 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/InlineScriptMediator.java
 (original)
+++ 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/InlineScriptMediator.java
 Thu Nov 16 11:04:58 2006
@@ -15,12 +15,14 @@
  */
 package org.apache.synapse.mediators.bsf;
 
+import java.util.Arrays;
+import java.util.Vector;
+
 import org.apache.bsf.BSFException;
 import org.apache.bsf.BSFManager;
 import org.apache.synapse.MessageContext;
 import org.apache.synapse.SynapseException;
 
-
 /**
  * An inline script mediator has the script source embedded in the config XML:
  * 
@@ -34,9 +36,12 @@
  */
 public class InlineScriptMediator extends ScriptMediator {
 
-    private String scriptName;
+    protected String scriptName;
 
-    private String scriptSrc;
+    protected String scriptSrc;
+    
+    protected static final String MC_VAR_NAME = "mc";
+    protected static final Vector PARAM_NAMES = new Vector(Arrays.asList(new 
String[]{ MC_VAR_NAME }));
 
     public InlineScriptMediator(String scriptName, String scriptSrc) {
         super(null, null);
@@ -47,9 +52,19 @@
     public boolean mediate(MessageContext synCtx) {
         try {
 
-            ThreadLocalMessageContext.setMC(new ScriptMessageContext(synCtx, 
convertor));
+            // This is a bit of a hack to deal with inconsistencies in the 
varrious BSF script engines.
+            // The JavaScript engine does not support BSFEngine.apply properly 
as it doesn't pass the
+            // param names and values to the script (actually this looks like 
a bug in BSFEngineImpl
+            // not the JavaScript engine). To circumvent this problem the 
ThreadLocalMessageContext is
+            // added as a declared bean. Some other engines (eg JRuby) seem to 
have a bug where they don't
+            // pick up declared beans so ScriptMessageContext is passed in as 
a param on the apply call.
+            
+            ScriptMessageContext scriptMC = new ScriptMessageContext(synCtx, 
convertor);
+            ThreadLocalMessageContext.setMC(scriptMC);
+            Vector paramValues = new Vector();
+            paramValues.add(scriptMC);
 
-            Object response = bsfEngine.eval(scriptName, 0, 0, scriptSrc);
+            Object response = bsfEngine.apply(scriptName, 0, 0, scriptSrc, 
PARAM_NAMES, paramValues);
 
             if (response instanceof Boolean) {
                 return ((Boolean) response).booleanValue();
@@ -64,11 +79,13 @@
 
     public void init() {
         try {
-            this.bsfManager.declareBean("mc", new ThreadLocalMessageContext(), 
ThreadLocalMessageContext.class);
+
+            this.bsfManager.declareBean(MC_VAR_NAME, new 
ThreadLocalMessageContext(), ThreadLocalMessageContext.class);
             String scriptLanguage = BSFManager.getLangFromFilename(scriptName);
             this.bsfEngine = bsfManager.loadScriptingEngine(scriptLanguage);
             this.convertor = createOMElementConvertor(scriptName);
             convertor.setEngine(bsfEngine);
+
         } catch (BSFException e) {
             throw new SynapseException(e);
         }

Added: 
incubator/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/convertors/JSOMElementConvertorTest.java
URL: 
http://svn.apache.org/viewvc/incubator/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/convertors/JSOMElementConvertorTest.java?view=auto&rev=475874
==============================================================================
--- 
incubator/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/convertors/JSOMElementConvertorTest.java
 (added)
+++ 
incubator/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/convertors/JSOMElementConvertorTest.java
 Thu Nov 16 11:04:58 2006
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.bsf.convertors;
+
+import junit.framework.TestCase;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.synapse.mediators.TestUtils;
+
+public class JSOMElementConvertorTest extends TestCase {
+    
+    public static final String XML = "<a><b>petra</b></a>";
+
+    public void testToAndFromScript() {
+        JSOMElementConvertor convertor = new JSOMElementConvertor();
+        Object o = convertor.toScript(TestUtils.createOMElement(XML));
+        OMElement om = convertor.fromScript(o);
+        assertEquals(XML, om.toString());
+    }
+
+}

Propchange: 
incubator/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/convertors/JSOMElementConvertorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/convertors/JSOMElementConvertorTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to