Coverage details for org.chwf.servlet.engine.URIMapper

LineHitsSource
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  */
491public abstract class URIMapper {
50  
51   /** The configuration. */
522  private static final Config CONFIG = ConfigFactory.getConfig(URIMapper.class);
53  
54   /** The default controller package. */
551  private static final String[] DEFAULT_CONTROLLER_PACKAGE =
56     { "test.org.chwf.servlet.engine" };
57  
58   /** Controller package. */
591  public static final String[] CONTROLLER_PACKAGE =
60     CONFIG.getList("controller.package", DEFAULT_CONTROLLER_PACKAGE);
61  
62   /** Controller class suffix. */
631  public static final String CONTROLLER_CLASS_SUFFIX =
64     CONFIG.get("controller.class_suffix", "Controller");
65  
66   /** Controller class suffix. */
671  public static final boolean PREPEND_CONTEXT =
68     CONFIG.getBoolean("server.redirect.prepend_context", true);
69  
70   /** Controller default view. */
711  public static final String DEFAULT_VIEW =
72     CONFIG.get("controller.default.view", "/index.view");
73  
74   /** Controller default error page. */
751  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. */
971  private static final Map CLASS_MAP = new HashMap();
98  
99   /** Data cache for uri-method mappings. */
1001  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 {
11014    Class cls = null;
111  
11214    synchronized (CLASS_MAP) {
11314      cls = (Class) CLASS_MAP.get(uri);
11414      if (cls == null) {
11514        cls = deriveControllerClass(uri);
11612        CLASS_MAP.put(uri, cls);
117       }
11812    }
119  
12012    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 {
13314      if (uri.endsWith(COMMAND_EXTENSION)) {
13414        int afterSlash = uri.lastIndexOf('/') + 1;
13514        int firstDot = uri.indexOf('.', afterSlash);
13614        String controller = uri.substring(afterSlash, firstDot);
13714        return getControllerFromShortName(controller);
138       } else {
1390        throw new ControllerException(
140           ControllerMessages.MESSAGE_UNKNOWN_URI_EXTENSION,
141           new Object[] { uri });
142       }
143     } catch (Exception ex) {
1442      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 {
16018    for (int i = 0; i < CONTROLLER_PACKAGE.length; i++) {
161       try {
16216        String className =
163           CONTROLLER_PACKAGE[i] + '.' + controller + CONTROLLER_CLASS_SUFFIX;
16416        return Class.forName(className);
165       } catch (ClassNotFoundException ex) {
166         // Ignore and try next package name
1672        i += 0; // Dummy operation to avoid checkstyle complaints
168       }
169     }
1702    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 {
18312    String method = null;
184  
18512    synchronized (METHOD_MAP) {
18612      method = (String) METHOD_MAP.get(uri);
18712      if (method == null) {
18812        method = deriveMethod(uri);
18912        METHOD_MAP.put(uri, method);
190       }
19112    }
192  
19312    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 {
20512      int afterSlash = uri.lastIndexOf('/') + 1;
20612      int lastDot = uri.lastIndexOf('.');
20712      String invocation = uri.substring(afterSlash, lastDot);
208  
20912      int invocationDot = invocation.indexOf('.') + 1;
21012      if (invocationDot == 0) {
2114        return DEFAULT_METHOD; // There was no invocation dot.
212       } else {
2138        return invocation.substring(invocationDot);
214       }
215     } catch (Exception ex) {
2160      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) {
23078    Config config = ConfigFactory.getConfig(controller);
23178    String view = config.get(method + VIEW_PROPERTY_SUFFIX, null);
23278    if (view == null) {
23377      view = config.get(DEFAULT_VIEW_PROPERTY, null);
234     }
23578    if (view == null) {
23677      view = DEFAULT_VIEW;
237     }
23878    if (Controller.REFERER_PAGE.equals(view)) {
2390      view = Controller.REFERER_PAGE;
240     }
24178    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) {
25278    Config config = ConfigFactory.getConfig(controller);
25378    String errorPage = config.get(method + ERRORPAGE_PROPERTY_SUFFIX, null);
25478    if (errorPage == null) {
25577      errorPage = config.get(DEFAULT_ERRORPAGE_PROPERTY, null);
256     }
25778    if (errorPage == null) {
25877      errorPage = DEFAULT_ERROR;
259     }
26078    return errorPage;
261   }
262 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.