KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > resources > EnvEntryForm


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.webapp.admin.resources;
18
19 import java.util.List JavaDoc;
20 import java.util.ArrayList JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
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 JavaDoc;
30
31 /**
32  * Form bean for the individual environment entry page.
33  *
34  * @author Manveen Kaur
35  * @version $Revision: 1.4 $ $Date: 2004/02/27 14:59:04 $
36  * @since 4.1
37  */

38
39 public final class EnvEntryForm extends BaseForm {
40
41
42     // ----------------------------------------------------- Instance Variables
43

44
45     // ------------------------------------------------------------- Properties
46

47
48     /**
49      * The name of the associated entry.
50      */

51     private String JavaDoc name = null;
52
53     public String JavaDoc getName() {
54         return (this.name);
55     }
56
57     public void setName(String JavaDoc name) {
58         this.name = name;
59     }
60
61     /**
62      * The type of the associated entry.
63      */

64     private String JavaDoc entryType = null;
65
66     public String JavaDoc getEntryType() {
67         return (this.entryType);
68     }
69
70     public void setEntryType(String JavaDoc entryType) {
71         this.entryType = entryType;
72     }
73
74
75     /**
76      * The value of the associated entry.
77      */

78     private String JavaDoc value = null;
79
80     public String JavaDoc getValue() {
81         return (this.value);
82     }
83
84     public void setValue(String JavaDoc value) {
85         this.value = value;
86     }
87
88     /**
89      * The description of the associated entry.
90      */

91     private String JavaDoc description = null;
92
93     public String JavaDoc getDescription() {
94         return (this.description);
95     }
96
97     public void setDescription(String JavaDoc description) {
98         this.description = description;
99     }
100
101     /**
102      * The value of override appl level entries.
103      */

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     /**
115      * The resource type of this environment entry.
116      */

117     private String JavaDoc resourcetype = null;
118     
119     /**
120      * Return the resource type of the environment entry this bean refers to.
121      */

122     public String JavaDoc getResourcetype() {
123         return this.resourcetype;
124     }
125
126     /**
127      * Set the resource type of the environment entry this bean refers to.
128      */

129     public void setResourcetype(String JavaDoc resourcetype) {
130         this.resourcetype = resourcetype;
131     }
132        
133     /**
134      * The path of this environment entry.
135      */

136     private String JavaDoc path = null;
137     
138     /**
139      * Return the path of the environment entry this bean refers to.
140      */

141     public String JavaDoc getPath() {
142         return this.path;
143     }
144
145     /**
146      * Set the path of the environment entry this bean refers to.
147      */

148     public void setPath(String JavaDoc path) {
149         this.path = path;
150     }
151        
152     /**
153      * The host of this environment entry.
154      */

155     private String JavaDoc host = null;
156     
157     /**
158      * Return the host of the environment entry this bean refers to.
159      */

160     public String JavaDoc getHost() {
161         return this.host;
162     }
163
164     /**
165      * Set the host of the environment entry this bean refers to.
166      */

167     public void setHost(String JavaDoc host) {
168         this.host = host;
169     }
170     
171        
172     /**
173      * The domain of this environment entry.
174      */

175     private String JavaDoc domain = null;
176     
177     /**
178      * Return the domain of the environment entry this bean refers to.
179      */

180     public String JavaDoc getDomain() {
181         return this.domain;
182     }
183
184     /**
185      * Set the domain of the environment entry this bean refers to.
186      */

187     public void setDomain(String JavaDoc domain) {
188         this.domain = domain;
189     }
190     
191     /**
192      * Precomputed list of entry type labels and values.
193      */

194     private static List JavaDoc typeVals = new ArrayList JavaDoc();
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     /**
210      * Return the typeVals.
211      */

212     public List JavaDoc getTypeVals() {
213         
214         return this.typeVals;
215         
216     }
217     
218     /**
219      * Set the typeVals.
220      */

221     public void setTypeVals(List JavaDoc typeVals) {
222         
223         this.typeVals = typeVals;
224         
225     }
226     
227     // --------------------------------------------------------- Public Methods
228

229     /**
230      * Reset all properties to their default values.
231      *
232      * @param mapping The mapping used to select this instance
233      * @param request The servlet request we are processing
234      */

235     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc 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     /**
247      * Validate the properties that have been set from this HTTP request,
248      * and return an <code>ActionErrors</code> object that encapsulates any
249      * validation errors that have been found. If no errors are found, return
250      * <code>null</code> or an <code>ActionErrors</code> object with no
251      * recorded error messages.
252      *
253      * @param mapping The mapping used to select this instance
254      * @param request The servlet request we are processing
255      */

256     
257     private ActionErrors errors = null;
258     
259     public ActionErrors validate(ActionMapping mapping,
260     HttpServletRequest JavaDoc request) {
261
262         errors = new ActionErrors();
263
264         String JavaDoc submit = request.getParameter("submit");
265         //if (submit != null) {
266

267             // name is a required field
268
if ((name == null) || (name.length() < 1)) {
269                 errors.add("name",
270                            new ActionError("resources.error.name.required"));
271             }
272
273             // value is a required field
274
if ((value == null) || (value.length() < 1)) {
275                 errors.add("value",
276                            new ActionError("resources.error.value.required"));
277             }
278
279             // Quotes not allowed in name
280
if ((name != null) && (name.indexOf('"') >= 0)) {
281                 errors.add("name",
282                            new ActionError("users.error.quotes"));
283             }
284
285             // Quotes not allowed in value
286
if ((value != null) && (value.indexOf('"') > 0)) {
287                 errors.add("value",
288                            new ActionError("users.error.quotes"));
289             }
290
291             // Quotes not allowed in description
292
if ((description != null) && (description.indexOf('"') > 0)) {
293                 errors.add("description",
294                            new ActionError("users.error.quotes"));
295             }
296             
297             // if cehcked, override will be sent as a request parameter
298
override = (request.getParameter("override") != null);
299             
300             if (validateType(entryType, value)) {
301                    errors.add("value",
302                            new ActionError("resources.error.value.mismatch"));
303             }
304         //}
305
return (errors);
306     }
307
308     /**
309      * Entry type must match type of value.
310      */

311     private boolean validateType(String JavaDoc entryType, String JavaDoc value) {
312         Class JavaDoc cls = null;
313         boolean mismatch = false;
314         try {
315             cls = Class.forName(entryType);
316             
317             if (Character JavaDoc.class.isAssignableFrom(cls)) {
318                 // Special handling is needed because the UI returns
319
// a string even if it is a character (single length string).
320
if (value.length() != 1) {
321                     mismatch = true;
322                 }
323             } else if (Boolean JavaDoc.class.isAssignableFrom(cls)) {
324                 // Special handling is needed because Boolean
325
// string constructor accepts anything other than
326
// true to be false
327
if (!("true".equalsIgnoreCase(value) ||
328                 "false".equalsIgnoreCase(value))) {
329                     mismatch = true;
330                 }
331             } else if (Number JavaDoc.class.isAssignableFrom(cls)) {
332                 // all numbers throw NumberFormatException if they are
333
// constructed with an incorrect number string
334
// We use the general string constructor to do this job
335
try {
336                     Class JavaDoc[] parameterTypes = {String JavaDoc.class};
337                     Constructor JavaDoc ct = cls.getConstructor(parameterTypes);
338                     Object JavaDoc arglist1[] = {value};
339                     Object JavaDoc retobj = ct.newInstance(arglist1);
340                 } catch (Exception JavaDoc e) {
341                     mismatch = true;
342                 }
343             } else if (String JavaDoc.class.isAssignableFrom(cls)) {
344                 // all strings are allowed
345
} else {
346                 // validation for other types not implemented yet
347
errors.add("entryType",
348                        new ActionError("resources.error.entryType.notimpl"));
349             }
350         } catch (ClassNotFoundException JavaDoc cnfe) {
351             // entry type has an invalid entry
352
errors.add("entryType",
353                        new ActionError("resources.error.entryType.invalid"));
354          }
355         return mismatch;
356     }
357     
358 }
359
Popular Tags