refactor(KitScriptHelper): 替换 Rhino JavaScript 引擎为 EvalEx 表达式引擎
This commit is contained in:
+9
-30
@@ -13,13 +13,14 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package cn.odboy.framework.context;
|
package cn.odboy.framework.context;
|
||||||
|
|
||||||
|
import com.ezylang.evalex.EvaluationException;
|
||||||
|
import com.ezylang.evalex.Expression;
|
||||||
|
import com.ezylang.evalex.data.EvaluationValue;
|
||||||
|
import com.ezylang.evalex.parser.ParseException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.mozilla.javascript.Context;
|
|
||||||
import org.mozilla.javascript.Scriptable;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,32 +33,10 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component
|
@Component
|
||||||
public class KitScriptHelper {
|
public class KitScriptHelper {
|
||||||
|
|
||||||
public Object evalObj(String script, Map<String, Object> args) {
|
public Object evalObj(String script, Map<String, Object> args) throws EvaluationException, ParseException {
|
||||||
try (Context rhino = Context.enter()) {
|
Expression expression = new Expression(script);
|
||||||
Scriptable scope = rhino.initStandardObjects();
|
expression.withValues(args);
|
||||||
bindArgumentsToScope(scope, args);
|
EvaluationValue result = expression.evaluate();
|
||||||
Object result = rhino.evaluateString(scope, script, "JavaScript", 1, null);
|
return result.getValue();
|
||||||
log.info("执行结果: {}", Context.toString(result));
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void bindArgumentsToScope(Scriptable scope, Map<?, ?> args) {
|
|
||||||
if (args == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (Map.Entry<?, ?> entry : args.entrySet()) {
|
|
||||||
Object jsValue = Context.javaToJS(entry.getValue(), scope);
|
|
||||||
scope.put(entry.getKey().toString(), scope, jsValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void bindArgumentsToScope(Scriptable scope, Object args) {
|
|
||||||
if (args == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 单个对象参数
|
|
||||||
Object jsValue = Context.javaToJS(args, scope);
|
|
||||||
scope.put("args", scope, jsValue);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-4
@@ -4,15 +4,16 @@
|
|||||||
<property name="log.name" value="cutejava"/>
|
<property name="log.name" value="cutejava"/>
|
||||||
<property name="log.home" value="/var/logs"/>
|
<property name="log.home" value="/var/logs"/>
|
||||||
<property name="log.charset" value="utf-8"/>
|
<property name="log.charset" value="utf-8"/>
|
||||||
<property name="log.pattern"
|
<property name="log.pattern" value="%red(%contextName) - %red(%d{yyyy-MM-dd HH:mm:ss.SSS}) - %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{36}) - %msg%n"/>
|
||||||
value="%red(%contextName) - %red(%d{yyyy-MM-dd HH:mm:ss.SSS}) - %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{36}) - %msg%n"/>
|
|
||||||
<!-- Spring 日志级别控制 -->
|
<!-- Spring 日志级别控制 -->
|
||||||
<logger name="org.springframework" level="WARN"/>
|
<logger name="org.springframework" level="warn"/>
|
||||||
<!-- DnsServerAddressStreamProviders调整为ERROR -->
|
<!-- DnsServerAddressStreamProviders调整为ERROR -->
|
||||||
<logger name="io.netty.resolver.dns.DnsServerAddressStreamProviders" level="ERROR"/>
|
<logger name="io.netty.resolver.dns.DnsServerAddressStreamProviders" level="ERROR"/>
|
||||||
<!-- 设置其他类的日志级别为 ERROR -->
|
<!-- 设置其他类的日志级别为 ERROR -->
|
||||||
<logger name="org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]" level="ERROR"/>
|
<logger name="org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]" level="ERROR"/>
|
||||||
<logger name="org.springframework.web.servlet.DispatcherServlet" level="ERROR"/>
|
<logger name="org.springframework.web.servlet.DispatcherServlet" level="ERROR"/>
|
||||||
|
|
||||||
<!--输出到控制台-->
|
<!--输出到控制台-->
|
||||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||||
@@ -20,6 +21,7 @@
|
|||||||
<charset>${log.charset}</charset>
|
<charset>${log.charset}</charset>
|
||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</appender>
|
||||||
|
|
||||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||||
<File>${log.home}/${log.name}.log</File>
|
<File>${log.home}/${log.name}.log</File>
|
||||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||||
@@ -31,10 +33,11 @@
|
|||||||
<pattern>${log.pattern}</pattern>
|
<pattern>${log.pattern}</pattern>
|
||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</appender>
|
||||||
|
|
||||||
<root level="INFO">
|
<root level="INFO">
|
||||||
<!--普通日志输出到控制台-->
|
<!--普通日志输出到控制台-->
|
||||||
<appender-ref ref="CONSOLE"/>
|
<appender-ref ref="CONSOLE"/>
|
||||||
<!--普通日志输出到文件-->
|
<!--普通日志输出到文件-->
|
||||||
<appender-ref ref="FILE"/>
|
<appender-ref ref="FILE"/>
|
||||||
</root>
|
</root>
|
||||||
</configuration>
|
</configuration>
|
||||||
+9
-29
@@ -15,10 +15,12 @@
|
|||||||
*/
|
*/
|
||||||
package cn.odboy.framework.context;
|
package cn.odboy.framework.context;
|
||||||
|
|
||||||
|
import com.ezylang.evalex.EvaluationException;
|
||||||
|
import com.ezylang.evalex.Expression;
|
||||||
|
import com.ezylang.evalex.data.EvaluationValue;
|
||||||
|
import com.ezylang.evalex.parser.ParseException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.mozilla.javascript.Context;
|
|
||||||
import org.mozilla.javascript.Scriptable;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,32 +33,10 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component
|
@Component
|
||||||
public class KitScriptHelper {
|
public class KitScriptHelper {
|
||||||
|
|
||||||
public Object evalObj(String script, Map<String, Object> args) {
|
public Object evalObj(String script, Map<String, Object> args) throws EvaluationException, ParseException {
|
||||||
try (Context rhino = Context.enter()) {
|
Expression expression = new Expression(script);
|
||||||
Scriptable scope = rhino.initStandardObjects();
|
expression.withValues(args);
|
||||||
bindArgumentsToScope(scope, args);
|
EvaluationValue result = expression.evaluate();
|
||||||
Object result = rhino.evaluateString(scope, script, "JavaScript", 1, null);
|
return result.getValue();
|
||||||
log.info("执行结果: {}", Context.toString(result));
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void bindArgumentsToScope(Scriptable scope, Map<?, ?> args) {
|
|
||||||
if (args == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (Map.Entry<?, ?> entry : args.entrySet()) {
|
|
||||||
Object jsValue = Context.javaToJS(entry.getValue(), scope);
|
|
||||||
scope.put(entry.getKey().toString(), scope, jsValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void bindArgumentsToScope(Scriptable scope, Object args) {
|
|
||||||
if (args == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 单个对象参数
|
|
||||||
Object jsValue = Context.javaToJS(args, scope);
|
|
||||||
scope.put("args", scope, jsValue);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user