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: package org.apache.roller.ui.rendering.velocity;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.InputStream;
022: import java.io.UnsupportedEncodingException;
023: import org.apache.commons.collections.ExtendedProperties;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.velocity.exception.ResourceNotFoundException;
027: import org.apache.velocity.runtime.resource.Resource;
028: import org.apache.velocity.runtime.resource.loader.ResourceLoader;
029: import org.apache.roller.RollerException;
030: import org.apache.roller.business.RollerFactory;
031: import org.apache.roller.pojos.WeblogTemplate;
032:
033: /**
034: * This is a simple template file loader that loads templates
035: * from the Roller instance instead of plain files.
036: *
037: * RollerResourceLoader makes use of RollerFactory.
038: *
039: * @author <a href="mailto:lance@brainopolis.com">Lance Lavandowska</a>
040: * @version $Id: RollerResourceLoader.java,v 1.9 2005/01/15 03:32:49 snoopdave Exp $
041: */
042: public class RollerResourceLoader extends ResourceLoader {
043:
044: private static Log mLogger = LogFactory
045: .getLog(RollerResourceLoader.class);
046:
047: public void init(ExtendedProperties configuration) {
048: if (mLogger.isDebugEnabled()) {
049: mLogger.debug(configuration);
050: }
051: }
052:
053: public boolean isSourceModified(Resource resource) {
054: return (resource.getLastModified() != readLastModified(
055: resource, "checking timestamp"));
056: }
057:
058: public long getLastModified(Resource resource) {
059: return readLastModified(resource, "getting timestamp");
060: }
061:
062: /**
063: * Get an InputStream so that the Runtime can build a
064: * template with it.
065: *
066: * @param name name of template
067: * @return InputStream containing template
068: */
069: public InputStream getResourceStream(String name)
070: throws ResourceNotFoundException {
071:
072: if (name == null || name.length() == 0) {
073: throw new ResourceNotFoundException(
074: "Need to specify a template name!");
075: }
076:
077: try {
078: WeblogTemplate page = RollerFactory.getRoller()
079: .getUserManager().getPage(name);
080:
081: if (page == null) {
082: throw new ResourceNotFoundException(
083: "RollerResourceLoader: page \"" + name
084: + "\" not found");
085: }
086: return new ByteArrayInputStream(page.getContents()
087: .getBytes("UTF-8"));
088: } catch (UnsupportedEncodingException uex) {
089: // This should never actually happen. We expect UTF-8 in all JRE installation.
090: // This rethrows as a Runtime exception after logging.
091: mLogger.error(uex);
092: throw new RuntimeException(uex);
093: } catch (RollerException re) {
094: String msg = "RollerResourceLoader Error: "
095: + "database problem trying to load resource "
096: + name;
097: mLogger.error(msg, re);
098: throw new ResourceNotFoundException(msg);
099: }
100: }
101:
102: /**
103: * Fetches the last modification time of the resource
104: *
105: * @param resource Resource object we are finding timestamp of
106: * @param i_operation string for logging, indicating caller's intention
107: *
108: * @return timestamp as long
109: */
110: private long readLastModified(Resource resource, String i_operation) {
111:
112: /*
113: * get the template name from the resource
114: */
115: String name = resource.getName();
116: try {
117: WeblogTemplate page = RollerFactory.getRoller()
118: .getUserManager().getPage(name);
119:
120: if (mLogger.isDebugEnabled()) {
121: mLogger.debug(name + ": resource="
122: + resource.getLastModified() + " vs. page="
123: + page.getLastModified().getTime());
124: }
125: return page.getLastModified().getTime();
126: } catch (RollerException re) {
127: mLogger.error("Error " + i_operation, re);
128: }
129: return 0;
130: }
131:
132: }
|