Coverage Report - org.mule.modules.boot.LibraryDownloader
 
Classes in this File Line Coverage Branch Coverage Complexity
LibraryDownloader
0%
0/91
0%
0/36
2.583
LibraryDownloader$Library
0%
0/5
N/A
2.583
 
 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.ClassUtils;
 14  
 import org.mule.util.FileUtils;
 15  
 import org.mule.util.NumberUtils;
 16  
 import org.mule.util.StringUtils;
 17  
 
 18  
 import java.io.File;
 19  
 import java.io.IOException;
 20  
 import java.net.URL;
 21  
 import java.net.UnknownHostException;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Enumeration;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Properties;
 27  
 
 28  
 import org.apache.commons.httpclient.ConnectTimeoutException;
 29  
 import org.apache.commons.httpclient.HostConfiguration;
 30  
 import org.apache.commons.httpclient.HttpClient;
 31  
 import org.apache.commons.httpclient.HttpMethod;
 32  
 import org.apache.commons.httpclient.HttpState;
 33  
 import org.apache.commons.httpclient.HttpStatus;
 34  
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 35  
 import org.apache.commons.httpclient.auth.AuthScope;
 36  
 import org.apache.commons.httpclient.methods.GetMethod;
 37  
 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
 38  
 
 39  
 public class LibraryDownloader
 40  
 {
 41  
     private static final int HTTP_CONNECTION_TIMEOUT = 10000;
 42  
     private static final String REPO_CENTRAL = "http://repo1.maven.org/maven2";
 43  
     private static final String BOOTSTRAP_LIBRARY_DOWNLOAD_DESCRIPTION_PREFIX = "mule.bootstrap.library.download.description.";
 44  
 
 45  
     private static final String PROXY_ERROR_MESSAGE = "Unable to reach remote repository. This is most likely because you are behind a firewall and missing proxy settings in Mule."
 46  
         + "Proxy options can be passed during startup as follows:\n"
 47  
         + " -M-Dhttp.proxyHost=YOUR_HOST\n"
 48  
         + " -M-Dhttp.proxyPort=YOUR_PORT\n"
 49  
         + " -M-Dhttp.proxyUsername=YOUR_USERNAME\n"
 50  
         + " -M-Dhttp.proxyPassword=YOUR_PASSWORD\n";
 51  
     
 52  
     private MuleBootstrapUtils.ProxyInfo proxyInfo;
 53  
     private HostConfiguration hostConfig;
 54  
     private HttpState httpState;
 55  
 
 56  
     private File muleHome;
 57  0
     private File mavenRepo = null;
 58  
     private HttpClient client;
 59  
     
 60  0
     private List libraryDownloadDescriptions = new ArrayList();
 61  
 
 62  
     public LibraryDownloader(File muleHome)
 63  
     {
 64  0
         this(muleHome, new MuleBootstrapUtils.ProxyInfo(
 65  
             System.getProperty("http.proxyHost"), 
 66  
             System.getProperty("http.proxyPort"), 
 67  
             System.getProperty("http.proxyUsername"), 
 68  
             System.getProperty("http.proxyPassword")));
 69  0
     }
 70  
     
 71  
     public LibraryDownloader(File muleHome, String proxyHost, String proxyPort, String proxyUsername, String proxyPassword)
 72  
     {
 73  0
         this(muleHome, new MuleBootstrapUtils.ProxyInfo(proxyHost, proxyPort, proxyUsername, proxyPassword));
 74  0
     }
 75  
     
 76  
     public LibraryDownloader(File muleHome, MuleBootstrapUtils.ProxyInfo proxyInfo)
 77  0
     {
 78  0
         this.muleHome = muleHome;
 79  0
         this.proxyInfo = proxyInfo;
 80  
 
 81  0
         configureMavenRepository();
 82  0
         configureHttpClient();
 83  0
         configureHttpProxy();
 84  
         
 85  0
         readLibraryDownloadDescriptions();
 86  0
     }
 87  
     
 88  
     public List downloadLibraries() throws IOException
 89  
     {
 90  0
         List libraryUrls = new ArrayList();
 91  0
         Exception proxyException = null;
 92  
         try
 93  
         {
 94  0
             Iterator iter = libraryDownloadDescriptions.iterator();
 95  0
             while (iter.hasNext())
 96  
             {
 97  0
                 URL libraryUrl = downloadLibrary((Library) iter.next());
 98  0
                 if (libraryUrl != null)
 99  
                 {
 100  0
                     libraryUrls.add(libraryUrl);
 101  
                 }
 102  0
             }
 103  
         }
 104  0
         catch (UnknownHostException uhe)
 105  
         {
 106  0
             proxyException = uhe;
 107  
         }
 108  0
         catch (ConnectTimeoutException cte)
 109  
         {
 110  0
             proxyException = cte;
 111  
         }
 112  
         finally
 113  
         {
 114  0
             if (proxyException != null)
 115  
             {
 116  0
                 System.err.println();
 117  0
                 IOException ex = new IOException(PROXY_ERROR_MESSAGE);
 118  0
                 ex.initCause(proxyException);
 119  0
                 throw ex;
 120  
             }
 121  
         }
 122  0
         return libraryUrls;        
 123  
     }
 124  
     
 125  
     private void configureMavenRepository()
 126  
     {
 127  0
         String mavenRepoVar = System.getProperty("m2.repo");
 128  0
         if (!StringUtils.isBlank(mavenRepoVar))
 129  
         {
 130  0
             mavenRepo = new File(mavenRepoVar).getAbsoluteFile();
 131  0
             if (!mavenRepo.exists() || !mavenRepo.isDirectory())
 132  
             {
 133  0
                 mavenRepo = null;
 134  
             }
 135  
         }
 136  0
     }
 137  
 
 138  
     private void configureHttpClient()
 139  
     {
 140  0
         client = new HttpClient();
 141  0
         HttpConnectionManagerParams connParams = new HttpConnectionManagerParams();
 142  0
         connParams.setConnectionTimeout(HTTP_CONNECTION_TIMEOUT);
 143  0
         client.getHttpConnectionManager().setParams(connParams);
 144  0
     }
 145  
     
 146  
     private void configureHttpProxy()
 147  
     {
 148  0
         hostConfig = new HostConfiguration();
 149  0
         if (StringUtils.isNotBlank(proxyInfo.host))
 150  
         {
 151  0
             hostConfig.setProxy(proxyInfo.host, NumberUtils.toInt(proxyInfo.port));
 152  
         }
 153  0
         httpState = new HttpState();
 154  0
         if (StringUtils.isNotBlank(proxyInfo.username))
 155  
         {
 156  0
             httpState.setProxyCredentials(new AuthScope(null, -1, null, null),
 157  
                 new UsernamePasswordCredentials(proxyInfo.username, proxyInfo.password));
 158  
         }
 159  0
     }
 160  
     
 161  
     private void readLibraryDownloadDescriptions()
 162  
     {
 163  0
         Properties properties = System.getProperties();
 164  0
         Enumeration keys = properties.keys();
 165  
         
 166  0
         while (keys.hasMoreElements())
 167  
         {
 168  0
             String key = (String) keys.nextElement();
 169  0
             if (key.startsWith(BOOTSTRAP_LIBRARY_DOWNLOAD_DESCRIPTION_PREFIX))
 170  
             {
 171  0
                 String[] descriptions = properties.getProperty(key).split(",");
 172  0
                 if (descriptions != null && descriptions.length == 3)
 173  
                 {
 174  0
                     libraryDownloadDescriptions.add(new Library(descriptions[0].trim(), descriptions[1].trim(), descriptions[2].trim()));
 175  
                 }
 176  
             }
 177  0
         }
 178  0
     }
 179  
     
 180  
     private URL downloadLibrary(Library library) throws IOException
 181  
     {
 182  0
         URL libraryUrl = null;
 183  0
         if (! ClassUtils.isClassOnPath(library.testClassName, MuleBootstrapUtils.class))
 184  
         {
 185  0
             libraryUrl = downloadLibraryFromLocalRepository(library.jarPath, library.jarName);
 186  0
             if (libraryUrl == null)
 187  
             {
 188  0
                 libraryUrl = downloadLibraryFromRemoteRepository(library.jarPath, library.jarName);
 189  
             }            
 190  
         }
 191  0
         return libraryUrl;
 192  
     }
 193  
 
 194  
     private URL downloadLibraryFromLocalRepository(String path, String destinationFileName) throws IOException
 195  
     {
 196  0
         URL libraryUrl = null;
 197  0
         if (mavenRepo != null)
 198  
         {
 199  0
             File sourceFile = new File(mavenRepo, path + File.separator + destinationFileName).getCanonicalFile();
 200  0
             if (sourceFile.exists())
 201  
             {
 202  0
                 System.out.print("Copying from local repository " + sourceFile.getAbsolutePath() + " ...");
 203  0
                 File destinationFile = new File(new File(muleHome, DefaultMuleClassPathConfig.USER_DIR).getCanonicalFile(), destinationFileName).getCanonicalFile();
 204  0
                 FileUtils.copyFile(sourceFile, destinationFile);
 205  0
                 System.out.println("done");
 206  0
                 libraryUrl = destinationFile.toURL();
 207  
             }
 208  
         }
 209  0
         return libraryUrl;
 210  
     }
 211  
 
 212  
     private URL downloadLibraryFromRemoteRepository(String path, String destinationFileName) throws IOException
 213  
     {
 214  0
         URL libraryUrl = null;
 215  0
         HttpMethod httpMethod = new GetMethod(REPO_CENTRAL + path + '/' + destinationFileName);
 216  
         try
 217  
         {
 218  0
             System.out.print("Downloading " + httpMethod.getURI() + " ...");
 219  0
             client.executeMethod(hostConfig, httpMethod, httpState);
 220  0
             if (httpMethod.getStatusCode() == HttpStatus.SC_OK)
 221  
             {
 222  0
                 File destinationFile = new File(new File(muleHome, DefaultMuleClassPathConfig.USER_DIR), destinationFileName);
 223  0
                 FileUtils.copyStreamToFile(httpMethod.getResponseBodyAsStream(), destinationFile);
 224  0
                 System.out.println("done");
 225  0
                 libraryUrl = destinationFile.toURL();
 226  0
             }
 227  
             else
 228  
             {
 229  0
                 System.out.println();
 230  0
                 throw new IOException("HTTP request failed: " + httpMethod.getStatusLine().toString());
 231  
             }
 232  
         }
 233  
         finally
 234  
         {
 235  0
             httpMethod.releaseConnection();
 236  0
         }
 237  0
         return libraryUrl;
 238  
     }
 239  
     
 240  
     private static class Library
 241  
     {
 242  
         public String testClassName;
 243  
         public String jarPath;
 244  
         public String jarName;
 245  
         
 246  
         public Library(String testClassName, String jarPath, String jarName)
 247  0
         {
 248  0
             this.testClassName = testClassName;
 249  0
             this.jarPath = jarPath;
 250  0
             this.jarName = jarName;
 251  0
         }
 252  
     }
 253  
 }