001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/utils/mvc/impl/LocalVelocityConfigurer.java $
003: * $Id: LocalVelocityConfigurer.java 14230 2006-09-05 18:02:51Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.utils.mvc.impl;
021:
022: import java.io.IOException;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Properties;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.velocity.app.VelocityEngine;
030: import org.apache.velocity.exception.VelocityException;
031: import org.apache.velocity.runtime.RuntimeConstants;
032: import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
033: import org.sakaiproject.metaobj.utils.mvc.intf.VelocityEngineFactory;
034: import org.springframework.beans.BeansException;
035: import org.springframework.beans.factory.InitializingBean;
036: import org.springframework.context.ApplicationContext;
037: import org.springframework.context.ApplicationContextAware;
038: import org.springframework.web.context.WebApplicationContext;
039:
040: /**
041: * Created by IntelliJ IDEA.
042: * User: John Ellis
043: * Date: Jul 5, 2005
044: * Time: 4:21:23 PM
045: * To change this template use File | Settings | File Templates.
046: */
047: public class LocalVelocityConfigurer implements InitializingBean,
048: ApplicationContextAware, VelocityEngineFactory {
049:
050: protected final transient Log logger = LogFactory
051: .getLog(getClass());
052:
053: private String resourceLoaderPath;
054:
055: private boolean preferFileSystemAccess = true;
056:
057: private Map velocityProperties;
058:
059: private VelocityEngine velocityEngine;
060:
061: private WebApplicationContext webApplicationContext;
062:
063: /**
064: * Prepare the VelocityEngine instance and return it.
065: *
066: * @return the VelocityEngine instance
067: * @throws java.io.IOException if the config file wasn't found
068: * @throws org.apache.velocity.exception.VelocityException
069: * on Velocity initialization failure
070: */
071: public VelocityEngine createVelocityEngine() throws IOException,
072: VelocityException {
073: VelocityEngine velocityEngine = new VelocityEngine();
074: Properties props = new Properties();
075:
076: // Merge local properties if set.
077: if (!this .velocityProperties.isEmpty()) {
078: props.putAll(this .velocityProperties);
079: }
080:
081: // Apply properties to VelocityEngine.
082: for (Iterator it = props.entrySet().iterator(); it.hasNext();) {
083: Map.Entry entry = (Map.Entry) it.next();
084: if (!(entry.getKey() instanceof String)) {
085: throw new IllegalArgumentException(
086: "Illegal property key [" + entry.getKey()
087: + "]: only Strings allowed");
088: }
089: velocityEngine.setProperty((String) entry.getKey(), entry
090: .getValue());
091: }
092:
093: initResourceLoader(velocityEngine);
094:
095: try {
096: // Perform actual initialization.
097: velocityEngine.init();
098: } catch (IOException ex) {
099: throw ex;
100: } catch (VelocityException ex) {
101: throw ex;
102: } catch (RuntimeException ex) {
103: throw ex;
104: } catch (Exception ex) {
105: logger
106: .error(
107: "Why does VelocityEngine throw a generic checked exception, after all?",
108: ex);
109: throw new VelocityException(ex.getMessage());
110: }
111:
112: return velocityEngine;
113: }
114:
115: public String getRealPath(String path) {
116: return webApplicationContext.getServletContext().getRealPath(
117: path);
118: }
119:
120: /**
121: * Initialize a SpringResourceLoader for the given VelocityEngine.
122: *
123: * @param velocityEngine the VelocityEngine to configure
124: * @see org.springframework.ui.velocity.SpringResourceLoader
125: */
126: protected void initResourceLoader(VelocityEngine velocityEngine) {
127: velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER,
128: "file");
129: velocityEngine.setProperty("file.resource.loader.description",
130: "Velocity File Resource Loader");
131: velocityEngine.setProperty("file.resource.loader.class",
132: FileResourceLoader.class.getName());
133: velocityEngine.setProperty("file.resource.loader.path",
134: getRealPath(getResourceLoaderPath()));
135: velocityEngine
136: .setProperty("file.resource.loader.cache", "true");
137: velocityEngine.setProperty(
138: "file.resource.loader.modificationCheckInterval", "0");
139: }
140:
141: /**
142: * Initialize VelocityEngineFactory's VelocityEngine
143: * if not overridden by a preconfigured VelocityEngine.
144: *
145: * @see #createVelocityEngine
146: */
147: public void afterPropertiesSet() throws IOException,
148: VelocityException {
149: if (this .velocityEngine == null) {
150: this .velocityEngine = createVelocityEngine();
151: }
152: }
153:
154: public VelocityEngine getVelocityEngine() {
155: return this .velocityEngine;
156: }
157:
158: public String getResourceLoaderPath() {
159: return resourceLoaderPath;
160: }
161:
162: public void setResourceLoaderPath(String resourceLoaderPath) {
163: this .resourceLoaderPath = resourceLoaderPath;
164: }
165:
166: public boolean isPreferFileSystemAccess() {
167: return preferFileSystemAccess;
168: }
169:
170: public void setPreferFileSystemAccess(boolean preferFileSystemAccess) {
171: this .preferFileSystemAccess = preferFileSystemAccess;
172: }
173:
174: public Map getVelocityProperties() {
175: return velocityProperties;
176: }
177:
178: public void setVelocityProperties(Map velocityProperties) {
179: this .velocityProperties = velocityProperties;
180: }
181:
182: public void setApplicationContext(
183: ApplicationContext applicationContext)
184: throws BeansException {
185: this .webApplicationContext = (WebApplicationContext) applicationContext;
186: }
187: }
|