Author: antelder
Date: Sat Oct 21 06:35:31 2006
New Revision: 466397
URL: http://svn.apache.org/viewvc?view=rev&rev=466397
Log:
Srcipt mediator based on BSF and a sample for it
Added:
incubator/synapse/trunk/scratch/ant/BSF/
incubator/synapse/trunk/scratch/ant/BSF/repository/
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/synapse_sample_js.xml
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/synapse_sample_script.xml
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/xmethodsStockquoteIn.js
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/xmethodsStockquoteOut.js
incubator/synapse/trunk/scratch/ant/BSF/src/
incubator/synapse/trunk/scratch/ant/BSF/src/META-INF/
incubator/synapse/trunk/scratch/ant/BSF/src/META-INF/services/
incubator/synapse/trunk/scratch/ant/BSF/src/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
incubator/synapse/trunk/scratch/ant/BSF/src/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer
incubator/synapse/trunk/scratch/ant/BSF/src/org/
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/
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
Added:
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/synapse_sample_js.xml
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/synapse_sample_js.xml?view=auto&rev=466397
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/synapse_sample_js.xml
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/synapse_sample_js.xml
Sat Oct 21 06:35:31 2006
@@ -0,0 +1,35 @@
+<synapse xmlns="http://ws.apache.org/ns/synapse"
xmlns:js="http://ws.apache.org/ns/synapse/js">
+ <rules>
+
+ <in>
+ <js:javascript><![CDATA[
+ function mediate(msg) {
+ msg.setPayloadXML(
+ <ns1:getQuote xmlns:ns1="urn:xmethods-delayed-quotes"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
+ <symbol xsi:type="xsd:string"> {
msg.getPayloadXML()..*::symbol.toString() } </symbol>
+ </ns1:getQuote>);
+ }
+ ]]></js:javascript>
+
+ <header name="To" value="http://64.124.140.30:9090/soap"/>
+
+ </in>
+
+ <out>
+ <js:javascript><![CDATA[
+ function mediate(msg) {
+ msg.setPayloadXML(
+ <ns:getQuoteResponse xmlns:ns="http://services.samples/xsd">
+ <ns:return>
+ <ns:last> { msg.getPayloadXML()..Result.toString() }
</ns:last>
+ </ns:return>
+ </ns:getQuoteResponse>);
+ }
+ ]]></js:javascript>
+ </out>
+
+ <send/>
+
+ </rules>
+</synapse>
+
Added:
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/synapse_sample_script.xml
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/synapse_sample_script.xml?view=auto&rev=466397
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/synapse_sample_script.xml
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/synapse_sample_script.xml
Sat Oct 21 06:35:31 2006
@@ -0,0 +1,25 @@
+<synapse xmlns="http://ws.apache.org/ns/synapse">
+
+ <definitions>
+
+ <set-property name="xmethodsStockquoteIn"
src="file:synapse_repository/conf/sample/xmethodsStockquoteIn.js"/>
+ <set-property name="xmethodsStockquoteOut"
src="file:synapse_repository/conf/sample/xmethodsStockquoteOut.js"/>
+
+ </definitions>
+
+ <rules>
+
+ <in>
+ <script key="xmethodsStockquoteIn"/>
+ <header name="To" value="http://64.124.140.30:9090/soap"/>
+ </in>
+
+ <out>
+ <script key="xmethodsStockquoteOut"/>
+ </out>
+
+ <send/>
+
+ </rules>
+
+</synapse>
\ No newline at end of file
Added:
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/xmethodsStockquoteIn.js
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/xmethodsStockquoteIn.js?view=auto&rev=466397
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/xmethodsStockquoteIn.js
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/xmethodsStockquoteIn.js
Sat Oct 21 06:35:31 2006
@@ -0,0 +1,14 @@
+<x><![CDATA[
+
+ function mediate(msg) {
+
+ var payload = new XML(msg.getPayloadXML());
+ var newPayload = <ns1:getQuote xmlns:ns1="urn:xmethods-delayed-quotes"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
+ <symbol xsi:type="xsd:string"> {
payload..*::symbol.toString() } </symbol>
+ </ns1:getQuote>;
+ msg.setPayloadXML(newPayload);
+ }
+
+]]></x>
+
+
Added:
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/xmethodsStockquoteOut.js
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/xmethodsStockquoteOut.js?view=auto&rev=466397
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/xmethodsStockquoteOut.js
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/repository/conf/sample/xmethodsStockquoteOut.js
Sat Oct 21 06:35:31 2006
@@ -0,0 +1,19 @@
+<x><![CDATA[
+
+ function mediate(msg) {
+
+ var payload = new XML(msg.getPayloadXML());
+
+ var newPayload = <ns:getQuoteResponse
xmlns:ns="http://services.samples/xsd">
+ <ns:return>
+ <ns:last> { payload..Result.toString() } </ns:last>
+ </ns:return>
+ </ns:getQuoteResponse>;
+
+ msg.setPayloadXML(newPayload);
+ }
+
+
+]]></x>
+
+
Added:
incubator/synapse/trunk/scratch/ant/BSF/src/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/src/META-INF/services/org.apache.synapse.config.xml.MediatorFactory?view=auto&rev=466397
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
Sat Oct 21 06:35:31 2006
@@ -0,0 +1,6 @@
+org.apache.synapse.mediators.validate.ValidateMediatorFactory
+org.apache.synapse.mediators.transform.XSLTMediatorFactory
+org.apache.synapse.mediators.spring.SpringMediatorFactory
+org.apache.synapse.mediators.json.JsonMediatorFactory
+org.apache.synapse.mediators.javascript.JavaScriptMediatorFactory
+org.apache.synapse.mediators.bsf.ScriptMediatorFactory
Added:
incubator/synapse/trunk/scratch/ant/BSF/src/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer
URL:
http://svn.apache.org/viewvc/incubator/synapse/trunk/scratch/ant/BSF/src/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer?view=auto&rev=466397
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer
Sat Oct 21 06:35:31 2006
@@ -0,0 +1,5 @@
+org.apache.synapse.mediators.validate.ValidateMediatorSerializer
+org.apache.synapse.mediators.transform.XSLTMediatorSerializer
+org.apache.synapse.mediators.spring.SpringMediatorSerializer
+org.apache.synapse.mediators.json.JsonMediatorSerializer
+org.apache.synapse.mediators.javascript.JavaScriptMediatorSerializer
Added:
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=auto&rev=466397
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediator.java
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediator.java
Sat Oct 21 06:35:31 2006
@@ -0,0 +1,90 @@
+/*
+ * 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.axiom.om.OMElement;
+import org.apache.bsf.BSFEngine;
+import org.apache.bsf.BSFException;
+import org.apache.bsf.BSFManager;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.config.Property;
+import org.apache.synapse.config.SynapseConfiguration;
+import org.apache.synapse.mediators.AbstractMediator;
+
+/**
+ * A Synapse mediator that calls a function in any scripting language
supportted by BSF.
+ */
+public class ScriptMediator extends AbstractMediator {
+
+ private String scriptKey;
+
+ private BSFEngine bsfEngine;
+
+ public ScriptMediator(String scriptKey) {
+ this.scriptKey = scriptKey;
+ }
+
+ public boolean mediate(MessageContext synCtx) {
+ try {
+
+ Object[] args = new Object[] { new ScriptMessageContext(synCtx) };
+ SynapseConfiguration synapseConfig = synCtx.getConfiguration();
+
+ Object response = getBSFEngine(synapseConfig).call(null,
"mediate", args);
+ if (response instanceof Boolean) {
+ return ((Boolean) response).booleanValue();
+ }
+
+ return true; // default to returning true
+
+ } catch (BSFException e) {
+ throw new SynapseException(e);
+ }
+ }
+
+ public synchronized BSFEngine getBSFEngine(SynapseConfiguration
synapseConfig) {
+
+ Property dp = synapseConfig.getPropertyObject(scriptKey);
+ boolean requiresRefresh = (dp != null) && (!dp.isCached() ||
dp.isExpired());
+
+ if (bsfEngine == null || requiresRefresh) {
+ OMElement el = (OMElement) synapseConfig.getProperty(scriptKey);
+ String scriptSrc = el.getText();
+ this.bsfEngine = createBSFEngine(dp.getSrc().toString(),
scriptSrc);
+ }
+
+ return bsfEngine;
+ }
+
+ public 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);
+
+ return bsfEngine;
+
+ } catch (BSFException e) {
+ throw new SynapseException(e.getTargetException());
+ }
+ }
+
+}
Added:
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=auto&rev=466397
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediatorFactory.java
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMediatorFactory.java
Sat Oct 21 06:35:31 2006
@@ -0,0 +1,52 @@
+/*
+ * 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 javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.config.xml.Constants;
+import org.apache.synapse.config.xml.MediatorFactory;
+
+/**
+ * Creates an instance of a Script mediator. <p/>
+ *
+ * <pre>
+ * <script key="property-key" <script/>
+ * </pre>
+ *
+ */
+public class ScriptMediatorFactory implements MediatorFactory {
+
+ private static final QName TAG_NAME = new
QName(Constants.SYNAPSE_NAMESPACE, "script");
+
+ public Mediator createMediator(OMElement elem) {
+
+ OMAttribute scriptKey = elem.getAttribute(new
QName(Constants.NULL_NAMESPACE, "key"));
+ if (scriptKey == null) {
+ throw new SynapseException("must specify 'key' for script source");
+ }
+ ScriptMediator sm = new ScriptMediator(scriptKey.getAttributeValue());
+ return sm;
+ }
+
+ public QName getTagQName() {
+ return TAG_NAME;
+ }
+}
Added:
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=auto&rev=466397
==============================================================================
---
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMessageContext.java
(added)
+++
incubator/synapse/trunk/scratch/ant/BSF/src/org/apache/synapse/mediators/bsf/ScriptMessageContext.java
Sat Oct 21 06:35:31 2006
@@ -0,0 +1,213 @@
+/*
+ * 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.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;
+import org.apache.axis2.addressing.RelatesTo;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.config.SynapseConfiguration;
+import org.apache.synapse.core.SynapseEnvironment;
+
+/**
+ * ScriptMessageContext decorates the Synapse MessageContext adding methods to
use the message payload XML.
+ */
+public class ScriptMessageContext implements MessageContext {
+
+ private MessageContext mc;
+
+ public ScriptMessageContext(MessageContext mc) {
+ this.mc = mc;
+ }
+
+ /**
+ * 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();
+ }
+
+ /**
+ * Set the SOAP body payload from a String
+ *
+ * @param payload
+ * @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);
+ }
+
+ // -- all the remainder just use the underlying MessageContext
+
+ public SynapseConfiguration getConfiguration() {
+ return mc.getConfiguration();
+ }
+
+ public void setConfiguration(SynapseConfiguration cfg) {
+ mc.setConfiguration(cfg);
+ }
+
+ public SynapseEnvironment getEnvironment() {
+ return mc.getEnvironment();
+ }
+
+ public void setEnvironment(SynapseEnvironment se) {
+ mc.setEnvironment(se);
+ }
+
+ public Object getProperty(String key) {
+ return mc.getConfiguration();
+ }
+
+ public void setProperty(String key, Object value) {
+ mc.setProperty(key, value);
+ }
+
+ public Set getPropertyKeySet() {
+ return mc.getPropertyKeySet();
+ }
+
+ public SOAPEnvelope getEnvelope() {
+ return mc.getEnvelope();
+ }
+
+ public void setEnvelope(SOAPEnvelope envelope) throws AxisFault {
+ mc.setEnvelope(envelope);
+ }
+
+ public EndpointReference getFaultTo() {
+ return mc.getFaultTo();
+ }
+
+ public void setFaultTo(EndpointReference reference) {
+ mc.setFaultTo(reference);
+ }
+
+ public EndpointReference getFrom() {
+ return mc.getFrom();
+ }
+
+ public void setFrom(EndpointReference reference) {
+ mc.setFrom(reference);
+ }
+
+ public String getMessageID() {
+ return mc.getMessageID();
+ }
+
+ public void setMessageID(String string) {
+ mc.setMessageID(string);
+ }
+
+ public RelatesTo getRelatesTo() {
+ return mc.getRelatesTo();
+ }
+
+ public void setRelatesTo(RelatesTo[] reference) {
+ mc.setRelatesTo(reference);
+ }
+
+ public EndpointReference getReplyTo() {
+ return mc.getReplyTo();
+ }
+
+ public void setReplyTo(EndpointReference reference) {
+ mc.setReplyTo(reference);
+ }
+
+ public EndpointReference getTo() {
+ return mc.getTo();
+ }
+
+ public void setTo(EndpointReference reference) {
+ mc.setTo(reference);
+ }
+
+ public void setWSAAction(String actionURI) {
+ mc.setWSAAction(actionURI);
+ }
+
+ public String getWSAAction() {
+ return mc.getWSAAction();
+ }
+
+ public String getSoapAction() {
+ return mc.getSoapAction();
+ }
+
+ public void setSoapAction(String string) {
+ mc.setSoapAction(string);
+ }
+
+ public void setMessageId(String messageID) {
+ mc.setMessageId(messageID);
+ }
+
+ public String getMessageId() {
+ return mc.getMessageId();
+ }
+
+ public boolean isDoingMTOM() {
+ return mc.isDoingMTOM();
+ }
+
+ public void setDoingMTOM(boolean b) {
+ mc.setDoingMTOM(b);
+ }
+
+ public boolean isDoingREST() {
+ return mc.isDoingREST();
+ }
+
+ public void setDoingREST(boolean b) {
+ mc.setDoingREST(b);
+ }
+
+ public boolean isSOAP11() {
+ return mc.isSOAP11();
+ }
+
+ public void setResponse(boolean b) {
+ mc.setResponse(b);
+ }
+
+ public boolean isResponse() {
+ return mc.isResponse();
+ }
+
+ public void setFaultResponse(boolean b) {
+ mc.setFaultResponse(b);
+ }
+
+ public boolean isFaultResponse() {
+ return mc.isFaultResponse();
+ }
+
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]