diff options
author | Tobias Oberlies | 2012-04-12 13:55:29 +0000 |
---|---|---|
committer | Tobias Oberlies | 2012-04-12 15:04:01 +0000 |
commit | d104b01da65aa65a7b8cfa43287cef8dbebbe168 (patch) | |
tree | b4d6217480d71c679876ae14f65e873e5cbef510 | |
parent | 4e22e50e855a886d2d3b2e662f17d1d50d0808d1 (diff) | |
download | rt.equinox.p2-d104b01da65aa65a7b8cfa43287cef8dbebbe168.tar.gz rt.equinox.p2-d104b01da65aa65a7b8cfa43287cef8dbebbe168.tar.xz rt.equinox.p2-d104b01da65aa65a7b8cfa43287cef8dbebbe168.zip |
347319 Allow repository factories which load repositories by URNsv20120412-1504
- Fix the response of the artifact/metadata repository factories in
case that the location to be loaded is not a URL: the factories
must return null or throw a ProvisionException with status
REPOSITORY_NOT_FOUND in order to make sure that the manager
continues to ask other factories. This allows to register a custom
factory which for example looks up instances from a registry by
uniform resource *identifiers* which are not uniform resource
*locators*.
- Added a test for loading from a custom, registry based repository
factory.
Bug: 347319 - [repository] SimpleArtifactRepositoryFactory prevents loading repositories from non-URL URIs
12 files changed, 170 insertions, 19 deletions
diff --git a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/CacheManager.java b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/CacheManager.java index 234b974a7..95afb681f 100644 --- a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/CacheManager.java +++ b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/CacheManager.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2010 IBM Corporation and others. + * Copyright (c) 2008, 2012 IBM Corporation and others. * 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 @@ -13,7 +13,7 @@ package org.eclipse.equinox.internal.p2.repository; import java.io.*; -import java.net.URI; +import java.net.*; import java.util.EventObject; import java.util.HashSet; import org.eclipse.core.runtime.*; @@ -102,6 +102,9 @@ public class CacheManager { * @throws OperationCanceledException - if user canceled */ public File createCache(URI repositoryLocation, String prefix, IProgressMonitor monitor) throws IOException, ProvisionException { + if (!isURL(repositoryLocation)) { + throw new ProvisionException(new Status(IStatus.ERROR, Activator.ID, ProvisionException.REPOSITORY_NOT_FOUND, NLS.bind(Messages.CacheManager_CannotLoadNonUrlLocation, repositoryLocation), null)); + } SubMonitor submonitor = SubMonitor.convert(monitor, 1000); try { @@ -261,6 +264,15 @@ public class CacheManager { return files; } + private static boolean isURL(URI location) { + try { + new URL(location.toASCIIString()); + } catch (MalformedURLException e) { + return false; + } + return true; + } + /** * Adds a {@link SynchronousProvisioningListener} to the event bus for * deleting cache files when the corresponding repository is deleted. diff --git a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/Messages.java b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/Messages.java index 61d8912b8..822f3c19f 100644 --- a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/Messages.java +++ b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/Messages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 IBM Corporation and others. + * Copyright (c) 2007, 2012 IBM Corporation and others. * 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 @@ -18,6 +18,8 @@ public class Messages extends NLS { private static final String BUNDLE_NAME = "org.eclipse.equinox.internal.p2.repository.messages"; //$NON-NLS-1$ public static String CacheManager_AuthenticationFaileFor_0; + + public static String CacheManager_CannotLoadNonUrlLocation; public static String CacheManager_FailedCommunicationWithRepo_0; public static String CacheManager_Neither_0_nor_1_found; public static String CacheManage_ErrorRenamingCache; diff --git a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/AbstractRepositoryManager.java b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/AbstractRepositoryManager.java index 01b260288..207eebcee 100644 --- a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/AbstractRepositoryManager.java +++ b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/AbstractRepositoryManager.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2011 IBM Corporation and others. + * Copyright (c) 2008, 2012 IBM Corporation and others. * 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 @@ -689,8 +689,8 @@ public abstract class AbstractRepositoryManager<T> implements IRepositoryManager */ private LocationProperties loadIndexFile(URI location, IProgressMonitor monitor) { LocationProperties locationProperties = LocationProperties.createEmptyIndexFile(); - //Handle the case of local repos - if ("memory".equals(location.getScheme())) //$NON-NLS-1$ + //Handle the case of in-memory repos + if (!isURL(location)) return locationProperties; if ("file".equals(location.getScheme())) { //$NON-NLS-1$ @@ -740,6 +740,15 @@ public abstract class AbstractRepositoryManager<T> implements IRepositoryManager return location; } + private static boolean isURL(URI location) { + try { + new URL(location.toASCIIString()); + } catch (MalformedURLException e) { + return false; + } + return true; + } + private IRepository<T> loadRepository(URI location, String suffix, String type, int flags, SubMonitor monitor) throws ProvisionException { IExtension[] providers = findMatchingRepositoryExtensions(suffix, type); // Loop over the candidates and return the first one that successfully loads diff --git a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/messages.properties b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/messages.properties index 4e59267be..ddd53d163 100644 --- a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/messages.properties +++ b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/messages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2007, 2010 IBM Corporation and others. +# Copyright (c) 2007, 2012 IBM Corporation and others. # 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 @@ -21,6 +21,7 @@ repo_loading = Loading the repository {0} CacheManager_Neither_0_nor_1_found=Neither {0} nor {1} found. CacheManager_AuthenticationFaileFor_0=Authentication failed for {0}. +CacheManager_CannotLoadNonUrlLocation=Cannot load repository from non-URL location {0} CacheManager_FailedCommunicationWithRepo_0=Communication with repository at {0} failed. CacheManage_ErrorRenamingCache=An error occurred while downloading {0}. The cache file {1} could not be renamed to {1}. diff --git a/bundles/org.eclipse.equinox.p2.tests/plugin.xml b/bundles/org.eclipse.equinox.p2.tests/plugin.xml index 3bf594c94..021e9cae4 100644 --- a/bundles/org.eclipse.equinox.p2.tests/plugin.xml +++ b/bundles/org.eclipse.equinox.p2.tests/plugin.xml @@ -99,4 +99,22 @@ class="org.eclipse.equinox.p2.tests.FailingMetadataRepositoryFactory"> </factory> </extension> + <extension + point="org.eclipse.equinox.p2.metadata.repository.metadataRepositories"> + <factory + class="org.eclipse.equinox.p2.tests.repository.RepositoryExtensionPointTest$TestMetadataRepositoryRegistry"> + </factory> + <filter + suffix="@testMetadataRepositoryRegistry"> + </filter> + </extension> + <extension + point="org.eclipse.equinox.p2.artifact.repository.artifactRepositories"> + <factory + class="org.eclipse.equinox.p2.tests.repository.RepositoryExtensionPointTest$TestArtifactRepositoryRegistry"> + </factory> + <filter + suffix="@testArtifactRepositoryRegistry"> + </filter> + </extension> </plugin> diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/TestMetadataRepository.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/TestMetadataRepository.java index fc3a7c1fd..aaa764170 100644 --- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/TestMetadataRepository.java +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/TestMetadataRepository.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 IBM Corporation and others. + * Copyright (c) 2007, 2012 IBM Corporation and others. * 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 @@ -48,7 +48,7 @@ public class TestMetadataRepository extends AbstractMetadataRepository { return null; } - public TestMetadataRepository(IProvisioningAgent agent, IInstallableUnit[] ius) { + public TestMetadataRepository(IProvisioningAgent agent, IInstallableUnit... ius) { super(agent, NAME, TYPE, VERSION, createLocation(), DESCRIPTION, PROVIDER, null); units.addAll(Arrays.asList(ius)); } diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/ant/MirrorTaskTest.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/ant/MirrorTaskTest.java index 02bc38cd3..903f59fc3 100644 --- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/ant/MirrorTaskTest.java +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/ant/MirrorTaskTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2011 IBM Corporation and others. + * Copyright (c) 2009, 2012 IBM Corporation and others. * 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 @@ -290,7 +290,7 @@ public class MirrorTaskTest extends AbstractAntProvisioningTest { * Test the handling of invalid destinations with the mirror task */ public void testMirrorWithInvalidSource() throws URISyntaxException { - URI location = new URI("invalid:/scheme2"); + URI location = new URI("unknown:/scheme2"); AntTaskElement mirror = createMirrorTask(TYPE_BOTH); mirror.addElement(createSourceElement(location, location)); @@ -308,7 +308,7 @@ public class MirrorTaskTest extends AbstractAntProvisioningTest { while (exception.getCause() != null && !(exception instanceof ProvisionException)) exception = exception.getCause(); assertTrue("Expecting a CoreException", exception instanceof CoreException); - assertEquals("Unexpected error code.", ProvisionException.REPOSITORY_FAILED_READ, ((CoreException) exception).getStatus().getCode()); + assertEquals("Unexpected error code.", ProvisionException.REPOSITORY_NOT_FOUND, ((CoreException) exception).getStatus().getCode()); } /* diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/metadata/repository/MetadataRepositoryManagerTest.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/metadata/repository/MetadataRepositoryManagerTest.java index ed751887c..93a0bd5a4 100644 --- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/metadata/repository/MetadataRepositoryManagerTest.java +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/metadata/repository/MetadataRepositoryManagerTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2011 IBM Corporation and others. + * Copyright (c) 2007, 2012 IBM Corporation and others. * 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 @@ -126,14 +126,14 @@ public class MetadataRepositoryManagerTest extends AbstractProvisioningTest { try { factory.load(location, 0, new NullProgressMonitor()); } catch (ProvisionException e) { - assertEquals(ProvisionException.REPOSITORY_FAILED_READ, e.getStatus().getCode()); + assertEquals(ProvisionException.REPOSITORY_NOT_FOUND, e.getStatus().getCode()); } factory = new UpdateSiteMetadataRepositoryFactory(); factory.setAgent(getAgent()); try { factory.load(location, 0, new NullProgressMonitor()); } catch (ProvisionException e) { - assertEquals(ProvisionException.REPOSITORY_FAILED_READ, e.getStatus().getCode()); + assertEquals(ProvisionException.REPOSITORY_NOT_FOUND, e.getStatus().getCode()); } } diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/repository/AllTests.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/repository/AllTests.java index 8022be168..41c85aeda 100644 --- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/repository/AllTests.java +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/repository/AllTests.java @@ -21,6 +21,7 @@ public class AllTests extends TestCase { TestSuite suite = new TestSuite(AllTests.class.getName()); suite.addTestSuite(RepositoryHelperTest.class); // suite.addTestSuite(FileReaderTest2.class); + suite.addTestSuite(RepositoryExtensionPointTest.class); return suite; } } diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/repository/RepositoryExtensionPointTest.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/repository/RepositoryExtensionPointTest.java new file mode 100644 index 000000000..0e39a8562 --- /dev/null +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/repository/RepositoryExtensionPointTest.java @@ -0,0 +1,83 @@ +/******************************************************************************* + * Copyright (c) 2012 SAP AG and others. + * 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: + * SAP AG - initial API and implementation + *******************************************************************************/ +package org.eclipse.equinox.p2.tests.repository; + +import static org.hamcrest.CoreMatchers.sameInstance; +import static org.junit.Assert.assertThat; + +import java.net.URI; +import java.util.Map; +import org.eclipse.core.runtime.*; +import org.eclipse.equinox.p2.core.ProvisionException; +import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; +import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactRepositoryFactory; +import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; +import org.eclipse.equinox.p2.repository.metadata.spi.MetadataRepositoryFactory; +import org.eclipse.equinox.p2.tests.*; + +public class RepositoryExtensionPointTest extends AbstractProvisioningTest { + /** + * A non-URL URI, e.g. for identifying a repository in a registry. + */ + static URI repositoryKey = URI.create("testregistry:repo"); + static IMetadataRepository metadataRepositoryInstance = new TestMetadataRepository(getAgent()); + static IArtifactRepository artifactRepositoryInstance = new TestArtifactRepository(getAgent(), repositoryKey); + + public static class TestMetadataRepositoryRegistry extends MetadataRepositoryFactory { + + @Override + public IMetadataRepository create(URI location, String name, String type, Map<String, String> properties) throws ProvisionException { + // none of the codes defined in ProvisionException really fit + int errorCode = 0; + Status status = new Status(IStatus.ERROR, TestActivator.PI_PROV_TESTS, errorCode, "Creating repositories of type " + type + " is not supported", null); + throw new ProvisionException(status); + } + + @Override + public IMetadataRepository load(URI location, int flags, IProgressMonitor monitor) { + if (repositoryKey.equals(location)) { + return metadataRepositoryInstance; + } + return null; + } + } + + public static class TestArtifactRepositoryRegistry extends ArtifactRepositoryFactory { + + @Override + public IArtifactRepository create(URI location, String name, String type, Map<String, String> properties) throws ProvisionException { + // none of the codes defined in ProvisionException really fit + int errorCode = 0; + Status status = new Status(IStatus.ERROR, TestActivator.PI_PROV_TESTS, errorCode, "Creating repositories of type " + type + " is not supported", null); + throw new ProvisionException(status); + } + + @Override + public IArtifactRepository load(URI location, int flags, IProgressMonitor monitor) { + if (repositoryKey.equals(location)) { + return artifactRepositoryInstance; + } + return null; + } + } + + public void testLoadMetadataRepositoryFromURIWithCustomScheme() throws Exception { + IMetadataRepository repo = loadMetadataRepository(repositoryKey); + + assertThat(repo, sameInstance(metadataRepositoryInstance)); + } + + public void testLoadArtifactRepositoryFromURIWithCustomScheme() throws Exception { + IArtifactRepository repo = loadArtifactRepository(repositoryKey); + + assertThat(repo, sameInstance(artifactRepositoryInstance)); + } +} diff --git a/bundles/org.eclipse.equinox.p2.updatesite/src/org/eclipse/equinox/internal/p2/updatesite/artifact/UpdateSiteArtifactRepositoryFactory.java b/bundles/org.eclipse.equinox.p2.updatesite/src/org/eclipse/equinox/internal/p2/updatesite/artifact/UpdateSiteArtifactRepositoryFactory.java index ca6343acd..ea9164574 100644 --- a/bundles/org.eclipse.equinox.p2.updatesite/src/org/eclipse/equinox/internal/p2/updatesite/artifact/UpdateSiteArtifactRepositoryFactory.java +++ b/bundles/org.eclipse.equinox.p2.updatesite/src/org/eclipse/equinox/internal/p2/updatesite/artifact/UpdateSiteArtifactRepositoryFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2010 IBM Corporation and others. + * Copyright (c) 2008, 2012 IBM Corporation and others. * 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 @@ -12,7 +12,7 @@ *******************************************************************************/ package org.eclipse.equinox.internal.p2.updatesite.artifact; -import java.net.URI; +import java.net.*; import java.util.*; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactDescriptor; @@ -52,6 +52,10 @@ public class UpdateSiteArtifactRepositoryFactory extends ArtifactRepositoryFacto if ((flags & IRepositoryManager.REPOSITORY_HINT_MODIFIABLE) > 0) { return null; } + if (!isURL(location)) { + return null; + } + IArtifactRepository repository = loadRepository(location, monitor); try { initializeRepository(repository, location, monitor); @@ -66,6 +70,15 @@ public class UpdateSiteArtifactRepositoryFactory extends ArtifactRepositoryFacto return new UpdateSiteArtifactRepository(location, repository); } + private static boolean isURL(URI location) { + try { + new URL(location.toASCIIString()); + } catch (MalformedURLException e) { + return false; + } + return true; + } + private void resetCache(IArtifactRepository repository) { repository.setProperty(PROP_SITE_CHECKSUM, "0"); //$NON-NLS-1$ repository.removeAll(); diff --git a/bundles/org.eclipse.equinox.p2.updatesite/src/org/eclipse/equinox/internal/p2/updatesite/metadata/UpdateSiteMetadataRepositoryFactory.java b/bundles/org.eclipse.equinox.p2.updatesite/src/org/eclipse/equinox/internal/p2/updatesite/metadata/UpdateSiteMetadataRepositoryFactory.java index d92c82fa1..d3142217a 100644 --- a/bundles/org.eclipse.equinox.p2.updatesite/src/org/eclipse/equinox/internal/p2/updatesite/metadata/UpdateSiteMetadataRepositoryFactory.java +++ b/bundles/org.eclipse.equinox.p2.updatesite/src/org/eclipse/equinox/internal/p2/updatesite/metadata/UpdateSiteMetadataRepositoryFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2010 IBM Corporation and others. + * Copyright (c) 2008, 2012 IBM Corporation and others. * 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 @@ -14,7 +14,7 @@ package org.eclipse.equinox.internal.p2.updatesite.metadata; import java.io.File; -import java.net.URI; +import java.net.*; import java.util.Map; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.metadata.repository.LocalMetadataRepository; @@ -50,6 +50,9 @@ public class UpdateSiteMetadataRepositoryFactory extends MetadataRepositoryFacto if ((flags & IRepositoryManager.REPOSITORY_HINT_MODIFIABLE) > 0) { return null; } + if (!isURL(location)) { + return null; + } IMetadataRepository repository = loadRepository(location, monitor); try { @@ -65,6 +68,15 @@ public class UpdateSiteMetadataRepositoryFactory extends MetadataRepositoryFacto return new UpdateSiteMetadataRepository(location, repository); } + private static boolean isURL(URI location) { + try { + new URL(location.toASCIIString()); + } catch (MalformedURLException e) { + return false; + } + return true; + } + private void resetCache(IMetadataRepository repository) { repository.setProperty(PROP_SITE_CHECKSUM, "0"); //$NON-NLS-1$ repository.removeAll(); |