KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > action > ScriptAction


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.action;
50
51 import java.io.IOException JavaDoc;
52 import java.io.InputStreamReader JavaDoc;
53
54 import javax.servlet.ServletContext JavaDoc;
55 import javax.servlet.http.HttpServletRequest JavaDoc;
56 import javax.servlet.http.HttpServletResponse JavaDoc;
57 import javax.servlet.http.HttpSession JavaDoc;
58
59 import com.anthonyeden.lib.config.Configuration;
60 import com.anthonyeden.lib.util.MessageUtilities;
61 import org.apache.bsf.util.IOUtils;
62 import org.apache.commons.logging.Log;
63 import org.apache.commons.logging.LogFactory;
64 import org.apache.commons.vfs.FileContent;
65 import org.apache.commons.vfs.FileName;
66 import org.apache.commons.vfs.FileObject;
67 import org.apache.commons.vfs.FileSystemException;
68 import org.jpublish.JPublishEngine;
69 import org.jpublish.RequestContext;
70 import org.jpublish.SiteContext;
71 import org.jpublish.page.Page;
72
73 /**
74  * An action which delegates to its registered script handler. Script actions have access to several varibles:
75  *
76  * <p>These are always available:</p>
77  *
78  * <p> <b>site</b> - The SiteContext<br> <b>syslog</b> - Standard logging stream (Log4J Category)<br> </p>
79  *
80  * <p>If there is a context defined when the action is executed (all actions excluding startup actions):</p>
81  *
82  * <p> <b>context</b> - The RequestContext<br> <b>application</b> - The ServletContext<br> <b>request</b> - The HTTP
83  * request<br> <b>response</b> - The HTTP response<br> <b>session</b> - The HTTP session<br> <b>page</b> - The Page
84  * object<br> </p>
85  *
86  * @author Anthony Eden
87  * @author David Jones
88  */

89
90 public class ScriptAction implements Action {
91
92     private static Log log = LogFactory.getLog(ScriptAction.class);
93
94     private SiteContext siteContext = null;
95     private FileObject file = null;
96     private ScriptHandlerDefinition handlerDefinition = null;
97     private FileContent fileContent = null;
98     private FileName fileName = null;
99
100     private String JavaDoc scriptLanguage = null;
101     private String JavaDoc scriptString = null;
102     private long timeLastLoaded = -1;
103
104     /**
105      * Construct a new ScriptAction for the given script.
106      *
107      * @param siteContext The SiteContext
108      * @param file The FileObject
109      */

110     public ScriptAction(SiteContext siteContext, FileObject file,
111             ScriptHandlerDefinition handlerDefinition) throws FileSystemException {
112         this.siteContext = siteContext;
113         this.file = file;
114         this.handlerDefinition = handlerDefinition;
115
116         this.fileName = file.getName();
117         this.fileContent = file.getContent();
118     }
119
120     /**
121      * Execute the action script represented by this ScriptAction.
122      *
123      * @param context The current context
124      * @param configuration The configuration object
125      */

126
127     public void execute(RequestContext context, Configuration configuration) {
128         long startTime = System.currentTimeMillis();
129         try {
130             if (log.isDebugEnabled()) {
131                 log.debug("Executing script: " + fileName);
132             }
133            
134             // retrieve a script handler instance
135
ScriptHandler handler = handlerDefinition.getInstance();
136             handler.setFile(file);
137             
138             // expose standard items in the context
139
if (context != null) {
140                 ServletContext JavaDoc application = context.getApplication();
141                 HttpServletRequest JavaDoc request = context.getRequest();
142                 HttpServletResponse JavaDoc response = context.getResponse();
143                 HttpSession JavaDoc session = context.getSession();
144                 Page page = context.getPage();
145                 
146                 // expose the context
147
handler.set("context", context, RequestContext.class);
148                 
149                 // expose the page object.
150
if (page == null) {
151                     log.debug("Page request is null");
152                 } else {
153                     handler.set("page", page, Page.class);
154                 }
155                 
156                 // expose standard HttpServlet objects
157
if (request == null) {
158                     log.debug("HTTP request is null");
159                 } else {
160                     handler.set("request", request,
161                             HttpServletRequest JavaDoc.class);
162                 }
163
164                 if (response == null) {
165                     log.debug("HTTP response is null");
166                 } else {
167                     handler.set("response", response,
168                             HttpServletResponse JavaDoc.class);
169                 }
170
171                 if (session == null) {
172                     log.debug("HTTP session is null");
173                 } else {
174                     handler.set("session", session, HttpSession JavaDoc.class);
175                 }
176
177                 if (application == null) {
178                     log.debug("ServletContext is null");
179                 } else {
180                     handler.set("application", application,
181                             ServletContext JavaDoc.class);
182                 }
183             }
184             
185             // these objects are exposed regardless if there is a context
186
// object or not. In other words they are accesible to startup
187
// actions
188
handler.set("syslog", SiteContext.syslog, Log.class);
189
190             if (siteContext == null) {
191                 log.debug("SiteContext is null");
192             } else {
193                 handler.set("site", siteContext, SiteContext.class);
194             }
195
196             if (configuration == null) {
197                 log.debug("Configuration is null");
198             } else {
199                 handler.set("configuration", configuration,
200                         Configuration.class);
201             }
202             
203             // check if reload is necessary
204
boolean reloadScript = false;
205             long scriptLastModified = fileContent.getLastModifiedTime();
206             if (scriptLastModified > timeLastLoaded) {
207                 if (log.isDebugEnabled()) {
208                     log.debug("Loading updated or new script: " + fileName);
209                 }
210                 reloadScript = true;
211             }
212             
213             // load or reload the script if necessary
214
if (reloadScript || scriptString == null) {
215                 synchronized (this) {
216                     if (reloadScript || scriptString == null) {
217                         timeLastLoaded = System.currentTimeMillis();
218                         scriptString = IOUtils.getStringFromReader(new InputStreamReader JavaDoc(fileContent.getInputStream()));
219                     }
220                 }
221             }
222             
223             // execute the script
224
handler.execute(fileName.getURI(), scriptString, context,
225                     configuration);
226
227         } catch (IOException JavaDoc e) {
228             Object JavaDoc[] args = {fileName.getURI(), e.getMessage()};
229             String JavaDoc msg = MessageUtilities.getMessage(getClass(),
230                     JPublishEngine.MESSAGE_PACKAGE, "scriptIOError", args);
231             throw new ScriptExecutionException(msg, e, fileName.getURI());
232         } catch (Exception JavaDoc e) {
233             Object JavaDoc[] args = {fileName.getURI(), e.getMessage()};
234             String JavaDoc msg = MessageUtilities.getMessage(getClass(),
235                     JPublishEngine.MESSAGE_PACKAGE, "scriptError", args);
236             throw new ScriptExecutionException(msg, e,
237                     fileName.getURI());
238         } finally {
239             String JavaDoc hostname = "?";
240             if (context != null) {
241                 HttpServletRequest JavaDoc request = context.getRequest();
242                 if (request != null) {
243                     hostname = request.getServerName();
244                 }
245             }
246             long elapsed = System.currentTimeMillis() - startTime;
247             log.info("Elapsed time for " + hostname + ":/" + fileName + ":" + elapsed + "ms");
248         }
249     }
250 }
251
Popular Tags