KickJava   Java API By Example, From Geeks To Geeks.

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


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.Runtime;
8 import org.apache.velocity.runtime.resource.Resource;
9 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
10 import org.roller.util.LRUCache2;
11
12 import java.io.ByteArrayInputStream JavaDoc;
13 import java.io.InputStream JavaDoc;
14
15 /**
16  * This is a simple template file loader that loads
17  * "preview" templates
18  * from a HashMap instance instead of plain files.
19  *
20  * Ideally this would use some smarter caching
21  * (re-implement with Commons-Cache?) so that the
22  * templates put here could expire in case the user
23  * forgot/neglected to clear their "work area".
24  *
25  * There is no configuration.
26  *
27  * @author <a HREF="mailto:lance@brainopolis.com">Lance Lavandowska</a>
28  * @version $Id: PreviewResourceLoader.java,v 1.19 2005/01/15 03:32:49 snoopdave Exp $
29 */

30 public class PreviewResourceLoader extends ResourceLoader
31 {
32     private static Log mLogger =
33         LogFactory.getFactory().getInstance(PreviewResourceLoader.class);
34
35     private static String JavaDoc cacheName = "PreviewCache";
36     
37     /**
38      * number of objects to store in the cache, older objects
39      * are pushed out (acts like LRU) if maxObjects reached.
40      * @TODO Add configuration for this maxObject in preview cache
41      */

42     private static int maxObjects = 50;
43
44     /**
45      * time objects are in cache before they expire - default one hour.
46      * Configuration parameter is in seconds which we convert
47      * to milliseconds.
48      * @TODO Add configuration for preview cache timeout
49      */

50     private static int time = 60 * 60 * 1000;
51     
52     private static LRUCache2 mCache = new LRUCache2(maxObjects, time);
53     
54     public void init( ExtendedProperties configuration)
55     {
56     }
57
58     public boolean isSourceModified(Resource resource)
59     {
60         return true;
61     }
62
63     public long getLastModified(Resource resource)
64     {
65         return 0;
66     }
67
68     /**
69      * Get an InputStream so that the Runtime can build a
70      * template with it.
71      *
72      * @param name name of template
73      * @return InputStream containing template
74     */

75     public InputStream JavaDoc getResourceStream( String JavaDoc name )
76     throws ResourceNotFoundException
77     {
78         if (name == null || name.length() == 0)
79         {
80             throw new ResourceNotFoundException(
81                 "Need to specify a template name!");
82         }
83
84         if (mLogger.isDebugEnabled())
85         {
86             mLogger.debug("PreviewResourceLoader.getResourceStream(" + name + ")");
87         }
88         
89         try
90         {
91             String JavaDoc template = PreviewResourceLoader.getTemplate(name);
92             if (template != null && mLogger.isDebugEnabled())
93             {
94                 mLogger.debug("PreviewResourceLoader found resource " + name);
95             }
96             return new ByteArrayInputStream JavaDoc( template.getBytes("UTF-8") );
97         }
98         catch (NullPointerException JavaDoc npe)
99         {
100             // to be expected if resource is not in cache
101
throw new ResourceNotFoundException("Resource not found in PreviewResourceLoader");
102         }
103         catch (Exception JavaDoc e)
104         {
105             String JavaDoc msg = "PreviewResourceLoader Error: " +
106                 "problem trying to load resource " + name + ": " + e.getMessage();
107             if (mLogger.isDebugEnabled())
108             {
109                 mLogger.debug( msg);
110             }
111             Runtime.error(msg );
112             throw new ResourceNotFoundException (msg);
113         }
114     }
115         
116     /**
117      * Set the temporary Template into memory.
118      */

119     public static void setTemplate(String JavaDoc id, String JavaDoc template, String JavaDoc username)
120     {
121         if (mLogger.isDebugEnabled())
122         {
123             mLogger.debug("PreviewResourceLoader.setTemplate("
124                 + id + ", template)");
125         
126         }
127         mCache.put(id, template);
128     }
129     
130     /**
131      * Get the temporary Template from the Map.
132      */

133     public static String JavaDoc getTemplate(String JavaDoc id)
134     {
135         if (mLogger.isDebugEnabled())
136         {
137             mLogger.debug("PreviewResourceLoader.getTemplate("
138                 + id + ")");
139         }
140             return (String JavaDoc) mCache.get( id );
141     }
142     
143     /**
144      * Remove the temporary Template from the Map.
145      */

146     public static void clearTemplate(String JavaDoc id)
147     {
148         if (mLogger.isDebugEnabled())
149         {
150             mLogger.debug("PreviewResourceLoader.clearTemplate(" + id + ")");
151         }
152         mCache.purge(new String JavaDoc[] {id});
153     }
154     
155     /**
156      * Clear all templates for this user.
157      * @param username
158      */

159     public static void clearAllTemplates(String JavaDoc username)
160     {
161         // TODO: add support for 'groups' to LRUCache2
162
mCache.purge();
163     }
164 }
Popular Tags