Author: antelder
Date: Wed Oct 25 03:06:09 2006
New Revision: 467597
URL: http://svn.apache.org/viewvc?view=rev&rev=467597
Log:
Get inline script mediators working. Would like to do this in trunk but i cant
get a clean build to go till people.a.o is back, so i'll keep it in the scratch
area for now
Added:
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/InlineScriptMediator.java
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ThreadLocalMessageContext.java
Modified:
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediator.java
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediatorFactory.java
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMessageContext.java
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/DefaultOMElementConvertor.java
Added:
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/InlineScriptMediator.java
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/InlineScriptMediator.java?view=auto&rev=467597
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/InlineScriptMediator.java
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/InlineScriptMediator.java
Wed Oct 25 03:06:09 2006
@@ -0,0 +1,76 @@
+/*
+ * 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;
+
+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:
+ *
+ * <pre>
+ * <script.LL&gt...src code...<script.LL/>
+ * </pre>
+ *
+ * <p>
+ * where LL is the script language name extension. The environment of the
script
+ * has the Synapse MessageContext predefined in a script variable named 'mc'.
+ */
+public class InlineScriptMediator extends ScriptMediator {
+
+ private String scriptName;
+
+ private String scriptSrc;
+
+ public InlineScriptMediator(String scriptName, String scriptSrc) {
+ super(null, null);
+ this.scriptName = scriptName;
+ this.scriptSrc = scriptSrc;
+ }
+
+ public boolean mediate(MessageContext synCtx) {
+ try {
+
+ ThreadLocalMessageContext.setMC(new ScriptMessageContext(synCtx,
convertor));
+
+ Object response = bsfEngine.eval(scriptName, 0, 0, scriptSrc);
+
+ if (response instanceof Boolean) {
+ return ((Boolean) response).booleanValue();
+ }
+
+ return true; // default to returning true
+
+ } catch (BSFException e) {
+ throw new SynapseException(e);
+ }
+ }
+
+ public void init() {
+ try {
+ this.bsfManager.declareBean("mc", 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);
+ }
+ }
+}
Modified:
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediator.java
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediator.java?view=diff&rev=467597&r1=467596&r2=467597
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediator.java
(original)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediator.java
Wed Oct 25 03:06:09 2006
@@ -28,28 +28,40 @@
import org.apache.synapse.mediators.bsf.convertors.OMElementConvertor;
/**
- * A Synapse mediator that calls a function in any scripting language
supportted by BSF.
+ * A Synapse mediator that calls a function in any scripting language
supported by BSF. The ScriptMediator using a registry property to define the
+ * registry property which contains the script source.
+ * <p>
+ *
+ * <pre>
+ * <script key="property-key"
function="script-function-name" <script/>
+ * </pre>
+ *
+ * <p>
+ * The function is an optional attribute defining the name of the script
function to call, if not specified it defaults to a function named 'mediate'.
+ * The function takes a single parameter which is the Synapse MessageContext.
The function may return a boolean, if it does not then true is assumed.
*/
public class ScriptMediator extends AbstractMediator {
- private String scriptKey;
-
- private String functionName;
+ protected String scriptKey;
+
+ protected String functionName;
+
+ protected BSFEngine bsfEngine;
- private BSFEngine bsfEngine;
-
protected OMElementConvertor convertor;
+ protected BSFManager bsfManager;
+
public ScriptMediator(String scriptKey, String functionName) {
this.scriptKey = scriptKey;
this.functionName = functionName;
+ bsfManager = new BSFManager();
}
public boolean mediate(MessageContext synCtx) {
try {
- SynapseConfiguration synapseConfig = synCtx.getConfiguration();
- BSFEngine engine = getBSFEngine(synapseConfig);
+ BSFEngine engine = getBSFEngine(synCtx.getConfiguration());
Object[] args = new Object[] { new ScriptMessageContext(synCtx,
convertor) };
@@ -68,8 +80,8 @@
public synchronized BSFEngine getBSFEngine(SynapseConfiguration
synapseConfig) {
Property dp = synapseConfig.getPropertyObject(scriptKey);
-// boolean requiresRefresh = (dp != null) && (!dp.isCached() ||
dp.isExpired());
-// if (bsfEngine == null || requiresRefresh) { TODO: sort out caching
+ // boolean requiresRefresh = (dp != null) && (!dp.isCached() ||
dp.isExpired());
+ // if (bsfEngine == null || requiresRefresh) { TODO: sort out caching
if (bsfEngine == null) {
OMElement el = (OMElement) synapseConfig.getProperty(scriptKey);
String scriptSrc = el.getText();
@@ -85,9 +97,6 @@
protected BSFEngine createBSFEngine(String scriptName, String scriptSrc) {
try {
- BSFManager bsfManager = new BSFManager();
- bsfManager.setClassLoader(BSFManager.class.getClassLoader());
-
String scriptLanguage = BSFManager.getLangFromFilename(scriptName);
BSFEngine bsfEngine =
bsfManager.loadScriptingEngine(scriptLanguage);
bsfEngine.exec(scriptName, 0, 0, scriptSrc);
@@ -103,11 +112,11 @@
OMElementConvertor oc = null;
int lastDot = scriptName.lastIndexOf('.');
if (lastDot > -1) {
- String suffix = scriptName.substring(lastDot+1).toUpperCase();
+ String suffix = scriptName.substring(lastDot + 1).toUpperCase();
String className = OMElementConvertor.class.getName();
int i = className.lastIndexOf('.');
- String packageName = className.substring(0, i+1);
- String convertorClassName = packageName + suffix +
className.substring(i+1);
+ String packageName = className.substring(0, i + 1);
+ String convertorClassName = packageName + suffix +
className.substring(i + 1);
try {
oc = (OMElementConvertor) Class.forName(convertorClassName,
true, getClass().getClassLoader()).newInstance();
} catch (Exception e) {
Modified:
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediatorFactory.java
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediatorFactory.java?view=diff&rev=467597&r1=467596&r2=467597
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediatorFactory.java
(original)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediatorFactory.java
Wed Oct 25 03:06:09 2006
@@ -26,11 +26,43 @@
/**
* Creates an instance of a Script mediator. <p/>
- *
+ * <p>
+ * There are two ways of defining a script mediator, either using a registry
property or
+ * inline in the Synapse config XML.
+ * <p>
+ * A script mediator using a registry property is defined as follows:
+ * <p>
* <pre>
* <script key="property-key"
function="script-function-name" <script/>
* </pre>
- *
+ * <p>
+ * The property-key is a Synapse registry property containing the script
source. The function is an
+ * optional attribute defining the name of the script function to call, if not
specified it
+ * defaults to a function named 'mediate'. The function takes a single
parameter which is the
+ * Synapse MessageContext. The function may return a boolean, if it does not
then true is assumed.
+ * <p>
+ * An inline script mediator has the script source embedded in the config XML:
+ * <pre>
+ * <script.LL>...src code...<script.LL/>
+ * </pre>
+ * <p>
+ * where LL is the script language name extension. The environment of the
script has the Synapse
+ * MessageContext predefined in a script variable named 'mc'.
+ * <p>
+ * An example of an inline mediator using JavaScript/E4X which returns false
if the SOAP message
+ * body contains an element named 'symbol' which has a value of 'IBM' would be:
+ * <p>
+ * <pre>
+ * <script.js>mc.getPayloadXML()..symbol != "IBM";<script.js/>
+ * </pre>
+ * <p>
+ * The boolean response from the inlined mediator is either the response from
the evaluation of the
+ * script statements or if that result is not a boolean then a response of
true is assumed.
+ * <p>
+ * The MessageContext passed in to the script mediator has additional methods
over the Synapse
+ * MessageContext to enable working with the XML in a way natural to the
scripting language. For
+ * example when using JavaScript get/setPayloadXML use E4X XML objects, when
using Ruby they
+ * use REXML documents.
*/
public class ScriptMediatorFactory implements MediatorFactory {
@@ -38,15 +70,20 @@
public Mediator createMediator(OMElement elem) {
+ ScriptMediator sm;
+
OMAttribute scriptKey = elem.getAttribute(new
QName(Constants.NULL_NAMESPACE, "key"));
- if (scriptKey == null) {
- throw new SynapseException("must specify 'key' for script source");
+ if (scriptKey != null) {
+ OMAttribute function = elem.getAttribute(new
QName(Constants.NULL_NAMESPACE, "function"));
+ String functionName = (function == null) ? "mediate" :
function.getAttributeValue();
+ sm = new ScriptMediator(scriptKey.getAttributeValue(),
functionName);
+ } else if (elem.getLocalName().indexOf('.') > -1){
+ sm = new InlineScriptMediator(elem.getLocalName(), elem.getText());
+ ((InlineScriptMediator)sm).init();
+ } else {
+ throw new SynapseException("must specify 'key' attribute or inline
script source");
}
- OMAttribute function = elem.getAttribute(new
QName(Constants.NULL_NAMESPACE, "function"));
- String functionName = (function == null) ? "mediate" :
function.getAttributeValue();
-
- ScriptMediator sm = new ScriptMediator(scriptKey.getAttributeValue(),
functionName);
return sm;
}
Modified:
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMessageContext.java
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMessageContext.java?view=diff&rev=467597&r1=467596&r2=467597
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMessageContext.java
(original)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMessageContext.java
Wed Oct 25 03:06:09 2006
@@ -29,7 +29,7 @@
import org.apache.synapse.mediators.bsf.convertors.OMElementConvertor;
/**
- * ScriptMessageContext decorates the Synapse MessageContext adding methods to
use the message payload XML.
+ * ScriptMessageContext decorates the Synapse MessageContext adding methods to
use the message payload XML in a way natural to the scripting language.
*/
public class ScriptMessageContext implements MessageContext {
@@ -58,7 +58,7 @@
* @param payload
* @throws XMLStreamException
*/
- public void setPayloadXML(Object payload) throws XMLStreamException {
+ public void setPayloadXML(Object payload) {
mc.getEnvelope().getBody().setFirstChild(convertor.fromScript(payload));
}
Added:
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ThreadLocalMessageContext.java
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ThreadLocalMessageContext.java?view=auto&rev=467597
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ThreadLocalMessageContext.java
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ThreadLocalMessageContext.java
Wed Oct 25 03:06:09 2006
@@ -0,0 +1,224 @@
+/*
+ * 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;
+
+import java.util.Set;
+
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.addressing.EndpointReference;
+import org.apache.axis2.addressing.RelatesTo;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.config.SynapseConfiguration;
+import org.apache.synapse.core.SynapseEnvironment;
+
+/**
+ * This delegates all method calls to thread specific MessageContext.
+ *
+ * This is required for the InlineScriptMediator to enable concurrent requests
to run against the same inline script. As there is a single BSFEngine
+ * holding the inline script fragment and the MessageContext is pre-registered
in that BSFEngine means there is a single MessageContext instance
+ * shared over all requests. Using this class as the single pre-registered
MessageContext enables delegating all the method calls to a thread specific
+ * MessageContext instance that is registerd on the thread just prior to the
script being invoked.
+ */
+public class ThreadLocalMessageContext implements MessageContext {
+
+ private static ThreadLocal threadLocalMC = new ThreadLocal();
+
+ public static void setMC(ScriptMessageContext mc) {
+ threadLocalMC.set(mc);
+ }
+
+ public ScriptMessageContext getMC() {
+ return (ScriptMessageContext) threadLocalMC.get();
+ }
+
+ // non-MessageContext helpers on the ScriptMessageContext class
+
+ public Object getPayloadXML() {
+ return getMC().getPayloadXML();
+ }
+
+ public void setPayloadXML(Object payload) {
+ getMC().setPayloadXML(payload);
+ }
+
+ public Object getEnvelopeXML() {
+ return getMC().getEnvelopeXML();
+ }
+
+ public void setTo(String reference) {
+ getMC().setTo(reference);
+ }
+
+ public void setFaultTo(String reference) {
+ getMC().setFaultTo(reference);
+ }
+
+ public void setFrom(String reference) {
+ getMC().setFrom(reference);
+ }
+
+ public void setReplyTo(String reference) {
+ getMC().setReplyTo(reference);
+ }
+
+ // -- all the MessageContext interface methods
+
+ public SynapseConfiguration getConfiguration() {
+ return getMC().getConfiguration();
+ }
+
+ public SOAPEnvelope getEnvelope() {
+ return getMC().getEnvelope();
+ }
+
+ public SynapseEnvironment getEnvironment() {
+ return getMC().getEnvironment();
+ }
+
+ public EndpointReference getFaultTo() {
+ return getMC().getFaultTo();
+ }
+
+ public EndpointReference getFrom() {
+ return getMC().getFrom();
+ }
+
+ public String getMessageID() {
+ return getMC().getMessageID();
+ }
+
+ public String getMessageId() {
+ return getMC().getMessageId();
+ }
+
+ public Object getProperty(String key) {
+ return getMC().getProperty(key);
+ }
+
+ public Set getPropertyKeySet() {
+ return getMC().getPropertyKeySet();
+ }
+
+ public RelatesTo getRelatesTo() {
+ return getMC().getRelatesTo();
+ }
+
+ public EndpointReference getReplyTo() {
+ return getMC().getReplyTo();
+ }
+
+ public String getSoapAction() {
+ return getMC().getSoapAction();
+ }
+
+ public EndpointReference getTo() {
+ return getMC().getTo();
+ }
+
+ public String getWSAAction() {
+ return getMC().getWSAAction();
+ }
+
+ public boolean isDoingMTOM() {
+ return getMC().isDoingMTOM();
+ }
+
+ public boolean isDoingREST() {
+ return getMC().isDoingREST();
+ }
+
+ public boolean isFaultResponse() {
+ return getMC().isFaultResponse();
+ }
+
+ public boolean isResponse() {
+ return getMC().isResponse();
+ }
+
+ public boolean isSOAP11() {
+ return getMC().isSOAP11();
+ }
+
+ public void setConfiguration(SynapseConfiguration cfg) {
+ getMC().setConfiguration(cfg);
+ }
+
+ public void setDoingMTOM(boolean b) {
+ getMC().setDoingMTOM(b);
+ }
+
+ public void setDoingREST(boolean b) {
+ getMC().setDoingREST(b);
+ }
+
+ public void setEnvelope(SOAPEnvelope envelope) throws AxisFault {
+ getMC().setEnvelope(envelope);
+ }
+
+ public void setEnvironment(SynapseEnvironment se) {
+ getMC().setEnvironment(se);
+ }
+
+ public void setFaultResponse(boolean b) {
+ getMC().setFaultResponse(b);
+ }
+
+ public void setFaultTo(EndpointReference reference) {
+ getMC().setFaultTo(reference);
+ }
+
+ public void setFrom(EndpointReference reference) {
+ getMC().setFrom(reference);
+ }
+
+ public void setMessageID(String string) {
+ getMC().setMessageID(string);
+ }
+
+ public void setMessageId(String messageID) {
+ getMC().setMessageId(messageID);
+ }
+
+ public void setProperty(String key, Object value) {
+ getMC().setProperty(key, value);
+ }
+
+ public void setRelatesTo(RelatesTo[] reference) {
+ getMC().setRelatesTo(reference);
+ }
+
+ public void setReplyTo(EndpointReference reference) {
+ getMC().setReplyTo(reference);
+ }
+
+ public void setResponse(boolean b) {
+ getMC().setResponse(b);
+ }
+
+ public void setSoapAction(String string) {
+ getMC().setSoapAction(string);
+ }
+
+ public void setTo(EndpointReference reference) {
+ getMC().setTo(reference);
+ }
+
+ public void setWSAAction(String actionURI) {
+ getMC().setWSAAction(actionURI);
+ }
+
+}
Modified:
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/DefaultOMElementConvertor.java
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/DefaultOMElementConvertor.java?view=diff&rev=467597&r1=467596&r2=467597
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/DefaultOMElementConvertor.java
(original)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/DefaultOMElementConvertor.java
Wed Oct 25 03:06:09 2006
@@ -47,8 +47,6 @@
}
public void setEngine(BSFEngine e) {
- // TODO Auto-generated method stub
-
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]