View Javadoc

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      static String proxyHostname = System.getProperty("http.proxyHost");
41      static String proxyPort = System.getProperty("http.proxyPort");
42      static String proxyUsername = System.getProperty("http.proxyUsername");
43      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      private File mavenRepo = null;
51      private HttpClient client;
52  
53      public LibraryDownloader(File muleHome)
54      {
55          this.muleHome = muleHome;
56  
57          String mavenRepoVar = System.getProperty("m2.repo");
58          if (!StringUtils.isBlank(mavenRepoVar))
59          {
60              mavenRepo = new File(mavenRepoVar).getAbsoluteFile();
61              if (!mavenRepo.exists() || !mavenRepo.isDirectory())
62              {
63                  mavenRepo = null;
64              }
65          }
66  
67          client = new HttpClient();
68          // Set the connection timeout to 10 seconds.
69          HttpConnectionManagerParams connParams = new HttpConnectionManagerParams();
70          connParams.setConnectionTimeout(10000);
71          client.getHttpConnectionManager().setParams(connParams);
72  
73          // Configure HTTP proxy support if needed.
74          hostConfig = new HostConfiguration();
75          if (StringUtils.isNotBlank(proxyHostname))
76          {
77              hostConfig.setProxy(proxyHostname, NumberUtils.toInt(proxyPort));
78          }
79          httpState = new HttpState();
80          if (StringUtils.isNotBlank(proxyUsername))
81          {
82              httpState.setProxyCredentials(new AuthScope(null, -1, null, null),
83                  new UsernamePasswordCredentials(proxyUsername, proxyPassword));
84          }
85      }
86      
87      public LibraryDownloader(File muleHome, String proxyHostname, String proxyPort, String proxyUsername, String proxyPassword)
88      {
89          this.muleHome = muleHome;
90  
91          String mavenRepoVar = System.getProperty("m2.repo");
92          if (!StringUtils.isBlank(mavenRepoVar))
93          {
94              mavenRepo = new File(mavenRepoVar).getAbsoluteFile();
95              if (!mavenRepo.exists() || !mavenRepo.isDirectory())
96              {
97                  mavenRepo = null;
98              }
99          }
100 
101         client = new HttpClient();
102         // Set the connection timeout to 10 seconds.
103         HttpConnectionManagerParams connParams = new HttpConnectionManagerParams();
104         connParams.setConnectionTimeout(10000);
105         client.getHttpConnectionManager().setParams(connParams);
106 
107         // Configure HTTP proxy support if needed.
108         hostConfig = new HostConfiguration();
109         if (StringUtils.isNotBlank(proxyHostname))
110         {
111             hostConfig.setProxy(proxyHostname, NumberUtils.toInt(proxyPort));
112         }
113         httpState = new HttpState();
114         if (StringUtils.isNotBlank(proxyUsername))
115         {
116             httpState.setProxyCredentials(new AuthScope(null, -1, null, null),
117                 new UsernamePasswordCredentials(proxyUsername, proxyPassword));
118         }
119     }
120 
121     public List downloadLibraries() throws IOException
122     {
123         List libraries = new ArrayList();
124         try
125         {
126             libraries.add(getLibrary(REPO_CENTRAL, "/javax/activation/activation/1.1/activation-1.1.jar",
127                 "activation-1.1.jar"));
128             libraries.add(getLibrary(REPO_CENTRAL, "/javax/mail/mail/1.4/mail-1.4.jar", "mail-1.4.jar"));
129             return libraries;
130         }
131         catch (UnknownHostException uhe)
132         {
133             System.err.println();
134             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             ex.initCause(uhe);
137             throw ex;
138         }
139         catch (ConnectTimeoutException e)
140         {
141             System.err.println();
142             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             ex.initCause(e);
145             throw ex;
146         }
147     }
148 
149     private URL getLibrary(String repository, String path, String destinationFileName) throws IOException
150     {
151         URL url = null;
152         if (mavenRepo != null)
153         {
154             url = copyLibrary(path, destinationFileName);
155         }
156         if (url == null)
157         {
158             url = downloadLibrary(repository, path, destinationFileName);
159         }
160         return url;
161     }
162 
163     private URL copyLibrary(String path, String destinationFileName) throws IOException
164     {
165         File sourceFile = new File(mavenRepo, path).getCanonicalFile();
166         if (sourceFile.exists())
167         {
168             System.out.print("Copying from local repository " + sourceFile.getAbsolutePath() + " ...");
169             File destinationFile = new File(new File(muleHome, DefaultMuleClassPathConfig.USER_DIR)
170                 .getCanonicalFile(), destinationFileName).getCanonicalFile();
171             FileUtils.copyFile(sourceFile, destinationFile);
172             System.out.println("done");
173             return destinationFile.toURL();
174         }
175         else
176             return null;
177     }
178 
179     private URL downloadLibrary(String repository, String path, String destinationFileName)
180         throws IOException
181     {
182         String url = repository + path;
183         HttpMethod httpMethod = new GetMethod(url);
184         try
185         {
186             System.out.print("Downloading " + url + " ...");
187             client.executeMethod(hostConfig, httpMethod, httpState);
188             if (httpMethod.getStatusCode() == HttpStatus.SC_OK)
189             {
190                 File destinationFile = new File(new File(muleHome, DefaultMuleClassPathConfig.USER_DIR),
191                     destinationFileName);
192                 FileUtils.copyStreamToFile(httpMethod.getResponseBodyAsStream(), destinationFile);
193                 System.out.println("done");
194                 return destinationFile.toURL();
195             }
196             else
197             {
198                 System.out.println();
199                 throw new IOException("HTTP request failed: " + httpMethod.getStatusLine().toString());
200             }
201         }
202         finally
203         {
204             httpMethod.releaseConnection();
205         }
206     }
207 }