1 44 package org.jpublish; 45 46 import java.io.OutputStream ; 47 import java.io.OutputStreamWriter ; 48 import java.io.Writer ; 49 import java.net.SocketException ; 50 import java.util.Iterator ; 51 52 import javax.servlet.http.HttpServletRequest ; 53 import javax.servlet.http.HttpServletResponse ; 54 55 import org.apache.commons.logging.Log; 56 import org.apache.commons.logging.LogFactory; 57 import org.jpublish.action.ActionManager; 58 import org.jpublish.component.ComponentMap; 59 import org.jpublish.exception.ExceptionHandler; 60 import org.jpublish.exception.ExceptionWrapper; 61 import org.jpublish.module.Module; 62 import org.jpublish.page.Page; 63 import org.jpublish.page.PageInstance; 64 import org.jpublish.repository.Repository; 65 import org.jpublish.repository.RepositoryWrapper; 66 import org.jpublish.resource.ResourceManager; 67 import org.jpublish.template.Template; 68 import org.jpublish.util.DateUtilities; 69 import org.jpublish.util.MessageUtilities; 70 import org.jpublish.util.NumberUtilities; 71 72 77 public class JPublishEngine { 78 79 82 public final static String MESSAGE_PACKAGE = "org.jpublish"; 83 84 private static final Log log = LogFactory.getLog(JPublishEngine.class); 85 86 private SiteContext siteContext = null; 87 private boolean provideStaticResources = true; 88 89 94 public JPublishEngine(SiteContext siteContext) { 95 this.siteContext = siteContext; 96 log.info("Initializing"); 97 try { 99 ActionManager actionManager = siteContext.getActionManager(); 100 actionManager.executeStartupActions(); 101 } catch (Exception e) { 102 log.error("Error executing startup actions: " + e.getMessage()); 103 e.printStackTrace(); 104 } 105 106 log.info("Engine initialized."); 107 } 108 109 114 public SiteContext getSiteContext() { 115 return siteContext; 116 } 117 118 123 public boolean getProvideStaticResources() { 124 return provideStaticResources; 125 } 126 127 132 public void setProvideStaticResources(boolean provideStaticResources) { 133 this.provideStaticResources = provideStaticResources; 134 } 135 136 139 public void destroy() { 140 try { 142 ActionManager actionManager = siteContext.getActionManager(); 143 actionManager.executeShutdownActions(); 144 } catch (Exception e) { 145 log.error("Error executing shutdown actions: " + e.getMessage()); 146 } 148 149 try { 151 Iterator modules = siteContext.getModules().iterator(); 152 while (modules.hasNext()) { 153 ((Module) modules.next()).destroy(); 154 } 155 } catch (Exception e) { 156 log.error("Error destroying modules: " + e.getMessage()); 157 } 159 } 160 161 169 public void render(OutputStream out) throws EntityNotFoundException, 170 RenderException { 171 RequestContext context = RequestContext.getRequestContext(); 172 String path = context.getPath(); 173 if (log.isDebugEnabled()) { 174 log.debug("Rendering: " + path); 175 } 176 177 context.put("dateUtilities", DateUtilities.getInstance()); 179 180 context.put("numberUtilities", NumberUtilities.getInstance()); 182 183 context.put("messageUtilities", MessageUtilities.getInstance()); 185 186 context.put("syslog", SiteContext.syslog); 188 189 context.put("site", siteContext); 191 192 if (siteContext.isDebug()) { 194 context.put("context", context); 195 } 196 197 Iterator repositories = siteContext.getRepositories().iterator(); 199 while (repositories.hasNext()) { 200 Repository repository = (Repository) repositories.next(); 201 if (log.isDebugEnabled()) { 202 log.debug("Adding " + repository.getClass().getName() + " as " + 203 repository.getName()); 204 } 205 context.put(repository.getName(), new RepositoryWrapper(repository, 206 context)); 207 } 208 209 try { 210 executePreEvaluationActions(context); 212 if (context.getStopProcessing() != null) { 213 return; 214 } 215 216 if (provideStaticResources) { 217 ResourceManager staticResourceManager = 219 siteContext.getStaticResourceManager(); 220 if (log.isDebugEnabled()) { 221 log.debug("Checking if static resource exists: " + path); 222 } 223 224 if (staticResourceManager.exists(path)) { 225 executeGlobalActions(context); 227 executePathActions(context); 228 executeParameterActions(context); 229 230 log.debug("Loading static resource"); 232 233 HttpServletResponse response = context.getResponse(); 235 if (response != null) { 236 response.setDateHeader("Last-Modified", 237 staticResourceManager.getLastModified(path)); 238 response.setContentLength((int) staticResourceManager.getContentLength(path)); 239 } 240 241 try { 242 staticResourceManager.load(path, out); 243 out.flush(); 244 } catch (SocketException e) { 245 if (log.isWarnEnabled()) { 246 log.warn("Error writing to output stream: " + 247 e.getMessage()); 248 } 249 } 250 return; 251 } else { 252 if (log.isDebugEnabled()) { 253 log.debug("Static resource '" + path + "' not found"); 254 } 255 } 256 } 257 258 log.debug("Loading the page."); 260 PageInstance pageInstance = 261 siteContext.getPageManager().getPage(path); 262 Page page = new Page(pageInstance); 263 264 if (log.isDebugEnabled()) { 265 log.debug("Page path: " + page.getPath()); 266 } 267 268 context.disableCheckReservedNames(); 270 271 context.put("page", page); 273 274 context.put("components", new ComponentMap(context)); 276 277 if (siteContext.isProtectReservedNames()) { 279 context.enableCheckReservedNames(); 280 } 281 282 executeGlobalActions(context); 284 if (context.getStopProcessing() != null) { 285 return; 286 } 287 288 executePathActions(context); 289 if (context.getStopProcessing() != null) { 290 return; 291 } 292 293 executeParameterActions(context); 294 if (context.getStopProcessing() != null) { 295 return; 296 } 297 298 page.executeActions(context); 300 if (context.getStopProcessing() != null) { 301 return; 302 } 303 304 if (context.getRedirect() != null) { 305 return; 306 } 307 308 Template template = siteContext.getTemplateManager().getTemplate(page.getFullTemplateName()); 310 311 String outputEncoding = page.getEncoding(); 315 Writer writer = new OutputStreamWriter (out, outputEncoding); 316 317 if (log.isDebugEnabled()) { 319 log.debug("Merging with template " + template.getPath()); 320 } 321 template.merge(context, page, writer); 322 writer.flush(); 323 } catch (EntityNotFoundException e) { 324 throw e; 325 } catch (Throwable e) { 326 ExceptionWrapper error = new ExceptionWrapper(e, context); 328 Iterator exceptionHandlers = 329 siteContext.getExceptionHandlers(path).iterator(); 330 while (exceptionHandlers.hasNext()) { 331 ExceptionHandler h = 332 (ExceptionHandler) exceptionHandlers.next(); 333 h.handleException(error); 334 } 335 336 if (!error.isConsumed()) { 337 throw new RenderException(e.getMessage(), e); 338 } 339 } finally { 340 try { 341 if (context.getResponse().isCommitted()) { 342 log.warn( 343 "The response is already committed, but we're running post evaluation actions. BE CAREFUL"); 344 } 345 executePostEvaluationActions(context); 346 } catch (Throwable e) { 347 e.printStackTrace(); 348 log.error("Error executing post evaluation actions: " + 349 e.getMessage()); 350 } 351 } 352 } 353 354 359 protected void executeGlobalActions(RequestContext context) { 360 ActionManager actionManager = siteContext.getActionManager(); 361 log.debug("Executing global actions."); 362 actionManager.executeGlobalActions(context); 363 } 364 365 370 protected void executePathActions(RequestContext context) { 371 ActionManager actionManager = siteContext.getActionManager(); 372 log.debug("Executing path actions, path: " + context.getPath()); 373 actionManager.executePathActions(context.getPath(), context); 374 } 375 376 381 protected void executeParameterActions(RequestContext context) { 382 if (!siteContext.isParameterActionsEnabled()) { 383 return; 384 } 385 386 HttpServletRequest request = context.getRequest(); 387 if (request == null) { 388 return; 389 } 390 391 ActionManager actionManager = siteContext.getActionManager(); 392 log.debug("Executing parameter actions."); 393 String [] actionNames = request.getParameterValues(siteContext.getActionIdentifier()); 394 if (actionNames != null) { 395 for (int i = 0; i < actionNames.length; i++) { 396 if (log.isDebugEnabled()) { 397 log.debug("Executing paramater action: " + actionNames[i]); 398 } 399 actionManager.execute(actionNames[i], context); 400 } 401 } 402 } 403 404 410 protected boolean executePreEvaluationActions(RequestContext context) { 411 ActionManager actionManager = siteContext.getActionManager(); 412 log.debug("Executing pre-evaluation actions."); 413 return actionManager.executePreEvaluationActions(context.getPath(), 414 context); 415 } 416 417 423 protected boolean executePostEvaluationActions(RequestContext context) { 424 ActionManager actionManager = siteContext.getActionManager(); 425 log.debug("Executing post-evaluation actions."); 426 actionManager.executePostEvaluationActions(context.getPath(), context); 427 return false; 428 } 429 430 } 431 432
| Popular Tags
|