Java Code Examples for org.apache.velocity.app.VelocityEngine#setApplicationAttribute()

The following examples show how to use org.apache.velocity.app.VelocityEngine#setApplicationAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VelocityConfigurer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Provides a ClasspathResourceLoader in addition to any default or user-defined
 * loader in order to load the spring Velocity macros from the class path.
 * @see org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
 */
@Override
protected void postProcessVelocityEngine(VelocityEngine velocityEngine) {
	velocityEngine.setApplicationAttribute(ServletContext.class.getName(), this.servletContext);
	velocityEngine.setProperty(
			SPRING_MACRO_RESOURCE_LOADER_CLASS, ClasspathResourceLoader.class.getName());
	velocityEngine.addProperty(
			VelocityEngine.RESOURCE_LOADER, SPRING_MACRO_RESOURCE_LOADER_NAME);
	velocityEngine.addProperty(
			VelocityEngine.VM_LIBRARY, SPRING_MACRO_LIBRARY);

	if (logger.isInfoEnabled()) {
		logger.info("ClasspathResourceLoader with name '" + SPRING_MACRO_RESOURCE_LOADER_NAME +
				"' added to configured VelocityEngine");
	}
}
 
Example 2
Source File: VelocityConfigurer.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Provides a ClasspathResourceLoader in addition to any default or user-defined
 * loader in order to load the spring Velocity macros from the class path.
 * @see org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
 */
@Override
protected void postProcessVelocityEngine(VelocityEngine velocityEngine) {
	velocityEngine.setApplicationAttribute(ServletContext.class.getName(), this.servletContext);
	velocityEngine.setProperty(
			SPRING_MACRO_RESOURCE_LOADER_CLASS, ClasspathResourceLoader.class.getName());
	velocityEngine.addProperty(
			VelocityEngine.RESOURCE_LOADER, SPRING_MACRO_RESOURCE_LOADER_NAME);
	velocityEngine.addProperty(
			VelocityEngine.VM_LIBRARY, SPRING_MACRO_LIBRARY);

	if (logger.isInfoEnabled()) {
		logger.info("ClasspathResourceLoader with name '" + SPRING_MACRO_RESOURCE_LOADER_NAME +
				"' added to configured VelocityEngine");
	}
}
 
Example 3
Source File: VelocityInlineDispatcher.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public void init(ServletContext context) throws ServletException
{
	inlineMacros = MACROS;
	try
	{
		vengine = new VelocityEngine();

		vengine.setApplicationAttribute(ServletContext.class.getName(), context);
		vengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new SLF4JLogChute());

		Properties p = new Properties();
		p.load(this.getClass().getResourceAsStream("rwikivelocity.config"));
		vengine.init(p);
		vengine.getTemplate(inlineMacros);

	}
	catch (Exception ex)
	{
		throw new ServletException(ex);
	}
}
 
Example 4
Source File: VelocityInlineDispatcher.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public void init(ServletContext context) throws ServletException
{
	inlineMacros = MACROS;
	try
	{
		vengine = new VelocityEngine();

		vengine.setApplicationAttribute(ServletContext.class.getName(), context);
		vengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new SLF4JLogChute());

		Properties p = new Properties();
		p.load(this.getClass().getResourceAsStream("rwikivelocity.config"));
		vengine.init(p);
		vengine.getTemplate(inlineMacros);

	}
	catch (Exception ex)
	{
		throw new ServletException(ex);
	}
}
 
Example 5
Source File: StringResourceLoaderRepositoryTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testPreCreatedAppRepo() throws Exception
{
    VelocityEngine engine = newStringEngine("my.app.repo", false);
    MyRepo repo = new MyRepo();
    repo.put("you/bar.vm", "You look $bar");
    engine.setApplicationAttribute("my.app.repo", repo);

    String out = render(engine.getTemplate("you/bar.vm"));
    assertEquals(out, "You look horrible!");
}
 
Example 6
Source File: DefaultVelocityEngineProducer.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Produces
@ViewEngineConfig
public VelocityEngine getVelocityEngine() {
    VelocityEngine velocityEngine = new VelocityEngine();
    velocityEngine.setProperty("resource.loader", "webapp");
    velocityEngine.setProperty("webapp.resource.loader.class", ServletContextResourceLoader.class.getCanonicalName());
    velocityEngine.setApplicationAttribute("javax.servlet.ServletContext", servletContext);
    velocityEngine.init();
    return velocityEngine;
}
 
