diff options
author | Jason van Zyl | 2013-09-29 03:07:19 +0000 |
---|---|---|
committer | Igor Fedorenko | 2013-10-06 21:33:38 +0000 |
commit | b29933bbb8cf2659d05d42e288c9c14ba7a416ea (patch) | |
tree | eb493754e8711549d8e8fdf20b25089a24f72828 /org.eclipse.m2e.core | |
parent | 85527e1eb1638654d8f33a7106900ef9d1158bc2 (diff) | |
download | m2e-core-b29933bbb8cf2659d05d42e288c9c14ba7a416ea.tar.gz m2e-core-b29933bbb8cf2659d05d42e288c9c14ba7a416ea.tar.xz m2e-core-b29933bbb8cf2659d05d42e288c9c14ba7a416ea.zip |
418263 Replaced netty/ahc with okhttp
Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>
Diffstat (limited to 'org.eclipse.m2e.core')
7 files changed, 177 insertions, 321 deletions
diff --git a/org.eclipse.m2e.core/META-INF/MANIFEST.MF b/org.eclipse.m2e.core/META-INF/MANIFEST.MF index 4ded3855..44059ce3 100644 --- a/org.eclipse.m2e.core/META-INF/MANIFEST.MF +++ b/org.eclipse.m2e.core/META-INF/MANIFEST.MF @@ -12,8 +12,7 @@ Require-Bundle: org.eclipse.m2e.maven.runtime;bundle-version="[1.5.0,1.6.0)", org.eclipse.m2e.archetype.common;bundle-version="[1.5.0,1.6.0)", org.eclipse.m2e.maven.indexer;bundle-version="[1.5.0,1.6.0)", - org.slf4j.api;bundle-version="1.6.2", - com.ning.async-http-client;bundle-version="[1.6.5,1.6.6)" + org.slf4j.api;bundle-version="1.6.2" Bundle-ActivationPolicy: lazy Bundle-ClassPath: . Bundle-RequiredExecutionEnvironment: JavaSE-1.6, diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMavenConfiguration.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMavenConfiguration.java index 2ede00d6..0ffcb5ad 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMavenConfiguration.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMavenConfiguration.java @@ -30,7 +30,7 @@ public interface IMavenConfiguration { public boolean isOffline(); /** - * One of org.sonatype.aether.repository.RepositoryPolicy.UPDATE constants or null. If not null, the specified update + * One of org.eclipse.aether.repository.RepositoryPolicy.UPDATE constants or null. If not null, the specified update * policy overrides the update policies of the remote repositories being used for resolution. */ public String getGlobalUpdatePolicy(); diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenEmbeddedRuntime.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenEmbeddedRuntime.java index 16f78792..6673cc9d 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenEmbeddedRuntime.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenEmbeddedRuntime.java @@ -124,7 +124,7 @@ public class MavenEmbeddedRuntime implements MavenRuntime { addBundleClasspathEntries(allentries, mavenRuntimeBundle); // find and add more bundles - for(String sname : new String[] {"com.ning.async-http-client", "org.jboss.netty", "org.slf4j.api"}) { + for(String sname : new String[] {"org.slf4j.api"}) { Bundle dependency = findDependencyBundle(mavenRuntimeBundle, sname, new HashSet<Bundle>()); if(dependency != null) { addBundleClasspathEntries(allentries, dependency); diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/AetherClientResourceFetcher.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/AetherClientResourceFetcher.java new file mode 100644 index 00000000..a86d5084 --- /dev/null +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/AetherClientResourceFetcher.java @@ -0,0 +1,170 @@ +/******************************************************************************* + * Copyright (c) 2010 Sonatype, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sonatype, Inc. - initial API and implementation + *******************************************************************************/ + +package org.eclipse.m2e.core.internal.index.nexus; + +import io.tesla.aether.client.AetherClient; +import io.tesla.aether.client.AetherClientAuthentication; +import io.tesla.aether.client.AetherClientConfig; +import io.tesla.aether.client.AetherClientProxy; +import io.tesla.aether.client.Response; +import io.tesla.aether.okhttp.OkHttpAetherClient; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.HashMap; +import java.util.Map; + +import com.google.common.io.Closer; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; + +import org.apache.maven.index.updater.AbstractResourceFetcher; +import org.apache.maven.wagon.authentication.AuthenticationInfo; +import org.apache.maven.wagon.proxy.ProxyInfo; + +import org.eclipse.m2e.core.internal.MavenPluginActivator; + + +public class AetherClientResourceFetcher extends AbstractResourceFetcher { + + private AetherClient aetherClient; + + private final AuthenticationInfo authInfo; + + private final ProxyInfo proxyInfo; + + private final String userAgent; + + private final IProgressMonitor monitor; + + private String baseUrl; + + public AetherClientResourceFetcher(final AuthenticationInfo authInfo, final ProxyInfo proxyInfo, + final IProgressMonitor monitor) { + this.authInfo = authInfo; + this.proxyInfo = proxyInfo; + this.monitor = (monitor != null) ? monitor : new NullProgressMonitor(); + this.userAgent = MavenPluginActivator.getUserAgent(); + } + + public void connect(String id, String url) throws IOException { + aetherClient = new OkHttpAetherClient(new AetherClientConfigAdapter(authInfo, proxyInfo, userAgent, + new HashMap<String, String>())); + this.baseUrl = url; + } + + public void disconnect() throws IOException { + aetherClient.close(); + } + + public void retrieve(String name, File targetFile) throws IOException, FileNotFoundException { + + String url = baseUrl + "/" + name; + Response response = aetherClient.get(url); + + Closer closer = Closer.create(); + try { + InputStream is = closer.register(response.getInputStream()); + OutputStream os = closer.register(new BufferedOutputStream(new FileOutputStream(targetFile))); + final byte[] buffer = new byte[1024 * 1024]; + int n = 0; + while(-1 != (n = is.read(buffer))) { + os.write(buffer, 0, n); + if(monitor.isCanceled()) { + throw new OperationCanceledException(); + } + } + } finally { + closer.close(); + } + } + + class AetherClientConfigAdapter extends AetherClientConfig { + + int connectionTimeout; + + int requestTimeout; + + AuthenticationInfo authInfo; + + ProxyInfo proxyInfo; + + String userAgent; + + Map<String, String> headers; + + public AetherClientConfigAdapter(AuthenticationInfo authInfo, ProxyInfo proxyInfo, String userAgent, + Map<String, String> headers) { + this.authInfo = authInfo; + this.proxyInfo = proxyInfo; + this.userAgent = userAgent; + this.headers = headers; + } + + public String getUserAgent() { + return userAgent; + } + + public int getConnectionTimeout() { + return connectionTimeout; + } + + public int getRequestTimeout() { + return requestTimeout; + } + + public AetherClientProxy getProxy() { + + if(proxyInfo == null) { + return null; + } + + return new AetherClientProxy() { + + public String getHost() { + return proxyInfo.getHost(); + } + + public int getPort() { + return proxyInfo.getPort(); + } + + public AetherClientAuthentication getAuthentication() { + + if(proxyInfo != null && proxyInfo.getUserName() != null && proxyInfo.getPassword() != null) { + return new AetherClientAuthentication(proxyInfo.getUserName(), proxyInfo.getPassword()); + } + return null; + } + }; + } + + public AetherClientAuthentication getAuthentication() { + + if(authInfo != null) { + return new AetherClientAuthentication(authInfo.getUserName(), authInfo.getPassword()); + } + return null; + } + + public Map<String, String> getHeaders() { + return headers; + } + } +} diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/AsyncFetcher.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/AsyncFetcher.java deleted file mode 100644 index b9a8ad50..00000000 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/AsyncFetcher.java +++ /dev/null @@ -1,314 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2008-2010 Sonatype, Inc. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Sonatype, Inc. - initial API and implementation - *******************************************************************************/ - -package org.eclipse.m2e.core.internal.index.nexus; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.PipedInputStream; -import java.io.PipedOutputStream; -import java.net.HttpURLConnection; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Future; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.ning.http.client.BodyConsumer; -import com.ning.http.client.ProxyServer; -import com.ning.http.client.Realm.AuthScheme; -import com.ning.http.client.Response; -import com.ning.http.client.SimpleAsyncHttpClient; -import com.ning.http.client.SimpleAsyncHttpClient.ErrorDocumentBehaviour; -import com.ning.http.client.ThrowableHandler; -import com.ning.http.client.consumers.OutputStreamBodyConsumer; -import com.ning.http.client.simple.HeaderMap; -import com.ning.http.client.simple.SimpleAHCTransferListener; - -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.osgi.util.NLS; - -import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.io.RawInputStreamFacade; - -import org.apache.maven.index.updater.AbstractResourceFetcher; -import org.apache.maven.wagon.authentication.AuthenticationInfo; -import org.apache.maven.wagon.proxy.ProxyInfo; -import org.apache.maven.wagon.proxy.ProxyUtils; -import org.apache.maven.wagon.repository.Repository; - -import org.eclipse.m2e.core.internal.MavenPluginActivator; -import org.eclipse.m2e.core.internal.Messages; - - -/** - * A resource fetcher using Async HTTP Client. - * - * @author Benjamin Hanzelmann - */ -public class AsyncFetcher extends AbstractResourceFetcher { - - private static Logger log = LoggerFactory.getLogger(AsyncFetcher.class); - - private final AuthenticationInfo authInfo; - - private final ProxyInfo proxyInfo; - - private final String userAgent; - - private final IProgressMonitor monitor; - - private SimpleAsyncHttpClient httpClient; - - private String baseUrl; - - private final Map<String, Future<Response>> futures = new ConcurrentHashMap<String, Future<Response>>(); - - private final Map<String, Streams> streams = new ConcurrentHashMap<String, Streams>(); - - public AsyncFetcher(final AuthenticationInfo authInfo, final ProxyInfo proxyInfo, final IProgressMonitor monitor) { - this.authInfo = authInfo; - this.proxyInfo = proxyInfo; - this.monitor = (monitor != null) ? monitor : new NullProgressMonitor(); - this.userAgent = computeUserAgent(); - } - - void cancel(String url) { - Future<Response> future = futures.remove(url); - if(future != null) { - future.cancel(true); - } - } - - void closeStream(String url, Throwable exception) { - log.debug("Closing streams for {} due to {}", new Object[] {url, exception.getMessage(), exception}); - - Streams s = streams.remove(url); - - if(s == null) { - return; - } - - PipedErrorInputStream pis = s.in; - pis.setError(exception); - - try { - s.out.close(); - } catch(IOException ex) { - // we tried - } - } - - public void connect(String id, String url) { - httpClient = createClient(url); - baseUrl = url.endsWith("/") ? url : (url + '/'); //$NON-NLS-1$ - } - - private SimpleAsyncHttpClient createClient(String url) { - SimpleAsyncHttpClient.Builder sahcBuilder = new SimpleAsyncHttpClient.Builder(); - - sahcBuilder.setUserAgent(userAgent); - - sahcBuilder.setConnectionTimeoutInMs(15 * 1000); - sahcBuilder.setRequestTimeoutInMs(60 * 1000); - sahcBuilder.setCompressionEnabled(true); - sahcBuilder.setFollowRedirects(true); - sahcBuilder.setErrorDocumentBehaviour(ErrorDocumentBehaviour.OMIT); - sahcBuilder.setListener(new MonitorListener(monitor)); - - addAuthInfo(sahcBuilder); - addProxyInfo(url, sahcBuilder); - - return sahcBuilder.build(); - } - - private String computeUserAgent() { - return MavenPluginActivator.getUserAgent(); - } - - private void addAuthInfo(SimpleAsyncHttpClient.Builder configBuilder) { - if(authInfo != null && authInfo.getUserName() != null && authInfo.getUserName().length() > 0) { - configBuilder.setRealmScheme(AuthScheme.BASIC); - configBuilder.setRealmPrincipal(authInfo.getUserName()); - configBuilder.setRealmPassword(authInfo.getPassword()); - configBuilder.setRealmUsePreemptiveAuth(true); - } - } - - private void addProxyInfo(String url, SimpleAsyncHttpClient.Builder configBuilder) { - if(proxyInfo != null) { - Repository repo = new Repository("id", url); //$NON-NLS-1$ - if(!ProxyUtils.validateNonProxyHosts(proxyInfo, repo.getHost())) { - if(proxyInfo != null) { - ProxyServer.Protocol protocol = "https".equalsIgnoreCase(proxyInfo.getType()) ? ProxyServer.Protocol.HTTPS //$NON-NLS-1$ - : ProxyServer.Protocol.HTTP; - - configBuilder.setProxyProtocol(protocol); - configBuilder.setProxyHost(proxyInfo.getHost()); - configBuilder.setProxyPort(proxyInfo.getPort()); - configBuilder.setProxyPrincipal(proxyInfo.getUserName()); - configBuilder.setProxyPassword(proxyInfo.getPassword()); - } - } - } - } - - public void disconnect() { - baseUrl = null; - futures.clear(); - - if(httpClient != null) { - httpClient.close(); - } - - httpClient = null; - } - - @SuppressWarnings("deprecation") - public void retrieve(String name, File targetFile) throws IOException, FileNotFoundException { - InputStream is = retrieve(name); - try { - FileUtils.copyStreamToFile(new RawInputStreamFacade(is), targetFile); - } finally { - IOUtil.close(is); - } - } - - @Override - public InputStream retrieve(String name) throws IOException, FileNotFoundException { - final String url = buildUrl(baseUrl, name); - - monitor.subTask(NLS.bind(Messages.AsyncFetcher_task_fetching, url)); - - PipedErrorInputStream pis = new PipedErrorInputStream(); - PipedOutputStream pos = new PipedOutputStream(pis); - BodyConsumer consumer = new OutputStreamBodyConsumer(pos); - - streams.put(url, new Streams(pis, pos)); - - Future<Response> future = httpClient.derive().setUrl(url).build().get(consumer, new ErrorPropagator(url)); - - futures.put(url, future); - - return pis; - } - - private static String buildUrl(String baseUrl, String resourceName) { - String url = baseUrl; - - if(resourceName.startsWith("/")) { //$NON-NLS-1$ - url += resourceName.substring(1); - } else { - url += resourceName; - } - - return url; - } - - final class ErrorPropagator implements ThrowableHandler { - - private final String url; - - ErrorPropagator(String url) { - this.url = url; - } - - public void onThrowable(Throwable t) { - closeStream(this.url, t); - } - } - - static final class PipedErrorInputStream extends PipedInputStream { - - private volatile Throwable error; - - public PipedErrorInputStream() { - buffer = new byte[1024 * 128]; - } - - public void setError(Throwable t) { - if(error == null) { - error = t; - } - } - - private void checkError() throws IOException { - if(error != null) { - throw (IOException) new IOException(error.getMessage()).initCause(error); - } - } - - @Override - public synchronized int read() throws IOException { - checkError(); - int b = super.read(); - checkError(); - return b; - } - } - - private class MonitorListener implements SimpleAHCTransferListener { - - private IProgressMonitor monitor; - - public MonitorListener(IProgressMonitor monitor) { - this.monitor = monitor; - } - - private void checkCancelled(String url) { - if(monitor.isCanceled()) { - cancel(url); - } - } - - public void onStatus(String url, int code, String text) { - checkCancelled(url); - if(code != HttpURLConnection.HTTP_OK) { - closeStream(url, new IOException(NLS.bind(Messages.AsyncFetcher_error_server, code, text))); - } - } - - public void onHeaders(String url, HeaderMap arg1) { - checkCancelled(url); - } - - public void onBytesReceived(String url, long amount, long current, long total) { - checkCancelled(url); - monitor.subTask(NLS.bind(Messages.AsyncFetcher_task_fetching2, url, amount * 100 / total)); - } - - public void onBytesSent(String arg0, long arg1, long arg2, long arg3) { - // we only retrieve - } - - public void onCompleted(String arg0, int arg1, String arg2) { - monitor.subTask(""); //$NON-NLS-1$ - } - - } - - private final class Streams { - PipedErrorInputStream in; - - PipedOutputStream out; - - public Streams(PipedErrorInputStream pis, PipedOutputStream pos) { - this.in = pis; - this.out = pos; - } - } - -} diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/NexusIndexManager.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/NexusIndexManager.java index 71959134..b42bfbb6 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/NexusIndexManager.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/NexusIndexManager.java @@ -1178,11 +1178,12 @@ public class NexusIndexManager implements IndexManager, IMavenProjectChangedList protected IndexUpdateRequest newIndexUpdateRequest(IRepository repository, IndexingContext context, IProgressMonitor monitor) throws IOException, CoreException { + //TODO: remove Wagon API ProxyInfo proxyInfo = maven.getProxyInfo(repository.getProtocol()); AuthenticationInfo authenticationInfo = repository.getAuthenticationInfo(); - IndexUpdateRequest request = new IndexUpdateRequest(context, new AsyncFetcher(authenticationInfo, proxyInfo, - monitor)); + IndexUpdateRequest request = new IndexUpdateRequest(context, new AetherClientResourceFetcher(authenticationInfo, + proxyInfo, monitor)); File localRepo = repositoryRegistry.getLocalRepository().getBasedir(); File indexCacheBasedir = new File(localRepo, ".cache/m2e/" + MavenPluginActivator.getVersion()).getCanonicalFile(); //$NON-NLS-1$ File indexCacheDir = new File(indexCacheBasedir, repository.getUid()); diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/preferences/MavenPreferenceConstants.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/preferences/MavenPreferenceConstants.java index 607a1d34..b6b0b885 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/preferences/MavenPreferenceConstants.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/preferences/MavenPreferenceConstants.java @@ -31,7 +31,7 @@ public interface MavenPreferenceConstants { /** boolean */ public static final String P_OFFLINE = PREFIX + "offline"; //$NON-NLS-1$ - /** boolean. if true, use org.sonatype.aether.repository.RepositoryPolicy.UPDATE_POLICY_NEVER as global update policy */ + /** boolean. if true, use org.eclipse.aether.repository.RepositoryPolicy.UPDATE_POLICY_NEVER as global update policy */ public static final String P_GLOBAL_UPDATE_NEVER = PREFIX + "globalUpdatePolicy"; //$NON-NLS-1$ /** boolean */ |