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: * ThemeResourceLoader.java
020: *
021: * Created on June 28, 2005, 12:25 PM
022: */
023:
024: package org.apache.roller.ui.rendering.velocity;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.InputStream;
028: import java.io.UnsupportedEncodingException;
029: import org.apache.commons.collections.ExtendedProperties;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.velocity.exception.ResourceNotFoundException;
033: import org.apache.velocity.runtime.resource.Resource;
034: import org.apache.velocity.runtime.resource.loader.ResourceLoader;
035: import org.apache.roller.RollerException;
036: import org.apache.roller.business.ThemeNotFoundException;
037: import org.apache.roller.business.RollerFactory;
038: import org.apache.roller.business.ThemeManager;
039: import org.apache.roller.pojos.Theme;
040: import org.apache.roller.pojos.ThemeTemplate;
041:
042: /**
043: * The ThemeResourceLoader is a Velocity template loader which loads
044: * templates from shared themes.
045: *
046: * @author Allen Gilliland
047: */
048: public class ThemeResourceLoader extends ResourceLoader {
049:
050: private static Log mLogger = LogFactory.getFactory().getInstance(
051: ThemeResourceLoader.class);
052:
053: public void init(ExtendedProperties configuration) {
054: mLogger.debug(configuration);
055: }
056:
057: public InputStream getResourceStream(String name)
058: throws ResourceNotFoundException {
059:
060: mLogger.debug("Looking up resource named ... " + name);
061:
062: if (name == null || name.length() < 1) {
063: throw new ResourceNotFoundException(
064: "Need to specify a template name!");
065: }
066:
067: try {
068: // parse the name ... theme templates name are <theme>:<template>
069: String[] split = name.split(":", 2);
070: if (split.length < 2)
071: throw new ResourceNotFoundException(
072: "Invalid ThemeRL key " + name);
073:
074: // lookup the template from the proper theme
075: ThemeManager themeMgr = RollerFactory.getRoller()
076: .getThemeManager();
077: Theme theme = themeMgr.getTheme(split[0]);
078: ThemeTemplate template = theme.getTemplate(split[1]);
079:
080: if (template == null)
081: throw new ResourceNotFoundException("Template ["
082: + split[1]
083: + "] doesn't seem to be part of theme ["
084: + split[0] + "]");
085:
086: mLogger.debug("Resource found!");
087:
088: // return the input stream
089: return new ByteArrayInputStream(template.getContents()
090: .getBytes("UTF-8"));
091:
092: } catch (UnsupportedEncodingException uex) {
093: // We expect UTF-8 in all JRE installation.
094: // This rethrows as a Runtime exception after logging.
095: mLogger.error(uex);
096: throw new RuntimeException(uex);
097:
098: } catch (ThemeNotFoundException tnfe) {
099: String msg = "ThemeResourceLoader Error: "
100: + tnfe.getMessage();
101: mLogger.error(msg, tnfe);
102: throw new ResourceNotFoundException(msg);
103:
104: } catch (RollerException re) {
105: String msg = "RollerResourceLoader Error: "
106: + re.getMessage();
107: mLogger.error(msg, re);
108: throw new ResourceNotFoundException(msg);
109: }
110: }
111:
112: public boolean isSourceModified(Resource resource) {
113: return (resource.getLastModified() != this
114: .getLastModified(resource));
115: }
116:
117: public long getLastModified(Resource resource) {
118: long last_mod = 0;
119: String name = resource.getName();
120:
121: mLogger
122: .debug("Checking last modified time for resource named ... "
123: + name);
124:
125: if (name == null || name.length() < 1)
126: return last_mod;
127:
128: try {
129: // parse the name ... theme templates name are <theme>:<template>
130: String[] split = name.split(":", 2);
131: if (split.length < 2)
132: return last_mod;
133:
134: // lookup the template from the proper theme
135: ThemeManager themeMgr = RollerFactory.getRoller()
136: .getThemeManager();
137: Theme theme = themeMgr.getTheme(split[0]);
138: ThemeTemplate template = theme.getTemplate(split[1]);
139:
140: if (template == null)
141: return last_mod;
142:
143: last_mod = template.getLastModified().getTime();
144:
145: } catch (ThemeNotFoundException tnfe) {
146: // ignore
147: } catch (RollerException re) {
148: // we don't like to see this happen, but oh well
149: }
150:
151: return last_mod;
152: }
153:
154: }
|