Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 332   Methods: 11
NCLOC: 128   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SetProxy.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  * notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  * notice, this list of conditions and the following disclaimer in
 16   
  * the documentation and/or other materials provided with the
 17   
  * distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  * any, must include the following acknowlegement:
 21   
  * "This product includes software developed by the
 22   
  * Apache Software Foundation (http://www.apache.org/)."
 23   
  * Alternately, this acknowlegement may appear in the software itself,
 24   
  * if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  * Foundation" must not be used to endorse or promote products derived
 28   
  * from this software without prior written permission. For written
 29   
  * permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  * nor may "Apache" appear in their names without prior written
 33   
  * permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.optional.net;
 56   
 
 57   
 import java.lang.reflect.InvocationTargetException;
 58   
 import java.lang.reflect.Method;
 59   
 import java.util.Properties;
 60   
 import org.apache.tools.ant.BuildException;
 61   
 import org.apache.tools.ant.Project;
 62   
 import org.apache.tools.ant.Task;
 63   
 import org.apache.tools.ant.util.JavaEnvUtils;
 64   
 
 65   
 /**
 66   
  * Sets Java's web proxy properties, so that tasks and code run in
 67   
  * the same JVM can have through-the-firewall access to remote web sites,
 68   
  * and remote ftp sites.
 69   
  * You can nominate an http and ftp proxy, or a socks server, reset the server
 70   
  * settings, or do nothing at all.
 71   
  * <p>
 72   
  * Examples
 73   
  * <pre>&lt;setproxy/&gt;</pre>
 74   
  * do nothing
 75   
  * <pre>&lt;setproxy proxyhost="firewall"/&gt;</pre>
 76   
  * set the proxy to firewall:80
 77   
  * <pre>&lt;setproxy proxyhost="firewall" proxyport="81"/&gt;</pre>
 78   
  * set the proxy to firewall:81
 79   
  * <pre>&lt;setproxy proxyhost=""/&gt;</pre>
 80   
  * stop using the http proxy; don't change the socks settings
 81   
  * <pre>&lt;setproxy socksproxyhost="socksy"/&gt;</pre>
 82   
  * use socks via socksy:1080
 83   
  * <pre>&lt;setproxy socksproxyhost=""/&gt;</pre>
 84   
  * stop using the socks server
 85   
  * You can set a username and password for http with the <tt>proxyHost</tt>
 86   
  * and <tt>proxyPassword</tt> attributes. On Java1.4 and above these can also be
 87   
  * used against SOCKS5 servers.
 88   
 
 89   
 
 90   
  * @see <a href="http://java.sun.com/j2se/1.4/docs/guide/net/properties.html">
 91   
  *  java 1.4 network property list</a>
 92   
  * @author Steve Loughran
 93   
   *@since       Ant 1.5
 94   
  * @ant.task category="network"
 95   
  */
 96   
 public class SetProxy extends Task {
 97   
 
 98   
     /**
 99   
      * proxy details
 100   
      */
 101   
     protected String proxyHost = null;
 102   
 
 103   
     /**
 104   
      * name of proxy port
 105   
      */
 106   
     protected int proxyPort = 80;
 107   
 
 108   
     /**
 109   
      * socks host.
 110   
      */
 111   
     private String socksProxyHost = null;
 112   
 
 113   
     /**
 114   
      * Socks proxy port. Default is 1080.
 115   
      */
 116   
     private int socksProxyPort = 1080;
 117   
 
 118   
 
 119   
     /**
 120   
      * list of non proxy hosts
 121   
      */
 122   
     private String nonProxyHosts = null;
 123   
 
 124   
     /**
 125   
      * user for http only
 126   
      */
 127   
     private String proxyUser=null;
 128   
 
 129   
     /**
 130   
      * password for http only
 131   
      */
 132   
     private String proxyPassword=null;
 133   
 
 134   
     /**
 135   
      * the HTTP/ftp proxy host. Set this to "" for the http proxy
 136   
      * option to be disabled
 137   
      *
 138   
      * @param hostname the new proxy hostname
 139   
      */
 140  0
     public void setProxyHost(String hostname) {
 141  0
         proxyHost = hostname;
 142   
     }
 143   
 
 144   
 
 145   
     /**
 146   
      * the HTTP/ftp proxy port number; default is 80
 147   
      *
 148   
      * @param port port number of the proxy
 149   
      */
 150  0
     public void setProxyPort(int port) {
 151  0
         proxyPort = port;
 152   
     }
 153   
 
 154   
     /**
 155   
      * The name of a Socks server. Set to "" to turn socks
 156   
      * proxying off.
 157   
      *
 158   
      * @param host The new SocksProxyHost value
 159   
      */
 160  0
     public void setSocksProxyHost(String host) {
 161  0
         this.socksProxyHost = host;
 162   
     }
 163   
 
 164   
 
 165   
     /**
 166   
      * Set the ProxyPort for socks connections. The default value is 1080
 167   
      *
 168   
      * @param port The new SocksProxyPort value
 169   
      */
 170  0
     public void setSocksProxyPort(int port) {
 171  0
         this.socksProxyPort = port;
 172   
     }
 173   
 
 174   
 
 175   
     /**
 176   
      * A list of hosts to bypass the proxy on. These should be separated
 177   
      * with the vertical bar character '|'. Only in Java 1.4 does ftp use
 178   
      * this list.
 179   
      * e.g. fozbot.corp.sun.com|*.eng.sun.com
 180   
      * @param nonProxyHosts lists of hosts to talk direct to
 181   
      */
 182  0
     public void setNonProxyHosts(String nonProxyHosts) {
 183  0
         this.nonProxyHosts = nonProxyHosts;
 184   
     }
 185   
 
 186   
     /**
 187   
      * set the proxy user. Probably requires a password to accompany this
 188   
      * setting. Default=""
 189   
      * @param proxyUser username
 190   
      * @since Ant1.6
 191   
      */
 192  0
     public void setProxyUser(String proxyUser) {
 193  0
         this.proxyUser = proxyUser;
 194   
     }
 195   
 
 196   
     /**
 197   
      * set the password for the proxy. used only if the proxyUser is set
 198   
      * @param proxyPassword password to go with the username
 199   
      * @since Ant1.6
 200   
      */
 201  0
     public void setProxyPassword(String proxyPassword) {
 202  0
         this.proxyPassword = proxyPassword;
 203   
     }
 204   
 
 205   
     /**
 206   
      * if the proxy port and host settings are not null, then the settings
 207   
      * get applied these settings last beyond the life of the object and
 208   
      * apply to all network connections
 209   
      * Relevant docs: buglist #4183340
 210   
      */
 211   
 
 212  0
     public void applyWebProxySettings() {
 213  0
         boolean settingsChanged = false;
 214  0
         boolean enablingProxy = false;
 215  0
         Properties sysprops = System.getProperties();
 216  0
         if (proxyHost != null) {
 217  0
             settingsChanged = true;
 218  0
             if (proxyHost.length() != 0) {
 219  0
                 traceSettingInfo();
 220  0
                 enablingProxy = true;
 221  0
                 sysprops.put("http.proxyHost", proxyHost);
 222  0
                 String portString = Integer.toString(proxyPort);
 223  0
                 sysprops.put("http.proxyPort", portString);
 224  0
                 sysprops.put("https.proxyHost", proxyHost);
 225  0
                 sysprops.put("https.proxyPort", portString);
 226  0
                 sysprops.put("ftp.proxyHost", proxyHost);
 227  0
                 sysprops.put("ftp.proxyPort", portString);
 228  0
                 if (nonProxyHosts != null) {
 229  0
                     sysprops.put("http.nonProxyHosts", nonProxyHosts);
 230  0
                     sysprops.put("https.nonProxyHosts", nonProxyHosts);
 231  0
                     sysprops.put("ftp.nonProxyHosts", nonProxyHosts);
 232   
                 }
 233  0
                 if(proxyUser!=null) {
 234  0
                     sysprops.put("http.proxyUser", proxyUser);
 235  0
                     sysprops.put("http.proxyPassword", proxyPassword);
 236   
                 }
 237   
             } else {
 238  0
                 log("resetting http proxy", Project.MSG_VERBOSE);
 239  0
                 sysprops.remove("http.proxyHost");
 240  0
                 sysprops.remove("http.proxyPort");
 241  0
                 sysprops.remove("http.proxyUser");
 242  0
                 sysprops.remove("http.proxyPassword");
 243  0
                 sysprops.remove("https.proxyHost");
 244  0
                 sysprops.remove("https.proxyPort");
 245  0
                 sysprops.remove("ftp.proxyHost");
 246  0
                 sysprops.remove("ftp.proxyPort");
 247   
             }
 248   
         }
 249   
 
 250   
         //socks
 251  0
         if (socksProxyHost != null) {
 252  0
             settingsChanged = true;
 253  0
             if (socksProxyHost.length() != 0) {
 254  0
                 enablingProxy = true;
 255  0
                 sysprops.put("socksProxyHost", socksProxyHost);
 256  0
                 sysprops.put("socksProxyPort", Integer.toString(socksProxyPort));
 257  0
                 if (proxyUser != null) {
 258   
                     //this may be a java1.4 thingy only
 259  0
                     sysprops.put("java.net.socks.username", proxyUser);
 260  0
                     sysprops.put("java.net.socks.password", proxyPassword);
 261   
                 }
 262   
 
 263   
             } else {
 264  0
                 log("resetting socks proxy", Project.MSG_VERBOSE);
 265  0
                 sysprops.remove("socksProxyHost");
 266  0
                 sysprops.remove("socksProxyPort");
 267  0
                 sysprops.remove("java.net.socks.username");
 268  0
                 sysprops.remove("java.net.socks.password");
 269   
             }
 270   
         }
 271   
 
 272   
 
 273   
         //for Java1.1 we need to tell the system that the settings are new
 274  0
         if (settingsChanged &&
 275   
             JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
 276  0
             legacyResetProxySettingsCall(enablingProxy);
 277   
         }
 278   
     }
 279   
 
 280   
     /**
 281   
      * list out what is going on
 282   
      */
 283  0
     private void traceSettingInfo() {
 284  0
         log("Setting proxy to "
 285   
                 + (proxyHost != null ? proxyHost : "''")
 286   
                 + ":" + proxyPort,
 287   
                 Project.MSG_VERBOSE);
 288   
     }
 289   
 
 290   
 
 291   
     /**
 292   
      * make a call to sun.net.www.http.HttpClient.resetProperties();
 293   
      * this is only needed for java 1.1; reflection is used to stop the compiler
 294   
      * whining, and in case cleanroom JVMs dont have the class.
 295   
      * @return true if we did something
 296   
      */
 297   
 
 298  0
     protected boolean legacyResetProxySettingsCall(boolean setProxy) {
 299  0
         System.getProperties().put("http.proxySet", new Boolean(setProxy).toString());
 300  0
         try {
 301  0
             Class c = Class.forName("sun.net.www.http.HttpClient");
 302  0
             Method reset = c.getMethod("resetProperties", null);
 303  0
             reset.invoke(null, null);
 304  0
             return true;
 305   
         }
 306   
         catch (ClassNotFoundException cnfe) {
 307  0
             return false;
 308   
         }
 309   
         catch (NoSuchMethodException e) {
 310  0
             return false;
 311   
         }
 312   
         catch (IllegalAccessException e) {
 313  0
             return false;
 314   
         }
 315   
         catch (InvocationTargetException e) {
 316  0
             return false;
 317   
         }
 318   
     }
 319   
 
 320   
 
 321   
     /**
 322   
      * Does the work.
 323   
      *
 324   
      * @exception BuildException thrown in unrecoverable error.
 325   
      */
 326  0
     public void execute() throws BuildException {
 327  0
         applyWebProxySettings();
 328   
     }
 329   
 
 330   
 }
 331   
 
 332