root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / util / PortProcessor.java @ 15280
History | View | Annotate | Download (2.97 KB)
1 | 15280 | rgaitan | /*
|
---|---|---|---|
2 | * Copyright (C) 2004 Thorsten Kamann
|
||
3 | *
|
||
4 | * File : PortProcessor.java
|
||
5 | * Description : Arises the port until it is free
|
||
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 java.net.InetAddress; |
||
27 | import java.net.ServerSocket; |
||
28 | |||
29 | import com.izforge.izpack.panels.ProcessingClient; |
||
30 | import com.izforge.izpack.panels.Processor; |
||
31 | |||
32 | /**
|
||
33 | * Checks whether the value of the field comtemt is a port and is free. If
|
||
34 | * false the next free port will be searched.
|
||
35 | * @author Thorsten Kamann <thorsten.kamann@planetes.de>
|
||
36 | */
|
||
37 | public class PortProcessor implements Processor { |
||
38 | |||
39 | public String process (ProcessingClient client){ |
||
40 | String retValue = ""; |
||
41 | String host = "localhost"; |
||
42 | int port = 0; |
||
43 | int oPort = 0; |
||
44 | boolean found = false; |
||
45 | InetAddress inet = null; |
||
46 | ServerSocket socket = null; |
||
47 | |||
48 | try{
|
||
49 | if (client.getNumFields()>1){ |
||
50 | host = client.getFieldContents(0);
|
||
51 | oPort = Integer.parseInt(client.getFieldContents(1)); |
||
52 | }else{
|
||
53 | oPort = Integer.parseInt(client.getFieldContents(0)); |
||
54 | } |
||
55 | }catch (Exception ex){ |
||
56 | return getReturnValue(client, null, null); |
||
57 | } |
||
58 | |||
59 | port = oPort; |
||
60 | while (!found){
|
||
61 | try{
|
||
62 | inet = InetAddress.getByName(host);
|
||
63 | socket = new ServerSocket(port, 0, inet); |
||
64 | if (socket.getLocalPort() > 0){ |
||
65 | found = true;
|
||
66 | retValue = getReturnValue(client, null, String.valueOf(port)); |
||
67 | }else{
|
||
68 | port++; |
||
69 | } |
||
70 | }catch (java.net.BindException ex){
|
||
71 | port++; |
||
72 | }catch (Exception ex){ |
||
73 | return getReturnValue(client, null, null); |
||
74 | }finally{
|
||
75 | try{
|
||
76 | socket.close(); |
||
77 | }catch (Exception ex){} |
||
78 | } |
||
79 | } |
||
80 | return retValue;
|
||
81 | } |
||
82 | |||
83 | /**
|
||
84 | * Creates the return value
|
||
85 | * @param client The ProcessingClient
|
||
86 | */
|
||
87 | private String getReturnValue(ProcessingClient client, String host, String port){ |
||
88 | String retValue = ""; |
||
89 | String _host = ""; |
||
90 | String _port = ""; |
||
91 | |||
92 | if (client.getNumFields()>1){ |
||
93 | _host = (host == null)? client.getFieldContents(0): host; |
||
94 | _port = (port == null)? client.getFieldContents(1): port; |
||
95 | retValue = _host+"*"+_port;
|
||
96 | }else{
|
||
97 | _port = (port == null)? client.getFieldContents(0): port; |
||
98 | retValue = _port; |
||
99 | } |
||
100 | |||
101 | return retValue;
|
||
102 | } |
||
103 | } |