Skip to content

Commit

Permalink
Merge pull request #2215 from arunans23/scriptfix
Browse files Browse the repository at this point in the history
Create new synapse property to change default js engine for script mediator
  • Loading branch information
arunans23 authored Sep 13, 2024
2 parents 91cfeba + 82101f0 commit 0ff6ec5
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import static org.apache.synapse.mediators.bsf.ScriptMediatorConstants.JAVA_SCRIPT;
import static org.apache.synapse.mediators.bsf.ScriptMediatorConstants.MC_VAR_NAME;
import static org.apache.synapse.mediators.bsf.ScriptMediatorConstants.NASHORN;
import static org.apache.synapse.mediators.bsf.ScriptMediatorConstants.NASHORN_JAVA_SCRIPT;
import static org.apache.synapse.mediators.bsf.ScriptMediatorConstants.ORACLE_NASHORN_NAME;
import static org.apache.synapse.mediators.bsf.ScriptMediatorConstants.POOL_SIZE_PROPERTY;
import static org.apache.synapse.mediators.bsf.ScriptMediatorConstants.RHINO_JAVA_SCRIPT;
import static org.apache.synapse.mediators.bsf.access.control.AccessControlConstants.CLASS_PREFIXES;
import static org.apache.synapse.mediators.bsf.access.control.AccessControlConstants.ENABLE;
import static org.apache.synapse.mediators.bsf.access.control.AccessControlConstants.LIMIT_CLASS_ACCESS_PREFIX;
Expand Down Expand Up @@ -89,41 +96,6 @@ public class ScriptMediator extends AbstractMediator {

private static final Log logger = LogFactory.getLog(ScriptMediator.class.getName());

/**
* The name of the variable made available to the scripting language to access the message
*/
private static final String MC_VAR_NAME = "mc";

/**
* Name of the java script language
*/
private static final String JAVA_SCRIPT = "js";

/**
* Name of the java script language with usage of nashorn engine.
*/
private static final String NASHORN_JAVA_SCRIPT = "nashornJs";

/**
*
*/
private static final String RHINO_JAVA_SCRIPT = "rhinoJs";

/**
* Name of the nashorn java script engine.
*/
private static final String NASHORN = "nashorn";

/**
* Name of the graalvm js engine.
*/
private static final String GRAALVM = "graal.js";

/**
* Factory Name for Oracle Nashorn Engine. Built-in Nashorn engine in JDK 8 to JDK 11
*/
private static final String ORACLE_NASHORN_NAME = "Oracle Nashorn";

/**
* The registry entry key for a script loaded from the registry
* Handle both static and dynamic(Xpath) Keys
Expand Down Expand Up @@ -178,10 +150,6 @@ public class ScriptMediator extends AbstractMediator {
* Pool size
*/
private int poolSize = DEFAULT_POOL_SIZE;
/**
* Pool size property name
*/
private static String POOL_SIZE_PROPERTY = "synapse.script.mediator.pool.size";
/**
* Pool ScriptEngine Resources
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you 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;

public class ScriptMediatorConstants {

/**
* The name of the variable made available to the scripting language to access the message
*/
public static final String MC_VAR_NAME = "mc";

/**
* Name of the java script language
*/
public static final String JAVA_SCRIPT = "js";

/**
* Name of the java script language with usage of nashorn engine.
*/
public static final String NASHORN_JAVA_SCRIPT = "nashornJs";

/**
* Name of the java script language with usage of rhino engine.
*/
public static final String RHINO_JAVA_SCRIPT = "rhinoJs";

/**
* Name of the java script language with usage of rhino engine.
*/
public static final String GRAAL_JAVA_SCRIPT = "graalJs";

/**
* Name of the nashorn java script engine.
*/
public static final String NASHORN = "nashorn";

/**
* Name of the graalvm js engine.
*/
public static final String GRAALVM = "graal.js";

/**
* Factory Name for Oracle Nashorn Engine. Built-in Nashorn engine in JDK 8 to JDK 11
*/
public static final String ORACLE_NASHORN_NAME = "Oracle Nashorn";

/**
* Pool size property name
*/
public static String POOL_SIZE_PROPERTY = "synapse.script.mediator.pool.size";

/**
* Default Script Engine
*/
public static String DEFAULT_SCRIPT_ENGINE = "synapse.script.mediator.default.engine";
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.synapse.Mediator;
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.SynapseException;
import org.apache.synapse.config.SynapsePropertiesLoader;
import org.apache.synapse.config.xml.AbstractMediatorFactory;
import org.apache.synapse.config.xml.XMLConfigConstants;
import org.apache.synapse.mediators.Value;
Expand All @@ -37,6 +38,11 @@
import java.util.TreeMap;
import java.util.*;

import static org.apache.synapse.mediators.bsf.ScriptMediatorConstants.DEFAULT_SCRIPT_ENGINE;
import static org.apache.synapse.mediators.bsf.ScriptMediatorConstants.GRAAL_JAVA_SCRIPT;
import static org.apache.synapse.mediators.bsf.ScriptMediatorConstants.JAVA_SCRIPT;
import static org.apache.synapse.mediators.bsf.ScriptMediatorConstants.RHINO_JAVA_SCRIPT;

/**
* Creates an instance of a Script mediator for inline or external script mediation for BSF
* scripting languages.
Expand Down Expand Up @@ -106,7 +112,13 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) {
mediator = new ScriptMediator(langAtt.getAttributeValue(),
includeKeysMap, generatedKey, functionName,classLoader);
} else {
mediator = new ScriptMediator(langAtt.getAttributeValue(), elem.getText(),classLoader);
String language = langAtt.getAttributeValue();
if (language.equals(JAVA_SCRIPT) &&
(RHINO_JAVA_SCRIPT.equals(SynapsePropertiesLoader.getPropertyValue(
DEFAULT_SCRIPT_ENGINE, GRAAL_JAVA_SCRIPT)))) {
language = RHINO_JAVA_SCRIPT;
}
mediator = new ScriptMediator(language, elem.getText(),classLoader);
}

processAuditStatus(mediator, elem);
Expand Down

0 comments on commit 0ff6ec5

Please sign in to comment.