KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > scripting > GroovyPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

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 JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import javax.servlet.ServletConfig JavaDoc;
46 import java.io.File JavaDoc;
47 import java.util.Map JavaDoc;
48
49 /**
50  * GroovyPlugin
51  *
52  * @author David Czarnecki
53  * @version $Id: GroovyPlugin.java,v 1.1 2006/03/23 06:59:08 czarneckid Exp $
54  * @since blojsom 3.0
55  */

56 public class GroovyPlugin implements Plugin {
57
58     private Log _logger = LogFactory.getLog(GroovyPlugin.class);
59
60     private static final String JavaDoc GROOVY_SCRIPTS_PARAM = "groovy-scripts";
61     private static final String JavaDoc GROOVY_SCRIPTS_COUNTER = "BLOJSOM_GROOVY_PLUGIN_SCRIPTS_COUNTER";
62     private static final String JavaDoc GROOVY_SCRIPTS_LIST = "BLOJSOM_GROOVY_PLUGIN_SCRIPTS_LIST";
63
64     private ServletConfig JavaDoc _servletConfig;
65
66     /**
67      * Default constructor.
68      */

69     public GroovyPlugin() {
70     }
71
72     /**
73      * Set the {@link ServletConfig}
74      *
75      * @param servletConfig {@link ServletConfig}
76      */

77     public void setServletConfig(ServletConfig JavaDoc servletConfig) {
78         _servletConfig = servletConfig;
79     }
80
81     /**
82      * Initialize this plugin. This method only called when the plugin is instantiated.
83      *
84      * @throws org.blojsom.plugin.PluginException
85      * If there is an error initializing the plugin
86      */

87     public void init() throws PluginException {
88     }
89
90     /**
91      * Process the blog entries
92      *
93      * @param httpServletRequest Request
94      * @param httpServletResponse Response
95      * @param blog {@link Blog} instance
96      * @param context Context
97      * @param entries Blog entries retrieved for the particular request
98      * @return Modified set of blog entries
99      * @throws PluginException If there is an error processing the blog entries
100      */

101     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
102         String JavaDoc[] scripts = null;
103
104         // Parse the scripts to execute
105
if (context.containsKey(GROOVY_SCRIPTS_LIST)) {
106             scripts = (String JavaDoc[]) context.get(GROOVY_SCRIPTS_LIST);
107         } else {
108             String JavaDoc 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         // Check for which script we should process if there was more than one
125
Integer JavaDoc scriptToProcess;
126         if (context.containsKey(GROOVY_SCRIPTS_COUNTER)) {
127             scriptToProcess = (Integer JavaDoc) context.get(GROOVY_SCRIPTS_COUNTER);
128         } else {
129             scriptToProcess = new Integer JavaDoc(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 JavaDoc scriptFile = new File JavaDoc(_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 JavaDoc 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             // Increment the script to process counter
156
scriptToProcess = new Integer JavaDoc(scriptToProcess.intValue() + 1);
157             context.put(GROOVY_SCRIPTS_COUNTER, scriptToProcess);
158         } catch (Exception JavaDoc e) {
159             if (_logger.isErrorEnabled()) {
160                 _logger.error(e);
161             }
162         }
163
164         return entries;
165     }
166
167     /**
168      * Perform any cleanup for the plugin. Called after {@link #process}.
169      *
170      * @throws org.blojsom.plugin.PluginException
171      * If there is an error performing cleanup for this plugin
172      */

173     public void cleanup() throws PluginException {
174     }
175
176     /**
177      * Called when BlojsomServlet is taken out of service
178      *
179      * @throws org.blojsom.plugin.PluginException
180      * If there is an error in finalizing this plugin
181      */

182     public void destroy() throws PluginException {
183     }
184 }
Popular Tags