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
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
@@ -32,32 +33,10 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class KitScriptHelper {
|
||||
|
||||
public Object evalObj(String script, Map<String, Object> args) {
|
||||
try (Context rhino = Context.enter()) {
|
||||
Scriptable scope = rhino.initStandardObjects();
|
||||
bindArgumentsToScope(scope, args);
|
||||
Object result = rhino.evaluateString(scope, script, "JavaScript", 1, null);
|
||||
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);
|
||||
public Object evalObj(String script, Map<String, Object> args) throws EvaluationException, ParseException {
|
||||
Expression expression = new Expression(script);
|
||||
expression.withValues(args);
|
||||
EvaluationValue result = expression.evaluate();
|
||||
return result.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
+6
-3
@@ -4,15 +4,16 @@
|
||||
<property name="log.name" value="cutejava"/>
|
||||
<property name="log.home" value="/var/logs"/>
|
||||
<property name="log.charset" value="utf-8"/>
|
||||
<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"/>
|
||||
<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"/>
|
||||
|
||||
<!-- Spring 日志级别控制 -->
|
||||
<logger name="org.springframework" level="WARN"/>
|
||||
<logger name="org.springframework" level="warn"/>
|
||||
<!-- DnsServerAddressStreamProviders调整为ERROR -->
|
||||
<logger name="io.netty.resolver.dns.DnsServerAddressStreamProviders" level="ERROR"/>
|
||||
<!-- 设置其他类的日志级别为 ERROR -->
|
||||
<logger name="org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]" level="ERROR"/>
|
||||
<logger name="org.springframework.web.servlet.DispatcherServlet" level="ERROR"/>
|
||||
|
||||
<!--输出到控制台-->
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
@@ -20,6 +21,7 @@
|
||||
<charset>${log.charset}</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<File>${log.home}/${log.name}.log</File>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
@@ -31,6 +33,7 @@
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<!--普通日志输出到控制台-->
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
+9
-29
@@ -15,10 +15,12 @@
|
||||
*/
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
@@ -31,32 +33,10 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class KitScriptHelper {
|
||||
|
||||
public Object evalObj(String script, Map<String, Object> args) {
|
||||
try (Context rhino = Context.enter()) {
|
||||
Scriptable scope = rhino.initStandardObjects();
|
||||
bindArgumentsToScope(scope, args);
|
||||
Object result = rhino.evaluateString(scope, script, "JavaScript", 1, null);
|
||||
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);
|
||||
public Object evalObj(String script, Map<String, Object> args) throws EvaluationException, ParseException {
|
||||
Expression expression = new Expression(script);
|
||||
expression.withValues(args);
|
||||
EvaluationValue result = expression.evaluate();
|
||||
return result.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user