Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Servlet20Broker |
|
| 3.0;3 |
1 | /* | |
2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted under the terms of either of the following | |
6 | * Open Source licenses: | |
7 | * | |
8 | * The GNU General Public License, version 2, or any later version, as | |
9 | * published by the Free Software Foundation | |
10 | * (http://www.fsf.org/copyleft/gpl.html); | |
11 | * | |
12 | * or | |
13 | * | |
14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
15 | * | |
16 | * This software is provided "as is", with NO WARRANTY, not even the | |
17 | * implied warranties of fitness to purpose, or merchantability. You | |
18 | * assume all risks and liabilities associated with its use. | |
19 | * | |
20 | * See www.webmacro.org for more information on the WebMacro project. | |
21 | */ | |
22 | ||
23 | package org.webmacro.servlet; | |
24 | ||
25 | import org.slf4j.Logger; | |
26 | import org.slf4j.LoggerFactory; | |
27 | ||
28 | import org.webmacro.Broker; | |
29 | import org.webmacro.InitException; | |
30 | ||
31 | import javax.servlet.Servlet; | |
32 | import javax.servlet.ServletContext; | |
33 | import java.io.InputStream; | |
34 | import java.net.URL; | |
35 | import java.util.Properties; | |
36 | ||
37 | /** | |
38 | * An implementation of Broker tailored for Servlet 2.0/2.1 | |
39 | * environments. | |
40 | * <p> | |
41 | * Loads templates and other resources from: | |
42 | * <ol> | |
43 | * <li> the class path, | |
44 | * </p> | |
45 | * writes log messages to the servlet log. | |
46 | * | |
47 | * @author Brian Goetz | |
48 | * @author Marc Palmer (wj5@wangjammers.org) | |
49 | * @since 0.96 | |
50 | */ | |
51 | ||
52 | public class Servlet20Broker extends ServletBroker | |
53 | { | |
54 | ||
55 | 2 | static Logger _log = LoggerFactory.getLogger(Servlet20Broker.class); |
56 | ||
57 | protected ClassLoader _servletClassLoader; | |
58 | ||
59 | protected Servlet20Broker (ServletContext sc, | |
60 | ClassLoader cl, | |
61 | Properties additionalProperties) throws InitException | |
62 | { | |
63 | 2 | super(sc); |
64 | 2 | _servletClassLoader = cl; |
65 | ||
66 | 2 | String propertySource = WEBMACRO_DEFAULTS + ", " + WEBMACRO_PROPERTIES; |
67 | 2 | loadDefaultSettings(); |
68 | 2 | loadSettings(WEBMACRO_PROPERTIES, true); |
69 | 2 | if (additionalProperties != null && additionalProperties.keySet().size() > 0) |
70 | { | |
71 | 0 | propertySource += ", (additional Properties)"; |
72 | 0 | loadSettings(additionalProperties); |
73 | } | |
74 | 2 | propertySource += ", (System Properties)"; |
75 | 2 | loadSystemSettings(); |
76 | ||
77 | 2 | _log.info("Loaded settings from " + propertySource); |
78 | 2 | init(); |
79 | 2 | } |
80 | ||
81 | /** | |
82 | * Get a Servlet API 2.0 compatible broker for the Servlet specified. | |
83 | * @param s The servlet | |
84 | * @param additionalProperties | |
85 | * @return The broker for the servlet context. | |
86 | * @throws InitException | |
87 | */ | |
88 | public static Broker getBroker (Servlet s, Properties additionalProperties) throws InitException | |
89 | { | |
90 | 10 | ServletContext sc = s.getServletConfig().getServletContext(); |
91 | 10 | ClassLoader cl = s.getClass().getClassLoader(); |
92 | 10 | return _getBroker(sc, cl, additionalProperties, true, |
93 | s.getClass().getName()); | |
94 | } | |
95 | ||
96 | /** | |
97 | * Get a Servlet API 2.0 compatible broker for the ServletContext specified. | |
98 | * @param sc The Servlet context | |
99 | * @param cl A ClassLoader to use, presumably the webapp classloader | |
100 | * @param additionalProperties | |
101 | * @return The broker for the servlet context | |
102 | * @throws InitException | |
103 | * @since 2.1 JSDK | |
104 | */ | |
105 | public static Broker getBroker (ServletContext sc, ClassLoader cl, | |
106 | Properties additionalProperties) throws InitException | |
107 | { | |
108 | 0 | return _getBroker(sc, cl, additionalProperties, false, sc.toString()); |
109 | } | |
110 | ||
111 | /** | |
112 | * Get an existing instance of the Servlet 2.0/2.1 broker or create a new one. | |
113 | * Templates will be retrieved relative to the ServletContext root | |
114 | * and classes loaded from the ClassLoader passed in. NOTE: Templates | |
115 | * will <b>not</b> be loaded from the classpath. | |
116 | * @param sc The ServletContext to template access | |
117 | * @param cl The ClassLoader for class loading, typically servlet or | |
118 | * JSP page's class loader | |
119 | * @param additionalProperties | |
120 | * @param fromServlet true if it is actually an initialization derived from | |
121 | * a Servlet instance passed in - just for nicer logging output | |
122 | * @param servletOrContextName Name of the servlet or context originating this broker, | |
123 | * for nicer logging | |
124 | * @return The broker for the servlet context | |
125 | * @throws org.webmacro.InitException | |
126 | * @since 2.1 JSDK | |
127 | */ | |
128 | protected static Broker _getBroker (ServletContext sc, | |
129 | ClassLoader cl, Properties additionalProperties, boolean fromServlet, | |
130 | String servletOrContextName) throws InitException | |
131 | { | |
132 | try | |
133 | { | |
134 | 10 | Object key = cl; |
135 | 10 | if (additionalProperties != null && additionalProperties.keySet().size() > 0) |
136 | 0 | key = new PropertiesPair(cl, additionalProperties); |
137 | ||
138 | 10 | Broker b = findBroker(key); |
139 | 10 | if (b == null) |
140 | { | |
141 | 2 | b = new Servlet20Broker(sc, cl, additionalProperties); |
142 | 2 | register(key, b); |
143 | } | |
144 | else | |
145 | 8 | _log.info( |
146 | (fromServlet ? "Servlet " : "ServletContext ") | |
147 | + servletOrContextName | |
148 | + " joining Broker" + " " + b.getName()); | |
149 | 10 | return b; |
150 | } | |
151 | 0 | catch (InitException e) |
152 | { | |
153 | 0 | _log.error("Failed to initialized WebMacro from "+ |
154 | (fromServlet ? "Servlet " : "ServletContext ") | |
155 | + servletOrContextName); | |
156 | 0 | throw e; |
157 | } | |
158 | } | |
159 | ||
160 | /** Get a resource (file) from the the Broker's class loader. */ | |
161 | public URL getResource (String name) | |
162 | { | |
163 | 10 | URL u = _servletClassLoader.getResource(name); |
164 | 10 | if (u == null) |
165 | 4 | u = super.getResource(name); |
166 | 10 | return u; |
167 | } | |
168 | ||
169 | /** | |
170 | * Get a resource (file) from the Broker's class loader. | |
171 | */ | |
172 | public InputStream getResourceAsStream (String name) | |
173 | { | |
174 | 0 | InputStream is = _servletClassLoader.getResourceAsStream(name); |
175 | 0 | if (is == null) |
176 | 0 | is = super.getResourceAsStream(name); |
177 | 0 | return is; |
178 | } | |
179 | ||
180 | /** | |
181 | * Loads a class by name. Uses the servlet classloader to load the | |
182 | * class. If the class is not found uses the Broker classForName | |
183 | * implementation. | |
184 | */ | |
185 | public Class classForName (String name) throws ClassNotFoundException | |
186 | { | |
187 | 94 | Class cls = null; |
188 | try | |
189 | { | |
190 | 94 | cls = _servletClassLoader.loadClass(name); |
191 | } | |
192 | 0 | catch (ClassNotFoundException e) |
193 | { | |
194 | 94 | } |
195 | ||
196 | 94 | if (cls == null) |
197 | 0 | cls = super.classForName(name); |
198 | ||
199 | 94 | return cls; |
200 | } | |
201 | ||
202 | } |