fix
Helius
2022-07-08 6601d3ecbdcb94a014eaebe275bf824e2c25ef1a
fix
1 files added
1 files modified
36 ■■■■■ changed files
src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java 9 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/utils/GroovySingleton.java 27 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/core/node/AttrNode.java
@@ -8,6 +8,7 @@
import com.xcong.farmer.cms.core.tag.TagsEnum;
import com.xcong.farmer.cms.core.template.Configuration;
import com.xcong.farmer.cms.core.template.TemplateConfiguration;
import com.xcong.farmer.cms.utils.GroovySingleton;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import org.apache.commons.text.StringSubstitutor;
@@ -213,7 +214,8 @@
//                }
//            }
            Binding binding = new Binding();
            GroovyShell groovyShell = GroovySingleton.getSingleton();
            Binding binding = groovyShell.getContext();
            for (Map.Entry<String, Object> entry : this.parserData.entrySet()) {
                String fieldKey = entry.getKey();
                Map<String, Object> data = (Map<String, Object>) entry.getValue();
@@ -224,13 +226,14 @@
                binding.setProperty("system", systemData);
            }
            GroovyShell shell = new GroovyShell(binding);
            Object evaluate = shell.evaluate(group);
//            GroovyShell shell = new GroovyShell(binding);
            Object evaluate = groovyShell.evaluate(group);
            if (evaluate == null) {
                targetData.put(group, "");
            } else {
                targetData.put(group, evaluate.toString());
            }
            groovyShell.getClassLoader().clearCache();
        }
        StringSubstitutor str = new StringSubstitutor(targetData);
src/main/java/com/xcong/farmer/cms/utils/GroovySingleton.java
New file
@@ -0,0 +1,27 @@
package com.xcong.farmer.cms.utils;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
/**
 * @author wzy
 * @date 2022-07-08
 **/
public class GroovySingleton {
    private volatile static GroovyShell groovyShell;
    private GroovySingleton (){}
    public static GroovyShell getSingleton() {
        if (groovyShell == null) {
            synchronized (GroovySingleton.class) {
                if (groovyShell == null) {
                    Binding binding = new Binding();
                    groovyShell = new GroovyShell(binding);
                }
            }
        }
        return groovyShell;
    }
}