Coverage Report - org.mule.modules.boot.LibraryDownloader
 
Classes in this File Line Coverage Branch Coverage Complexity
LibraryDownloader
0%
0/81
0%
0/12
4.333
 
 1  
 /*
 2  
  * $Id
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.modules.boot;
 12  
 
 13  
 import org.mule.util.FileUtils;
 14  
 import org.mule.util.NumberUtils;
 15  
 import org.mule.util.StringUtils;
 16  
 
 17  
 import java.io.File;
 18  
 import java.io.IOException;
 19  
 import java.net.URL;
 20  
 import java.net.UnknownHostException;
 21  
 import java.util.ArrayList;
 22  
 import java.util.List;
 23  
 
 24  
 import org.apache.commons.httpclient.ConnectTimeoutException;
 25  
 import org.apache.commons.httpclient.HostConfiguration;
 26  
 import org.apache.commons.httpclient.HttpClient;
 27  
 import org.apache.commons.httpclient.HttpMethod;
 28  
 import org.apache.commons.httpclient.HttpState;
 29  
 import org.apache.commons.httpclient.HttpStatus;
 30  
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 31  
 import org.apache.commons.httpclient.auth.AuthScope;
 32  
 import org.apache.commons.httpclient.methods.GetMethod;
 33  
 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
 34  
 
 35  
 public class LibraryDownloader
 36  
 {
 37  
     static final int STARTUP_TIMEOUT = 120000;
 38  
     static final String REPO_CENTRAL = "http://repo1.maven.org/maven2";
 39  
 
 40  0
     static String proxyHostname = System.getProperty("http.proxyHost");
 41  0
     static String proxyPort = System.getProperty("http.proxyPort");
 42  0
     static String proxyUsername = System.getProperty("http.proxyUsername");
 43  0
     static String proxyPassword = System.getProperty("http.proxyPassword");
 44  
 
 45  
     // HTTP proxy configuration
 46  
     private static HostConfiguration hostConfig;
 47  
     private static HttpState httpState;
 48  
 
 49  
     private File muleHome;
 50  0
     private File mavenRepo = null;
 51  
     private HttpClient client;
 52  
 
 53  
     public LibraryDownloader(File muleHome)
 54  0
     {
 55  0
         this.muleHome = muleHome;
 56  
 
 57  0
         String mavenRepoVar = System.getProperty("m2.repo");
 58  0
         if (!StringUtils.isBlank(mavenRepoVar))
 59  
         {
 60  0
             mavenRepo = new File(mavenRepoVar).getAbsoluteFile();
 61  0
             if (!mavenRepo.exists() || !mavenRepo.isDirectory())
 62  
             {
 63  0
                 mavenRepo = null;
 64  
             }
 65  
         }
 66  
 
 67  0
         client = new HttpClient();
 68  
         // Set the connection timeout to 10 seconds.
 69  0
         HttpConnectionManagerParams connParams = new HttpConnectionManagerParams();
 70  0
         connParams.setConnectionTimeout(10000);
 71  0
         client.getHttpConnectionManager().setParams(connParams);
 72  
 
 73  
         // Configure HTTP proxy support if needed.
 74  0
         hostConfig = new HostConfiguration();
 75  0
         if (StringUtils.isNotBlank(proxyHostname))
 76  
         {
 77  0
             hostConfig.setProxy(proxyHostname, NumberUtils.toInt(proxyPort));
 78  
         }
 79  0
         httpState = new HttpState();
 80  0
         if (StringUtils.isNotBlank(proxyUsername))
 81  
         {
 82  0
             httpState.setProxyCredentials(new AuthScope(null, -1, null, null),
 83  
                 new UsernamePasswordCredentials(proxyUsername, proxyPassword));
 84  
         }
 85  0
     }
 86  
     
 87  
     public LibraryDownloader(File muleHome, String proxyHostname, String proxyPort, String proxyUsername, String proxyPassword)
 88  0
     {
 89  0
         this.muleHome = muleHome;
 90  
 
 91  0
         String mavenRepoVar = System.getProperty("m2.repo");
 92  0
         if (!StringUtils.isBlank(mavenRepoVar))
 93  
         {
 94  0
             mavenRepo = new File(mavenRepoVar).getAbsoluteFile();
 95  0
             if (!mavenRepo.exists() || !mavenRepo.isDirectory())
 96  
             {
 97  0
                 mavenRepo = null;
 98  
             }
 99  
         }
 100  
 
 101  0
         client = new HttpClient();
 102  
         // Set the connection timeout to 10 seconds.
 103  0
         HttpConnectionManagerParams connParams = new HttpConnectionManagerParams();
 104  0
         connParams.setConnectionTimeout(10000);
 105  0
         client.getHttpConnectionManager().setParams(connParams);
 106  
 
 107  
         // Configure HTTP proxy support if needed.
 108  0
         hostConfig = new HostConfiguration();
 109  0
         if (StringUtils.isNotBlank(proxyHostname))
 110  
         {
 111  0
             hostConfig.setProxy(proxyHostname, NumberUtils.toInt(proxyPort));
 112  
         }
 113  0
         httpState = new HttpState();
 114  0
         if (StringUtils.isNotBlank(proxyUsername))
 115  
         {
 116  0
             httpState.setProxyCredentials(new AuthScope(null, -1, null, null),
 117  
                 new UsernamePasswordCredentials(proxyUsername, proxyPassword));
 118  
         }
 119  0
     }
 120  
 
 121  
     public List downloadLibraries() throws IOException
 122  
     {
 123  0
         List libraries = new ArrayList();
 124  
         try
 125  
         {
 126  0
             libraries.add(getLibrary(REPO_CENTRAL, "/javax/activation/activation/1.1/activation-1.1.jar",
 127  
                 "activation-1.1.jar"));
 128  0
             libraries.add(getLibrary(REPO_CENTRAL, "/javax/mail/mail/1.4/mail-1.4.jar", "mail-1.4.jar"));
 129  0
             return libraries;
 130  
         }
 131  0
         catch (UnknownHostException uhe)
 132  
         {
 133  0
             System.err.println();
 134  0
             IOException ex = new IOException(
 135  
                 "Unable to reach a remote repository, this is most likely because you are behind a firewall and have not configured your HTTP proxy settings in $MULE_HOME/conf/wrapper.conf.");
 136  0
             ex.initCause(uhe);
 137  0
             throw ex;
 138  
         }
 139  0
         catch (ConnectTimeoutException e)
 140  
         {
 141  0
             System.err.println();
 142  0
             IOException ex = new IOException(
 143  
                 "Unable to reach a remote repository, this is most likely because you are behind a firewall and have not configured your HTTP proxy settings in $MULE_HOME/conf/wrapper.conf.");
 144  0
             ex.initCause(e);
 145  0
             throw ex;
 146  
         }
 147  
     }
 148  
 
 149  
     private URL getLibrary(String repository, String path, String destinationFileName) throws IOException
 150  
     {
 151  0
         URL url = null;
 152  0
         if (mavenRepo != null)
 153  
         {
 154  0
             url = copyLibrary(path, destinationFileName);
 155  
         }
 156  0
         if (url == null)
 157  
         {
 158  0
             url = downloadLibrary(repository, path, destinationFileName);
 159  
         }
 160  0
         return url;
 161  
     }
 162  
 
 163  
     private URL copyLibrary(String path, String destinationFileName) throws IOException
 164  
     {
 165  0
         File sourceFile = new File(mavenRepo, path).getCanonicalFile();
 166  0
         if (sourceFile.exists())
 167  
         {
 168  0
             System.out.print("Copying from local repository " + sourceFile.getAbsolutePath() + " ...");
 169  0
             File destinationFile = new File(new File(muleHome, DefaultMuleClassPathConfig.USER_DIR)
 170  
                 .getCanonicalFile(), destinationFileName).getCanonicalFile();
 171  0
             FileUtils.copyFile(sourceFile, destinationFile);
 172  0
             System.out.println("done");
 173  0
             return destinationFile.toURL();
 174  
         }
 175  
         else
 176  0
             return null;
 177  
     }
 178  
 
 179  
     private URL downloadLibrary(String repository, String path, String destinationFileName)
 180  
         throws IOException
 181  
     {
 182  0
         String url = repository + path;
 183  0
         HttpMethod httpMethod = new GetMethod(url);
 184  
         try
 185  
         {
 186  0
             System.out.print("Downloading " + url + " ...");
 187  0
             client.executeMethod(hostConfig, httpMethod, httpState);
 188  0
             if (httpMethod.getStatusCode() == HttpStatus.SC_OK)
 189  
             {
 190  0
                 File destinationFile = new File(new File(muleHome, DefaultMuleClassPathConfig.USER_DIR),
 191  
                     destinationFileName);
 192  0
                 FileUtils.copyStreamToFile(httpMethod.getResponseBodyAsStream(), destinationFile);
 193  0
                 System.out.println("done");
 194  0
                 return destinationFile.toURL();
 195  
             }
 196  
             else
 197  
             {
 198  0
                 System.out.println();
 199  0
                 throw new IOException("HTTP request failed: " + httpMethod.getStatusLine().toString());
 200  
             }
 201  
         }
 202  
         finally
 203  
         {
 204  0
             httpMethod.releaseConnection();
 205  
         }
 206  
     }
 207  
 }