Example 7
Source File: VelocityView.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the Velocity runtime, first calling
 * loadConfiguration(JeeConfig) to get a
 * java.util.Properties
 * of configuration information
 * and then calling velocityEngine.init().  Override this
 * to do anything to the environment before the
 * initialization of the singleton takes place, or to
 * initialize the singleton in other ways.
 *
 * @param config servlet configuration parameters
 * @param velocity VelocityEngine instance
 */
protected void init(JeeConfig config, final VelocityEngine velocity)
{
    // put the servlet context into Velocity's application attributes,
    // where the WebappResourceLoader can find them
    velocity.setApplicationAttribute(SERVLET_CONTEXT_KEY, this.servletContext);

    // configure the engine itself
    configure(config, velocity);

    // now all is ready - init Velocity
    try
    {
        if (System.getSecurityManager() != null)
        {
            AccessController.doPrivileged(
                new PrivilegedAction<Void>()
                {
                    @Override
                    public Void run()
                    {
                        velocity.init();
                        return null;
                    }
                });
        }
        else
        {
            velocity.init();
        }
    }
    catch(Exception e)
    {
        String msg = "Could not initialize VelocityEngine";
        getLog().error(msg, e);
        e.printStackTrace();
        throw new RuntimeException(msg + ": "+e, e);
    }
}
 
Example 8
Source File: VelocityLoginRenderEngine.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void init() throws Exception {
	/*try
	{
		styleAble = serverConfigurationService.getBoolean("portal.styleable", false);
		styleAbleContentSummary = serverConfigurationService.getBoolean("portal.styleable.contentSummary", false);
	}
	catch (Exception ex)
	{
		log
				.warn("No Server configuration service available, assuming default settings ");
	}*/
	
	if ( sessionManager == null ) {
		log.warn("No session Manager, assuming test mode ");
	}

	vengine = new VelocityEngine();

	vengine.setApplicationAttribute(ServletContext.class.getName(), context);

	vengine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, new SLF4JLogChute());

	Properties p = new Properties();
	InputStream in = this.getClass().getResourceAsStream(loginConfig);
	if ( in == null ) {
		throw new RuntimeException("Unable to load configuration " + loginConfig);
	} else {
		log.info("Loaded " + loginConfig);
	}
	p.load(in);
	vengine.init(p);
	availableLoginSkins = new ArrayList();
	Map m = new HashMap();
	m.put("name", "defaultskin");
	m.put("display", "Default");
	availableLoginSkins.add(m);

	vengine.getTemplate("/vm/defaultskin/macros.vm");
}
 
Example 9
Source File: VelocityLoginRenderEngine.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void init() throws Exception {
	/*try
	{
		styleAble = serverConfigurationService.getBoolean("portal.styleable", false);
		styleAbleContentSummary = serverConfigurationService.getBoolean("portal.styleable.contentSummary", false);
	}
	catch (Exception ex)
	{
		log
				.warn("No Server configuration service available, assuming default settings ");
	}*/
	
	if ( sessionManager == null ) {
		log.warn("No session Manager, assuming test mode ");
	}

	vengine = new VelocityEngine();

	vengine.setApplicationAttribute(ServletContext.class.getName(), context);

	vengine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, new SLF4JLogChute());

	Properties p = new Properties();
	InputStream in = this.getClass().getResourceAsStream(loginConfig);
	if ( in == null ) {
		throw new RuntimeException("Unable to load configuration " + loginConfig);
	} else {
		log.info("Loaded " + loginConfig);
	}
	p.load(in);
	vengine.init(p);
	availableLoginSkins = new ArrayList();
	Map m = new HashMap();
	m.put("name", "defaultskin");
	m.put("display", "Default");
	availableLoginSkins.add(m);

	vengine.getTemplate("/vm/defaultskin/macros.vm");
}
 
Example 10
Source File: VelocityEngineFactory.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize a SpringResourceLoader for the given VelocityEngine.
 * <p>Called by {@code initVelocityResourceLoader}.
 * @param velocityEngine the VelocityEngine to configure
 * @param resourceLoaderPath the path to load Velocity resources from
 * @see SpringResourceLoader
 * @see #initVelocityResourceLoader
 */
protected void initSpringResourceLoader(VelocityEngine velocityEngine, String resourceLoaderPath) {
	velocityEngine.setProperty(
			RuntimeConstants.RESOURCE_LOADER, SpringResourceLoader.NAME);
	velocityEngine.setProperty(
			SpringResourceLoader.SPRING_RESOURCE_LOADER_CLASS, SpringResourceLoader.class.getName());
	velocityEngine.setProperty(
			SpringResourceLoader.SPRING_RESOURCE_LOADER_CACHE, "true");
	velocityEngine.setApplicationAttribute(
			SpringResourceLoader.SPRING_RESOURCE_LOADER, getResourceLoader());
	velocityEngine.setApplicationAttribute(
			SpringResourceLoader.SPRING_RESOURCE_LOADER_PATH, resourceLoaderPath);
}
 
