KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > view > velocity > JPublishResourceLoader


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.view.velocity;
50
51 import java.io.IOException JavaDoc;
52 import java.io.InputStream JavaDoc;
53
54 import com.anthonyeden.lib.util.ClassUtilities;
55 import org.apache.commons.collections.ExtendedProperties;
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58 import org.apache.velocity.exception.ResourceNotFoundException;
59 import org.apache.velocity.runtime.RuntimeServices;
60 import org.apache.velocity.runtime.resource.Resource;
61 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
62 import org.jpublish.SiteContext;
63 import org.jpublish.view.ContentSource;
64
65 /**
66  * Custom resource loader for the Velocity engine.
67  *
68  * @author Anthony Eden
69  * @since 2.0
70  */

71
72 public class JPublishResourceLoader extends ResourceLoader {
73
74     private static final String JavaDoc VM_GLOBAL_LIBRARY = "VM_global_library.vm";
75     private Log log =
76             LogFactory.getLog(JPublishResourceLoader.class);
77     private SiteContext siteContext;
78
79     /**
80      * Set the SiteContext.
81      *
82      * @param siteContext The SiteContext
83      */

84
85     public void setSiteContext(SiteContext siteContext) {
86         this.siteContext = siteContext;
87     }
88
89     /**
90      * Initialize the ResourceLoader.
91      *
92      * @param configuration The ExtendedProperties object
93      */

94
95     public void init(ExtendedProperties configuration) {
96         log.debug("init()");
97     }
98
99     /**
100      * Common init method for ResourceLoaders.
101      *
102      * @param rs The RuntimeServices
103      * @param configuration The configuration
104      */

105
106     public void commonInit(RuntimeServices rs,
107             ExtendedProperties configuration) {
108         log.debug("commonInit()");
109
110         super.commonInit(rs, configuration);
111
112         Object JavaDoc siteContext = rs.getProperty("jpublish.resource.loader.siteContext");
113         if (log.isDebugEnabled()) {
114             log.debug("SiteContext from rs: " + siteContext);
115         }
116         if (siteContext == null) {
117             siteContext = configuration.get("jpublish.resource.loader.siteContext");
118             if (log.isDebugEnabled()) {
119                 log.debug("SiteContext from configuration: " + siteContext);
120             }
121         }
122
123         setSiteContext((SiteContext) siteContext);
124
125     }
126
127     /**
128      * Get the InputStream for the resource.
129      *
130      * @param name The resource name
131      * @return The InputStream
132      * @throws ResourceNotFoundException
133      */

134     public InputStream JavaDoc getResourceStream(String JavaDoc name)
135             throws ResourceNotFoundException {
136         if (VM_GLOBAL_LIBRARY.equals(name)) {
137             log.debug("Loading global library");
138             return ClassUtilities.getResourceAsStream(name);
139         }
140
141         if (log.isDebugEnabled()) {
142             log.debug("getResourceStream(" + name + ")");
143         }
144         try {
145             return getContentSource(name).getInputStream();
146         } catch (IOException JavaDoc e) {
147             log.error("IO error getting resource stream");
148             if (log.isDebugEnabled()) {
149                 e.printStackTrace();
150             }
151             throw new ResourceNotFoundException("An IO error occured:" + e);
152         }
153     }
154
155     /**
156      * Return true if the source is modified.
157      *
158      * @param resource The Resource
159      * @return True if it is modified
160      */

161     public boolean isSourceModified(Resource resource) {
162         boolean isModified =
163                 resource.getLastModified() != getLastModified(resource);
164         if (log.isDebugEnabled()) {
165             log.debug("isSourceModified=" + isModified);
166         }
167         return isModified;
168     }
169
170     /**
171      * Return get the last modified time for the resource.
172      *
173      * @param resource The Resource
174      * @return The last modified time
175      */

176     public long getLastModified(Resource resource) {
177         try {
178             if (VM_GLOBAL_LIBRARY.equals(resource.getName())) {
179                 return -1;
180             }
181
182             if (log.isDebugEnabled()) {
183                 log.debug("getLastModified(" + resource.getName() + ")");
184             }
185             ContentSource contentSource = getContentSource(resource.getName());
186             long lastModified = contentSource.getLastModified();
187             return lastModified;
188         } catch (IOException JavaDoc e) {
189             log.error("An IO exception occured while retrieving the last " +
190                     "modified time");
191             return -1;
192         }
193     }
194
195     /**
196      * Get the named content object.
197      *
198      * @param name The content name
199      * @return The Content object
200      */

201
202     protected ContentSource getContentSource(String JavaDoc name) {
203         if (log.isDebugEnabled()) {
204             log.debug("getContent(" + name + ")");
205         }
206         return siteContext.getContentSource(name);
207     }
208
209 }
210
Popular Tags