1 31 package org.blojsom.plugin.scripting; 32 33 import groovy.lang.GroovyClassLoader; 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.blojsom.blog.Blog; 37 import org.blojsom.blog.Entry; 38 import org.blojsom.plugin.Plugin; 39 import org.blojsom.plugin.PluginException; 40 import org.blojsom.util.BlojsomUtils; 41 import org.blojsom.util.BlojsomConstants; 42 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 import javax.servlet.ServletConfig ; 46 import java.io.File ; 47 import java.util.Map ; 48 49 56 public class GroovyPlugin implements Plugin { 57 58 private Log _logger = LogFactory.getLog(GroovyPlugin.class); 59 60 private static final String GROOVY_SCRIPTS_PARAM = "groovy-scripts"; 61 private static final String GROOVY_SCRIPTS_COUNTER = "BLOJSOM_GROOVY_PLUGIN_SCRIPTS_COUNTER"; 62 private static final String GROOVY_SCRIPTS_LIST = "BLOJSOM_GROOVY_PLUGIN_SCRIPTS_LIST"; 63 64 private ServletConfig _servletConfig; 65 66 69 public GroovyPlugin() { 70 } 71 72 77 public void setServletConfig(ServletConfig servletConfig) { 78 _servletConfig = servletConfig; 79 } 80 81 87 public void init() throws PluginException { 88 } 89 90 101 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 102 String [] scripts = null; 103 104 if (context.containsKey(GROOVY_SCRIPTS_LIST)) { 106 scripts = (String []) context.get(GROOVY_SCRIPTS_LIST); 107 } else { 108 String scriptsParam = BlojsomUtils.getRequestValue(GROOVY_SCRIPTS_PARAM, httpServletRequest); 109 if (scriptsParam != null) { 110 scripts = BlojsomUtils.parseCommaList(scriptsParam); 111 context.put(GROOVY_SCRIPTS_LIST, scripts); 112 } 113 } 114 115 116 if (scripts == null) { 117 if (_logger.isInfoEnabled()) { 118 _logger.info("No scripts to process"); 119 } 120 121 return entries; 122 } 123 124 Integer scriptToProcess; 126 if (context.containsKey(GROOVY_SCRIPTS_COUNTER)) { 127 scriptToProcess = (Integer ) context.get(GROOVY_SCRIPTS_COUNTER); 128 } else { 129 scriptToProcess = new Integer (0); 130 } 131 132 if (scriptToProcess == null || scriptToProcess.intValue() < 0 || scriptToProcess.intValue() > scripts.length) { 133 if (_logger.isErrorEnabled()) { 134 _logger.error("Groovy scripts counter is null or value is out of range"); 135 } 136 137 return entries; 138 } 139 140 File scriptFile = new File (_servletConfig.getServletContext().getRealPath(BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY) + BlojsomConstants.DEFAULT_BLOGS_DIRECTORY + blog.getBlogId() + "/" + BlojsomUtils.normalize(scripts[scriptToProcess.intValue()])); 141 if (_logger.isDebugEnabled()) { 142 _logger.debug("Processing script file: " + scriptFile.toString()); 143 } 144 145 GroovyClassLoader groovyClassLoader = new GroovyClassLoader(this.getClass().getClassLoader()); 146 Plugin plugin; 147 try { 148 Class scriptPluginClazz = groovyClassLoader.parseClass(scriptFile); 149 plugin = (Plugin) scriptPluginClazz.newInstance(); 150 plugin.init(); 151 entries = plugin.process(httpServletRequest, httpServletResponse, blog, context, entries); 152 plugin.cleanup(); 153 plugin.destroy(); 154 155 scriptToProcess = new Integer (scriptToProcess.intValue() + 1); 157 context.put(GROOVY_SCRIPTS_COUNTER, scriptToProcess); 158 } catch (Exception e) { 159 if (_logger.isErrorEnabled()) { 160 _logger.error(e); 161 } 162 } 163 164 return entries; 165 } 166 167 173 public void cleanup() throws PluginException { 174 } 175 176 182 public void destroy() throws PluginException { 183 } 184 } | Popular Tags |