| author | Olav Lenz | 2013-01-10 09:23:49 (EST) |
|---|---|---|
| committer | Patrick Gottschaemmer | 2013-01-10 09:23:49 (EST) |
| commit | 77b89dbeb92f8a008a5ffe648f21dd90bf6b6ad7 (patch) (side-by-side diff) | |
| tree | 4a72b6a0faba7a6cbb298fe078fb1313d77a7f16 | |
| parent | 1a17b22163573d1150483935f71d4cbd2e7ba84f (diff) | |
| download | org.eclipse.recommenders-77b89dbeb92f8a008a5ffe648f21dd90bf6b6ad7.zip org.eclipse.recommenders-77b89dbeb92f8a008a5ffe648f21dd90bf6b6ad7.tar.gz org.eclipse.recommenders-77b89dbeb92f8a008a5ffe648f21dd90bf6b6ad7.tar.bz2 | |
[rcp] use a ProxySelector to set the correct proxy in RemoteRepositoryrefs/changes/21/9321/4
This fixes a bug which occurs due to wrong proxy settings in the
RemoteRepository. Furthermore, proxies can now be changed by user
without a need for restart of eclipse.
Bug: 394532
Change-Id: Ic3090ce055c9d2333fcb6533a25be6a886408144
5 files changed, 188 insertions, 32 deletions
diff --git a/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/repo/ModelRepository.java b/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/repo/ModelRepository.java index 5922c1d..7a27da1 100644 --- a/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/repo/ModelRepository.java +++ b/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/repo/ModelRepository.java @@ -7,6 +7,7 @@ * * Contributors: * Marcel Bruch - initial API and implementation. + * Patrick Gottschaemmer, Olav Lenz - Introduced ProxySelector */ package org.eclipse.recommenders.internal.rcp.repo; @@ -27,13 +28,10 @@ import javax.inject.Singleton; import org.apache.commons.lang3.StringUtils; import org.apache.maven.repository.internal.DefaultServiceLocator; import org.apache.maven.repository.internal.MavenRepositorySystemSession; -import org.eclipse.core.net.proxy.IProxyData; -import org.eclipse.core.net.proxy.IProxyService; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.recommenders.internal.rcp.wiring.RecommendersModule.LocalModelRepositoryLocation; import org.eclipse.recommenders.internal.rcp.wiring.RecommendersModule.RemoteModelRepositoryLocation; import org.eclipse.recommenders.rcp.repo.IModelRepository; -import org.eclipse.recommenders.utils.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonatype.aether.AbstractRepositoryListener; @@ -54,7 +52,7 @@ import org.sonatype.aether.installation.InstallRequest; import org.sonatype.aether.installation.InstallationException; import org.sonatype.aether.repository.Authentication; import org.sonatype.aether.repository.LocalRepository; -import org.sonatype.aether.repository.Proxy; +import org.sonatype.aether.repository.ProxySelector; import org.sonatype.aether.repository.RemoteRepository; import org.sonatype.aether.resolution.DependencyRequest; import org.sonatype.aether.resolution.DependencyResolutionException; @@ -81,13 +79,13 @@ public class ModelRepository implements IModelRepository { private RepositorySystem system; private RemoteRepository remote; - private IProxyService proxy; + private ProxySelector proxySelector; @Inject public ModelRepository(@LocalModelRepositoryLocation File localLocation, - @RemoteModelRepositoryLocation String remoteLocation, @Nullable IProxyService proxy) throws Exception { + @RemoteModelRepositoryLocation String remoteLocation, ProxySelector proxySelector) throws Exception { this.location = localLocation; - this.proxy = proxy; + this.proxySelector = proxySelector; this.system = createRepositorySystem(); setRemote(remoteLocation); } @@ -106,8 +104,10 @@ public class ModelRepository implements IModelRepository { return locator.getService(RepositorySystem.class); } - private DefaultRepositorySystemSession newSession() { + private synchronized DefaultRepositorySystemSession newSession() { MavenRepositorySystemSession session = new MavenRepositorySystemSession(); + session.setProxySelector(proxySelector); + remote.setProxy(proxySelector.getProxy(remote)); LocalRepository localRepo = new LocalRepository(location); session.setLocalRepositoryManager(system.newLocalRepositoryManager(localRepo)); return session; @@ -129,7 +129,8 @@ public class ModelRepository implements IModelRepository { if (url.startsWith("file:")) { // try file: File file = new File(new URI(url)); - if (file.exists()) return of(file.lastModified() + ""); + if (file.exists()) + return of(file.lastModified() + ""); return absent(); } Response r = http.prepareHead(url).execute().get(); @@ -196,7 +197,8 @@ public class ModelRepository implements IModelRepository { } @Override - public synchronized File resolve(Artifact artifact, final IProgressMonitor monitor) throws DependencyResolutionException { + public synchronized File resolve(Artifact artifact, final IProgressMonitor monitor) + throws DependencyResolutionException { monitor.subTask("Resolving..."); DefaultRepositorySystemSession session = newSession(); session.setDependencySelector(new TheArtifactOnlyDependencySelector()); @@ -277,8 +279,8 @@ public class ModelRepository implements IModelRepository { } private Optional<VersionRangeResult> resolveVersionRange(Artifact a) { - VersionRangeRequest rangeRequest = - new VersionRangeRequest(a, Collections.singletonList(remote), a.getClassifier()); + VersionRangeRequest rangeRequest = new VersionRangeRequest(a, Collections.singletonList(remote), + a.getClassifier()); try { VersionRangeResult range = system.resolveVersionRange(newSession(), rangeRequest); return of(range); @@ -303,24 +305,23 @@ public class ModelRepository implements IModelRepository { return absent(); } + /** + * setRemote(String url) is kept for backwards compatibility with the interface, will be changed soon with + * refactoring of the new models api. + */ @Override - public void setRemote(String url) { + public synchronized void setRemote(String url) { remote = new RemoteRepository("remote-models", "default", url); - if (proxy == null) return; - - URI uri = URI.create(url); - for (IProxyData data : proxy.select(uri)) { - String host = data.getHost(); - if (host != null) { - String type = data.getType(); - int port = data.getPort(); - String userId = data.getUserId(); - String password = data.getPassword(); - Authentication auth = new Authentication(userId, password); - Proxy p = new Proxy(type, host, port, auth); - remote.setProxy(p); - } - } + remote.setProxy(proxySelector.getProxy(remote)); + } + + public synchronized void setRemoteRepository(RemoteRepository remote) { + this.remote = remote; + remote.setProxy(proxySelector.getProxy(remote)); + } + + public synchronized RemoteRepository getRemoteRepository() { + return remote; } public static class TheArtifactOnlyDependencySelector implements DependencySelector { @@ -336,7 +337,7 @@ public class ModelRepository implements IModelRepository { } } - public String getRemote() { + public String getRemoteUrl() { return remote.getUrl(); } diff --git a/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/repo/ServiceBasedProxySelector.java b/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/repo/ServiceBasedProxySelector.java new file mode 100644 index 0000000..229dd9d --- a/dev/null +++ b/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/repo/ServiceBasedProxySelector.java @@ -0,0 +1,49 @@ +/** + * Copyright (c) 2010, 2012 Darmstadt University of Technology. + * 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: + * Marcel Bruch - initial API and implementation. + * Patrick Gottschaemmer, Olav Lenz - introduced ProxySelector + */ +package org.eclipse.recommenders.internal.rcp.repo; + +import java.net.URI; + +import org.eclipse.core.net.proxy.IProxyData; +import org.eclipse.core.net.proxy.IProxyService; +import org.sonatype.aether.repository.Authentication; +import org.sonatype.aether.repository.Proxy; +import org.sonatype.aether.repository.ProxySelector; +import org.sonatype.aether.repository.RemoteRepository; + +public class ServiceBasedProxySelector implements ProxySelector { + + private final IProxyService proxyService; + + public ServiceBasedProxySelector(IProxyService proxyService) { + this.proxyService = proxyService; + } + + @Override + public Proxy getProxy(RemoteRepository remote) { + if (proxyService != null && proxyService.isProxiesEnabled()) { + URI uri = URI.create(remote.getUrl()); + IProxyData[] entries = proxyService.select(uri); + + if (entries.length > 0) { + IProxyData proxyData = entries[0]; + String type = proxyData.getType().toLowerCase(); + String host = proxyData.getHost(); + int port = proxyData.getPort(); + Authentication auth = new Authentication(proxyData.getUserId(), proxyData.getPassword()); + return new Proxy(type, host, port, auth); + } + } + return null; + } + +}
\ No newline at end of file diff --git a/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/wiring/RecommendersModule.java b/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/wiring/RecommendersModule.java index 682377f..e6c7b60 100644 --- a/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/wiring/RecommendersModule.java +++ b/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/wiring/RecommendersModule.java @@ -48,6 +48,7 @@ import org.eclipse.recommenders.internal.rcp.providers.JavaModelEventsProvider; import org.eclipse.recommenders.internal.rcp.providers.JavaSelectionProvider; import org.eclipse.recommenders.internal.rcp.repo.ModelRepository; import org.eclipse.recommenders.internal.rcp.repo.ModelRepositoryIndex; +import org.eclipse.recommenders.internal.rcp.repo.ServiceBasedProxySelector; import org.eclipse.recommenders.rcp.IAstProvider; import org.eclipse.recommenders.rcp.IClasspathEntryInfoProvider; import org.eclipse.recommenders.rcp.RecommendersPlugin; @@ -69,6 +70,7 @@ import org.eclipse.ui.progress.UIJob; import org.osgi.framework.Bundle; import org.osgi.framework.FrameworkUtil; import org.osgi.util.tracker.ServiceTracker; +import org.sonatype.aether.repository.ProxySelector; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.EventBus; @@ -273,13 +275,13 @@ public class RecommendersModule extends AbstractModule implements Module { } @Provides - protected IProxyService provideProxyService() { + protected ProxySelector provideProxyService() { Bundle bundle = FrameworkUtil.getBundle(getClass()); ServiceTracker tracker = new ServiceTracker(bundle.getBundleContext(), IProxyService.class.getName(), null); tracker.open(); IProxyService service = (IProxyService) tracker.getService(); tracker.close(); - return service; + return new ServiceBasedProxySelector(service); } @Provides diff --git a/tests/org.eclipse.recommenders.tests.rcp/manual/org/eclipse/recommenders/tests/rcp/repo/RepositoryConsistencyManualTests.java b/tests/org.eclipse.recommenders.tests.rcp/manual/org/eclipse/recommenders/tests/rcp/repo/RepositoryConsistencyManualTests.java index 983fb11..3edd350 100644 --- a/tests/org.eclipse.recommenders.tests.rcp/manual/org/eclipse/recommenders/tests/rcp/repo/RepositoryConsistencyManualTests.java +++ b/tests/org.eclipse.recommenders.tests.rcp/manual/org/eclipse/recommenders/tests/rcp/repo/RepositoryConsistencyManualTests.java @@ -56,7 +56,7 @@ public class RepositoryConsistencyManualTests { assertFalse(call.isEmpty()); for (Artifact model : call) { Optional<String> remoteEtag = repo.remoteEtag(model); - assertTrue("no etag for " + model + " in " + repo.getRemote(), remoteEtag.isPresent()); + assertTrue("no etag for " + model + " in " + repo.getRemoteUrl(), remoteEtag.isPresent()); } System.out.println(); } diff --git a/tests/org.eclipse.recommenders.tests.rcp/src/org/eclipse/recommenders/tests/rcp/repo/ProxySelectorTest.java b/tests/org.eclipse.recommenders.tests.rcp/src/org/eclipse/recommenders/tests/rcp/repo/ProxySelectorTest.java new file mode 100644 index 0000000..351cd5e --- a/dev/null +++ b/tests/org.eclipse.recommenders.tests.rcp/src/org/eclipse/recommenders/tests/rcp/repo/ProxySelectorTest.java @@ -0,0 +1,104 @@ +/** + * Copyright (c) 2010, 2012 Darmstadt University of Technology. + * 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: + * Patrick Gottschaemmer, Olav Lenz - initial Implementation. + */ +package org.eclipse.recommenders.tests.rcp.repo; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.net.URI; + +import org.eclipse.core.net.proxy.IProxyData; +import org.eclipse.core.net.proxy.IProxyService; +import org.eclipse.recommenders.internal.rcp.repo.ServiceBasedProxySelector; +import org.junit.Test; +import org.mockito.Mockito; +import org.sonatype.aether.repository.Proxy; +import org.sonatype.aether.repository.ProxySelector; +import org.sonatype.aether.repository.RemoteRepository; + +public class ProxySelectorTest { + + private static final boolean PROXY_ENABLED = true; + private static final boolean PROXY_DISABLED = false; + private static final IProxyData[] EMPTY_PROXY_DATA = new IProxyData[] {}; + private static final IProxyService NO_PROXY_SERVICE = null; + + @Test + public void testNoProxyEnabled() { + IProxyService proxyService = mockProxyService(PROXY_DISABLED, EMPTY_PROXY_DATA); + ProxySelector proxySelector = new ServiceBasedProxySelector(proxyService); + + assertNull(proxySelector.getProxy(mockRemoteRepository("http://example.org/"))); + } + + @Test + public void testProxyServiceNull() { + ProxySelector proxySelector = new ServiceBasedProxySelector(NO_PROXY_SERVICE); + + assertNull(proxySelector.getProxy(mockRemoteRepository("http://example.org/"))); + } + + @Test + public void testNoProxyDataEntries() { + IProxyService proxyService = mockProxyService(PROXY_ENABLED, EMPTY_PROXY_DATA); + ProxySelector proxySelector = new ServiceBasedProxySelector(proxyService); + + assertNull(proxySelector.getProxy(mockRemoteRepository("http://example.org/"))); + } + + @Test + public void testSimpleProxy() { + IProxyData mock = mockProxyData("HTTP", "example.com", 8080); + IProxyService proxyService = mockProxyService(PROXY_ENABLED, new IProxyData[] { mock }); + ProxySelector proxySelector = new ServiceBasedProxySelector(proxyService); + + Proxy proxy = proxySelector.getProxy(mockRemoteRepository("http://example.org/")); + assertEquals(proxy.getType(), "http"); + assertEquals(proxy.getHost(), "example.com"); + assertEquals(proxy.getPort(), 8080); + } + + @Test + public void testFirstProxyHasPrecendence() { + IProxyData mock1 = mockProxyData("HTTP", "example.com", 8080); + IProxyData mock2 = mockProxyData("HTTP", "example.net", 8081); + IProxyService proxyService = mockProxyService(PROXY_ENABLED, new IProxyData[] { mock1, mock2 }); + ProxySelector proxySelector = new ServiceBasedProxySelector(proxyService); + + Proxy proxy = proxySelector.getProxy(mockRemoteRepository("http://example.org/")); + assertEquals(proxy.getType(), "http"); + assertEquals(proxy.getHost(), "example.com"); + assertEquals(proxy.getPort(), 8080); + } + + private IProxyService mockProxyService(boolean proxyEnabled, IProxyData[] entries) { + IProxyService proxyService = mock(IProxyService.class); + when(proxyService.isProxiesEnabled()).thenReturn(proxyEnabled); + when(proxyService.select(Mockito.any(URI.class))).thenReturn(entries); + return proxyService; + } + + private IProxyData mockProxyData(String type, String host, int port) { + IProxyData proxyData = mock(IProxyData.class); + when(proxyData.getType()).thenReturn(type); + when(proxyData.getHost()).thenReturn(host); + when(proxyData.getPort()).thenReturn(port); + return proxyData; + } + + private RemoteRepository mockRemoteRepository(String url) { + RemoteRepository remote = mock(RemoteRepository.class); + when(remote.getUrl()).thenReturn(url); + return remote; + } +}
\ No newline at end of file |

