KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > install > validators > ActiveDirectoryBackupHostnameValidator


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.install.validators;
21
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import com.sslexplorer.boot.CodedException;
27 import com.sslexplorer.boot.PropertyDefinition;
28 import com.sslexplorer.boot.PropertyList;
29 import com.sslexplorer.boot.PropertyValidator;
30 import com.sslexplorer.core.CoreException;
31 import com.sslexplorer.input.validators.ErrorConstants;
32
33 /**
34  */

35 public class ActiveDirectoryBackupHostnameValidator implements PropertyValidator {
36
37     /* (non-Javadoc)
38      * @see com.sslexplorer.boot.PropertyValidator#validate(com.sslexplorer.boot.PropertyDefinition, java.lang.String, java.util.Properties)
39      */

40     public void validate(PropertyDefinition definition, String JavaDoc value, Properties JavaDoc properties) throws CodedException {
41         PropertyList propertyList = new PropertyList(value);
42         for (String JavaDoc propertyValue : propertyList) {
43             validate(propertyValue);
44         }
45     }
46
47     private static void validate(String JavaDoc value) throws CoreException {
48         int indexOf = value.indexOf(":");
49         if (indexOf == -1) {
50             if (!isHostNameValid(value)) {
51                 throw new CoreException(ErrorConstants.ERR_STRING_ISNT_IP_ADDRESS, ErrorConstants.CATEGORY_NAME, ErrorConstants.BUNDLE_NAME, null, value);
52             }
53         } else {
54             String JavaDoc hostName = value.substring(0, indexOf);
55             String JavaDoc port = value.substring(indexOf + 1);
56             if (!isHostNameValid(hostName) || !isPortValid(port)) {
57                 throw new CoreException(ErrorConstants.ERR_STRING_ISNT_IP_ADDRESS, ErrorConstants.CATEGORY_NAME, ErrorConstants.BUNDLE_NAME, null, value);
58             }
59         }
60     }
61
62     private static boolean isHostNameValid(String JavaDoc hostName) {
63         try {
64             InetAddress.getByName(hostName);
65             return true;
66         } catch (UnknownHostException JavaDoc e) {
67             return false;
68         }
69     }
70
71     private static boolean isPortValid(String JavaDoc port) {
72         try {
73             Integer.parseInt(port);
74             return true;
75         } catch (NumberFormatException JavaDoc e) {
76             return false;
77         }
78     }
79 }
Popular Tags