Author: ruwan
Date: Wed Sep 5 18:45:31 2007
New Revision: 573130
URL: http://svn.apache.org/viewvc?rev=573130&view=rev
Log:
Commiting Upul's patch on SYNAPSE-126 with minor reformatting
Modified:
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediatorFactory.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?rev=573130&r1=573129&r2=573130&view=diff
==============================================================================
---
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
Wed Sep 5 18:45:31 2007
@@ -30,6 +30,9 @@
import org.apache.synapse.mediators.AbstractMediator;
import javax.script.*;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.Iterator;
/**
* A Synapse mediator that calls a function in any scripting language
supported by the BSF.
@@ -67,6 +70,10 @@
*/
private String language;
/**
+ * The map of included scripts; key = registry entry key, value = script
source
+ */
+ private Map includes = new TreeMap();
+ /**
* The optional name of the function to be invoked, defaults to mediate
*/
private String function = "mediate";
@@ -117,9 +124,10 @@
* @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) {
+ public ScriptMediator(String language, Map includeKeysMap, String key,
String function) {
this.language = language;
this.key = key;
+ this.includes = includeKeysMap;
if (function != null) {
this.function = function;
}
@@ -278,6 +286,27 @@
scriptEngine.eval(scriptSourceCode);
}
}
+
+ // load <include /> scripts; reload each script if needed
+ for(Iterator iter = includes.keySet().iterator(); iter.hasNext();) {
+ String includeKey = (String) iter.next();
+ String includeSourceCode = (String) includes.get(includeKey);
+ Entry includeEntry =
synCtx.getConfiguration().getEntryDefinition(includeKey);
+ boolean includeEntryNeedsReload = (entry != null) &&
entry.isDynamic()
+ && (!entry.isCached() || entry.isExpired());
+ synchronized (resourceLock) {
+ if (includeSourceCode == null || needsReload) {
+ Object o = synCtx.getEntry(includeKey);
+ if (o instanceof OMElement) {
+ includeSourceCode = ((OMElement) (o)).getText();
+ } else if (o instanceof String) {
+ includeSourceCode = (String) o;
+ }
+ includes.put(includeKey, includeSourceCode);
+ scriptEngine.eval(includeSourceCode);
+ }
+ }
+ }
}
protected void initScriptEngine() {
Modified:
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediatorFactory.java
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediatorFactory.java?rev=573130&r1=573129&r2=573130&view=diff
==============================================================================
---
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediatorFactory.java
(original)
+++
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediatorFactory.java
Wed Sep 5 18:45:31 2007
@@ -28,6 +28,10 @@
import org.apache.synapse.config.xml.AbstractMediatorFactory;
import org.apache.synapse.config.xml.Constants;
+import java.util.Map;
+import java.util.Iterator;
+import java.util.TreeMap;
+
/**
* Creates an instance of a Script mediator for inline or external script
mediation for BSF
* scripting languages.
@@ -36,6 +40,7 @@
* <script [key="entry-key"]
* [function="script-function-name"]
language="javascript|groovy|ruby">
* (text | xml)?
+ * <include key="entry-key" />
* </script>
* </pre>
*
@@ -46,11 +51,16 @@
* 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.
+ *
+ * For external script mediation, that is when using key, function, language
attributes,
+ * <include key"entry-key" /> is used to include one or more
additional script files.
*/
public class ScriptMediatorFactory extends AbstractMediatorFactory {
private static final QName TAG_NAME = new
QName(Constants.SYNAPSE_NAMESPACE, "script");
+ private static final QName INCLUDE_Q = new
QName(Constants.SYNAPSE_NAMESPACE, "include");
+
public Mediator createMediator(OMElement elem) {
ScriptMediator mediator;
@@ -66,16 +76,41 @@
throw new SynapseException("Cannot use 'function' attribute
without 'key' attribute for a script mediator");
}
+ Map includeKeysMap = getIncludeKeysMap(elem);
+
if (keyAtt != null) {
String functionName = (funcAtt == null ? null :
funcAtt.getAttributeValue());
- mediator = new ScriptMediator(
- langAtt.getAttributeValue(), keyAtt.getAttributeValue(),
functionName);
+ mediator = new ScriptMediator(langAtt.getAttributeValue(),
+ includeKeysMap, keyAtt.getAttributeValue(), functionName);
} else {
mediator = new ScriptMediator(langAtt.getAttributeValue(),
elem.getText());
}
initMediator(mediator, elem);
return mediator;
+ }
+
+ private Map getIncludeKeysMap(OMElement elem) {
+ // get <include /> scripts
+ // map key = registry entry key, value = script source
+ // at this time map values are null, later loaded
+ // from void ScriptMediator.prepareExternalScript(MessageContext synCtx)
+
+ Map includeKeysMap = new TreeMap(); // TreeMap used to keep given
scripts order if needed
+ Iterator iter = elem.getChildrenWithName(INCLUDE_Q);
+ while (iter.hasNext()) {
+ OMElement includeElem = (OMElement) iter.next();
+ OMAttribute key = includeElem.getAttribute(new
QName(Constants.NULL_NAMESPACE, "key"));
+
+ if (key == null) {
+ throw new SynapseException("Cannot use 'include' element
without 'key' attribute for a script mediator");
+ }
+
+ String keyText = key.getAttributeValue();
+ includeKeysMap.put(keyText, null);
+ }
+
+ return includeKeysMap;
}
public QName getTagQName() {
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]