KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > java15 > ProxyDiagnostics


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.util.java15;
20
21 import org.apache.tools.ant.BuildException;
22
23 import java.net.ProxySelector JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URISyntaxException JavaDoc;
26 import java.net.Proxy JavaDoc;
27 import java.net.SocketAddress JavaDoc;
28 import java.net.InetSocketAddress JavaDoc;
29 import java.net.InetAddress JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /**
34  * This class exists to create a string that tells diagnostics about the current
35  * state of proxy diagnostics.
36  * It does this in its toString operator.
37  * Java1.5+ is needed to compile this class; its interface is classic typeless
38  * Java.
39  * @since Ant 1.7
40  */

41 public class ProxyDiagnostics {
42
43     private String JavaDoc destination;
44
45     private URI JavaDoc destURI;
46
47     /** {@value} */
48     public static final String JavaDoc DEFAULT_DESTINATION = "http://ant.apache.org/";
49
50     /**
51      * create a diagnostics binding for a specific URI
52      * @param destination dest to bind to
53      * @throws BuildException if the URI is malformed.
54      */

55     public ProxyDiagnostics(String JavaDoc destination) {
56         this.destination = destination;
57         try {
58             this.destURI = new URI JavaDoc(destination);
59         } catch (URISyntaxException JavaDoc e) {
60             throw new BuildException(e);
61         }
62     }
63
64     /**
65      * create a proxy diagnostics tool bound to
66      * {@link #DEFAULT_DESTINATION}
67      */

68     public ProxyDiagnostics() {
69         this(DEFAULT_DESTINATION);
70     }
71
72     /**
73      * Get the diagnostics for proxy information.
74      * @return the information.
75      */

76     public String JavaDoc toString() {
77         ProxySelector JavaDoc selector = ProxySelector.getDefault();
78         List JavaDoc list = selector.select(destURI);
79         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
80         Iterator JavaDoc proxies = list.listIterator();
81         while (proxies.hasNext()) {
82             Proxy JavaDoc proxy = (Proxy JavaDoc) proxies.next();
83             SocketAddress JavaDoc 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 JavaDoc) {
89                     InetSocketAddress JavaDoc ina = (InetSocketAddress JavaDoc) 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 JavaDoc 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