Skip to content

Commit

Permalink
[KARAF-5308] Use upfront loading for repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
cschneider committed Aug 15, 2017
1 parent eb95bce commit a75faba
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 63 deletions.
Expand Up @@ -254,15 +254,14 @@ void bootDone() {
public void registerListener(FeaturesListener listener) {
listeners.add(listener);
try {
Set<String> repositories = new TreeSet<>();
Set<String> repositoriesList = new TreeSet<>();
Map<String, Set<String>> 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<String, Set<String>> entry : installedFeatures.entrySet()) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -610,7 +609,7 @@ protected Map<String, Map<String, Feature>> 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);
}
Expand Down Expand Up @@ -979,7 +978,7 @@ private <T> Set<T> diff(Set<T> s1, Set<T> s2) {

@Override
public Repository createRepository(URI uri) throws Exception {
return repositories.create(uri, true, true);
return repositories.create(uri, true);
}

@Override
Expand Down
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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())
Expand All @@ -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);
}
}
}
Expand Down
Expand Up @@ -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));
}
Expand Down
Expand Up @@ -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 {
Expand Down Expand Up @@ -56,12 +56,15 @@ public void testBlacklistBundle() {
assertTrue(bundles.noneMatch(b -> b.getLocation().equals(blacklisted)));
}

private Stream<Feature> blacklistWith(String blacklistClause) {
URL url = getClass().getResource("f02.xml");
Features features = JaxbUtil.unmarshal(url.toExternalForm(), true);

private Stream<org.apache.karaf.features.Feature> 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();
}
}
Expand Up @@ -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];

Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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<String, Bundle> bundles = new HashMap<>();
bundles.put("a100", createTestBundle(1, Bundle.ACTIVE, dataDir, "a100"));
Expand Down
Expand Up @@ -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);
}

Expand All @@ -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);
}

}

0 comments on commit a75faba

Please sign in to comment.