| 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.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 | ||
| 52 | 1 | public 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. */ | |
| 64 | 1 | 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 | */ | |
| 84 | 78 | public MethodConfig(ControllerConfig controllerConfig, Method method) { |
| 85 | 78 | this.controllerConfig = controllerConfig; |
| 86 | 78 | this.method = method; |
| 87 | 78 | this.config = ConfigFactory.getConfig(controllerConfig.getController()); |
| 88 | 78 | types = this.getMethod().getParameterTypes(); |
| 89 | 78 | } |
| 90 | ||
| 91 | /** | |
| 92 | * The controller class. | |
| 93 | * | |
| 94 | * @return The controller class. | |
| 95 | */ | |
| 96 | public Class getController() { | |
| 97 | 234 | return this.controllerConfig.getController(); |
| 98 | } | |
| 99 | ||
| 100 | /** | |
| 101 | * The method. | |
| 102 | * | |
| 103 | * @return The method. | |
| 104 | */ | |
| 105 | public Method getMethod() { | |
| 106 | 234 | 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() { | |
| 115 | 234 | 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 | ||
| 126 | 78 | int typesLength = this.getMethod().getParameterTypes().length; |
| 127 | 78 | if (typesLength == 0) { |
| 128 | 50 | return EMPTY_STRING_ARRAY; |
| 129 | } | |
| 130 | 28 | String[] parameters = getParametersFromConfig(); |
| 131 | 28 | if ((parameters == null)) { |
| 132 | 0 | throw new ControllerException( |
| 133 | ControllerMessages.MESSAGE_PARAMETERS_MISCONFIGURED, | |
| 134 | new Object[] { getMethodName(), getController().getName()}); | |
| 135 | } | |
| 136 | 28 | if (parameters.length != typesLength) { |
| 137 | 0 | throw new ControllerException( |
| 138 | ControllerMessages.MESSAGE_PARAMETER_LIST_BAD_SIZE, | |
| 139 | new Object[] { getMethodName(), getController().getName()}); | |
| 140 | } | |
| 141 | 28 | 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 { | |
| 151 | 78 | Object[] defaults = getDefaultsFromConfig(); |
| 152 | 78 | if (defaults == null) { |
| 153 | 72 | defaults = new Object[getParameterTypes().length]; |
| 154 | 72 | Arrays.fill(defaults, Controller.DEFAULT_NONE); |
| 155 | } | |
| 156 | 78 | if (defaults.length != getParameterTypes().length) { |
| 157 | 0 | throw new ControllerException( |
| 158 | ControllerMessages.MESSAGE_DEFAULT_LIST_BAD_SIZE, | |
| 159 | new Object[] { getMethodName(), getController().getName()}); | |
| 160 | } | |
| 161 | 110 | for (int i = 0; i < getParameterTypes().length; i++) { |
| 162 | 32 | if (getParameterTypes()[i].equals(Boolean.class) |
| 163 | || getParameterTypes()[i].equals(Boolean.TYPE)) { | |
| 164 | 2 | if (defaults[i].equals(Controller.DEFAULT_NONE)) { |
| 165 | 2 | defaults[i] = Boolean.FALSE; |
| 166 | } | |
| 167 | } | |
| 168 | } | |
| 169 | 78 | 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() { | |
| 178 | 78 | 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() { | |
| 187 | 78 | 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() { | |
| 196 | 78 | String key = getMethodName() + ROLES_KEY; |
| 197 | 78 | 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() { | |
| 206 | 28 | String parametersProperty = getMethodName() + PARAMETERS_PROPERTY; |
| 207 | 28 | String[] list = config.getList(parametersProperty, EMPTY_STRING_ARRAY); |
| 208 | 28 | if (list != null) { |
| 209 | 60 | for (int i = 0; i < list.length; i++) { |
| 210 | 32 | list[i] = list[i].trim(); |
| 211 | } | |
| 212 | } | |
| 213 | 28 | return list; |
| 214 | } | |
| 215 | ||
| 216 | /** | |
| 217 | * Get parameter defaults from configuration. | |
| 218 | * | |
| 219 | * @return Parameter defaults. | |
| 220 | */ | |
| 221 | private Object[] getDefaultsFromConfig() { | |
| 222 | 78 | Object[] defaults = null; |
| 223 | try { | |
| 224 | 78 | String[] defaultStrings = null; |
| 225 | 78 | String defaultsProperty = getMethodName() + DEFAULTS_PROPERTY; |
| 226 | 78 | defaultStrings = config.getList(defaultsProperty); |
| 227 | 6 | defaults = new Object[defaultStrings.length]; |
| 228 | 15 | for (int i = 0; i < defaults.length; i++) { |
| 229 | 9 | String defaultString = defaultStrings[i]; |
| 230 | 9 | if (Controller.DEFAULT_NONE.equals(defaultString)) { |
| 231 | 2 | defaults[i] = Controller.DEFAULT_NONE; |
| 232 | 7 | } else if (Controller.DEFAULT_NEW_OBJECT.equals(defaultString)) { |
| 233 | 4 | defaults[i] = Controller.DEFAULT_NEW_OBJECT; |
| 234 | } else { | |
| 235 | try { | |
| 236 | 3 | defaults[i] = |
| 237 | Converter.getConverter(getParameterTypes()[i]).parse( | |
| 238 | defaultString); | |
| 239 | 2 | } catch (ConversionException ex) { |
| 240 | 1 | defaults[i] = defaultString; |
| 241 | } | |
| 242 | } | |
| 243 | } | |
| 244 | 6 | } catch (ConfigurationException ex) { |
| 245 | /* Ignore and return null. */ | |
| 246 | 72 | return null; |
| 247 | } | |
| 248 | 6 | return defaults; |
| 249 | } | |
| 250 | ||
| 251 | /** | |
| 252 | * Get parameter types. | |
| 253 | * | |
| 254 | * @return parameter types. | |
| 255 | */ | |
| 256 | private Class[] getParameterTypes() { | |
| 257 | 325 | return types; |
| 258 | } | |
| 259 | ||
| 260 | /** | |
| 261 | * The method name. | |
| 262 | * | |
| 263 | * @return The method name. | |
| 264 | */ | |
| 265 | private String getMethodName() { | |
| 266 | 574 | return method.getName(); |
| 267 | } | |
| 268 | ||
| 269 | /** | |
| 270 | * The controller config. | |
| 271 | * | |
| 272 | * @return The controller config. | |
| 273 | */ | |
| 274 | public ControllerConfig getControllerConfig() { | |
| 275 | 0 | return controllerConfig; |
| 276 | } | |
| 277 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |