001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.velocity; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.util.Properties; 022 023import javax.servlet.ServletContext; 024 025import org.apache.velocity.app.Velocity; 026import org.apache.wicket.Application; 027import org.apache.wicket.IInitializer; 028import org.apache.wicket.WicketRuntimeException; 029import org.apache.wicket.core.util.file.WebApplicationPath; 030import org.apache.wicket.protocol.http.WebApplication; 031import org.apache.wicket.util.io.IOUtils; 032import org.apache.wicket.util.resource.IResourceStream; 033import org.apache.wicket.util.resource.ResourceStreamNotFoundException; 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036 037/** 038 * An implementation of {@link org.apache.wicket.IInitializer} for the Velocity Runtime Singleton. 039 * If Application is an instance of WebApplication, Initializer will retrieve 040 * "velocityPropertiesFolder" as an initparam to point to the directory the properties file lives 041 * in, and "velocity.properties" for the name of the properties file. If the params don't exist, 042 * then velocity.properties next to this class will be loaded. 043 * 044 */ 045public class Initializer implements IInitializer 046{ 047 private static final Logger log = LoggerFactory.getLogger(Initializer.class); 048 049 /** 050 * {@inheritDoc} 051 */ 052 @Override 053 public void init(final Application application) 054 { 055 Properties props = getVelocityProperties(application); 056 057 try 058 { 059 Velocity.init(props); 060 log.info("Initialized Velocity successfully"); 061 } 062 catch (Exception e) 063 { 064 throw new WicketRuntimeException(e); 065 } 066 } 067 068 private Properties getVelocityProperties(final Application application) 069 { 070 String velocityPropertiesFile = "velocity.properties"; 071 072 if (application instanceof WebApplication) 073 { 074 WebApplication webapp = (WebApplication)application; 075 ServletContext servletContext = webapp.getServletContext(); 076 String propertiesFolder = servletContext.getInitParameter("velocityPropertiesFolder"); 077 String propsFile = servletContext.getInitParameter("velocity.properties"); 078 079 if (null != propsFile) 080 { 081 velocityPropertiesFile = propsFile; 082 } 083 084 if (null != propertiesFolder) 085 { 086 WebApplicationPath webPath = new WebApplicationPath(servletContext, 087 propertiesFolder); 088 IResourceStream stream = webPath.find(Initializer.class, velocityPropertiesFile); 089 InputStream is; 090 try 091 { 092 is = stream.getInputStream(); 093 Properties props = new Properties(); 094 props.load(is); 095 return props; 096 } 097 catch (IOException | ResourceStreamNotFoundException e) 098 { 099 throw new WicketRuntimeException(e); 100 } finally 101 { 102 try 103 { 104 IOUtils.close(stream); 105 } 106 catch (IOException e) 107 { 108 log.error(e.getMessage(), e); 109 } 110 } 111 } 112 } 113 114 // if it's not a web app, load from the package 115 InputStream is = Initializer.class.getResourceAsStream("velocity.properties"); 116 try 117 { 118 Properties props = new Properties(); 119 props.load(is); 120 return props; 121 } 122 catch (Exception e) 123 { 124 throw new WicketRuntimeException(e); 125 } 126 finally 127 { 128 try 129 { 130 IOUtils.close(is); 131 } 132 catch (IOException e) 133 { 134 log.error(e.getMessage(), e); 135 } 136 } 137 } 138 139 /** 140 * {@inheritDoc} 141 */ 142 @Override 143 public void destroy(final Application application) 144 { 145 } 146}