KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > ProxySetup


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;
20
21 import org.apache.tools.ant.Project;
22
23 /**
24  * Code to do proxy setup. This is just factored out of the main system just to
25  * keep everything else less convoluted.
26  * @since Ant1.7
27  */

28
29 public class ProxySetup {
30
31     /**
32      * owner project; used for logging and extracting properties
33      */

34     private Project owner;
35
36     /**
37      * Java1.5 property that enables use of system proxies.
38      * @value
39      */

40     public static final String JavaDoc USE_SYSTEM_PROXIES = "java.net.useSystemProxies";
41     /** the http proxyhost property */
42     public static final String JavaDoc HTTP_PROXY_HOST = "http.proxyHost";
43     /** the http proxyport property */
44     public static final String JavaDoc HTTP_PROXY_PORT = "http.proxyPort";
45     /** the https proxyhost property */
46     public static final String JavaDoc HTTPS_PROXY_HOST = "https.proxyHost";
47     /** the https proxyport property */
48     public static final String JavaDoc HTTPS_PROXY_PORT = "https.proxyPort";
49     /** the ftp proxyhost property */
50     public static final String JavaDoc FTP_PROXY_HOST = "ftp.proxyHost";
51     /** the ftp proxyport property */
52     public static final String JavaDoc FTP_PROXY_PORT = "ftp.proxyPort";
53     /** the ftp proxyport property */
54     public static final String JavaDoc HTTP_NON_PROXY_HOSTS = "http.nonProxyHosts";
55     /** the http hosts not to be proxied property */
56     public static final String JavaDoc HTTPS_NON_PROXY_HOSTS = "https.nonProxyHosts";
57     /** the ftp hosts not to be proxied property */
58     public static final String JavaDoc FTP_NON_PROXY_HOSTS = "ftp.nonProxyHosts";
59     /** the http proxy username property */
60     public static final String JavaDoc HTTP_PROXY_USERNAME = "http.proxyUser";
61     /** the http proxy password property */
62     public static final String JavaDoc HTTP_PROXY_PASSWORD = "http.proxyPassword";
63     /** the socks proxy host property */
64     public static final String JavaDoc SOCKS_PROXY_HOST = "socksProxyHost";
65     /** the socks proxy port property */
66     public static final String JavaDoc SOCKS_PROXY_PORT = "socksProxyPort";
67     /** the socks proxy username property */
68     public static final String JavaDoc SOCKS_PROXY_USERNAME = "java.net.socks.username";
69     /** the socks proxy password property */
70     public static final String JavaDoc SOCKS_PROXY_PASSWORD = "java.net.socks.password";
71
72
73     /**
74      * create a proxy setup class bound to this project
75      * @param owner the project that owns this setup.
76      */

77     public ProxySetup(Project owner) {
78         this.owner = owner;
79     }
80
81     /**
82      * Get the current system property settings
83      * @return current value; null for none or no access
84      */

85     public static String JavaDoc getSystemProxySetting() {
86         try {
87             return System.getProperty(USE_SYSTEM_PROXIES);
88         } catch (SecurityException JavaDoc e) {
89             //if you cannot read it, you won't be able to write it either
90
return null;
91         }
92     }
93
94     /**
95      * turn proxies on;
96      * if the proxy key is already set to some value: leave alone.
97      * if an ant property of the value {@link #USE_SYSTEM_PROXIES}
98      * is set, use that instead. Else set to "true".
99      */

100     public void enableProxies() {
101         if (!(getSystemProxySetting() != null)) {
102             String JavaDoc proxies = owner.getProperty(USE_SYSTEM_PROXIES);
103             if (proxies == null || Project.toBoolean(proxies)) {
104                 proxies = "true";
105             }
106             String JavaDoc message = "setting " + USE_SYSTEM_PROXIES + " to " + proxies;
107             try {
108                 owner.log(message, Project.MSG_DEBUG);
109                 System.setProperty(USE_SYSTEM_PROXIES, proxies);
110             } catch (SecurityException JavaDoc e) {
111                 //log security exceptions and continue; it aint that
112
//important and may be quite common running Ant embedded.
113
owner.log("Security Exception when " + message);
114             }
115         }
116     }
117
118 }
119
Popular Tags