root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / util / HostAddressValidator.java @ 15280
History | View | Annotate | Download (2.11 KB)
1 |
/*
|
---|---|
2 |
* Copyright (C) 2004 Thorsten Kamann
|
3 |
*
|
4 |
* File : HostAddressValidator.java
|
5 |
* Description : Validates a given host:port whether it is available or not
|
6 |
* Author's email : thorsten.kamann@planetes.de
|
7 |
* Author's Website : http://www.izforge.com
|
8 |
*
|
9 |
* This program is free software; you can redistribute it and/or
|
10 |
* modify it under the terms of the GNU General Public License
|
11 |
* as published by the Free Software Foundation; either version 2
|
12 |
* of the License, or any later version.
|
13 |
*
|
14 |
* This program is distributed in the hope that it will be useful,
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17 |
* GNU General Public License for more details.
|
18 |
*
|
19 |
* You should have received a copy of the GNU General Public License
|
20 |
* along with this program; if not, write to the Free Software
|
21 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
22 |
*/
|
23 |
|
24 |
package com.izforge.izpack.util; |
25 |
|
26 |
import com.izforge.izpack.panels.ProcessingClient; |
27 |
import com.izforge.izpack.panels.Validator; |
28 |
|
29 |
import java.net.InetAddress; |
30 |
import java.net.ServerSocket; |
31 |
|
32 |
/**
|
33 |
* A validator to check wheter a host:port is available (free).
|
34 |
*
|
35 |
* This validator can be used for rule input fields in the UserInputPanel to make
|
36 |
* sure that the port the user entered is not in use.
|
37 |
*
|
38 |
* @author thorque
|
39 |
*/
|
40 |
public class HostAddressValidator implements Validator |
41 |
{ |
42 |
|
43 |
public boolean validate(ProcessingClient client){ |
44 |
InetAddress inet = null; |
45 |
String host = ""; |
46 |
int port = 0; |
47 |
boolean retValue = false; |
48 |
int numfields = client.getNumFields();
|
49 |
|
50 |
try {
|
51 |
host = client.getFieldContents(0);
|
52 |
port = Integer.parseInt(client.getFieldContents(1)); |
53 |
}catch (Exception e) { |
54 |
return false; |
55 |
} |
56 |
|
57 |
try{
|
58 |
inet = InetAddress.getByName(host);
|
59 |
ServerSocket socket = new ServerSocket(port, 0, inet); |
60 |
if (socket.getLocalPort() > 0){ |
61 |
retValue = true;
|
62 |
}else{
|
63 |
retValue = false;
|
64 |
} |
65 |
socket.close(); |
66 |
}catch (Exception ex){ |
67 |
retValue = false;
|
68 |
} |
69 |
return retValue;
|
70 |
} |
71 |
|
72 |
} |