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

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.lang.reflect.Method;
36 import java.util.Arrays;
37  
38 import org.chwf.config.Config;
39 import org.chwf.config.ConfigFactory;
40 import org.chwf.config.ConfigurationException;
41 import org.chwf.converter.ConversionException;
42 import org.chwf.converter.Converter;
43 import org.chwf.servlet.Controller;
44 import org.chwf.servlet.ControllerException;
45  
46 /**
47  * Class managing controller method configuration data.
48  *
49  * @author Paul Strack
50  */
51  
521public class MethodConfig {
53  
54   /** The parameter suffix for properties. */
55   private static final String PARAMETERS_PROPERTY = ".parameters";
56  
57   /** The defaults suffix for properties. */
58   private static final String DEFAULTS_PROPERTY = ".defaults";
59  
60   /** Key for roles configuration. */
61   private static final String ROLES_KEY = ".roles";
62  
63   /** An empty string array. */
641  private static final String[] EMPTY_STRING_ARRAY = new String[0];
65  
66   /** The controller config. */
67   private final ControllerConfig controllerConfig;
68  
69   /** The method, used for reflection. */
70   private final Method method;
71  
72   /** The raw configuration data. */
73   private final Config config;
74  
75   /** The method parameter types. */
76   private final Class[] types;
77  
78   /**
79    * Constructor.
80    *
81    * @param controllerConfig The controller config.
82    * @param method The method.
83    */
8478  public MethodConfig(ControllerConfig controllerConfig, Method method) {
8578    this.controllerConfig = controllerConfig;
8678    this.method = method;
8778    this.config = ConfigFactory.getConfig(controllerConfig.getController());
8878    types = this.getMethod().getParameterTypes();
8978  }
90  
91   /**
92    * The controller class.
93    *
94    * @return The controller class.
95    */
96   public Class getController() {
97234    return this.controllerConfig.getController();
98   }
99  
100   /**
101    * The method.
102    *
103    * @return The method.
104    */
105   public Method getMethod() {
106234    return method;
107   }
108  
109   /**
110    * Whether this method is an initializers (getter).
111    *
112    * @return Whether this method is an initializers (getter).
113    */
114   public boolean isInitializer() {
115234    return getMethodName().startsWith("get");
116   }
117  
118   /**
119    * Get the method parameter names parameter names.
120    *
121    * @return The parameter names as an array of strings.
122    * @throws ControllerException If parameter are not properly configured.
123    */
124   public String[] getParameterNames() throws ControllerException {
125  
12678    int typesLength = this.getMethod().getParameterTypes().length;
12778    if (typesLength == 0) {
12850      return EMPTY_STRING_ARRAY;
129     }
13028    String[] parameters = getParametersFromConfig();
13128    if ((parameters == null)) {
1320      throw new ControllerException(
133         ControllerMessages.MESSAGE_PARAMETERS_MISCONFIGURED,
134         new Object[] { getMethodName(), getController().getName()});
135     }
13628    if (parameters.length != typesLength) {
1370      throw new ControllerException(
138         ControllerMessages.MESSAGE_PARAMETER_LIST_BAD_SIZE,
139         new Object[] { getMethodName(), getController().getName()});
140     }
14128    return parameters;
142   }
143  
144   /**
145    * Get the method parameter defaults.
146    *
147    * @return Default values for all parameters.
148    * @throws ControllerException If default list is incorrect.
149    */
150   public Object[] getParameterDefaults() throws ControllerException {
15178    Object[] defaults = getDefaultsFromConfig();
15278    if (defaults == null) {
15372      defaults = new Object[getParameterTypes().length];
15472      Arrays.fill(defaults, Controller.DEFAULT_NONE);
155     }
15678    if (defaults.length != getParameterTypes().length) {
1570      throw new ControllerException(
158         ControllerMessages.MESSAGE_DEFAULT_LIST_BAD_SIZE,
159         new Object[] { getMethodName(), getController().getName()});
160     }
161110    for (int i = 0; i < getParameterTypes().length; i++) {
16232      if (getParameterTypes()[i].equals(Boolean.class)
163         || getParameterTypes()[i].equals(Boolean.TYPE)) {
1642        if (defaults[i].equals(Controller.DEFAULT_NONE)) {
1652          defaults[i] = Boolean.FALSE;
166         }
167       }
168     }
16978    return defaults;
170   }
171  
172   /**
173    * The default view for the method.
174    *
175    * @return The default view for the method.
176    */
177   public String getDefaultView() {
17878    return URIMapper.deriveDefaultView(getController(), getMethodName());
179   }
180  
181   /**
182    * The default error page for the method.
183    *
184    * @return The default error page for the method.
185    */
186   public String getDefaultErrorPage() {
18778    return URIMapper.deriveDefaultErrorPage(getController(), getMethodName());
188   }
189  
190   /**
191    * Get the security roles for this controller method.
192    *
193    * @return The array of roles.
194    */
195   public String[] getRoles() {
19678    String key = getMethodName() + ROLES_KEY;
19778    return config.getList(key, EMPTY_STRING_ARRAY);
198   }
199  
200   /**
201    * Get parameter names from configuration.
202    *
203    * @return Parameter defaults.
204    */
205   private String[] getParametersFromConfig() {
20628    String parametersProperty = getMethodName() + PARAMETERS_PROPERTY;
20728    String[] list = config.getList(parametersProperty, EMPTY_STRING_ARRAY);
20828    if (list != null) {
20960      for (int i = 0; i < list.length; i++) {
21032        list[i] = list[i].trim();
211       }
212     }
21328    return list;
214   }
215  
216   /**
217    * Get parameter defaults from configuration.
218    *
219    * @return Parameter defaults.
220    */
221   private Object[] getDefaultsFromConfig() {
22278    Object[] defaults = null;
223     try {
22478      String[] defaultStrings = null;
22578      String defaultsProperty = getMethodName() + DEFAULTS_PROPERTY;
22678      defaultStrings = config.getList(defaultsProperty);
2276      defaults = new Object[defaultStrings.length];
22815      for (int i = 0; i < defaults.length; i++) {
2299        String defaultString = defaultStrings[i];
2309        if (Controller.DEFAULT_NONE.equals(defaultString)) {
2312          defaults[i] = Controller.DEFAULT_NONE;
2327        } else if (Controller.DEFAULT_NEW_OBJECT.equals(defaultString)) {
2334          defaults[i] = Controller.DEFAULT_NEW_OBJECT;
234         } else {
235           try {
2363            defaults[i] =
237               Converter.getConverter(getParameterTypes()[i]).parse(
238                 defaultString);
2392          } catch (ConversionException ex) {
2401            defaults[i] = defaultString;
241           }
242         }
243       }
2446    } catch (ConfigurationException ex) {
245       /* Ignore and return null. */
24672      return null;
247     }
2486    return defaults;
249   }
250  
251   /**
252    * Get parameter types.
253    *
254    * @return parameter types.
255    */
256   private Class[] getParameterTypes() {
257325    return types;
258   }
259  
260   /**
261    * The method name.
262    *
263    * @return The method name.
264    */
265   private String getMethodName() {
266574    return method.getName();
267   }
268  
269   /**
270    * The controller config.
271    *
272    * @return The controller config.
273    */
274   public ControllerConfig getControllerConfig() {
2750    return controllerConfig;
276   }
277 }

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.