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: */
017: package org.apache.wicket.velocity;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.Properties;
022:
023: import javax.servlet.ServletContext;
024:
025: import org.apache.velocity.app.Velocity;
026: import org.apache.wicket.Application;
027: import org.apache.wicket.IInitializer;
028: import org.apache.wicket.WicketRuntimeException;
029: import org.apache.wicket.protocol.http.WebApplication;
030: import org.apache.wicket.util.file.WebApplicationPath;
031: import org.apache.wicket.util.resource.IResourceStream;
032: import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
033: import org.slf4j.Logger;
034: import org.slf4j.LoggerFactory;
035:
036: /**
037: * An implementation of {@link wicket.IInitializer} for the Velocity Runtime
038: * Singleton. If Application is an instance of WebApplication, Initializer will
039: * retrieve "velocityPropertiesFolder" as an initparam to point to the directory
040: * the properties file lives in, and "velocity.properties" for the name of the
041: * properties file. If the params don't exist, then velocity.properties next to
042: * this class will be loaded.
043: *
044: */
045: public class Initializer implements IInitializer {
046: private static final Logger log = LoggerFactory
047: .getLogger(Initializer.class);
048:
049: private String velocityPropertiesFile = "velocity.properties";
050:
051: private String velocityPropertiesFolder;
052:
053: /**
054: * @see org.apache.wicket.IInitializer#init(org.apache.wicket.Application)
055: */
056: public void init(Application application) {
057: Properties props = getVelocityProperties(application);
058:
059: try {
060: if (null != props) {
061: Velocity.init(props);
062: } else {
063: Velocity.init();
064: }
065: log.info("Initialized Velocity successfully");
066: } catch (Exception e) {
067: throw new WicketRuntimeException(e);
068: }
069: }
070:
071: private Properties getVelocityProperties(Application application) {
072: if (application instanceof WebApplication) {
073: WebApplication webapp = (WebApplication) application;
074: ServletContext servletContext = webapp.getServletContext();
075: ServletContext sc = servletContext;
076: velocityPropertiesFolder = sc
077: .getInitParameter("velocityPropertiesFolder");
078: String propsFile = servletContext
079: .getInitParameter("velocity.properties");
080:
081: if (null != propsFile) {
082: velocityPropertiesFile = propsFile;
083: }
084:
085: if (null != velocityPropertiesFolder) {
086: WebApplicationPath webPath = new WebApplicationPath(sc);
087: webPath.add(velocityPropertiesFolder);
088: IResourceStream stream = webPath.find(
089: Initializer.class, velocityPropertiesFile);
090: InputStream is = null;
091: try {
092: is = stream.getInputStream();
093: Properties props = new Properties();
094: props.load(is);
095: return props;
096: } catch (IOException e) {
097: throw new WicketRuntimeException(e);
098: } catch (ResourceStreamNotFoundException e) {
099: throw new WicketRuntimeException(e);
100: } finally {
101: if (is != null) {
102: try {
103: is.close();
104: } catch (IOException e) {
105: log.error(e.getMessage(), e);
106: }
107: }
108: }
109: }
110: }
111:
112: // if it's not a web app, load from the package
113: InputStream is = Initializer.class
114: .getResourceAsStream("velocity.properties");
115: try {
116: Properties props = new Properties();
117: props.load(is);
118: return props;
119: } catch (Exception e) {
120: throw new WicketRuntimeException(e);
121: } finally {
122: if (is != null) {
123: try {
124: is.close();
125: } catch (IOException e) {
126: log.error(e.getMessage(), e);
127: }
128: }
129: }
130: }
131:
132: }
|