yangys
2024-04-02 1a77b1a7c072b136925d6d73c4d8f017ca016e3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.qianwen.core.tool.script.engine.SpEL;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qianwen.core.tool.script.engine.ExecuteResult;
import com.qianwen.core.tool.script.engine.ListenerSupportEngine;
import com.qianwen.core.tool.script.engine.ScriptContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
 
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/script/engine/SpEL/SpElEngine.class */
public class SpElEngine extends ListenerSupportEngine {
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    protected final Map<String, SpelScriptContext> cache = new ConcurrentHashMap();
    protected final ExpressionParser parser = new SpelExpressionParser();
    private List<ContextCall> contextCalls = new ArrayList();
 
    /* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/script/engine/SpEL/SpElEngine$ContextCall.class */
    public interface ContextCall {
        void init(StandardEvaluationContext context);
    }
 
    @Override // org.springblade.core.tool.script.engine.DynamicScriptEngine
    public boolean compiled(String id) {
        return this.cache.containsKey(id);
    }
 
    @Override // org.springblade.core.tool.script.engine.DynamicScriptEngine
    public void init(String... contents) throws Exception {
    }
 
    @Override // org.springblade.core.tool.script.engine.DynamicScriptEngine
    public boolean remove(String id) {
        return this.cache.remove(id) != null;
    }
 
    @Override // org.springblade.core.tool.script.engine.DynamicScriptEngine
    public ScriptContext getContext(String id) {
        return this.cache.get(id);
    }
 
    @Override // org.springblade.core.tool.script.engine.DynamicScriptEngine
    public boolean compile(String id, String code) throws Exception {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("compile SpEL {} : {}", id, code);
        }
        this.cache.put(id, new SpelScriptContext(id, DigestUtils.md5Hex(code), this.parser.parseExpression(code)));
        return false;
    }
 
    @Override // org.springblade.core.tool.script.engine.DynamicScriptEngine
    public ExecuteResult execute(String id) {
        return execute(id, new HashMap());
    }
 
    @Override // org.springblade.core.tool.script.engine.DynamicScriptEngine
    public ExecuteResult execute(String id, Map<String, Object> param) {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("execute SpEL {} : {}", id, param);
        }
        ExecuteResult result = new ExecuteResult();
        long start = System.currentTimeMillis();
        SpelScriptContext scriptContext = this.cache.get(id);
        try {
            if (scriptContext != null) {
                doListenerBefore(scriptContext);
                scriptContext = this.cache.get(id);
                Map<String, Object> param2 = new HashMap<>(param);
                param2.putAll(getGlobalVariable());
                StandardEvaluationContext context = new StandardEvaluationContext(param2);
                for (Map.Entry<String, Object> entry : param2.entrySet()) {
                    context.setVariable(entry.getKey(), entry.getValue());
                }
                this.contextCalls.forEach(contextCall -> {
                    contextCall.init(context);
                });
                Object obj = scriptContext.getScript().getValue(context);
                result.setSuccess(true);
                result.setResult(obj);
            } else {
                result.setSuccess(false);
                result.setResult(null);
                result.setMessage(String.format("SpEL: %s not found!", id));
            }
            long end = System.currentTimeMillis();
            result.setUseTime(end - start);
        } catch (Exception e) {
            result.setException(e);
        }
        doListenerAfter(scriptContext, result);
        return result;
    }
 
    public void addContextCall(ContextCall contextCall) {
        this.contextCalls.add(contextCall);
    }
 
    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/script/engine/SpEL/SpElEngine$SpelScriptContext.class */
    public class SpelScriptContext extends ScriptContext {
        private Expression script;
 
        public SpelScriptContext(String id, String md5, Expression script) {
            super(id, md5);
            this.script = script;
        }
 
        public Expression getScript() {
            return this.script;
        }
    }
}