001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2004 Thorsten Kamman
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.izforge.izpack.util;
023:
024: import java.net.InetAddress;
025: import java.net.ServerSocket;
026:
027: import com.izforge.izpack.panels.ProcessingClient;
028: import com.izforge.izpack.panels.Processor;
029:
030: /**
031: * Checks whether the value of the field comtemt is a port and is free. If false the next free port
032: * will be searched.
033: *
034: * @author Thorsten Kamann <thorsten.kamann@planetes.de>
035: */
036: public class PortProcessor implements Processor {
037:
038: public String process(ProcessingClient client) {
039: String retValue = "";
040: String host = "localhost";
041: int port = 0;
042: int oPort = 0;
043: boolean found = false;
044: InetAddress inet = null;
045: ServerSocket socket = null;
046:
047: try {
048: if (client.getNumFields() > 1) {
049: host = client.getFieldContents(0);
050: oPort = Integer.parseInt(client.getFieldContents(1));
051: } else {
052: oPort = Integer.parseInt(client.getFieldContents(0));
053: }
054: } catch (Exception ex) {
055: return getReturnValue(client, null, null);
056: }
057:
058: port = oPort;
059: while (!found) {
060: try {
061: inet = InetAddress.getByName(host);
062: socket = new ServerSocket(port, 0, inet);
063: if (socket.getLocalPort() > 0) {
064: found = true;
065: retValue = getReturnValue(client, null, String
066: .valueOf(port));
067: } else {
068: port++;
069: }
070: } catch (java.net.BindException ex) {
071: port++;
072: } catch (Exception ex) {
073: return getReturnValue(client, null, null);
074: } finally {
075: try {
076: socket.close();
077: } catch (Exception ex) {
078: }
079: }
080: }
081: return retValue;
082: }
083:
084: /**
085: * Creates the return value
086: *
087: * @param client The ProcessingClient
088: */
089: private String getReturnValue(ProcessingClient client, String host,
090: String port) {
091: String retValue = "";
092: String _host = "";
093: String _port = "";
094:
095: if (client.getNumFields() > 1) {
096: _host = (host == null) ? client.getFieldContents(0) : host;
097: _port = (port == null) ? client.getFieldContents(1) : port;
098: retValue = _host + "*" + _port;
099: } else {
100: _port = (port == null) ? client.getFieldContents(0) : port;
101: retValue = _port;
102: }
103:
104: return retValue;
105: }
106: }
|