KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > PortProcessor


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2004 Thorsten Kamman
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.util;
23
24 import java.net.InetAddress JavaDoc;
25 import java.net.ServerSocket JavaDoc;
26
27 import com.izforge.izpack.panels.ProcessingClient;
28 import com.izforge.izpack.panels.Processor;
29
30 /**
31  * Checks whether the value of the field comtemt is a port and is free. If false the next free port
32  * will be searched.
33  *
34  * @author Thorsten Kamann <thorsten.kamann@planetes.de>
35  */

36 public class PortProcessor implements Processor
37 {
38
39     public String JavaDoc process(ProcessingClient client)
40     {
41         String JavaDoc retValue = "";
42         String JavaDoc host = "localhost";
43         int port = 0;
44         int oPort = 0;
45         boolean found = false;
46         InetAddress JavaDoc inet = null;
47         ServerSocket JavaDoc socket = null;
48
49         try
50         {
51             if (client.getNumFields() > 1)
52             {
53                 host = client.getFieldContents(0);
54                 oPort = Integer.parseInt(client.getFieldContents(1));
55             }
56             else
57             {
58                 oPort = Integer.parseInt(client.getFieldContents(0));
59             }
60         }
61         catch (Exception JavaDoc ex)
62         {
63             return getReturnValue(client, null, null);
64         }
65
66         port = oPort;
67         while (!found)
68         {
69             try
70             {
71                 inet = InetAddress.getByName(host);
72                 socket = new ServerSocket JavaDoc(port, 0, inet);
73                 if (socket.getLocalPort() > 0)
74                 {
75                     found = true;
76                     retValue = getReturnValue(client, null, String.valueOf(port));
77                 }
78                 else
79                 {
80                     port++;
81                 }
82             }
83             catch (java.net.BindException JavaDoc ex)
84             {
85                 port++;
86             }
87             catch (Exception JavaDoc ex)
88             {
89                 return getReturnValue(client, null, null);
90             }
91             finally
92             {
93                 try
94                 {
95                     socket.close();
96                 }
97                 catch (Exception JavaDoc ex)
98                 {}
99             }
100         }
101         return retValue;
102     }
103
104     /**
105      * Creates the return value
106      *
107      * @param client The ProcessingClient
108      */

109     private String JavaDoc getReturnValue(ProcessingClient client, String JavaDoc host, String JavaDoc port)
110     {
111         String JavaDoc retValue = "";
112         String JavaDoc _host = "";
113         String JavaDoc _port = "";
114
115         if (client.getNumFields() > 1)
116         {
117             _host = (host == null) ? client.getFieldContents(0) : host;
118             _port = (port == null) ? client.getFieldContents(1) : port;
119             retValue = _host + "*" + _port;
120         }
121         else
122         {
123             _port = (port == null) ? client.getFieldContents(0) : port;
124             retValue = _port;
125         }
126
127         return retValue;
128     }
129 }
130
Popular Tags