ZTemplatesContextListener.java :  » Web-Framework » ztemplates » org » ztemplates » web » Java Open Source

Java Open Source » Web Framework » ztemplates 
ztemplates » org » ztemplates » web » ZTemplatesContextListener.java
/*
 * Copyright 2007 Gerd Ziegler (www.gerdziegler.de)
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 * @author www.gerdziegler.de
 */
package org.ztemplates.web;

import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.apache.velocity.app.Velocity;
import org.ztemplates.render.velocity.ZVelocityRenderer;
import org.ztemplates.web.impl.ZClasspathServiceImpl;

public class ZTemplatesContextListener implements ServletContextListener
{

  private static final Logger log = Logger.getLogger(ZTemplatesContextListener.class);


  public void contextInitialized(ServletContextEvent servletContextEvent)
  {
    ServletContext ctx = servletContextEvent.getServletContext();
    ZTemplatesContextListener.initVelocity(ctx);
    log.info("context initialized");

    ZClasspathServiceImpl.preloadClassRepository(ctx);
  }


  public void contextDestroyed(ServletContextEvent arg0)
  {
    log.info("context destroyed");
  }


  private static void initVelocity(ServletContext servletContext)
  {
    try
    {
      String KEY = "file.resource.loader.path";
      log.info("initializing standalone Velocity...");
      log.info("loading velocity properties...");
      Properties prop = getVelocityProperties(servletContext);
      String templatePath = prop.getProperty(KEY);
      if (templatePath != null)
      {
        String realTemplatePath = servletContext.getRealPath(templatePath) + "," + templatePath;
        log.info("converting " + KEY + " - [" + templatePath + "] to [" + realTemplatePath + "]");
        prop.setProperty(KEY, realTemplatePath);
      }
      prop.list(System.out);
      Velocity.init(prop);
    }
    catch (Exception e)
    {
      log.error("error while initializing velocity:", e);
    }
  }


  private static Properties getVelocityProperties(ServletContext servletContext) throws Exception
  {
    String propFile = "WEB-INF/velocity.properties";
    Properties prop = ZVelocityRenderer.getVelocityProperties(propFile);
    if (prop != null)
    {
      return prop;
    }

    InputStream in = servletContext.getResourceAsStream(propFile);
    if (in != null)
    {
      log.info("loading velocity properties from ServletContext.getResourceAsStream(" + propFile + ")");
      prop = new Properties();
      prop.load(in);
      in.close();
      return prop;
    }
    else
    {
      log.info("not found velocity properties in ServletContext.getResourceAsStream(" + propFile + ")");
    }
    return ZVelocityRenderer.getVelocityProperties();
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.