org.zodiac.template.velocity.spring.view.VelocityConfigurer Maven / Gradle / Ivy
The newest version!
package org.zodiac.template.velocity.spring.view;
import java.io.IOException;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ResourceLoaderAware;
import org.zodiac.sdk.toolkit.util.AssertUtil;
import org.zodiac.template.velocity.spring.ui.VelocityEngineFactory;
public class VelocityConfigurer extends VelocityEngineFactory
implements VelocityConfig, InitializingBean, ResourceLoaderAware {
/* the name of the resource loader for Spring's bind macros */
private static final String SPRING_MACRO_RESOURCE_LOADER_NAME = "springMacro";
/* the key for the class of Spring's bind macro resource loader */
//private static final String SPRING_MACRO_RESOURCE_LOADER_CLASS = "springMacro.resource.loader.class";
private static final String SPRING_MACRO_RESOURCE_LOADER_CLASS = String.format("%s.%s.class", RuntimeConstants.RESOURCE_LOADER, SPRING_MACRO_RESOURCE_LOADER_NAME);
/* the name of Spring's default bind macro library */
private static final String SPRING_MACRO_LIBRARY = "org/zodiac/template/velocity/spring/view/spring.vm";
private VelocityEngine velocityEngine;
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
/**
* Initialize VelocityEngineFactory's VelocityEngine if not overridden by a pre-configured VelocityEngine.
*
* @throws IOException IOException
* @throws VelocityException VelocityException
*/
@Override
public void afterPropertiesSet() throws IOException, VelocityException {
if (this.velocityEngine == null) {
this.velocityEngine = createVelocityEngine();
}
}
@Override
protected void postProcessVelocityEngine(VelocityEngine velocityEngine) {
velocityEngine.setProperty(SPRING_MACRO_RESOURCE_LOADER_CLASS, ClasspathResourceLoader.class.getName());
/*velocityEngine.addProperty(RuntimeConstants.RESOURCE_LOADER, SPRING_MACRO_RESOURCE_LOADER_NAME);*/
velocityEngine.addProperty(RuntimeConstants.RESOURCE_LOADERS, SPRING_MACRO_RESOURCE_LOADER_NAME);
velocityEngine.addProperty(RuntimeConstants.VM_LIBRARY, SPRING_MACRO_LIBRARY);
if (logger.isInfoEnabled()) {
logger.info("ClasspathResourceLoader with name '" + SPRING_MACRO_RESOURCE_LOADER_NAME
+ "' added to configured VelocityEngine");
}
}
@Override
public VelocityEngine getVelocityEngine() {
AssertUtil.state(this.velocityEngine != null, "No velocityEngine available");
return this.velocityEngine;
}
}