KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jmxdebug > web > velocity > WebappLoader


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

17
18 package org.apache.geronimo.jmxdebug.web.velocity;
19
20 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
21 import org.apache.velocity.runtime.resource.Resource;
22 import org.apache.velocity.exception.ResourceNotFoundException;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 import org.apache.commons.collections.ExtendedProperties;
28
29 /**
30  * Simple webapp loader. This code has been recycled from contributions to Velocity
31  * by me.
32  *
33  * @version $Rev: 45929 $ $Date: 2004-09-11 17:10:38 -0700 (Sat, 11 Sep 2004) $
34  */

35 public class WebappLoader extends ResourceLoader {
36
37     public ServletContext JavaDoc servletContext = null;
38
39     public static String JavaDoc KEY = "org.apache.geronimo.console.web.velocity.WebappLoader";
40
41     /**
42      * This is abstract in the base class, so we need it
43      */

44     public void init(ExtendedProperties configuration) {
45
46         rsvc.info("WebappLoader : initialization starting.");
47
48         Object JavaDoc o = rsvc.getApplicationAttribute(KEY);
49
50         if (o instanceof WebappLoaderAppContext) {
51             servletContext = ((WebappLoaderAppContext) o).getServletContext();
52         }
53         else
54             rsvc.error("WebappLoader : unable to retrieve ServletContext");
55
56         rsvc.info("WebappLoader : initialization complete.");
57     }
58
59     /**
60      * Get an InputStream so that the Runtime can build a
61      * template with it.
62      *
63      * @param name name of template to get
64      * @return InputStream containing the template
65      * @throws org.apache.velocity.exception.ResourceNotFoundException
66      * if template not found
67      * in classpath.
68      */

69     public synchronized InputStream JavaDoc getResourceStream(String JavaDoc name)
70             throws ResourceNotFoundException {
71
72         if (name == null || name.length() == 0) {
73             throw new ResourceNotFoundException("No template name provided");
74         }
75
76         try {
77             if (!name.startsWith("/"))
78                 name = "/" + name;
79
80             return servletContext.getResourceAsStream(name);
81         }
82         catch (Exception JavaDoc fnfe) {
83             /*
84              * log and convert to a general Velocity ResourceNotFoundException
85              */

86
87             throw new ResourceNotFoundException(fnfe.getMessage());
88         }
89     }
90
91     /**
92      * Defaults to return false.
93      */

94     public boolean isSourceModified(Resource resource) {
95         return false;
96     }
97
98     /**
99      * Defaults to return 0
100      */

101     public long getLastModified(Resource resource) {
102         return 0;
103     }
104
105     public interface WebappLoaderAppContext {
106
107         public ServletContext JavaDoc getServletContext();
108
109     }
110 }
111
112
Popular Tags