KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
30  * Form bean for the individual data source page.
31  *
32  * @author Manveen Kaur
33  * @version $Revision: 1.4 $ $Date: 2004/02/27 14:59:04 $
34  * @since 4.1
35  */

36
37 public final class DataSourceForm extends BaseForm {
38
39
40     // ----------------------------------------------------- Instance Variables
41

42
43     // ------------------------------------------------------------- Properties
44

45
46     /**
47      * The url of the data source.
48      */

49     private String JavaDoc url = null;
50
51     public String JavaDoc getUrl() {
52         return (this.url);
53     }
54
55     public void setUrl(String JavaDoc url) {
56         this.url = url;
57     }
58
59     /**
60      * The JNDI name of the data source.
61      */

62     private String JavaDoc jndiName = null;
63
64     public String JavaDoc getJndiName() {
65         return (this.jndiName);
66     }
67
68     public void setJndiName(String JavaDoc jndiName) {
69         this.jndiName = jndiName;
70     }
71     
72     /**
73      * The JDBC driver class of the data source.
74      */

75     private String JavaDoc driverClass = null;
76
77     public String JavaDoc getDriverClass() {
78         return (this.driverClass);
79     }
80
81     public void setDriverClass(String JavaDoc driverClass) {
82         this.driverClass = driverClass;
83     }
84
85     
86     /**
87      * The username of the databse corresponding to the data source.
88      */

89     private String JavaDoc username = null;
90
91     public String JavaDoc getUsername() {
92         return (this.username);
93     }
94
95     public void setUsername(String JavaDoc username) {
96         this.username = username;
97     }
98
99     
100     /**
101      * The password of the database corresponding to the data source.
102      */

103     private String JavaDoc password = null;
104
105     public String JavaDoc getPassword() {
106         return (this.password);
107     }
108
109     public void setPassword(String JavaDoc password) {
110         this.password = password;
111     }
112
113     
114     /**
115      * The max number of active sessions to the data source.
116      */

117     private String JavaDoc active = null;
118
119     public String JavaDoc getActive() {
120         return (this.active);
121     }
122
123     public void setActive(String JavaDoc active) {
124         this.active = active;
125     }
126
127     /**
128      * The max number of idle connections to the data source.
129      */

130     private String JavaDoc idle = null;
131
132     public String JavaDoc getIdle() {
133         return (this.idle);
134     }
135
136     public void setIdle(String JavaDoc idle) {
137         this.idle = idle;
138     }
139
140     /**
141      * The maximum wait for a connection to the data source.
142      */

143     private String JavaDoc wait = null;
144
145     public String JavaDoc getWait() {
146         return (this.wait);
147     }
148
149     public void setWait(String JavaDoc wait) {
150         this.wait = wait;
151     }
152
153     /**
154      * The resource type of this data source.
155      */

156     private String JavaDoc resourcetype = null;
157     
158     /**
159      * Return the resource type of the data source this bean refers to.
160      */

161     public String JavaDoc getResourcetype() {
162         return this.resourcetype;
163     }
164
165     /**
166      * Set the resource type of the data source this bean refers to.
167      */

168     public void setResourcetype(String JavaDoc resourcetype) {
169         this.resourcetype = resourcetype;
170     }
171        
172     /**
173      * The path of this data source.
174      */

175     private String JavaDoc path = null;
176     
177     /**
178      * Return the path of the data source this bean refers to.
179      */

180     public String JavaDoc getPath() {
181         return this.path;
182     }
183
184     /**
185      * Set the path of the data source this bean refers to.
186      */

187     public void setPath(String JavaDoc path) {
188         this.path = path;
189     }
190        
191     /**
192      * The host of this data source.
193      */

194     private String JavaDoc host = null;
195     
196     /**
197      * Return the host of the data source this bean refers to.
198      */

199     public String JavaDoc getHost() {
200         return this.host;
201     }
202
203     /**
204      * Set the host of the data source this bean refers to.
205      */

206     public void setHost(String JavaDoc host) {
207         this.host = host;
208     }
209     
210        
211     /**
212      * The domain of this data source.
213      */

214     private String JavaDoc domain = null;
215     
216     /**
217      * Return the domain of the data source this bean refers to.
218      */

219     public String JavaDoc getDomain() {
220         return this.domain;
221     }
222
223     /**
224      * Set the domain of the data source this bean refers to.
225      */

226     public void setDomain(String JavaDoc domain) {
227         this.domain = domain;
228     }
229     
230     /**
231      * The validation query to the data source.
232      */

233     private String JavaDoc query = null;
234
235     public String JavaDoc getQuery() {
236         return (this.query);
237     }
238
239     public void setQuery(String JavaDoc query) {
240         this.query = query;
241     }
242     
243     /**
244      * The type of the resource.
245      */

246     private String JavaDoc type = null;
247
248     public String JavaDoc getType() {
249         return (this.type);
250     }
251
252     public void setType(String JavaDoc type) {
253         this.type = type;
254     }
255
256     // --------------------------------------------------------- Public Methods
257

258     /**
259      * Reset all properties to their default values.
260      *
261      * @param mapping The mapping used to select this instance
262      * @param request The servlet request we are processing
263      */

264     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
265
266         super.reset(mapping, request);
267         url = null;
268         jndiName = null;
269         driverClass = null;
270         username = null;
271         password = null;
272         type = null;
273     
274         active = null;
275         idle = null;
276         wait = null;
277         query = null;
278     }
279
280     /**
281      * Validate the properties that have been set from this HTTP request,
282      * and return an <code>ActionErrors</code> object that encapsulates any
283      * validation errors that have been found. If no errors are found, return
284      * <code>null</code> or an <code>ActionErrors</code> object with no
285      * recorded error messages.
286      *
287      * @param mapping The mapping used to select this instance
288      * @param request The servlet request we are processing
289      */

