1 18 19 package org.apache.tools.ant.util.java15; 20 21 import org.apache.tools.ant.BuildException; 22 23 import java.net.ProxySelector ; 24 import java.net.URI ; 25 import java.net.URISyntaxException ; 26 import java.net.Proxy ; 27 import java.net.SocketAddress ; 28 import java.net.InetSocketAddress ; 29 import java.net.InetAddress ; 30 import java.util.List ; 31 import java.util.Iterator ; 32 33 41 public class ProxyDiagnostics { 42 43 private String destination; 44 45 private URI destURI; 46 47 48 public static final String DEFAULT_DESTINATION = "http://ant.apache.org/"; 49 50 55 public ProxyDiagnostics(String destination) { 56 this.destination = destination; 57 try { 58 this.destURI = new URI (destination); 59 } catch (URISyntaxException e) { 60 throw new BuildException(e); 61 } 62 } 63 64 68 public ProxyDiagnostics() { 69 this(DEFAULT_DESTINATION); 70 } 71 72 76 public String toString() { 77 ProxySelector selector = ProxySelector.getDefault(); 78 List list = selector.select(destURI); 79 StringBuffer result = new StringBuffer (); 80 Iterator proxies = list.listIterator(); 81 while (proxies.hasNext()) { 82 Proxy proxy = (Proxy ) proxies.next(); 83 SocketAddress address = proxy.address(); 84 if (address == null) { 85 result.append("Direct connection\n"); 86 } else { 87 result.append(proxy.toString()); 88 if (address instanceof InetSocketAddress ) { 89 InetSocketAddress ina = (InetSocketAddress ) address; 90 result.append(' '); 91 result.append(ina.getHostName()); 92 result.append(':'); 93 result.append(ina.getPort()); 94 if (ina.isUnresolved()) { 95 result.append(" [unresolved]"); 96 } else { 97 InetAddress addr = ina.getAddress(); 98 result.append(" ["); 99 result.append(addr.getHostAddress()); 100 result.append(']'); 101 } 102 } 103 result.append('\n'); 104 } 105 } 106 return result.toString(); 107 } 108 109 110 111 } 112 | Popular Tags |