1 16 17 package org.apache.webapp.admin.resources; 18 19 import java.util.List ; 20 import java.util.ArrayList ; 21 22 import javax.servlet.http.HttpServletRequest ; 23 import org.apache.struts.action.ActionError; 24 import org.apache.struts.action.ActionErrors; 25 import org.apache.struts.action.ActionForm; 26 import org.apache.struts.action.ActionMapping; 27 import org.apache.webapp.admin.LabelValueBean; 28 29 import java.lang.reflect.Constructor ; 30 31 38 39 public final class EnvEntryForm extends BaseForm { 40 41 42 44 45 47 48 51 private String name = null; 52 53 public String getName() { 54 return (this.name); 55 } 56 57 public void setName(String name) { 58 this.name = name; 59 } 60 61 64 private String entryType = null; 65 66 public String getEntryType() { 67 return (this.entryType); 68 } 69 70 public void setEntryType(String entryType) { 71 this.entryType = entryType; 72 } 73 74 75 78 private String value = null; 79 80 public String getValue() { 81 return (this.value); 82 } 83 84 public void setValue(String value) { 85 this.value = value; 86 } 87 88 91 private String description = null; 92 93 public String getDescription() { 94 return (this.description); 95 } 96 97 public void setDescription(String description) { 98 this.description = description; 99 } 100 101 104 private boolean override = true; 105 106 public boolean getOverride() { 107 return (this.override); 108 } 109 110 public void setOverride(boolean override) { 111 this.override = override; 112 } 113 114 117 private String resourcetype = null; 118 119 122 public String getResourcetype() { 123 return this.resourcetype; 124 } 125 126 129 public void setResourcetype(String resourcetype) { 130 this.resourcetype = resourcetype; 131 } 132 133 136 private String path = null; 137 138 141 public String getPath() { 142 return this.path; 143 } 144 145 148 public void setPath(String path) { 149 this.path = path; 150 } 151 152 155 private String host = null; 156 157 160 public String getHost() { 161 return this.host; 162 } 163 164 167 public void setHost(String host) { 168 this.host = host; 169 } 170 171 172 175 private String domain = null; 176 177 180 public String getDomain() { 181 return this.domain; 182 } 183 184 187 public void setDomain(String domain) { 188 this.domain = domain; 189 } 190 191 194 private static List typeVals = new ArrayList (); 195 196 static { 197 typeVals.add(new LabelValueBean("java.lang.Boolean", "java.lang.Boolean")); 198 typeVals.add(new LabelValueBean("java.lang.Byte", "java.lang.Byte")); 199 typeVals.add(new LabelValueBean("java.lang.Character", "java.lang.Character")); 200 typeVals.add(new LabelValueBean("java.lang.Double", "java.lang.Double")); 201 typeVals.add(new LabelValueBean("java.lang.Float", "java.lang.Float")); 202 typeVals.add(new LabelValueBean("java.lang.Integer", "java.lang.Integer")); 203 typeVals.add(new LabelValueBean("java.lang.Long", "java.lang.Long")); 204 typeVals.add(new LabelValueBean("java.lang.Short", "java.lang.Short")); 205 typeVals.add(new LabelValueBean("java.lang.String", "java.lang.String")); 206 207 } 208 209 212 public List getTypeVals() { 213 214 return this.typeVals; 215 216 } 217 218 221 public void setTypeVals(List typeVals) { 222 223 this.typeVals = typeVals; 224 225 } 226 227 229 235 public void reset(ActionMapping mapping, HttpServletRequest request) { 236 237 super.reset(mapping, request); 238 name = null; 239 entryType = null; 240 value = null; 241 description = null; 242 override = false; 243 244 } 245 246 256 257 private ActionErrors errors = null; 258 259 public ActionErrors validate(ActionMapping mapping, 260 HttpServletRequest request) { 261 262 errors = new ActionErrors(); 263 264 String submit = request.getParameter("submit"); 265 267 if ((name == null) || (name.length() < 1)) { 269 errors.add("name", 270 new ActionError("resources.error.name.required")); 271 } 272 273 if ((value == null) || (value.length() < 1)) { 275 errors.add("value", 276 new ActionError("resources.error.value.required")); 277 } 278 279 if ((name != null) && (name.indexOf('"') >= 0)) { 281 errors.add("name", 282 new ActionError("users.error.quotes")); 283 } 284 285 if ((value != null) && (value.indexOf('"') > 0)) { 287 errors.add("value", 288 new ActionError("users.error.quotes")); 289 } 290 291 if ((description != null) && (description.indexOf('"') > 0)) { 293 errors.add("description", 294 new ActionError("users.error.quotes")); 295 } 296 297 override = (request.getParameter("override") != null); 299 300 if (validateType(entryType, value)) { 301 errors.add("value", 302 new ActionError("resources.error.value.mismatch")); 303 } 304 return (errors); 306 } 307 308 311 private boolean validateType(String entryType, String value) { 312 Class cls = null; 313 boolean mismatch = false; 314 try { 315 cls = Class.forName(entryType); 316 317 if (Character .class.isAssignableFrom(cls)) { 318 if (value.length() != 1) { 321 mismatch = true; 322 } 323 } else if (Boolean .class.isAssignableFrom(cls)) { 324 if (!("true".equalsIgnoreCase(value) || 328 "false".equalsIgnoreCase(value))) { 329 mismatch = true; 330 } 331 } else if (Number .class.isAssignableFrom(cls)) { 332 try { 336 Class [] parameterTypes = {String .class}; 337 Constructor ct = cls.getConstructor(parameterTypes); 338 Object arglist1[] = {value}; 339 Object retobj = ct.newInstance(arglist1); 340 } catch (Exception e) { 341 mismatch = true; 342 } 343 } else if (String .class.isAssignableFrom(cls)) { 344 } else { 346 errors.add("entryType", 348 new ActionError("resources.error.entryType.notimpl")); 349 } 350 } catch (ClassNotFoundException cnfe) { 351 errors.add("entryType", 353 new ActionError("resources.error.entryType.invalid")); 354 } 355 return mismatch; 356 } 357 358 } 359 | Popular Tags |