1 10 11 package org.mule.modules.boot; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.net.URL ; 16 import java.net.UnknownHostException ; 17 import java.util.ArrayList ; 18 import java.util.List ; 19 20 import org.apache.commons.httpclient.ConnectTimeoutException; 21 import org.apache.commons.httpclient.HostConfiguration; 22 import org.apache.commons.httpclient.HttpClient; 23 import org.apache.commons.httpclient.HttpMethod; 24 import org.apache.commons.httpclient.HttpState; 25 import org.apache.commons.httpclient.HttpStatus; 26 import org.apache.commons.httpclient.UsernamePasswordCredentials; 27 import org.apache.commons.httpclient.auth.AuthScope; 28 import org.apache.commons.httpclient.methods.GetMethod; 29 import org.apache.commons.httpclient.params.HttpConnectionManagerParams; 30 import org.mule.util.FileUtils; 31 import org.mule.util.NumberUtils; 32 import org.mule.util.StringUtils; 33 34 import org.tanukisoftware.wrapper.WrapperManager; 35 36 public class LibraryDownloader { 37 static final int STARTUP_TIMEOUT = 120000; 38 static final String REPO_CENTRAL = "http://www.ibiblio.org/maven2"; 39 static final String REPO_MULESOURCE = "http://dist.codehaus.org/mule/dependencies/maven2"; 40 41 static String proxyHostname = System.getProperty("http.proxyHost"); 42 static String proxyPort = System.getProperty("http.proxyPort"); 43 static String proxyUsername = System.getProperty("http.proxyUsername"); 44 static String proxyPassword = System.getProperty("http.proxyPassword"); 45 46 private static HostConfiguration hostConfig; 48 private static HttpState httpState; 49 50 private File muleHome; 51 private File mavenRepo = null; 52 private HttpClient client; 53 54 public LibraryDownloader(File muleHome) { 55 this.muleHome = muleHome; 56 57 String mavenRepoVar = System.getProperty("m2.repo"); 58 if (!StringUtils.isBlank(mavenRepoVar)) { 59 mavenRepo = new File (mavenRepoVar).getAbsoluteFile(); 60 if (!mavenRepo.exists() || !mavenRepo.isDirectory()) { 61 mavenRepo = null; 62 } 63 } 64 65 client = new HttpClient(); 66 HttpConnectionManagerParams connParams = new HttpConnectionManagerParams(); 68 connParams.setConnectionTimeout(10000); 69 client.getHttpConnectionManager().setParams(connParams); 70 71 hostConfig = new HostConfiguration(); 73 if (StringUtils.isNotBlank(proxyHostname)) { 74 hostConfig.setProxy(proxyHostname, NumberUtils.toInt(proxyPort)); 75 } 76 httpState = new HttpState(); 77 if (StringUtils.isNotBlank(proxyUsername)) { 78 httpState.setProxyCredentials(new AuthScope(null, -1, null, null), 79 new UsernamePasswordCredentials(proxyUsername, proxyPassword)); 80 } 81 } 82 83 public List downloadLibraries() throws IOException { 84 List libraries = new ArrayList (); 85 try { 86 libraries.add(getLibrary(REPO_CENTRAL, 87 "/javax/activation/activation/1.1/activation-1.1.jar", "activation.jar")); 88 libraries.add(getLibrary(REPO_CENTRAL, 89 "/javax/mail/mail/1.4/mail-1.4.jar", "mail.jar")); 90 libraries.add(getLibrary(REPO_MULESOURCE, 91 "/script/jsr223/1.0/jsr223-1.0.jar", "jsr223.jar")); 92 return libraries; 93 } catch (UnknownHostException uhe) { 94 System.err.println(); 95 IOException ex = new IOException ("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."); 96 ex.initCause(uhe); 97 throw ex; 98 } catch (ConnectTimeoutException e) { 99 System.err.println(); 100 IOException ex = new IOException ("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."); 101 ex.initCause(e); 102 throw ex; 103 } 104 } 105 106 private URL getLibrary(String repository, String path, String destinationFileName) throws IOException { 107 URL url = null; 108 if (mavenRepo != null) { 109 url = copyLibrary(path, destinationFileName); 110 } 111 if (url == null) { 112 url = downloadLibrary(repository, path, destinationFileName); 113 } 114 return url; 115 } 116 117 private URL copyLibrary(String path, String destinationFileName) throws IOException { 118 File sourceFile = new File (mavenRepo, path).getCanonicalFile(); 119 if (sourceFile.exists()) { 120 System.out.print("Copying from local repository " + sourceFile.getAbsolutePath() + " ..."); 121 File destinationFile = 122 new File (new File (muleHome, DefaultMuleClassPathConfig.FOLDER_USER).getCanonicalFile(), 123 destinationFileName).getCanonicalFile(); 124 FileUtils.copyFile(sourceFile, destinationFile); 125 System.out.println("done"); 126 return destinationFile.toURL(); 127 } 128 else return null; 129 } 130 131 private URL downloadLibrary(String repository, String path, String destinationFileName) throws IOException { 132 String url = repository + path; 133 HttpMethod httpMethod = new GetMethod(url); 134 try { 135 System.out.print("Downloading " + url + " ..."); 136 client.executeMethod(hostConfig, httpMethod, httpState); 137 if (httpMethod.getStatusCode() == HttpStatus.SC_OK) { 138 File destinationFile = 139 new File (new File (muleHome, DefaultMuleClassPathConfig.FOLDER_USER), destinationFileName); 140 FileUtils.copyStreamToFile(httpMethod.getResponseBodyAsStream(), destinationFile); 141 System.out.println("done"); 142 return destinationFile.toURL(); 143 } 144 else { 145 System.out.println(); 146 throw new IOException ("HTTP request failed: " + httpMethod.getStatusLine().toString()); 147 } 148 } 149 finally { 150 httpMethod.releaseConnection(); 151 } 152 } 153 } 154 | Popular Tags |