View Javadoc

1   /*
2    * Copyright 2011 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Loader for groovy script classes from the ElementCatalog
44   * 
45   * @author Dolf Dijkstra
46   * @since September 21,2012
47   */
48  /*
49   * alternative method:
50   * http://groovy.codehaus.org/Alternate+Spring-Groovy-Integration
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          /*ElementCatalog does not have Browser ACL, assumption here is that elements are updated as assets */
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              // ELEMENTNAME DESCRIPTION URL RESDETAILS1
78              // RESDETAILS2
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              // prevent case where resourcename is same as a jsp element.
95              if (url.endsWith(".groovy")) {
96                  // no loading based on fallback class name as the super method
97                  // has.
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 }