001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.util.java15;
020:
021: import org.apache.tools.ant.BuildException;
022:
023: import java.net.ProxySelector;
024: import java.net.URI;
025: import java.net.URISyntaxException;
026: import java.net.Proxy;
027: import java.net.SocketAddress;
028: import java.net.InetSocketAddress;
029: import java.net.InetAddress;
030: import java.util.List;
031: import java.util.Iterator;
032:
033: /**
034: * This class exists to create a string that tells diagnostics about the current
035: * state of proxy diagnostics.
036: * It does this in its toString operator.
037: * Java1.5+ is needed to compile this class; its interface is classic typeless
038: * Java.
039: * @since Ant 1.7
040: */
041: public class ProxyDiagnostics {
042:
043: private String destination;
044:
045: private URI destURI;
046:
047: /** {@value} */
048: public static final String DEFAULT_DESTINATION = "http://ant.apache.org/";
049:
050: /**
051: * create a diagnostics binding for a specific URI
052: * @param destination dest to bind to
053: * @throws BuildException if the URI is malformed.
054: */
055: public ProxyDiagnostics(String destination) {
056: this .destination = destination;
057: try {
058: this .destURI = new URI(destination);
059: } catch (URISyntaxException e) {
060: throw new BuildException(e);
061: }
062: }
063:
064: /**
065: * create a proxy diagnostics tool bound to
066: * {@link #DEFAULT_DESTINATION}
067: */
068: public ProxyDiagnostics() {
069: this (DEFAULT_DESTINATION);
070: }
071:
072: /**
073: * Get the diagnostics for proxy information.
074: * @return the information.
075: */
076: public String toString() {
077: ProxySelector selector = ProxySelector.getDefault();
078: List list = selector.select(destURI);
079: StringBuffer result = new StringBuffer();
080: Iterator proxies = list.listIterator();
081: while (proxies.hasNext()) {
082: Proxy proxy = (Proxy) proxies.next();
083: SocketAddress address = proxy.address();
084: if (address == null) {
085: result.append("Direct connection\n");
086: } else {
087: result.append(proxy.toString());
088: if (address instanceof InetSocketAddress) {
089: InetSocketAddress ina = (InetSocketAddress) address;
090: result.append(' ');
091: result.append(ina.getHostName());
092: result.append(':');
093: result.append(ina.getPort());
094: if (ina.isUnresolved()) {
095: result.append(" [unresolved]");
096: } else {
097: InetAddress addr = ina.getAddress();
098: result.append(" [");
099: result.append(addr.getHostAddress());
100: result.append(']');
101: }
102: }
103: result.append('\n');
104: }
105: }
106: return result.toString();
107: }
108:
109: }
|