1 48 49 package org.jpublish.action; 50 51 import java.io.IOException ; 52 import java.io.InputStreamReader ; 53 54 import javax.servlet.ServletContext ; 55 import javax.servlet.http.HttpServletRequest ; 56 import javax.servlet.http.HttpServletResponse ; 57 import javax.servlet.http.HttpSession ; 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 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 scriptLanguage = null; 101 private String scriptString = null; 102 private long timeLastLoaded = -1; 103 104 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 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 ScriptHandler handler = handlerDefinition.getInstance(); 136 handler.setFile(file); 137 138 if (context != null) { 140 ServletContext application = context.getApplication(); 141 HttpServletRequest request = context.getRequest(); 142 HttpServletResponse response = context.getResponse(); 143 HttpSession session = context.getSession(); 144 Page page = context.getPage(); 145 146 handler.set("context", context, RequestContext.class); 148 149 if (page == null) { 151 log.debug("Page request is null"); 152 } else { 153 handler.set("page", page, Page.class); 154 } 155 156 if (request == null) { 158 log.debug("HTTP request is null"); 159 } else { 160 handler.set("request", request, 161 HttpServletRequest .class); 162 } 163 164 if (response == null) { 165 log.debug("HTTP response is null"); 166 } else { 167 handler.set("response", response, 168 HttpServletResponse .class); 169 } 170 171 if (session == null) { 172 log.debug("HTTP session is null"); 173 } else { 174 handler.set("session", session, HttpSession .class); 175 } 176 177 if (application == null) { 178 log.debug("ServletContext is null"); 179 } else { 180 handler.set("application", application, 181 ServletContext .class); 182 } 183 } 184 185 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 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 if (reloadScript || scriptString == null) { 215 synchronized (this) { 216 if (reloadScript || scriptString == null) { 217 timeLastLoaded = System.currentTimeMillis(); 218 scriptString = IOUtils.getStringFromReader(new InputStreamReader (fileContent.getInputStream())); 219 } 220 } 221 } 222 223 handler.execute(fileName.getURI(), scriptString, context, 225 configuration); 226 227 } catch (IOException e) { 228 Object [] args = {fileName.getURI(), e.getMessage()}; 229 String msg = MessageUtilities.getMessage(getClass(), 230 JPublishEngine.MESSAGE_PACKAGE, "scriptIOError", args); 231 throw new ScriptExecutionException(msg, e, fileName.getURI()); 232 } catch (Exception e) { 233 Object [] args = {fileName.getURI(), e.getMessage()}; 234 String msg = MessageUtilities.getMessage(getClass(), 235 JPublishEngine.MESSAGE_PACKAGE, "scriptError", args); 236 throw new ScriptExecutionException(msg, e, 237 fileName.getURI()); 238 } finally { 239 String hostname = "?"; 240 if (context != null) { 241 HttpServletRequest 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 |