mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
clean history
This commit is contained in:
24
hutool-script/pom.xml
Normal file
24
hutool-script/pom.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-parent</artifactId>
|
||||
<version>4.6.2-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>hutool-script</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<description>Hutool 脚本执行封装</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@@ -0,0 +1,159 @@
|
||||
package cn.hutool.script;
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* 全功能引擎类,支持Compilable和Invocable
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class FullSupportScriptEngine implements ScriptEngine, Compilable, Invocable {
|
||||
|
||||
ScriptEngine engine;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param engine 脚本引擎
|
||||
*/
|
||||
public FullSupportScriptEngine(ScriptEngine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param nameOrExtOrMime 脚本名或者脚本语言扩展名或者MineType
|
||||
*/
|
||||
public FullSupportScriptEngine(String nameOrExtOrMime) {
|
||||
final ScriptEngineManager manager = new ScriptEngineManager();
|
||||
ScriptEngine engine = manager.getEngineByName(nameOrExtOrMime);
|
||||
if (null == engine) {
|
||||
engine = manager.getEngineByExtension(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
engine = manager.getEngineByMimeType(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
throw new NullPointerException(StrUtil.format("Script for [{}] not support !", nameOrExtOrMime));
|
||||
}
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------- Invocable
|
||||
@Override
|
||||
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return ((Invocable) engine).invokeMethod(thiz, name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return ((Invocable) engine).invokeFunction(name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Class<T> clasz) {
|
||||
return ((Invocable) engine).getInterface(clasz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Object thiz, Class<T> clasz) {
|
||||
return ((Invocable) engine).getInterface(thiz, clasz);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------- Compilable
|
||||
@Override
|
||||
public CompiledScript compile(String script) throws ScriptException {
|
||||
return ((Compilable) engine).compile(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompiledScript compile(Reader script) throws ScriptException {
|
||||
return ((Compilable) engine).compile(script);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------- ScriptEngine
|
||||
@Override
|
||||
public Object eval(String script, ScriptContext context) throws ScriptException {
|
||||
return engine.eval(script, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, ScriptContext context) throws ScriptException {
|
||||
return engine.eval(reader, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script) throws ScriptException {
|
||||
return engine.eval(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader) throws ScriptException {
|
||||
return engine.eval(reader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script, Bindings n) throws ScriptException {
|
||||
return engine.eval(script, n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, Bindings n) throws ScriptException {
|
||||
return engine.eval(reader, n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(String key, Object value) {
|
||||
engine.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String key) {
|
||||
return engine.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bindings getBindings(int scope) {
|
||||
return engine.getBindings(scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBindings(Bindings bindings, int scope) {
|
||||
engine.setBindings(bindings, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bindings createBindings() {
|
||||
return engine.createBindings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptContext getContext() {
|
||||
return engine.getContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContext(ScriptContext context) {
|
||||
engine.setContext(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptEngineFactory getFactory() {
|
||||
return engine.getFactory();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,136 @@
|
||||
package cn.hutool.script;
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* Javascript引擎类
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class JavaScriptEngine extends FullSupportScriptEngine{
|
||||
|
||||
public JavaScriptEngine() {
|
||||
super(new ScriptEngineManager().getEngineByName("javascript"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 引擎实例
|
||||
* @return 引擎实例
|
||||
*/
|
||||
public static JavaScriptEngine instance(){
|
||||
return new JavaScriptEngine();
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------- Invocable
|
||||
@Override
|
||||
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return ((Invocable)engine).invokeMethod(thiz, name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return ((Invocable)engine).invokeFunction(name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Class<T> clasz) {
|
||||
return ((Invocable)engine).getInterface(clasz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Object thiz, Class<T> clasz) {
|
||||
return ((Invocable)engine).getInterface(thiz, clasz);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------- Compilable
|
||||
@Override
|
||||
public CompiledScript compile(String script) throws ScriptException {
|
||||
return ((Compilable)engine).compile(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompiledScript compile(Reader script) throws ScriptException {
|
||||
return ((Compilable)engine).compile(script);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------- ScriptEngine
|
||||
@Override
|
||||
public Object eval(String script, ScriptContext context) throws ScriptException {
|
||||
return engine.eval(script, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, ScriptContext context) throws ScriptException {
|
||||
return engine.eval(reader, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script) throws ScriptException {
|
||||
return engine.eval(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader) throws ScriptException {
|
||||
return engine.eval(reader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script, Bindings n) throws ScriptException {
|
||||
return engine.eval(script, n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, Bindings n) throws ScriptException {
|
||||
return engine.eval(reader, n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(String key, Object value) {
|
||||
engine.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String key) {
|
||||
return engine.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bindings getBindings(int scope) {
|
||||
return engine.getBindings(scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBindings(Bindings bindings, int scope) {
|
||||
engine.setBindings(bindings, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bindings createBindings() {
|
||||
return engine.createBindings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptContext getContext() {
|
||||
return engine.getContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContext(ScriptContext context) {
|
||||
engine.setContext(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptEngineFactory getFactory() {
|
||||
return engine.getFactory();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,127 @@
|
||||
package cn.hutool.script;
|
||||
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* 脚本运行时异常
|
||||
*
|
||||
* @author xiaoleilu
|
||||
*/
|
||||
public class ScriptRuntimeException extends RuntimeException {
|
||||
private static final long serialVersionUID = 8247610319171014183L;
|
||||
|
||||
private String fileName;
|
||||
private int lineNumber = -1;
|
||||
private int columnNumber = -1;
|
||||
|
||||
public ScriptRuntimeException(Throwable e) {
|
||||
super(ExceptionUtil.getMessage(e), e);
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(String messageTemplate, Object... params) {
|
||||
super(StrUtil.format(messageTemplate, params));
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(String message, Throwable throwable) {
|
||||
super(message, throwable);
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(Throwable throwable, String messageTemplate, Object... params) {
|
||||
super(StrUtil.format(messageTemplate, params), throwable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a <code>ScriptException</code> with message, filename and linenumber to be used in error messages.
|
||||
*
|
||||
* @param message The string to use in the message
|
||||
*
|
||||
* @param fileName The file or resource name describing the location of a script error causing the <code>ScriptException</code> to be thrown.
|
||||
*
|
||||
* @param lineNumber A line number describing the location of a script error causing the <code>ScriptException</code> to be thrown.
|
||||
*/
|
||||
public ScriptRuntimeException(String message, String fileName, int lineNumber) {
|
||||
super(message);
|
||||
this.fileName = fileName;
|
||||
this.lineNumber = lineNumber;
|
||||
this.columnNumber = -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* <code>ScriptException</code> constructor specifying message, filename, line number and column number.
|
||||
*
|
||||
* @param message The message.
|
||||
* @param fileName The filename
|
||||
* @param lineNumber the line number.
|
||||
* @param columnNumber the column number.
|
||||
*/
|
||||
public ScriptRuntimeException(String message, String fileName, int lineNumber, int columnNumber) {
|
||||
super(message);
|
||||
this.fileName = fileName;
|
||||
this.lineNumber = lineNumber;
|
||||
this.columnNumber = columnNumber;
|
||||
}
|
||||
|
||||
public ScriptRuntimeException(ScriptException e) {
|
||||
super(e);
|
||||
this.fileName = e.getFileName();
|
||||
this.lineNumber = e.getLineNumber();
|
||||
this.columnNumber = e.getColumnNumber();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a message containing the String passed to a constructor as well as line and column numbers and filename if any of these are known.
|
||||
*
|
||||
* @return The error message.
|
||||
*/
|
||||
@Override
|
||||
public String getMessage() {
|
||||
String ret = super.getMessage();
|
||||
if (fileName != null) {
|
||||
ret += (" in " + fileName);
|
||||
if (lineNumber != -1) {
|
||||
ret += " at line number " + lineNumber;
|
||||
}
|
||||
|
||||
if (columnNumber != -1) {
|
||||
ret += " at column number " + columnNumber;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the line number on which an error occurred.
|
||||
*
|
||||
* @return The line number. Returns -1 if a line number is unavailable.
|
||||
*/
|
||||
public int getLineNumber() {
|
||||
return lineNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the column number on which an error occurred.
|
||||
*
|
||||
* @return The column number. Returns -1 if a column number is unavailable.
|
||||
*/
|
||||
public int getColumnNumber() {
|
||||
return columnNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the source of the script causing the error.
|
||||
*
|
||||
* @return The file name of the script or some other string describing the script source. May return some implementation-defined string such as <i><unknown></i> if a description of the
|
||||
* source is unavailable.
|
||||
*/
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
}
|
119
hutool-script/src/main/java/cn/hutool/script/ScriptUtil.java
Normal file
119
hutool-script/src/main/java/cn/hutool/script/ScriptUtil.java
Normal file
@@ -0,0 +1,119 @@
|
||||
package cn.hutool.script;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* 脚本工具类
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class ScriptUtil {
|
||||
|
||||
/**
|
||||
* 获得 {@link ScriptEngine} 实例
|
||||
*
|
||||
* @param name 脚本名称
|
||||
* @return {@link ScriptEngine} 实例
|
||||
*/
|
||||
public static ScriptEngine getScript(String name) {
|
||||
return new ScriptEngineManager().getEngineByName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得 Javascript引擎 {@link JavaScriptEngine}
|
||||
*
|
||||
* @return {@link JavaScriptEngine}
|
||||
*/
|
||||
public static JavaScriptEngine getJavaScriptEngine() {
|
||||
return new JavaScriptEngine();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return {@link CompiledScript}
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(String script) throws ScriptRuntimeException {
|
||||
try {
|
||||
return compile(script).eval();
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @param context 脚本上下文
|
||||
* @return {@link CompiledScript}
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(String script, ScriptContext context) throws ScriptRuntimeException {
|
||||
try {
|
||||
return compile(script).eval(context);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @param bindings 绑定的参数
|
||||
* @return {@link CompiledScript}
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(String script, Bindings bindings) throws ScriptRuntimeException {
|
||||
try {
|
||||
return compile(script).eval(bindings);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return {@link CompiledScript}
|
||||
* @throws ScriptRuntimeException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static CompiledScript compile(String script) throws ScriptRuntimeException {
|
||||
try {
|
||||
return compile(getJavaScriptEngine(), script);
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译脚本
|
||||
*
|
||||
* @param engine 引擎
|
||||
* @param script 脚本内容
|
||||
* @return {@link CompiledScript}
|
||||
* @throws ScriptException 脚本异常
|
||||
*/
|
||||
public static CompiledScript compile(ScriptEngine engine, String script) throws ScriptException {
|
||||
if (engine instanceof Compilable) {
|
||||
Compilable compEngine = (Compilable) engine;
|
||||
return compEngine.compile(script);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Script模块主要针对Java的javax.script封装,可以运行Javascript脚本。
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.script;
|
@@ -0,0 +1,33 @@
|
||||
package cn.hutool.script.test;
|
||||
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.script.ScriptRuntimeException;
|
||||
import cn.hutool.script.ScriptUtil;
|
||||
|
||||
/**
|
||||
* 脚本单元测试类
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class ScriptUtilTest {
|
||||
|
||||
@Test
|
||||
public void compileTest() {
|
||||
CompiledScript script = ScriptUtil.compile("print('Script test!');");
|
||||
try {
|
||||
script.eval();
|
||||
} catch (ScriptException e) {
|
||||
throw new ScriptRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void evalTest() {
|
||||
ScriptUtil.eval("print('Script test!');");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user