001/* 002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved. 003 * 004 * http://www.izforge.com/izpack/ 005 * http://developer.berlios.de/projects/izpack/ 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 022package com.izforge.izpack.util; 023 024import java.net.InetAddress; 025import java.net.ServerSocket; 026 027import com.izforge.izpack.panels.ProcessingClient; 028import 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 */ 036public class PortProcessor implements Processor 037{ 038 039 public String process(ProcessingClient client) 040 { 041 String retValue = ""; 042 String host = "localhost"; 043 int port = 0; 044 int oPort = 0; 045 boolean found = false; 046 InetAddress inet = null; 047 ServerSocket socket = null; 048 049 try 050 { 051 if (client.getNumFields() > 1) 052 { 053 host = client.getFieldContents(0); 054 oPort = Integer.parseInt(client.getFieldContents(1)); 055 } 056 else 057 { 058 oPort = Integer.parseInt(client.getFieldContents(0)); 059 } 060 } 061 catch (Exception ex) 062 { 063 return getReturnValue(client, null, null); 064 } 065 066 port = oPort; 067 while (!found) 068 { 069 try 070 { 071 inet = InetAddress.getByName(host); 072 socket = new ServerSocket(port, 0, inet); 073 if (socket.getLocalPort() > 0) 074 { 075 found = true; 076 retValue = getReturnValue(client, null, String.valueOf(port)); 077 } 078 else 079 { 080 port++; 081 } 082 } 083 catch (java.net.BindException ex) 084 { 085 port++; 086 } 087 catch (Exception ex) 088 { 089 return getReturnValue(client, null, null); 090 } 091 finally 092 { 093 try 094 { 095 socket.close(); 096 } 097 catch (Exception ex) 098 {} 099 } 100 } 101 return retValue; 102 } 103 104 /** 105 * Creates the return value 106 * 107 * @param client The ProcessingClient 108 */ 109 private String getReturnValue(ProcessingClient client, String host, String port) 110 { 111 String retValue = ""; 112 String _host = ""; 113 String _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}