forked from folio-org/mod-camunda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for multiple scripting languages to the Script Engine
A Java utility (ScriptEngineUtility) is provided to share functionality between the scripts. This functionality is written in Java, which essentially allows the scripts to execute Java code without having to actually be Java. The arguments passed to and return results passed from the scripts are expected to be in JSON string format. The ScriptEngineUtility should be used to convert to/from the JSON string to a JSON object for the script to process. Two new exceptions are provided: - ScriptEngineUnsupported = Error when the language (by file extension) is not available in the ScriptEngineType enum. - ScriptEngineLoadFailed = Error when the engine does not exist (module is not added to the pom.xml or something went wrong while loading). A pre-processor is added for handling the passed scripts. This is necessary namely for Python, which must have the correct number of spaces/tabs prepended to it. Currently, this is set to two spaces (both the engine.py and the pre-process method must have the same design). Other languages can potentially utilize this pre-processor, but only Python currently gets pre-processed. The engine.* resource files (such as engine.js) provide the engine template. The util.* resource files (such as util.js) provide additional utilities that are language dependent and therefore are not available in the ScriptEngineUtility. The following languages are supported: - Javascript (.js) - Java (.java) - Python (.py) - Groovy (.groovy) The following languages could be supported, if problems are resolved: - Ruby (.rb) (via JRuby) - Perl (.pl) (via Rakudo) Ruby is currently not working due to `java.lang.NoSuchFieldError: O_TMPFILE`. More research is needed and an upstream bugreport is referenced below. Perl could potentially be made to work via Rakudo, but is there a maven repository to add to the pom.xml? The engine.rb and engine.pl are stubbed out and have not been tested. Once the languages are imported and working properly, those engine.* files may need to be updated. see: jruby/jruby#5334 see: https://rakudo.org/
- Loading branch information
Showing
28 changed files
with
371 additions
and
965 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/org/folio/rest/exception/ScriptEngineLoadFailed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.folio.rest.exception; | ||
|
||
public class ScriptEngineLoadFailed extends Exception { | ||
private static final long serialVersionUID = 5176444311634173282L; | ||
|
||
private static final String MESSAGE = "The Scripting Engine, %s, failed to load."; | ||
|
||
public ScriptEngineLoadFailed(String extension) { | ||
super(String.format(MESSAGE, extension)); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/folio/rest/exception/ScriptEngineUnsupported.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.folio.rest.exception; | ||
|
||
public class ScriptEngineUnsupported extends Exception { | ||
private static final long serialVersionUID = 2176459311584173842L; | ||
|
||
private static final String MESSAGE = "The Scripting Engine, %s, is not supported."; | ||
|
||
public ScriptEngineUnsupported(String extension) { | ||
super(String.format(MESSAGE, extension)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.folio.rest.model; | ||
|
||
/** | ||
* Languages supported for use by scripting engine. | ||
* | ||
* This alone does not necessarily make the language available. | ||
* Additional changes to the pom.xml may be necessary to get any particular language working. | ||
*/ | ||
public enum ScriptEngineType { | ||
NONE(null), | ||
GROOVY("groovy"), | ||
JAVA("java"), | ||
JS("js"), | ||
PERL("pl"), | ||
PYTHON("py"), | ||
RUBY("rb"); | ||
|
||
public final String extension; | ||
|
||
/** | ||
* Initialize the scripting engine. | ||
* | ||
* @param extension | ||
* The language extension name, lower cased. | ||
*/ | ||
private ScriptEngineType(String extension) { | ||
this.extension = extension; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/main/java/org/folio/rest/utility/ScriptEngineUtility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package org.folio.rest.utility; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import org.camunda.bpm.engine.impl.util.json.JSONObject; | ||
|
||
/** | ||
* A collection of utilities methods intended to be directly used by scripts called by the scripting engine. | ||
*/ | ||
public class ScriptEngineUtility { | ||
private static final String EMAIL_REGEX = "^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$"; | ||
private static final String PHONE_REGEX = "^[\\+]?[(]?[0-9]{3}[)]?[-\\s\\.]?[0-9]{3}[-\\s\\.]?[0-9]{4,6}$"; | ||
private static final String URL_REGEX = "(http(s)?:\\\\\\/\\\\\\/.)?(www\\.)?[-a-zA-Z0-9@:%._\\\\\\+~#=]{2,256}\\\\\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\\\\\+.~#?&//=]*)"; | ||
|
||
/** | ||
* Check if a given string might be an e-mail. | ||
* | ||
* @param string | ||
* The string to check. | ||
* | ||
* @return | ||
* TRUE if matched, FALSE otherwise. | ||
*/ | ||
public boolean isEmailLike(String string) { | ||
return string.matches(EMAIL_REGEX); | ||
} | ||
|
||
/** | ||
* Check if a given string is a phone number. | ||
* | ||
* @param string | ||
* The string to check. | ||
* | ||
* @return | ||
* TRUE if matched, FALSE otherwise. | ||
*/ | ||
public boolean isPhone(String string) { | ||
Pattern pattern = Pattern.compile(PHONE_REGEX, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); | ||
return pattern.matcher(string).find(); | ||
} | ||
|
||
/** | ||
* Check if a given string might be a URL. | ||
* | ||
* @param string | ||
* The string to check. | ||
* | ||
* @return | ||
* TRUE if matched, FALSE otherwise. | ||
*/ | ||
public boolean isURLLike(String string) { | ||
boolean isLikeUrl = string.toLowerCase().indexOf("www.") != -1; | ||
|
||
isLikeUrl = isLikeUrl && string.toLowerCase().indexOf(".org") != -1; | ||
isLikeUrl = isLikeUrl && string.toLowerCase().indexOf(".edu") != -1; | ||
isLikeUrl = isLikeUrl && string.toLowerCase().indexOf(".net") != -1; | ||
isLikeUrl = isLikeUrl && string.toLowerCase().indexOf(".us") != -1; | ||
isLikeUrl = isLikeUrl && string.toLowerCase().indexOf(".io") != -1; | ||
isLikeUrl = isLikeUrl && string.toLowerCase().indexOf(".co") != -1; | ||
|
||
return isLikeUrl; | ||
} | ||
|
||
/** | ||
* Check if a given string is a valid URL. | ||
* | ||
* @param string | ||
* The string to check. | ||
* | ||
* @return | ||
* TRUE if matched, FALSE otherwise. | ||
*/ | ||
public boolean isValidUrl(String string) { | ||
return string.matches(URL_REGEX); | ||
} | ||
|
||
/** | ||
* Creating JSONObject. | ||
* | ||
* @return | ||
* A generated JSON object. | ||
*/ | ||
public JSONObject createJson() { | ||
return new JSONObject(); | ||
} | ||
|
||
/** | ||
* Decode a JSON string into a JSONObject. | ||
* | ||
* @param json | ||
* The JSON string to decode. | ||
* | ||
* @return | ||
* A generated JSON object, containing the decoded JSON string. | ||
*/ | ||
public JSONObject decodeJson(String json) { | ||
return new JSONObject(json); | ||
} | ||
|
||
/** | ||
* Encode a JSONObject into a JSON string. | ||
* | ||
* @param json | ||
* The JSONObject to encode. | ||
* | ||
* @return | ||
* A String containing the encoded JSON data. | ||
*/ | ||
public String encodeJson(JSONObject json) { | ||
return json.toString(2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import org.folio.rest.utility.ScriptEngineUtility | ||
|
||
def %s(String inArgs) { | ||
def scriptEngineUtility = new ScriptEngineUtility(); | ||
def args = scriptEngineUtility.decodeJson(inArgs); | ||
def returnObj = scriptEngineUtility.createJson(); | ||
%s | ||
return scriptEngineUtility.encodeJson(returnObj); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import org.camunda.bpm.engine.impl.util.json.JSONObject; | ||
import org.folio.rest.utility.ScriptEngineUtility; | ||
|
||
public String %s(String inArgs) { | ||
ScriptEngineUtility scriptEngineUtility = new ScriptEngineUtility(); | ||
JSONObject args = scriptEngineUtility.decodeJson(inArgs); | ||
JSONObject returnObj = scriptEngineUtility.createJson(); | ||
%s | ||
return scriptEngineUtility.encodeJson(returnObj); | ||
} |
Oops, something went wrong.