KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > velocity > RollerResourceLoader


1 package org.roller.presentation.velocity;
2
3 import org.apache.commons.collections.ExtendedProperties;
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.apache.velocity.exception.ResourceNotFoundException;
7 import org.apache.velocity.runtime.resource.Resource;
8 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
9 import org.roller.RollerException;
10 import org.roller.model.Roller;
11 import org.roller.pojos.PageData;
12 import org.roller.presentation.RollerContext;
13
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
17
18 /**
19  * This is a simple template file loader that loads templates
20  * from the Roller instance instead of plain files.
21  *
22  * RollerResourceLoader makes use of RollerFactory.
23  *
24  * @author <a HREF="mailto:lance@brainopolis.com">Lance Lavandowska</a>
25  * @version $Id: RollerResourceLoader.java,v 1.9 2005/01/15 03:32:49 snoopdave Exp $
26 */

27 public class RollerResourceLoader extends ResourceLoader
28 {
29     private static Log mLogger =
30         LogFactory.getFactory().getInstance(RollerResourceLoader.class);
31         
32     public void init( ExtendedProperties configuration)
33     {
34         if (mLogger.isDebugEnabled())
35         {
36             mLogger.debug(configuration);
37         }
38     }
39     
40     /**
41      * The web-app startup timing may be tricky. In the case that roller is
42      * null (which means that RollerContext may not have had a chance to call
43      * RollerFactory yet) ask RollerContext for Roller because it has the
44      * information necessary for RollerFactory to do its job.
45      * @return Roller
46      */

47     private Roller getRoller()
48     {
49         return RollerContext.getRoller( null );
50     }
51
52     public boolean isSourceModified(Resource resource)
53     {
54         return (resource.getLastModified() !=
55             readLastModified(resource, "checking timestamp"));
56     }
57
58     public long getLastModified(Resource resource)
59     {
60         return readLastModified(resource, "getting timestamp");
61     }
62
63     /**
64      * Get an InputStream so that the Runtime can build a
65      * template with it.
66      *
67      * @param name name of template
68      * @return InputStream containing template
69     */

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

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

118         String JavaDoc name = resource.getName();
119         try
120         {
121             PageData page = getPage( name );
122             
123             if (mLogger.isDebugEnabled())
124             {
125                 mLogger.debug(name + ": resource=" + resource.getLastModified() +
126                                 " vs. page=" + page.getUpdateTime().getTime());
127             }
128             return page.getUpdateTime().getTime();
129         }
130         catch (RollerException re)
131         {
132             mLogger.error( "Error " + i_operation, re );
133         }
134         return 0;
135     }
136
137     public PageData getPage(String JavaDoc id) throws RollerException
138     {
139         if (getRoller() == null) throw new RollerException(
140             "RollerResourceLoader.getRoller() returned NULL");
141         return getRoller().getUserManager().retrievePage(id); //retrievePageReadOnly( id );
142
}
143
144 }
145
Popular Tags