diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java index 90f4969ff43..27ccf97f4d9 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java @@ -254,15 +254,14 @@ void bootDone() { public void registerListener(FeaturesListener listener) { listeners.add(listener); try { - Set repositories = new TreeSet<>(); + Set repositoriesList = new TreeSet<>(); Map> installedFeatures = new TreeMap<>(); synchronized (lock) { - repositories.addAll(state.repositories); + repositoriesList.addAll(state.repositories); installedFeatures.putAll(copy(state.installedFeatures)); } - Blacklist blacklist = new Blacklist(cfg.blacklisted); - for (String uri : repositories) { - Repository repository = new RepositoryImpl(URI.create(uri), blacklist); + for (String uri : repositoriesList) { + Repository repository = repositories.create(URI.create(uri), false); listener.repositoryEvent(new RepositoryEvent(repository, RepositoryEvent.EventType.RepositoryAdded, true)); } for (Map.Entry> entry : installedFeatures.entrySet()) { @@ -357,7 +356,7 @@ public void addRepository(URI uri) throws Exception { @Override public void addRepository(URI uri, boolean install) throws Exception { - Repository repository = repositories.create(uri, true, true); + Repository repository = repositories.create(uri, true); synchronized (lock) { repositories.addRepository(repository); featureCache = null; @@ -610,7 +609,7 @@ protected Map> getFeatureCache() throws Exception { } try { if (repo == null) { - repo = repositories.create(URI.create(uri), true, false); + repo = repositories.create(URI.create(uri), false); synchronized (lock) { repositories.addRepository(repo); } @@ -979,7 +978,7 @@ private Set diff(Set s1, Set s2) { @Override public Repository createRepository(URI uri) throws Exception { - return repositories.create(uri, true, true); + return repositories.create(uri, true); } @Override diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/RepositoryCache.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/RepositoryCache.java index 489856641ae..801ffeb2b85 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/RepositoryCache.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/RepositoryCache.java @@ -38,11 +38,8 @@ public RepositoryCache(Blacklist blacklist) { this.blacklist = blacklist; } - public Repository create(URI uri, boolean load, boolean validate) throws Exception { - RepositoryImpl repo = new RepositoryImpl(uri, blacklist); - if (load) - repo.load(validate); - return repo; + public Repository create(URI uri, boolean validate) throws Exception { + return new RepositoryImpl(uri, blacklist, validate); } public void addRepository(Repository repository) throws Exception { diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/RepositoryImpl.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/RepositoryImpl.java index 0bdc174b600..0e9d703f6b6 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/RepositoryImpl.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/RepositoryImpl.java @@ -38,51 +38,44 @@ public class RepositoryImpl implements Repository { private Features features; public RepositoryImpl(URI uri) { - this(uri, null); + this(uri, null, false); } - public RepositoryImpl(URI uri, Blacklist blacklist) { + public RepositoryImpl(URI uri, Blacklist blacklist, boolean validate) { this.uri = uri; this.blacklist = blacklist; + load(validate); } public URI getURI() { return uri; } - public String getName() throws IOException { - load(); + public String getName() { return features.getName(); } - public URI[] getRepositories() throws IOException { - load(); + public URI[] getRepositories() { return features.getRepository().stream() .map(String::trim) .map(URI::create) .toArray(URI[]::new); } - public URI[] getResourceRepositories() throws IOException { - load(); + public URI[] getResourceRepositories() { return features.getResourceRepository().stream() .map(String::trim) .map(URI::create) .toArray(URI[]::new); } - public Feature[] getFeatures() throws IOException { - load(); + public Feature[] getFeatures() { return features.getFeature() .toArray(new Feature[features.getFeature().size()]); } - public void load() throws IOException { - load(false); - } - - public void load(boolean validate) throws IOException { + private void load(boolean validate) { if (features == null) { try ( InputStream inputStream = new InterruptibleInputStream(uri.toURL().openStream()) @@ -92,7 +85,7 @@ public void load(boolean validate) throws IOException { blacklist.blacklist(features); } } catch (Exception e) { - throw new IOException(e.getMessage() + " : " + uri, e); + throw new RuntimeException(e.getMessage() + " : " + uri, e); } } } diff --git a/features/core/src/test/java/org/apache/karaf/features/RepositoryTest.java b/features/core/src/test/java/org/apache/karaf/features/RepositoryTest.java index c630c6f448a..a6c7edef00a 100644 --- a/features/core/src/test/java/org/apache/karaf/features/RepositoryTest.java +++ b/features/core/src/test/java/org/apache/karaf/features/RepositoryTest.java @@ -177,9 +177,8 @@ public void testLoadRepoWithCapabilitiesAndRequirement() throws Exception { public void testShowWrongUriInException() throws Exception { String uri = "src/test/resources/org/apache/karaf/shell/features/repo1.xml"; - RepositoryImpl r = new RepositoryImpl(new URI(uri)); try { - r.load(); + new RepositoryImpl(new URI(uri)); } catch (Exception e) { assertTrue(e.getMessage().contains(uri)); } diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/service/BlacklistTest.java b/features/core/src/test/java/org/apache/karaf/features/internal/service/BlacklistTest.java index 9d09e5673ba..956d04b361c 100644 --- a/features/core/src/test/java/org/apache/karaf/features/internal/service/BlacklistTest.java +++ b/features/core/src/test/java/org/apache/karaf/features/internal/service/BlacklistTest.java @@ -18,14 +18,14 @@ import static org.junit.Assert.assertTrue; -import java.net.URL; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Arrays; import java.util.Collections; import java.util.stream.Stream; import org.apache.karaf.features.BundleInfo; -import org.apache.karaf.features.internal.model.Feature; -import org.apache.karaf.features.internal.model.Features; -import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.karaf.features.Feature; import org.junit.Test; public class BlacklistTest { @@ -56,12 +56,15 @@ public void testBlacklistBundle() { assertTrue(bundles.noneMatch(b -> b.getLocation().equals(blacklisted))); } - private Stream blacklistWith(String blacklistClause) { - URL url = getClass().getResource("f02.xml"); - Features features = JaxbUtil.unmarshal(url.toExternalForm(), true); - + private Stream blacklistWith(String blacklistClause) { + URI uri; + try { + uri = getClass().getResource("f02.xml").toURI(); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } Blacklist blacklist = new Blacklist(Collections.singletonList(blacklistClause)); - blacklist.blacklist(features); - return features.getFeature().stream(); + RepositoryImpl features = new RepositoryImpl(uri, blacklist, true); + return Arrays.asList(features.getFeatures()).stream(); } } diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/service/DeployerTest.java b/features/core/src/test/java/org/apache/karaf/features/internal/service/DeployerTest.java index c9cc4ae6b68..a08d7c569cd 100644 --- a/features/core/src/test/java/org/apache/karaf/features/internal/service/DeployerTest.java +++ b/features/core/src/test/java/org/apache/karaf/features/internal/service/DeployerTest.java @@ -71,7 +71,6 @@ public void testInstallSimpleFeature() throws Exception { TestDownloadManager manager = new TestDownloadManager(getClass(), dataDir); RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI()); - repo.load(true); Feature f100 = repo.getFeatures()[0]; Feature f101 = repo.getFeatures()[1]; @@ -145,7 +144,6 @@ public void testUpdateSimpleFeature() throws Exception { TestDownloadManager manager = new TestDownloadManager(getClass(), dataDir); RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI()); - repo.load(true); Feature f100 = repo.getFeatures()[0]; Feature f101 = repo.getFeatures()[1]; @@ -247,7 +245,6 @@ public void testUpdateServiceBundle() throws Exception { TestDownloadManager manager = new TestDownloadManager(getClass(), dataDir); RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI()); - repo.load(true); Feature f1 = repo.getFeatures()[0]; Bundle serviceBundle = createTestBundle(1, Bundle.ACTIVE, dataDir, "a100"); @@ -318,7 +315,6 @@ public void testPrerequisite() throws Exception { TestDownloadManager manager = new TestDownloadManager(getClass(), dataDir); RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI()); - repo.load(true); Feature f1 = repo.getFeatures()[0]; Feature f2 = repo.getFeatures()[1]; @@ -479,7 +475,6 @@ private void doTestPrereqOnPrereq(int scenario) throws Exception { TestDownloadManager manager = new TestDownloadManager(getClass(), dataDir); RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI()); - repo.load(true); Map bundles = new HashMap<>(); bundles.put("a100", createTestBundle(1, Bundle.ACTIVE, dataDir, "a100")); diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesValidationTest.java b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesValidationTest.java index 09badc5284d..e21c854e235 100644 --- a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesValidationTest.java +++ b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesValidationTest.java @@ -24,28 +24,28 @@ import java.net.URI; +import org.apache.karaf.features.Feature; import org.apache.karaf.features.Library; -import org.apache.karaf.features.internal.model.Features; -import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.karaf.features.Repository; import org.junit.Test; public class FeaturesValidationTest { @Test public void testNs10() throws Exception { - Features features = unmarshalAndValidate("f02.xml"); + Repository features = unmarshalAndValidate("f02.xml"); assertNotNull(features); } @Test public void testNs10NoName() throws Exception { - Features features = unmarshalAndValidate("f03.xml"); + Repository features = unmarshalAndValidate("f03.xml"); assertNotNull(features); } @Test public void testNs11() throws Exception { - Features features = unmarshalAndValidate("f04.xml");; + Repository features = unmarshalAndValidate("f04.xml");; assertNotNull(features); } @@ -61,27 +61,30 @@ public void testNs11NoName() throws Exception { @Test public void testNs12Unmarshall() throws Exception { - Features features = unmarshalAndValidate("f06.xml"); + Repository features = unmarshalAndValidate("f06.xml"); assertNotNull(features); } @Test public void testNs13() throws Exception { - Features features = unmarshalAndValidate("f07.xml"); + Repository features = unmarshalAndValidate("f07.xml"); assertNotNull(features); - assertEquals("2.5.6.SEC02", features.getFeature().get(0).getVersion()); - assertTrue(features.getFeature().get(1).isHidden()); - assertNotNull(features.getFeature().get(1).getLibraries()); - assertEquals(1, features.getFeature().get(0).getLibraries().size()); - assertEquals("my-library", features.getFeature().get(0).getLibraries().get(0).getLocation()); - assertEquals(Library.TYPE_ENDORSED, features.getFeature().get(0).getLibraries().get(0).getType()); - assertFalse(features.getFeature().get(0).getLibraries().get(0).isExport()); - assertTrue(features.getFeature().get(0).getLibraries().get(0).isDelegate()); + Feature f0 = features.getFeatures()[0]; + Feature f1 = features.getFeatures()[1]; + assertEquals("2.5.6.SEC02", f0.getVersion()); + assertTrue(f1.isHidden()); + assertNotNull(f1.getLibraries()); + assertEquals(1, f0.getLibraries().size()); + Library lib = f0.getLibraries().get(0); + assertEquals("my-library", lib.getLocation()); + assertEquals(Library.TYPE_ENDORSED, lib.getType()); + assertFalse(lib.isExport()); + assertTrue(lib.isDelegate()); } - private Features unmarshalAndValidate(String path) throws Exception { + private Repository unmarshalAndValidate(String path) throws Exception { URI uri = getClass().getResource(path).toURI(); - return JaxbUtil.unmarshal(uri.toASCIIString(), true); + return new RepositoryImpl(uri, null, true); } }