Statistics
| Revision:

root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / util / PortValidator.java @ 15280

History | View | Annotate | Download (2.24 KB)

1
/* 
2
*  Copyright (C) 2004 Thorsten Kamann
3
*
4
*  File :               PortValidator.java
5
*  Description :        Validates a given 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 whether a port is available (free) on the localhost.
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 PortValidator implements Validator
41
{
42

    
43
        public boolean validate(ProcessingClient client){
44
                InetAddress inet = null;
45
                String host = "localhost";
46
                boolean retValue = false;
47
                int numfields = client.getNumFields();
48

    
49
            for (int i = 0; i < numfields; i++){
50
                      String value = client.getFieldContents(i);
51

    
52
                      if ((value == null) || (value.length() == 0)){
53
                        return false;
54
                        }
55

    
56
                        try{
57
                    inet = InetAddress.getByName(host);
58
                                ServerSocket socket = new ServerSocket(Integer.parseInt(value), 0, inet);
59
                                if (socket.getLocalPort() > 0){
60
                                        retValue = true;
61
                                }else{
62
                                        retValue = false;
63
                                }
64
                                if (!retValue){
65
                                        break;
66
                                }
67
                            socket.close();
68
            }catch (Exception ex){
69
                    retValue = false;
70
                }
71
                }
72
                return retValue;
73
    }
74

    
75
}