Example 11
Source File: DefaultVelocityEngineProducer.java    From ozark with Apache License 2.0 5 votes vote down vote up
@Produces
@ViewEngineConfig
public VelocityEngine getVelocityEngine() {
    VelocityEngine velocityEngine = new VelocityEngine();
    velocityEngine.setProperty("resource.loader", "webapp");
    velocityEngine.setProperty("webapp.resource.loader.class", WebappResourceLoader.class.getCanonicalName());
    velocityEngine.setApplicationAttribute("javax.servlet.ServletContext", servletContext);
    velocityEngine.init();
    return velocityEngine;
}
 
Example 12
Source File: VelocityEngineFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize a SpringResourceLoader for the given VelocityEngine.
 * <p>Called by {@code initVelocityResourceLoader}.
 * @param velocityEngine the VelocityEngine to configure
 * @param resourceLoaderPath the path to load Velocity resources from
 * @see SpringResourceLoader
 * @see #initVelocityResourceLoader
 */
protected void initSpringResourceLoader(VelocityEngine velocityEngine, String resourceLoaderPath) {
	velocityEngine.setProperty(
			RuntimeConstants.RESOURCE_LOADER, SpringResourceLoader.NAME);
	velocityEngine.setProperty(
			SpringResourceLoader.SPRING_RESOURCE_LOADER_CLASS, SpringResourceLoader.class.getName());
	velocityEngine.setProperty(
			SpringResourceLoader.SPRING_RESOURCE_LOADER_CACHE, "true");
	velocityEngine.setApplicationAttribute(
			SpringResourceLoader.SPRING_RESOURCE_LOADER, getResourceLoader());
	velocityEngine.setApplicationAttribute(
			SpringResourceLoader.SPRING_RESOURCE_LOADER_PATH, resourceLoaderPath);
}
 
Example 13
Source File: VelocityEngineFactory.java    From scoold with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize a SpringResourceLoader for the given VelocityEngine.
 * <p>
 * Called by {@code initVelocityResourceLoader}.
 *
 * @param velocityEngine the VelocityEngine to configure
 * @param resourceLoaderPath the path to load Velocity resources from
 * @see SpringResourceLoader
 * @see #initVelocityResourceLoader
 */
protected void initSpringResourceLoader(VelocityEngine velocityEngine, String resourceLoaderPath) {
	velocityEngine.setProperty(
			RuntimeConstants.RESOURCE_LOADERS, SpringResourceLoader.NAME);
	velocityEngine.setProperty(
			SpringResourceLoader.SPRING_RESOURCE_LOADER_CLASS, SpringResourceLoader.class.getName());
	velocityEngine.setProperty(
			SpringResourceLoader.SPRING_RESOURCE_LOADER_CACHE, "true");
	velocityEngine.setApplicationAttribute(
			SpringResourceLoader.SPRING_RESOURCE_LOADER, getResourceLoader());
	velocityEngine.setApplicationAttribute(
			SpringResourceLoader.SPRING_RESOURCE_LOADER_PATH, resourceLoaderPath);
}
 
Example 14
Source File: VelocityEngineFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize a SpringResourceLoader for the given VelocityEngine.
 * <p>Called by {@code initVelocityResourceLoader}.
 * @param velocityEngine the VelocityEngine to configure
 * @param resourceLoaderPath the path to load Velocity resources from
 * @see SpringResourceLoader
 * @see #initVelocityResourceLoader
 */
protected void initSpringResourceLoader(VelocityEngine velocityEngine, String resourceLoaderPath) {
	velocityEngine.setProperty(
			RuntimeConstants.RESOURCE_LOADER, SpringResourceLoader.NAME);
	velocityEngine.setProperty(
			SpringResourceLoader.SPRING_RESOURCE_LOADER_CLASS, SpringResourceLoader.class.getName());
	velocityEngine.setProperty(
			SpringResourceLoader.SPRING_RESOURCE_LOADER_CACHE, "true");
	velocityEngine.setApplicationAttribute(
			SpringResourceLoader.SPRING_RESOURCE_LOADER, getResourceLoader());
	velocityEngine.setApplicationAttribute(
			SpringResourceLoader.SPRING_RESOURCE_LOADER_PATH, resourceLoaderPath);
}
 
