Line | Hits | Source |
---|---|---|
1 | /* | |
2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
4 | ||
5 | All rights reserved. | |
6 | ||
7 | Redistribution and use in source and binary forms, with or without | |
8 | modification, are permitted provided that the following conditions are met: | |
9 | ||
10 | 1. Redistributions of source code must retain the above copyright notice, this | |
11 | list of conditions and the following disclaimer. | |
12 | ||
13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
14 | this list of conditions and the following disclaimer in the documentation | |
15 | and/or other materials provided with the distribution. | |
16 | ||
17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
18 | may be used to endorse or promote products derived from this software without | |
19 | specific prior written permission. | |
20 | ||
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | package org.chwf.servlet.engine; | |
34 | ||
35 | import java.util.HashMap; | |
36 | import java.util.Map; | |
37 | ||
38 | import org.chwf.config.Config; | |
39 | import org.chwf.config.ConfigFactory; | |
40 | import org.chwf.servlet.Controller; | |
41 | import org.chwf.servlet.ControllerException; | |
42 | ||
43 | /** | |
44 | * Utility class for managing URI mapping's for Controller classes, methods, | |
45 | * views and error pages as well as configuration data. | |
46 | * | |
47 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
48 | */ | |
49 | 1 | public abstract class URIMapper { |
50 | ||
51 | /** The configuration. */ | |
52 | 2 | private static final Config CONFIG = ConfigFactory.getConfig(URIMapper.class); |
53 | ||
54 | /** The default controller package. */ | |
55 | 1 | private static final String[] DEFAULT_CONTROLLER_PACKAGE = |
56 | { "test.org.chwf.servlet.engine" }; | |
57 | ||
58 | /** Controller package. */ | |
59 | 1 | public static final String[] CONTROLLER_PACKAGE = |
60 | CONFIG.getList("controller.package", DEFAULT_CONTROLLER_PACKAGE); | |
61 | ||
62 | /** Controller class suffix. */ | |
63 | 1 | public static final String CONTROLLER_CLASS_SUFFIX = |
64 | CONFIG.get("controller.class_suffix", "Controller"); | |
65 | ||
66 | /** Controller class suffix. */ | |
67 | 1 | public static final boolean PREPEND_CONTEXT = |
68 | CONFIG.getBoolean("server.redirect.prepend_context", true); | |
69 | ||
70 | /** Controller default view. */ | |
71 | 1 | public static final String DEFAULT_VIEW = |
72 | CONFIG.get("controller.default.view", "/index.view"); | |
73 | ||
74 | /** Controller default error page. */ | |
75 | 1 | public static final String DEFAULT_ERROR = |
76 | CONFIG.get("controller.default.errorpage", "/error.jsp"); | |
77 | ||
78 | /** The command extension. */ | |
79 | public static final String COMMAND_EXTENSION = ".cmd"; | |
80 | ||
81 | /** The default method. */ | |
82 | public static final String DEFAULT_METHOD = "action"; | |
83 | ||
84 | /** The view suffix for properties. */ | |
85 | public static final String VIEW_PROPERTY_SUFFIX = ".view"; | |
86 | ||
87 | /** The error page suffix for properties. */ | |
88 | public static final String ERRORPAGE_PROPERTY_SUFFIX = ".errorpage"; | |
89 | ||
90 | /** The default view property. */ | |
91 | public static final String DEFAULT_VIEW_PROPERTY = "default.view"; | |
92 | ||
93 | /** The default error page suffix for constants. */ | |
94 | public static final String DEFAULT_ERRORPAGE_PROPERTY = "default.errorpage"; | |
95 | ||
96 | /** Data cache for uri-class mappings. */ | |
97 | 1 | private static final Map CLASS_MAP = new HashMap(); |
98 | ||
99 | /** Data cache for uri-method mappings. */ | |
100 | 1 | private static final Map METHOD_MAP = new HashMap(); |
101 | ||
102 | /** | |
103 | * Retrieve the controller class for the specified uri. | |
104 | * | |
105 | * @param uri The uri. | |
106 | * @return The controller class. | |
107 | * @throws ControllerException If the controller class can't be derived. | |
108 | */ | |
109 | static Class getControllerClass(String uri) throws ControllerException { | |
110 | 14 | Class cls = null; |
111 | ||
112 | 14 | synchronized (CLASS_MAP) { |
113 | 14 | cls = (Class) CLASS_MAP.get(uri); |
114 | 14 | if (cls == null) { |
115 | 14 | cls = deriveControllerClass(uri); |
116 | 12 | CLASS_MAP.put(uri, cls); |
117 | } | |
118 | 12 | } |
119 | ||
120 | 12 | return cls; |
121 | } | |
122 | ||
123 | /** | |
124 | * Derive the controller class from the uri if it is not cached. | |
125 | * | |
126 | * @param uri The uri. | |
127 | * @return The controller class. | |
128 | * @throws ControllerException If the controller class can't be derived. | |
129 | */ | |
130 | private static Class deriveControllerClass(String uri) | |
131 | throws ControllerException { | |
132 | try { | |
133 | 14 | if (uri.endsWith(COMMAND_EXTENSION)) { |
134 | 14 | int afterSlash = uri.lastIndexOf('/') + 1; |
135 | 14 | int firstDot = uri.indexOf('.', afterSlash); |
136 | 14 | String controller = uri.substring(afterSlash, firstDot); |
137 | 14 | return getControllerFromShortName(controller); |
138 | } else { | |
139 | 0 | throw new ControllerException( |
140 | ControllerMessages.MESSAGE_UNKNOWN_URI_EXTENSION, | |
141 | new Object[] { uri }); | |
142 | } | |
143 | } catch (Exception ex) { | |
144 | 2 | throw new ControllerException( |
145 | ControllerMessages.MESSAGE_NO_CONTROLLER_FOR_URI, | |
146 | new Object[] { uri, ex }); | |
147 | } | |
148 | } | |
149 | ||
150 | /** | |
151 | * Derive the controller class from its short class name (omitting the | |
152 | * controller package and class name suffix). | |
153 | * | |
154 | * @param controller The controller's short class name. | |
155 | * @return The controller class. | |
156 | * @throws ControllerException If the controller class can't be derived. | |
157 | */ | |
158 | public static Class getControllerFromShortName(String controller) | |
159 | throws ControllerException { | |
160 | 18 | for (int i = 0; i < CONTROLLER_PACKAGE.length; i++) { |
161 | try { | |
162 | 16 | String className = |
163 | CONTROLLER_PACKAGE[i] + '.' + controller + CONTROLLER_CLASS_SUFFIX; | |
164 | 16 | return Class.forName(className); |
165 | } catch (ClassNotFoundException ex) { | |
166 | // Ignore and try next package name | |
167 | 2 | i += 0; // Dummy operation to avoid checkstyle complaints |
168 | } | |
169 | } | |
170 | 2 | throw new ControllerException( |
171 | ControllerMessages.MESSAGE_CLASS_NOT_FOUND, | |
172 | new Object[] { controller }); | |
173 | } | |
174 | ||
175 | /** | |
176 | * Retrieve the method being invoked on the controller. | |
177 | * | |
178 | * @param uri The uri. | |
179 | * @return The method name. | |
180 | * @throws ControllerException If the method name can't be derived. | |
181 | */ | |
182 | static String getControllerMethod(String uri) throws ControllerException { | |
183 | 12 | String method = null; |
184 | ||
185 | 12 | synchronized (METHOD_MAP) { |
186 | 12 | method = (String) METHOD_MAP.get(uri); |
187 | 12 | if (method == null) { |
188 | 12 | method = deriveMethod(uri); |
189 | 12 | METHOD_MAP.put(uri, method); |
190 | } | |
191 | 12 | } |
192 | ||
193 | 12 | return method; |
194 | } | |
195 | ||
196 | /** | |
197 | * Derive the controller method from the uri if it is not cached. | |
198 | * | |
199 | * @param uri The uri. | |
200 | * @return The method name. | |
201 | * @throws ControllerException If the method name can't be derived. | |
202 | */ | |
203 | private static String deriveMethod(String uri) throws ControllerException { | |
204 | try { | |
205 | 12 | int afterSlash = uri.lastIndexOf('/') + 1; |
206 | 12 | int lastDot = uri.lastIndexOf('.'); |
207 | 12 | String invocation = uri.substring(afterSlash, lastDot); |
208 | ||
209 | 12 | int invocationDot = invocation.indexOf('.') + 1; |
210 | 12 | if (invocationDot == 0) { |
211 | 4 | return DEFAULT_METHOD; // There was no invocation dot. |
212 | } else { | |
213 | 8 | return invocation.substring(invocationDot); |
214 | } | |
215 | } catch (Exception ex) { | |
216 | 0 | throw new ControllerException( |
217 | ControllerMessages.MESSAGE_NO_METHOD_FOR_URI, | |
218 | new Object[] { uri, ex }); | |
219 | } | |
220 | } | |
221 | ||
222 | /** | |
223 | * Get the default view for the controller method. | |
224 | * | |
225 | * @param controller The controller. | |
226 | * @param method Its method. | |
227 | * @return The default view. | |
228 | */ | |
229 | static String deriveDefaultView(Class controller, String method) { | |
230 | 78 | Config config = ConfigFactory.getConfig(controller); |
231 | 78 | String view = config.get(method + VIEW_PROPERTY_SUFFIX, null); |
232 | 78 | if (view == null) { |
233 | 77 | view = config.get(DEFAULT_VIEW_PROPERTY, null); |
234 | } | |
235 | 78 | if (view == null) { |
236 | 77 | view = DEFAULT_VIEW; |
237 | } | |
238 | 78 | if (Controller.REFERER_PAGE.equals(view)) { |
239 | 0 | view = Controller.REFERER_PAGE; |
240 | } | |
241 | 78 | return view; |
242 | } | |
243 | ||
244 | /** | |
245 | * Get the default error page for the controller method. | |
246 | * | |
247 | * @param controller The controller. | |
248 | * @param method Its method. | |
249 | * @return The default view. | |
250 | */ | |
251 | static String deriveDefaultErrorPage(Class controller, String method) { | |
252 | 78 | Config config = ConfigFactory.getConfig(controller); |
253 | 78 | String errorPage = config.get(method + ERRORPAGE_PROPERTY_SUFFIX, null); |
254 | 78 | if (errorPage == null) { |
255 | 77 | errorPage = config.get(DEFAULT_ERRORPAGE_PROPERTY, null); |
256 | } | |
257 | 78 | if (errorPage == null) { |
258 | 77 | errorPage = DEFAULT_ERROR; |
259 | } | |
260 | 78 | return errorPage; |
261 | } | |
262 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |