1 48 49 package org.jpublish.view.velocity; 50 51 import java.io.*; 52 import java.util.Properties ; 53 54 import com.anthonyeden.lib.config.Configuration; 55 import com.anthonyeden.lib.config.ConfigurationException; 56 import com.anthonyeden.lib.util.IOUtilities; 57 import com.anthonyeden.lib.util.MessageUtilities; 58 import org.apache.commons.collections.ExtendedProperties; 59 import org.apache.commons.logging.Log; 60 import org.apache.commons.logging.LogFactory; 61 import org.apache.commons.vfs.FileContent; 62 import org.apache.commons.vfs.FileObject; 63 import org.apache.commons.vfs.FileSystemException; 64 import org.apache.commons.vfs.FileSystemManager; 65 import org.apache.velocity.app.VelocityEngine; 66 import org.apache.velocity.runtime.RuntimeConstants; 67 import org.jpublish.JPublishEngine; 68 import org.jpublish.JPublishRuntimeException; 69 import org.jpublish.RequestContext; 70 import org.jpublish.page.Page; 71 import org.jpublish.view.AbstractViewRenderer; 72 import org.jpublish.view.ViewRenderException; 73 74 80 81 public class VelocityViewRenderer extends AbstractViewRenderer { 82 83 private static final String DEFAULT_PROPERTIES_PATH = 84 "WEB-INF/velocity.properties"; 85 86 92 private Log log = LogFactory.getLog(VelocityViewRenderer.class); 93 private VelocityEngine velocityEngine = new VelocityEngine(); 94 private Properties velocityProperties = new Properties (); 95 96 private boolean resourceCacheEnabled = false; 97 private int resourceCacheInterval = 2; 98 99 104 105 public boolean isResourceCacheEnabled() { 106 return resourceCacheEnabled; 107 } 108 109 114 115 public void setResourceCacheEnabled(boolean resourceCacheEnabled) { 116 if (log.isDebugEnabled()) { 117 log.debug("Resource cache enabled: " + resourceCacheEnabled); 118 } 119 this.resourceCacheEnabled = resourceCacheEnabled; 120 } 121 122 127 128 public void setResourceCacheEnabled(String resourceCacheEnabled) { 129 setResourceCacheEnabled("true".equals(resourceCacheEnabled)); 130 } 131 132 138 139 public int getResourceCacheInterval() { 140 return resourceCacheInterval; 141 } 142 143 149 150 public void setResourceCacheInterval(int resourceCacheInterval) { 151 if (log.isDebugEnabled()) { 152 log.debug("Resource cache interval: " + resourceCacheInterval); 153 } 154 this.resourceCacheInterval = resourceCacheInterval; 155 } 156 157 163 164 public void setResourceCacheInterval(String resourceCacheInterval) { 165 if (resourceCacheInterval != null) { 166 setResourceCacheInterval(Integer.parseInt(resourceCacheInterval)); 167 } 168 } 169 170 173 174 public void init() { 175 try { 176 log.debug("init()"); 177 178 181 ExtendedProperties eprops = new ExtendedProperties(); 182 eprops.putAll(velocityProperties); 183 eprops.addProperty(RuntimeConstants.RESOURCE_LOADER, "jpublish"); 184 eprops.setProperty("jpublish.resource.loader.description", 185 "JPublish internal resource loader."); 186 eprops.setProperty("jpublish.resource.loader.class", 187 "org.jpublish.view.velocity.JPublishResourceLoader"); 188 eprops.setProperty("jpublish.resource.loader.siteContext", siteContext); 189 190 if (resourceCacheEnabled) { 191 eprops.setProperty("jpublish.resource.loader.cache", "true"); 192 eprops.setProperty("jpublish.resource.loader.modificationCheckInterval", 193 Integer.toString(getResourceCacheInterval())); 194 } 195 196 velocityEngine.setExtendedProperties(eprops); 197 198 log.debug("Initializing VelocityEngine"); 199 velocityEngine.init(); 200 201 log.info("Resource loader: " + velocityEngine.getProperty(VelocityEngine.RESOURCE_LOADER)); 202 } catch (Exception e) { 203 Object [] args = {e.getMessage()}; 204 throw new JPublishRuntimeException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 205 "initError", args), 206 e); 207 } 208 209 } 210 211 220 221 public void render(RequestContext context, String path, Writer out) 222 throws IOException, ViewRenderException { 223 try { 224 Page page = context.getPage(); 225 226 if (log.isDebugEnabled()) { 227 log.debug("Rendering: " + path); 228 log.debug("Content encoding: " + page.getEncoding()); 229 } 230 231 VelocityViewContext viewContext = new VelocityViewContext(context); 232 velocityEngine.mergeTemplate(path, page.getEncoding(), viewContext, 233 out); 234 } catch (IOException e) { 236 throw e; 237 } catch (NullPointerException e) { 238 Object [] args = {}; 239 throw new ViewRenderException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, "renderErrorNull", 240 args), e); 241 } catch (Exception e) { 242 Object [] args = {path, e.getMessage()}; 243 throw new ViewRenderException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, "renderError", 244 args), e); 245 } 246 } 247 248 257 258 public void render(RequestContext context, String path, OutputStream out) 259 throws IOException, ViewRenderException { 260 render(context, path, new OutputStreamWriter(out)); 261 } 262 263 276 277 public void render(RequestContext context, String path, Reader in, 278 Writer out) throws IOException, ViewRenderException { 279 try { 280 if (log.isDebugEnabled()) { 281 log.debug("Rendering: " + path); 282 } 283 284 VelocityViewContext viewContext = new VelocityViewContext(context); 285 velocityEngine.evaluate(viewContext, out, path, in); 286 } catch (IOException e) { 287 throw e; 288 } catch (Exception e) { 289 Object [] args = {path, e.getMessage()}; 290 throw new ViewRenderException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, "renderError", 291 args), e); 292 } 293 } 294 295 308 309 public void render(RequestContext context, String path, InputStream in, 310 OutputStream out) throws IOException, ViewRenderException { 311 render(context, path, new InputStreamReader(in), 312 new OutputStreamWriter(out)); 313 } 314 315 320 321 public void loadConfiguration(Configuration configuration) 322 throws ConfigurationException { 323 324 setResourceCacheEnabled(configuration.getChildValue("resource-cache-enabled")); 325 setResourceCacheInterval(configuration.getChildValue("resource-cache-interval")); 326 327 String propertiesPath = 328 configuration.getChildValue("velocity-properties"); 329 330 try { 331 332 if (propertiesPath == null) { 333 propertiesPath = DEFAULT_PROPERTIES_PATH; 334 } 335 336 FileSystemManager fileSystemManager = 337 siteContext.getContextFileSystemManager(); 338 FileObject baseFile = fileSystemManager.getBaseFile(); 339 FileObject propertiesFile = fileSystemManager.resolveFile(baseFile, propertiesPath); 340 341 if (propertiesFile.exists()) { 342 loadVelocityProperties(propertiesFile, configuration); 343 } else { 344 log.warn("No velocity.properties file found"); 345 } 346 } catch (IOException e) { 347 Object [] args = {propertiesPath}; 348 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), "org.jpublish", 349 "velocityPropertiesIOError", args), 350 e); 351 } 352 } 353 354 private void loadVelocityProperties(FileObject file, 355 Configuration configuration) throws IOException, FileSystemException { 356 if (log.isDebugEnabled()) { 357 log.debug("Velocity properties file: " + file); 358 } 359 360 FileContent content = file.getContent(); 361 InputStream in = null; 362 try { 363 in = content.getInputStream(); 364 velocityProperties.load(in); 365 366 370 389 390 } finally { 391 IOUtilities.close(in); 392 } 393 } 394 395 } 396 | Popular Tags |