Example 15
Source File: ControllerServlet2.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
@Override
public void init(ServletConfig servletConfig) throws ServletException
{
	super.init(servletConfig);

	ServletContext sc = servletConfig.getServletContext();

	wac = WebApplicationContextUtils.getWebApplicationContext(sc);
	if (wac == null)
	{
		throw new ServletException("Unable to get WebApplicationContext ");
	}
	searchBeanFactory = (SearchBeanFactory) wac.getBean("search-searchBeanFactory");
	if (searchBeanFactory == null)
	{
		throw new ServletException("Unable to get search-searchBeanFactory ");
	}
	sessionManager = (SessionManager) wac.getBean(SessionManager.class.getName());
	if (sessionManager == null)
	{
		throw new ServletException("Unable to get " + SessionManager.class.getName());
	}

	ServerConfigurationService  serverConfigurationService = (ServerConfigurationService) wac.getBean(ServerConfigurationService.class.getName());
	if (serverConfigurationService == null)
	{
		throw new ServletException("Unable to get " + ServerConfigurationService.class.getName());
	}
	serverUrl = serverConfigurationService.getServerUrl();

	searchBeanFactory.setContext(sc);

	inlineMacros = MACROS;
	InputStream is = null;
	try
	{
		vengine = new VelocityEngine();

		vengine.setApplicationAttribute(ServletContext.class.getName(), sc);
		vengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new SLF4JLogChute());

		Properties p = new Properties();
		is = this.getClass().getResourceAsStream("searchvelocity.config");
		p.load(is);
		vengine.init(p);
		vengine.getTemplate(inlineMacros);

	}
	catch (Exception ex)
	{
		throw new ServletException(ex);
	}
	finally
	{
		if (is !=null)
		{
			try {
				is.close();
			} catch (IOException e) {
				log.debug("exception thrown in Finally block");
			}
		}
	}
	contentTypes.put("opensearch", "application/opensearchdescription+xml");
	contentTypes.put("sakai.src", "application/opensearchdescription+xml" );
	contentTypes.put("rss20", "text/xml" );
}
 
Example 16
Source File: ControllerServlet2.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
@Override
public void init(ServletConfig servletConfig) throws ServletException
{
	super.init(servletConfig);

	ServletContext sc = servletConfig.getServletContext();

	wac = WebApplicationContextUtils.getWebApplicationContext(sc);
	if (wac == null)
	{
		throw new ServletException("Unable to get WebApplicationContext ");
	}
	searchBeanFactory = (SearchBeanFactory) wac.getBean("search-searchBeanFactory");
	if (searchBeanFactory == null)
	{
		throw new ServletException("Unable to get search-searchBeanFactory ");
	}
	sessionManager = (SessionManager) wac.getBean(SessionManager.class.getName());
	if (sessionManager == null)
	{
		throw new ServletException("Unable to get " + SessionManager.class.getName());
	}

	ServerConfigurationService  serverConfigurationService = (ServerConfigurationService) wac.getBean(ServerConfigurationService.class.getName());
	if (serverConfigurationService == null)
	{
		throw new ServletException("Unable to get " + ServerConfigurationService.class.getName());
	}
	serverUrl = serverConfigurationService.getServerUrl();

	searchBeanFactory.setContext(sc);

	inlineMacros = MACROS;
	InputStream is = null;
	try
	{
		vengine = new VelocityEngine();

		vengine.setApplicationAttribute(ServletContext.class.getName(), sc);
		vengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new SLF4JLogChute());

		Properties p = new Properties();
		is = this.getClass().getResourceAsStream("searchvelocity.config");
		p.load(is);
		vengine.init(p);
		vengine.getTemplate(inlineMacros);

	}
	catch (Exception ex)
	{
		throw new ServletException(ex);
	}
	finally
	{
		if (is !=null)
		{
			try {
				is.close();
			} catch (IOException e) {
				log.debug("exception thrown in Finally block");
			}
		}
	}
	contentTypes.put("opensearch", "application/opensearchdescription+xml");
	contentTypes.put("sakai.src", "application/opensearchdescription+xml" );
	contentTypes.put("rss20", "text/xml" );
}
 
Example 17
Source File: VelocityConfigurer.java    From scoold with Apache License 2.0 2 votes vote down vote up
/**
 * Provides a ClasspathResourceLoader in addition to any default or user-defined
 * loader in order to load the spring Velocity macros from the class path.
 * @see org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
 * @param velocityEngine engine
 */
@Override
protected void postProcessVelocityEngine(VelocityEngine velocityEngine) {
	velocityEngine.setApplicationAttribute(ServletContext.class.getName(), this.servletContext);
}