290     
291     private ActionErrors errors = null;
292     
293     public ActionErrors validate(ActionMapping mapping,
294     HttpServletRequest JavaDoc request) {
295
296         errors = new ActionErrors();
297
298         String JavaDoc submit = request.getParameter("submit");
299
300         //if (submit != null) {
301

302             // url is a required field
303
if ((url == null) || (url.length() < 1)) {
304                 errors.add("url",
305                            new ActionError("resources.error.url.required"));
306             }
307
308             // jndiName is a required field
309
if (( jndiName == null) || (jndiName.length() < 1)) {
310                 errors.add("jndiName",
311                            new ActionError("resources.error.jndiName.required"));
312             }
313
314             // driverClass is a required field
315
if ((driverClass == null) || (driverClass.length() < 1)) {
316                 errors.add("driverClass",
317                            new ActionError("resources.error.driverClass.required"));
318             }
319             
320             // username is a required field
321
if ((username == null) || (username.length() < 1)) {
322                 errors.add("username",
323                            new ActionError("users.error.username.required"));
324             }
325             
326             // commented out password can be an empty string
327
// password is a required field
328
//if ((password == null) || (password.length() < 1)) {
329
// errors.add("password",
330
// new ActionError("error.userPassword.required"));
331
//
332

333             // FIX ME -- need to do a range check
334
numberCheck("active", active , false, 0, 10000);
335             numberCheck("idle", idle , false, 0, 10000);
336             numberCheck("wait", wait , false, 0, 10000);
337
338             // Quotes not allowed in username
339
if ((username != null) && (username.indexOf('"') >= 0)) {
340                 errors.add("username",
341                            new ActionError("users.error.quotes"));
342             }
343
344             // Quotes not allowed in password
345
if ((password != null) && (password.indexOf('"') > 0)) {
346                 errors.add("password",
347                            new ActionError("users.error.quotes"));
348             }
349          //}
350
return (errors);
351     }
352  
353     /*
354      * Helper method to check that it is a required number and
355      * is a valid integer within the given range. (min, max).
356      *
357      * @param field The field name in the form for which this error occured.
358      * @param numText The string representation of the number.
359      * @param rangeCheck Boolean value set to true of reange check should be performed.
360      *
361      * @param min The lower limit of the range
362      * @param max The upper limit of the range
363      *
364      */

365     
366     private void numberCheck(String JavaDoc field, String JavaDoc numText, boolean rangeCheck,
367                              int min, int max) {
368         
369         // Check for 'is required'
370
if ((numText == null) || (numText.length() < 1)) {
371             errors.add(field, new ActionError("resources.error."+field+".required"));
372         } else {
373             
374             // check for 'must be a number' in the 'valid range'
375
try {
376                 int num = Integer.parseInt(numText);
377                 // perform range check only if required
378
if (rangeCheck) {
379                     if ((num < min) || (num > max ))
380                         errors.add( field,
381                         new ActionError("resources.error."+ field +".range"));
382                 }
383             } catch (NumberFormatException JavaDoc e) {
384                 errors.add(field,
385                 new ActionError("resources.integer.error"));
386             }
387         }
388     }
389     
390 }
391
Popular Tags