KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > velocity > RollerResourceLoader


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.ui.rendering.velocity;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import org.apache.commons.collections.ExtendedProperties;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.velocity.exception.ResourceNotFoundException;
27 import org.apache.velocity.runtime.resource.Resource;
28 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
29 import org.apache.roller.RollerException;
30 import org.apache.roller.model.RollerFactory;
31 import org.apache.roller.pojos.WeblogTemplate;
32
33
34 /**
35  * This is a simple template file loader that loads templates
36  * from the Roller instance instead of plain files.
37  *
38  * RollerResourceLoader makes use of RollerFactory.
39  *
40  * @author <a HREF="mailto:lance@brainopolis.com">Lance Lavandowska</a>
41  * @version $Id: RollerResourceLoader.java,v 1.9 2005/01/15 03:32:49 snoopdave Exp $
42  */

43 public class RollerResourceLoader extends ResourceLoader {
44     
45     private static Log mLogger = LogFactory.getLog(RollerResourceLoader.class);
46     
47     
48     public void init(ExtendedProperties configuration) {
49         if (mLogger.isDebugEnabled()) {
50             mLogger.debug(configuration);
51         }
52     }
53     
54     
55     public boolean isSourceModified(Resource resource) {
56         return (resource.getLastModified() !=
57                 readLastModified(resource, "checking timestamp"));
58     }
59     
60     
61     public long getLastModified(Resource resource) {
62         return readLastModified(resource, "getting timestamp");
63     }
64     
65     /**
66      * Get an InputStream so that the Runtime can build a
67      * template with it.
68      *
69      * @param name name of template
70      * @return InputStream containing template
71      */

72     public InputStream JavaDoc getResourceStream(String JavaDoc name)
73             throws ResourceNotFoundException {
74         
75         if (name == null || name.length() == 0) {
76             throw new ResourceNotFoundException("Need to specify a template name!");
77         }
78         
79         try {
80             WeblogTemplate page =
81                     RollerFactory.getRoller().getUserManager().getPage(name);
82             
83             if (page == null) {
84                 throw new ResourceNotFoundException(
85                         "RollerResourceLoader: page \"" +
86                         name + "\" not found");
87             }
88             return new ByteArrayInputStream JavaDoc( page.getContents().getBytes("UTF-8") );
89         } catch (UnsupportedEncodingException JavaDoc uex) {
90             // This should never actually happen. We expect UTF-8 in all JRE installation.
91
// This rethrows as a Runtime exception after logging.
92
mLogger.error(uex);
93             throw new RuntimeException JavaDoc(uex);
94         } catch (RollerException re) {
95             String JavaDoc msg = "RollerResourceLoader Error: " +
96                     "database problem trying to load resource " + name;
97             mLogger.error( msg, re );
98             throw new ResourceNotFoundException(msg);
99         }
100     }
101     
102     
103     /**
104      * Fetches the last modification time of the resource
105      *
106      * @param resource Resource object we are finding timestamp of
107      * @param i_operation string for logging, indicating caller's intention
108      *
109      * @return timestamp as long
110      */

111     private long readLastModified(Resource resource, String JavaDoc i_operation) {
112         
113         /*
114          * get the template name from the resource
115          */

116         String JavaDoc name = resource.getName();
117         try {
118             WeblogTemplate page =
119                     RollerFactory.getRoller().getUserManager().getPage(name);
120             
121             if (mLogger.isDebugEnabled()) {
122                 mLogger.debug(name + ": resource=" + resource.getLastModified() +
123                         " vs. page=" + 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 }
133
Popular Tags