001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.velocity;
020:
021: import java.io.InputStream;
022: import java.util.Properties;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.roller.config.RollerConfig;
026: import org.apache.roller.ui.core.RollerContext;
027: import org.apache.velocity.Template;
028: import org.apache.velocity.app.VelocityEngine;
029: import org.apache.velocity.exception.ParseErrorException;
030: import org.apache.velocity.exception.ResourceNotFoundException;
031:
032: /**
033: * Represents the VelocityEngine used by Roller.
034: *
035: * We construct our own instance of VelocityEngine, initialize it, and provide
036: * access to the instance via the Singleton getInstance() method.
037: */
038: public class RollerVelocity {
039:
040: public static final String VELOCITY_CONFIG = "/WEB-INF/velocity.properties";
041:
042: private static Log log = LogFactory.getLog(RollerVelocity.class);
043:
044: private static VelocityEngine velocityEngine = null;
045:
046: static {
047: log.info("Initializing Velocity Rendering Engine");
048:
049: // initialize the Velocity engine
050: Properties velocityProps = new Properties();
051:
052: try {
053: InputStream instream = RollerContext.getServletContext()
054: .getResourceAsStream(VELOCITY_CONFIG);
055:
056: velocityProps.load(instream);
057:
058: // need to dynamically add old macro libraries if they are enabled
059: if (RollerConfig
060: .getBooleanProperty("rendering.legacyModels.enabled")) {
061: String macroLibraries = (String) velocityProps
062: .get("velocimacro.library");
063: String oldLibraries = RollerConfig
064: .getProperty("velocity.oldMacroLibraries");
065:
066: // set the new value
067: velocityProps.setProperty("velocimacro.library",
068: oldLibraries + "," + macroLibraries);
069: }
070:
071: log.debug("Velocity engine props = " + velocityProps);
072:
073: // construct the VelocityEngine
074: velocityEngine = new VelocityEngine();
075:
076: // init velocity with our properties
077: velocityEngine.init(velocityProps);
078:
079: } catch (Exception e) {
080: throw new RuntimeException(e);
081: }
082: }
083:
084: /**
085: * Access to the VelocityEngine.
086: *
087: * This shouldn't ever be needed, but it's here just in case someone
088: * really needs to do something special.
089: */
090: public static VelocityEngine getEngine() {
091: return velocityEngine;
092: }
093:
094: /**
095: * Convenience static method for looking up a template.
096: */
097: public static Template getTemplate(String name)
098: throws ResourceNotFoundException, ParseErrorException,
099: Exception {
100: return velocityEngine.getTemplate(name);
101: }
102:
103: /**
104: * Convenience static method for looking up a template.
105: */
106: public static Template getTemplate(String name, String encoding)
107: throws ResourceNotFoundException, ParseErrorException,
108: Exception {
109: return velocityEngine.getTemplate(name, encoding);
110: }
111:
112: }
|