Author: antelder
Date: Sat Oct 21 09:47:21 2006
New Revision: 466426
URL: http://svn.apache.org/viewvc?view=rev&rev=466426
Log:
Add support for E4X to the BSF mediator
Added:
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/DefaultOMElementConvertor.java
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/JSOMElementConvertor.java
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/OMElementConvertor.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/ScriptMessageContext.java
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=466426&r1=466425&r2=466426
==============================================================================
---
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
Sat Oct 21 09:47:21 2006
@@ -24,6 +24,8 @@
import org.apache.synapse.config.Property;
import org.apache.synapse.config.SynapseConfiguration;
import org.apache.synapse.mediators.AbstractMediator;
+import org.apache.synapse.mediators.bsf.convertors.DefaultOMElementConvertor;
+import org.apache.synapse.mediators.bsf.convertors.OMElementConvertor;
/**
* A Synapse mediator that calls a function in any scripting language
supportted by BSF.
@@ -35,6 +37,8 @@
private String functionName;
private BSFEngine bsfEngine;
+
+ protected OMElementConvertor convertor;
public ScriptMediator(String scriptKey, String functionName) {
this.scriptKey = scriptKey;
@@ -44,10 +48,12 @@
public boolean mediate(MessageContext synCtx) {
try {
- Object[] args = new Object[] { new ScriptMessageContext(synCtx) };
SynapseConfiguration synapseConfig = synCtx.getConfiguration();
+ BSFEngine engine = getBSFEngine(synapseConfig);
+
+ Object[] args = new Object[] { new ScriptMessageContext(synCtx,
convertor) };
- Object response = getBSFEngine(synapseConfig).call(null,
functionName, args);
+ Object response = engine.call(null, functionName, args);
if (response instanceof Boolean) {
return ((Boolean) response).booleanValue();
}
@@ -67,7 +73,9 @@
if (bsfEngine == null || requiresRefresh) {
OMElement el = (OMElement) synapseConfig.getProperty(scriptKey);
String scriptSrc = el.getText();
- this.bsfEngine = createBSFEngine(dp.getSrc().toString(),
scriptSrc);
+ String scriptName = dp.getSrc().toString();
+ this.bsfEngine = createBSFEngine(scriptName, scriptSrc);
+ this.convertor = createOMElementConvertor(scriptName);
}
return bsfEngine;
@@ -88,6 +96,27 @@
} catch (BSFException e) {
throw new SynapseException(e.getTargetException());
}
+ }
+
+ protected OMElementConvertor createOMElementConvertor(String scriptName) {
+ OMElementConvertor oc = null;
+ int lastDot = scriptName.lastIndexOf('.');
+ if (lastDot > -1) {
+ 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);
+ try {
+ oc = (OMElementConvertor) Class.forName(convertorClassName,
true, getClass().getClassLoader()).newInstance();
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+ if (oc == null) {
+ oc = new DefaultOMElementConvertor();
+ }
+ return oc;
}
}
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=466426&r1=466425&r2=466426
==============================================================================
---
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
Sat Oct 21 09:47:21 2006
@@ -15,13 +15,10 @@
*/
package org.apache.synapse.mediators.bsf;
-import java.io.ByteArrayInputStream;
import java.util.Set;
import javax.xml.stream.XMLStreamException;
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
@@ -29,6 +26,7 @@
import org.apache.synapse.MessageContext;
import org.apache.synapse.config.SynapseConfiguration;
import org.apache.synapse.core.SynapseEnvironment;
+import org.apache.synapse.mediators.bsf.convertors.OMElementConvertor;
/**
* ScriptMessageContext decorates the Synapse MessageContext adding methods to
use the message payload XML.
@@ -37,18 +35,20 @@
private MessageContext mc;
- public ScriptMessageContext(MessageContext mc) {
+ private OMElementConvertor convertor;
+
+ public ScriptMessageContext(MessageContext mc, OMElementConvertor
convertor) {
this.mc = mc;
+ this.convertor = convertor;
}
/**
- * Get the SOAP Body payload.
- * The payload is the first element inside the SOAP <Body> tags
+ * Get the SOAP Body payload. The payload is the first element inside the
SOAP <Body> tags
*
* @return the XML SOAP Body
*/
- public String getPayloadXML() {
- return mc.getEnvelope().getBody().getFirstElement().toString();
+ public Object getPayloadXML() {
+ return
convertor.toScript(mc.getEnvelope().getBody().getFirstElement());
}
/**
@@ -58,10 +58,22 @@
* @throws XMLStreamException
*/
public void setPayloadXML(Object payload) throws XMLStreamException {
- byte[] xmlBytes = payload.toString().getBytes();
- StAXOMBuilder builder = new StAXOMBuilder(new
ByteArrayInputStream(xmlBytes));
- OMElement omElement = builder.getDocumentElement();
- mc.getEnvelope().getBody().setFirstChild(omElement);
+
mc.getEnvelope().getBody().setFirstChild(convertor.fromScript(payload));
+ }
+
+ // helpers to set EPRs from a script string
+
+ public void setTo(String reference) {
+ mc.setTo(new EndpointReference(reference));
+ }
+ public void setFaultTo(String reference) {
+ mc.setFaultTo(new EndpointReference(reference));
+ }
+ public void setFrom(String reference) {
+ mc.setFrom(new EndpointReference(reference));
+ }
+ public void setReplyTo(String reference) {
+ mc.setReplyTo(new EndpointReference(reference));
}
// -- all the remainder just use the underlying MessageContext
Added:
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=auto&rev=466426
==============================================================================
---
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/convertors/DefaultOMElementConvertor.java
Sat Oct 21 09:47:21 2006
@@ -0,0 +1,48 @@
+/*
+ * 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 java.io.ByteArrayInputStream;
+
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.synapse.SynapseException;
+
+/**
+ * The DefaultOMElementConvertor converts between Synapse OMElements and
Strings
+ */
+public class DefaultOMElementConvertor implements OMElementConvertor {
+
+ public OMElement fromScript(Object o) {
+ try {
+
+ byte[] xmlBytes = o.toString().getBytes();
+ StAXOMBuilder builder = new StAXOMBuilder(new
ByteArrayInputStream(xmlBytes));
+ OMElement omElement = builder.getDocumentElement();
+ return omElement;
+
+ } catch (XMLStreamException e) {
+ throw new SynapseException(e);
+ }
+ }
+
+ public Object toScript(OMElement omElement) {
+ return omElement.toString();
+ }
+
+}
Added:
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/JSOMElementConvertor.java
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/JSOMElementConvertor.java?view=auto&rev=466426
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/JSOMElementConvertor.java
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/JSOMElementConvertor.java
Sat Oct 21 09:47:21 2006
@@ -0,0 +1,92 @@
+/*
+ * 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 java.io.ByteArrayInputStream;
+
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.synapse.SynapseException;
+import org.apache.xmlbeans.XmlObject;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.Wrapper;
+import org.mozilla.javascript.xml.XMLObject;
+
+/**
+ * JSObjectConvertor converts between Synapse OMElements and JavaScript E4X
XML objects
+ */
+public class JSOMElementConvertor extends DefaultOMElementConvertor {
+
+ protected Scriptable scope;
+
+ public JSOMElementConvertor() {
+ Context cx = Context.enter();
+ try {
+ this.scope = cx.initStandardObjects();
+ } finally {
+ Context.exit();
+ }
+ }
+
+ public Object toScript(OMElement o) {
+ XmlObject xml;
+ try {
+ xml = XmlObject.Factory.parse(new
ByteArrayInputStream(o.toString().getBytes()));
+ } catch (Exception e) {
+ throw new SynapseException("exception getting message XML: " + e);
+ }
+
+ Context cx = Context.enter();
+ try {
+
+ Object wrappedXML = cx.getWrapFactory().wrap(cx, scope, xml,
XmlObject.class);
+ Scriptable jsXML = cx.newObject(scope, "XML", new Object[] {
wrappedXML });
+
+ return jsXML;
+
+ } finally {
+ Context.exit();
+ }
+ }
+
+ public OMElement fromScript(Object o) {
+ if (!(o instanceof XMLObject)) {
+ return super.fromScript(o);
+ }
+
+ // TODO: E4X Bug? Shouldn't need this copy, but without it the outer
element gets lost???
+ Scriptable jsXML = (Scriptable)
ScriptableObject.callMethod((Scriptable) o, "copy", new Object[0]);
+ Wrapper wrapper = (Wrapper) ScriptableObject.callMethod(jsXML,
"getXmlObject", new Object[0]);
+ Object response = wrapper.unwrap();
+
+ try {
+
+ byte[] xmlBytes = response.toString().getBytes();
+ StAXOMBuilder builder = new StAXOMBuilder(new
ByteArrayInputStream(xmlBytes));
+ OMElement omElement = builder.getDocumentElement();
+
+ return omElement;
+
+ } catch (XMLStreamException e) {
+ throw new SynapseException(e);
+ }
+ }
+
+}
Added:
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/OMElementConvertor.java
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/OMElementConvertor.java?view=auto&rev=466426
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/OMElementConvertor.java
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/convertors/OMElementConvertor.java
Sat Oct 21 09:47:21 2006
@@ -0,0 +1,41 @@
+/*
+ * 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 org.apache.axiom.om.OMElement;
+
+/**
+ * The OMElementConvertor interface enables customizing the conversion of
+ * XML between Synapse and a script language. Some script languages have their
+ * own ways of using XML, such as E4X in JavaScript or REXML in Ruby. But BSF
+ * has no support for those so Synapse needs to handle this itself, which is
what
+ * the OMElementConvertor does.
+ *
+ * Which OMElementConvertor type to use is discovered based on the file name
suffix of
+ * the mediator script. The suffix is converted to uppercase and used as the
prefix to
+ * the OMElementConvertor classname. For example, with a JavaScript script
named myscript.js
+ * the .js suffix is taken to make the convertor class name
+ * "org.apache.synapse.mediators.bsf.convertors.JSOMElementConvertor"
+ * If the convertor class is not found then a default convertor is used which
converts
+ * XML to a String representation.
+ */
+public interface OMElementConvertor {
+
+ public Object toScript(OMElement omElement);
+
+ public OMElement fromScript(Object o);
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]