1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.fatwire.gst.foundation.groovy;
18
19 import groovy.util.GroovyScriptEngine;
20
21 import java.io.IOException;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.servlet.ServletContext;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.commons.logging.Log;
30
31 import COM.FutureTense.Interfaces.ICS;
32 import COM.FutureTense.Interfaces.Utilities;
33
34 import com.fatwire.cs.core.db.PreparedStmt;
35 import com.fatwire.cs.core.db.StatementParam;
36 import com.fatwire.gst.foundation.facade.ics.ICSFactory;
37 import com.fatwire.gst.foundation.facade.logging.LogUtil;
38 import com.fatwire.gst.foundation.facade.runtag.render.LogDep;
39 import com.fatwire.gst.foundation.facade.sql.Row;
40 import com.fatwire.gst.foundation.facade.sql.SqlHelper;
41
42
43
44
45
46
47
48
49
50
51
52 public class GroovyElementCatalogLoader extends DiskGroovyLoader {
53 private PreparedStmt stmt;
54 private Log logger = LogUtil.getLog(getClass());
55
56 public GroovyElementCatalogLoader(ServletContext servletContext) {
57 super(servletContext);
58
59 stmt = new PreparedStmt("SELECT * FROM ElementCatalog WHERE elementname=?",
60 Collections.singletonList("CSElement"));
61 stmt.setElement(0, java.sql.Types.VARCHAR);
62
63 }
64
65 @Override
66 public Object load(String resourceName) throws Exception {
67 ICS ics = ICSFactory.getOrCreateICS();
68
69 if (logger.isDebugEnabled())
70 logger.debug("Loading groovy script " + resourceName);
71 if (ics.IsElement(resourceName)) {
72
73 final StatementParam param = stmt.newParam();
74 param.setString(0, resourceName);
75 Row row = SqlHelper.selectSingle(ics, stmt, param);
76
77
78
79
80 String url = row.getString("url");
81 String res1 = row.getString("resdetails1");
82 String res2 = row.getString("resdetails2");
83 Map<String, String> m = new HashMap<String, String>();
84 Utilities.getParams(res1, m, false);
85 Utilities.getParams(res2, m, false);
86 String tid = m.get("tid");
87 String eid = m.get("eid");
88 if (StringUtils.isNotBlank(tid)) {
89 LogDep.logDep(ics, "Template", tid);
90 }
91 if (StringUtils.isNotBlank(tid)) {
92 LogDep.logDep(ics, "CSElement", eid);
93 }
94
95 if (url.endsWith(".groovy")) {
96
97
98 if (logger.isDebugEnabled()) {
99 logger.debug("Found element for " + resourceName + " => " + url);
100 }
101 Class<?> x = getGroovyScriptEngine().loadScriptByName(url);
102 return x.newInstance();
103
104 } else {
105 return super.load(resourceName);
106 }
107 } else {
108 return super.load(resourceName);
109 }
110
111 }
112
113 public void bootEngine(final String path) {
114 String[] root = new String[2];
115 ICS ics = ICSFactory.getOrCreateICS();
116
117 String elementCatalogDefDir = ics.ResolveVariables("CS.CatalogDir.ElementCatalog");
118 root[0] = elementCatalogDefDir;
119 root[1] = path;
120
121 GroovyScriptEngine gse;
122 try {
123 gse = new GroovyScriptEngine(root, Thread.currentThread().getContextClassLoader());
124 gse.getConfig().setRecompileGroovySource(true);
125 gse.getConfig().setMinimumRecompilationInterval(getMinimumRecompilationInterval());
126 setGroovyScriptEngine(gse);
127
128 } catch (IOException e) {
129 throw new RuntimeException(e);
130 }
131
132 }
133
134 }