| 1 21 22 package com.izforge.izpack.util; 23 24 import java.net.InetAddress ; 25 import java.net.ServerSocket ; 26 27 import com.izforge.izpack.panels.ProcessingClient; 28 import com.izforge.izpack.panels.Processor; 29 30 36 public class PortProcessor implements Processor 37 { 38 39 public String process(ProcessingClient client) 40 { 41 String retValue = ""; 42 String host = "localhost"; 43 int port = 0; 44 int oPort = 0; 45 boolean found = false; 46 InetAddress inet = null; 47 ServerSocket 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 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 (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 ex) 84 { 85 port++; 86 } 87 catch (Exception ex) 88 { 89 return getReturnValue(client, null, null); 90 } 91 finally 92 { 93 try 94 { 95 socket.close(); 96 } 97 catch (Exception ex) 98 {} 99 } 100 } 101 return retValue; 102 } 103 104 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 } 130 | Popular Tags |