01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.
15: *
16: */
17:
18: package org.jpublish.util;
19:
20: import javax.servlet.ServletContext;
21:
22: import org.apache.commons.logging.Log;
23: import org.apache.commons.logging.LogFactory;
24:
25: import com.anthonyeden.lib.resource.ResourceRecipient;
26: import com.anthonyeden.lib.resource.ResourceException;
27: import com.anthonyeden.lib.resource.AbstractResourceLoader;
28:
29: /** Implementation of the ResourceLoader interface which loads data from
30: the servlet context. This ResourceLoader does not monitor the resource.
31:
32: @author Anthony Eden
33: */
34:
35: public class ServletContextResourceLoader extends
36: AbstractResourceLoader {
37:
38: private static final Log log = LogFactory
39: .getLog(ServletContextResourceLoader.class);
40:
41: private ServletContext servletContext;
42:
43: /** Get the ServletContext.
44:
45: @return The ServletContext
46: */
47:
48: public ServletContext getServletContext() {
49: return servletContext;
50: }
51:
52: /** Set the ServletContext.
53:
54: @param servletContext The ServletContext
55: */
56:
57: public void setServletContext(ServletContext servletContext) {
58: this .servletContext = servletContext;
59: }
60:
61: /** Load the resource specified by the given path. This ResourceLoader
62: does not monitor the resource even if the value of monitor is true.
63:
64: @param path The path
65: @param handler The ResourceRecipient callback
66: @param monitor Ignored by this resource loader
67: @throws ResourceException
68: */
69:
70: public void loadResource(String path, ResourceRecipient handler,
71: boolean monitor) throws ResourceException {
72: try {
73: if (log.isDebugEnabled())
74: log.debug("Resource path: " + path);
75: handler.load(servletContext.getResourceAsStream(path));
76: } catch (Exception e) {
77: e.printStackTrace();
78: throw new ResourceException(e);
79: }
80: }
81:
82: }
|