1 16 17 package org.springframework.ui.velocity; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.velocity.app.VelocityEngine; 29 import org.apache.velocity.exception.VelocityException; 30 import org.apache.velocity.runtime.RuntimeConstants; 31 32 import org.springframework.core.io.DefaultResourceLoader; 33 import org.springframework.core.io.Resource; 34 import org.springframework.core.io.ResourceLoader; 35 import org.springframework.core.io.support.PropertiesLoaderUtils; 36 import org.springframework.util.StringUtils; 37 38 73 public class VelocityEngineFactory { 74 75 protected final Log logger = LogFactory.getLog(getClass()); 76 77 private Resource configLocation; 78 79 private final Map velocityProperties = new HashMap (); 80 81 private String resourceLoaderPath; 82 83 private ResourceLoader resourceLoader = new DefaultResourceLoader(); 84 85 private boolean preferFileSystemAccess = true; 86 87 private boolean overrideLogging = true; 88 89 90 96 public void setConfigLocation(Resource configLocation) { 97 this.configLocation = configLocation; 98 } 99 100 112 public void setVelocityProperties(Properties velocityProperties) { 113 setVelocityPropertiesMap(velocityProperties); 114 } 115 116 121 public void setVelocityPropertiesMap(Map velocityPropertiesMap) { 122 if (velocityPropertiesMap != null) { 123 this.velocityProperties.putAll(velocityPropertiesMap); 124 } 125 } 126 127 154 public void setResourceLoaderPath(String resourceLoaderPath) { 155 this.resourceLoaderPath = resourceLoaderPath; 156 } 157 158 165 public void setResourceLoader(ResourceLoader resourceLoader) { 166 this.resourceLoader = resourceLoader; 167 } 168 169 172 protected ResourceLoader getResourceLoader() { 173 return this.resourceLoader; 174 } 175 176 188 public void setPreferFileSystemAccess(boolean preferFileSystemAccess) { 189 this.preferFileSystemAccess = preferFileSystemAccess; 190 } 191 192 195 protected boolean isPreferFileSystemAccess() { 196 return this.preferFileSystemAccess; 197 } 198 199 204 public void setOverrideLogging(boolean overrideLogging) { 205 this.overrideLogging = overrideLogging; 206 } 207 208 209 215 public VelocityEngine createVelocityEngine() throws IOException , VelocityException { 216 VelocityEngine velocityEngine = newVelocityEngine(); 217 Properties props = new Properties (); 218 219 if (this.configLocation != null) { 221 if (logger.isInfoEnabled()) { 222 logger.info("Loading Velocity config from [" + this.configLocation + "]"); 223 } 224 PropertiesLoaderUtils.fillProperties(props, this.configLocation); 225 } 226 227 if (!this.velocityProperties.isEmpty()) { 229 props.putAll(this.velocityProperties); 230 } 231 232 if (this.resourceLoaderPath != null) { 234 initVelocityResourceLoader(velocityEngine, this.resourceLoaderPath); 235 } 236 237 if (this.overrideLogging) { 239 velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new CommonsLoggingLogSystem()); 240 } 241 242 for (Iterator it = props.entrySet().iterator(); it.hasNext();) { 244 Map.Entry entry = (Map.Entry ) it.next(); 245 if (!(entry.getKey() instanceof String )) { 246 throw new IllegalArgumentException ( 247 "Illegal property key [" + entry.getKey() + "]: only Strings allowed"); 248 } 249 velocityEngine.setProperty((String ) entry.getKey(), entry.getValue()); 250 } 251 252 postProcessVelocityEngine(velocityEngine); 253 254 try { 255 velocityEngine.init(); 257 } 258 catch (IOException ex) { 259 throw ex; 260 } 261 catch (VelocityException ex) { 262 throw ex; 263 } 264 catch (RuntimeException ex) { 265 throw ex; 266 } 267 catch (Exception ex) { 268 logger.error("Why does VelocityEngine throw a generic checked exception, after all?", ex); 269 throw new VelocityException(ex.toString()); 270 } 271 272 return velocityEngine; 273 } 274 275 284 protected VelocityEngine newVelocityEngine() throws IOException , VelocityException { 285 return new VelocityEngine(); 286 } 287 288 299 protected void initVelocityResourceLoader(VelocityEngine velocityEngine, String resourceLoaderPath) { 300 if (isPreferFileSystemAccess()) { 301 try { 304 StringBuffer resolvedPath = new StringBuffer (); 305 String [] paths = StringUtils.commaDelimitedListToStringArray(resourceLoaderPath); 306 for (int i = 0; i < paths.length; i++) { 307 String path = paths[i]; 308 Resource resource = getResourceLoader().getResource(path); 309 File file = resource.getFile(); if (logger.isDebugEnabled()) { 311 logger.debug("Resource loader path [" + path + "] resolved to file [" + file.getAbsolutePath() + "]"); 312 } 313 resolvedPath.append(file.getAbsolutePath()); 314 if (i < paths.length - 1) { 315 resolvedPath.append(','); 316 } 317 } 318 velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); 319 velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "true"); 320 velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, resolvedPath.toString()); 321 } 322 catch (IOException ex) { 323 if (logger.isDebugEnabled()) { 324 logger.debug("Cannot resolve resource loader path [" + resourceLoaderPath + 325 "] to [java.io.File]: using SpringResourceLoader", ex); 326 } 327 initSpringResourceLoader(velocityEngine, resourceLoaderPath); 328 } 329 } 330 else { 331 if (logger.isDebugEnabled()) { 334 logger.debug("File system access not preferred: using SpringResourceLoader"); 335 } 336 initSpringResourceLoader(velocityEngine, resourceLoaderPath); 337 } 338 } 339 340 348 protected void initSpringResourceLoader(VelocityEngine velocityEngine, String resourceLoaderPath) { 349 velocityEngine.setProperty( 350 RuntimeConstants.RESOURCE_LOADER, SpringResourceLoader.NAME); 351 velocityEngine.setProperty( 352 SpringResourceLoader.SPRING_RESOURCE_LOADER_CLASS, SpringResourceLoader.class.getName()); 353 velocityEngine.setProperty( 354 SpringResourceLoader.SPRING_RESOURCE_LOADER_CACHE, "true"); 355 velocityEngine.setApplicationAttribute( 356 SpringResourceLoader.SPRING_RESOURCE_LOADER, getResourceLoader()); 357 velocityEngine.setApplicationAttribute( 358 SpringResourceLoader.SPRING_RESOURCE_LOADER_PATH, resourceLoaderPath); 359 } 360 361 372 protected void postProcessVelocityEngine(VelocityEngine velocityEngine) 373 throws IOException , VelocityException { 374 } 375 376 } 377 | Popular Tags |