KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > utilities > postinstall > DataValidator


1 /*
2  * DataValidator.java
3  *
4  * Created on October 13, 2003, 7:00 AM
5  */

6
7 package com.quikj.application.utilities.postinstall;
8
9 import java.io.*;
10 import java.util.*;
11
12 /**
13  *
14  * @author amit
15  */

16 public class DataValidator
17 {
18     
19     /** Holds value of property params. */
20     private ConfigParams params;
21     
22     /** Creates a new instance of DataValidator */
23     public DataValidator(ConfigParams params)
24     {
25         this.params = params;
26     }
27     
28     public String JavaDoc validateSMTP(String JavaDoc server, boolean write)
29     {
30         if (server.length() == 0)
31         {
32             return "** The SMTP server host name has not been entered";
33         }
34         
35         if (write == true)
36         {
37             ConfigElement e = new ConfigElement();
38             e.setParamName("smtp-server");
39             e.setParamValue(server);
40             e.setReplacePattern("$$ACE(ACE_SMTP_SERVER)");
41             params.put(e);
42         }
43         return null;
44     }
45     
46     public String JavaDoc validateOperatorEmail(String JavaDoc op_email, boolean write)
47     {
48         if (op_email.length() > 0)
49         {
50             if (validateEmail(op_email) == false)
51             {
52                 return "** The email address does not have a valid format";
53             }
54         }
55         
56         if (write == true)
57         {
58             ConfigElement e = new ConfigElement();
59             e.setParamName("operator-email");
60             e.setParamValue(op_email);
61             e.setReplacePattern("$$ACE(ACE_OPERATOR_EMAIL)");
62             params.put(e);
63         }
64         
65         return null;
66     }
67     
68     public String JavaDoc validateLogEmail(String JavaDoc log_email, boolean write)
69     {
70         if (log_email.length() > 0)
71         {
72             if (validateEmail(log_email) == false)
73             {
74                 return "** The email address does not have a valid format";
75             }
76         }
77         
78         if (write == true)
79         {
80             ConfigElement e = new ConfigElement();
81             e.setParamName("log-email");
82             e.setParamValue(log_email);
83             e.setReplacePattern("$$ACE(ACE_LOG_EMAIL)");
84             params.put(e);
85             if (log_email.length() == 0)
86             {
87                 // comment out the whole thing
88
e = new ConfigElement();
89                 e.setParamName("log-email-comment-start");
90                 e.setParamValue("<!--");
91                 e.setReplacePattern("$$ACE(ACE_LOG_EMAIL_COMMENT_START)");
92                 e.setDisplay(ConfigElement.DISPLAY_NONE);
93                 params.put(e);
94                 
95                 e = new ConfigElement();
96                 e.setParamName("log-email-comment-end");
97                 e.setParamValue("-->");
98                 e.setReplacePattern("$$ACE(ACE_LOG_EMAIL_COMMENT_END)");
99                 e.setDisplay(ConfigElement.DISPLAY_NONE);
100                 params.put(e);
101                 
102             }
103             else
104             {
105                 e = new ConfigElement();
106                 e.setParamName("log-email-comment-start");
107                 e.setParamValue("");
108                 e.setReplacePattern("$$ACE(ACE_LOG_EMAIL_COMMENT_START)");
109                 e.setDisplay(ConfigElement.DISPLAY_NONE);
110                 params.put(e);
111                 
112                 e = new ConfigElement();
113                 e.setParamName("log-email-comment-end");
114                 e.setParamValue("");
115                 e.setReplacePattern("$$ACE(ACE_LOG_EMAIL_COMMENT_END)");
116                 e.setDisplay(ConfigElement.DISPLAY_NONE);
117                 params.put(e);
118             }
119         }
120         return null;
121     }
122     
123     public String JavaDoc validateOOSEmail(String JavaDoc oos_email, boolean write)
124     {
125         if (oos_email.length() > 0)
126         {
127             if (validateEmail(oos_email) == false)
128             {
129                 return "** The email address does not have a valid format";
130             }
131         }
132         
133         if (write == true)
134         {
135             ConfigElement e = new ConfigElement();
136             e.setParamName("oos-email");
137             e.setParamValue(oos_email);
138             e.setReplacePattern("$$ACE(ACE_OOS_EMAIL)");
139             params.put(e);
140         }
141         return null;
142     }
143     
144     public String JavaDoc validateDBHostName(String JavaDoc host, boolean write)
145     {
146         if (host.length() == 0)
147         {
148             return "** The host name cannot be blank";
149         }
150         
151         if (write == true)
152         {
153             ConfigElement e = new ConfigElement();
154             e.setParamName("sql-host");
155             e.setParamValue(host);
156             e.setReplacePattern("$$ACE(ACE_SQL_HOST)");
157             params.put(e);
158         }
159         return null;
160     }
161     
162     public String JavaDoc validateDBUser(String JavaDoc user, boolean write)
163     {
164         if (user.length() == 0)
165         {
166             return "** The user name field cannot be left blank";
167         }
168         
169         if (write == true)
170         {
171             ConfigElement e = new ConfigElement();
172             e.setParamName("sql-user");
173             e.setParamValue(user);
174             e.setReplacePattern("$$ACE(ACE_SQL_USER)");
175             params.put(e);
176         }
177         return null;
178     }
179     
180     public String JavaDoc validateDBPassword(String JavaDoc password, String JavaDoc verify_password,
181     boolean write)
182     {
183         if (password.equals(verify_password) == false)
184         {
185             return "** The passwords do not match";
186         }
187         
188         if (write == true)
189         {
190             ConfigElement e = new ConfigElement();
191             e.setParamName("sql-password");
192             e.setParamValue(password);
193             e.setReplacePattern("$$ACE(ACE_SQL_PASSWORD)");
194             e.setDisplay(ConfigElement.DISPLAY_PARAM);
195             params.put(e);
196         }
197         
198         return null;
199     }
200     
201     
202     public String JavaDoc validateEmbeddedWebServer(boolean ws, boolean write)
203     {
204         if (write == true)
205         {
206             ConfigElement e = new ConfigElement();
207             e.setParamName("ace-use-bundled-https");
208             e.setReplacePattern("$$ACE(ACE_ENABLE_WEB)");
209             if (ws == true)
210             {
211                 e.setParamValue("yes");
212             }
213             else
214             {
215                 e.setParamValue("no");
216             }
217             params.put(e);
218         }
219         return null;
220     }
221     
222     public String JavaDoc validateAceHome(String JavaDoc name, boolean write)
223     {
224         if (name.length() == 0)
225         {
226             return "** You must enter a valid folder location";
227         }
228         
229         File f = new File(name);
230         if (f.exists() == false)
231         {
232             return "** The specified folder does not exist";
233         }
234         
235         if (f.isDirectory() == false)
236         {
237             return "** The specified name is not a folder";
238         }
239         
240         if (f.canWrite() == false)
241         {
242             return "** You do not have permissions to modify files and folders";
243         }
244         
245         File nf = new File (f.getAbsolutePath(), "sql/init_ace.sql.orig");
246         if (nf.exists() == false)
247         {
248             return "** This does not look like a folder where Ace Operator is installed";
249         }
250         
251         if (write == true)
252         {
253             ConfigElement e = new ConfigElement();
254             e.setParamName("ace-home");
255             e.setParamValue(f.getAbsolutePath());
256             e.setReplacePattern("$$ACE(ACE_HOME)");
257             params.put(e);
258         }
259         return null;
260     }
261     
262     public String JavaDoc validateHostName(String JavaDoc host, boolean write)
263     {
264         if (host.length() == 0)
265         {
266             return "** You must enter a valid host name";
267         }
268         
269         if (write == true)
270         {
271             ConfigElement e = new ConfigElement();
272             e.setParamName("ace-host");
273             e.setParamValue(host);
274             e.setReplacePattern("$$ACE(ACE_HOST_NAME)");
275             params.put(e);
276         }
277         return null;
278     }
279     
280     public String JavaDoc validateJavaHome(String JavaDoc jname, boolean write)
281     {
282         if (jname.length() == 0)
283         {
284             return "** You must enter a valid Java home folder location";
285         }
286         
287         File f = new File(jname);
288         if (f.exists() == false)
289         {
290             return "** The specified java home folder does not exist";
291         }
292         
293         if (f.isDirectory() == false)
294         {
295             return "** The specified java home folder name is not a folder";
296         }
297         
298         if (write == true)
299         {
300             ConfigElement e = new ConfigElement();
301             e.setParamName("java-home");
302             e.setParamValue(f.getAbsolutePath());
303             e.setReplacePattern("$$ACE(JAVA_HOME)");
304             params.put(e);
305         }
306         return null;
307     }
308     
309     /** Getter for property param.
310      * @return Value of property param.
311      *
312      */

313     public ConfigParams getParams()
314     {
315         return this.params;
316     }
317     
318     /** Setter for property param.
319      * @param param New value of property param.
320      *
321      */

322     public void setParams(ConfigParams params)
323     {
324         this.params = params;
325     }
326     
327     private boolean validateEmail(String JavaDoc email)
328     {
329         char[] cmail = email.toCharArray();
330         for (int i = 0; i < cmail.length; i++)
331         {
332             if (Character.isWhitespace(cmail[i]) == true)
333             {
334                 return false;
335             }
336         }
337         
338         StringTokenizer tokens = new StringTokenizer(email, "@");
339         int count = tokens.countTokens();
340         if (count != 2)
341         {
342             return false;
343         }
344         
345         return true;
346     }
347     
348     public static String JavaDoc findAceHome (String JavaDoc os)
349     {
350         String JavaDoc def = null;
351         
352         File f = new File(".");
353         String JavaDoc abs_path = f.getAbsolutePath();
354         if (abs_path.endsWith(File.separator + ".") == true)
355         {
356             abs_path = abs_path.substring(0, abs_path.length() - 2);
357         }
358         
359         if (fileExists(abs_path, "sql/init_ace.sql.orig") == true)
360         {
361             def = abs_path;
362         }
363         else if (os.startsWith("Windows") == true)
364         {
365             if (fileExists("C:\\", "ace") == true)
366             {
367                 if (fileExists("C:\\ace", "sql/init_ace.sql.orig") == true)
368                 {
369                     def = "C:\\ace";
370                 }
371             }
372         }
373         else // linux/unix
374
{
375             if (fileExists("/opt", "ace") == true)
376             {
377                 if (fileExists("/opt/ace", "sql/init_ace.sql.orig") == true)
378                 {
379                     def = "/opt/ace";
380                 }
381             }
382             else if (fileExists("/usr", "ace") == true)
383             {
384                 if (fileExists("/usr/ace", "sql/init_ace.sql.orig") == true)
385                 {
386                     def = "/usr/ace";
387                 }
388             }
389             if (fileExists("/usr/local", "ace") == true)
390             {
391                 if (fileExists("/usr/local/ace", "sql/init_ace.sql.orig") == true)
392                 {
393                     def = "/usr/local/ace";
394                 }
395             }
396         }
397         
398         return def;
399     }
400     
401     public static boolean fileExists(String JavaDoc dir, String JavaDoc file)
402     {
403         File f = new File(dir, file);
404         return f.exists();
405     }
406 }
407
Popular Tags