use of org.apache.karaf.features.Feature in project karaf by apache.
the class FeatureDeploymentListener method bundleChanged.
public synchronized void bundleChanged(BundleEvent bundleEvent) {
// Only handle resolved and uninstalled events
if (bundleEvent.getType() != BundleEvent.RESOLVED && bundleEvent.getType() != BundleEvent.UNINSTALLED) {
return;
}
Bundle bundle = bundleEvent.getBundle();
try {
// Remove previous informations
List<URI> repsToRemove = new ArrayList<>();
List<String> reqsToRemove = new ArrayList<>();
// Remove old properties
String prefix = "bundle." + bundle.getBundleId();
String countStr = (String) properties.remove(prefix + ".reps.count");
if (countStr != null) {
int count = Integer.parseInt(countStr);
for (int i = 0; i < count; i++) {
String rep = (String) properties.remove(prefix + ".reps.item" + i);
repsToRemove.add(URI.create(rep));
}
}
countStr = (String) properties.remove(prefix + ".reqs.count");
if (countStr != null) {
int count = Integer.parseInt(countStr);
for (int i = 0; i < count; i++) {
String req = (String) properties.remove(prefix + ".reqs.item" + i);
reqsToRemove.add(req);
}
}
saveProperties();
// Compute new information
List<URI> repsToAdd = new ArrayList<>();
List<String> reqsToAdd = new ArrayList<>();
if (bundleEvent.getType() == BundleEvent.RESOLVED) {
Enumeration featuresUrlEnumeration = bundle.findEntries("/META-INF/" + FEATURE_PATH + "/", "*.xml", false);
while (featuresUrlEnumeration != null && featuresUrlEnumeration.hasMoreElements()) {
URL url = (URL) featuresUrlEnumeration.nextElement();
URI uri = url.toURI();
repsToAdd.add(uri);
Repository rep = featuresService.createRepository(uri);
Stream.of(rep.getFeatures()).filter(f -> f.getInstall() == null || Feature.DEFAULT_INSTALL_MODE.equals(f.getInstall())).map(f -> "feature:" + f.getName() + "/" + new VersionRange(f.getVersion(), true)).forEach(reqsToAdd::add);
}
if (!repsToAdd.isEmpty()) {
properties.put(prefix + ".reps.count", Integer.toString(repsToAdd.size()));
for (int i = 0; i < repsToAdd.size(); i++) {
properties.put(prefix + ".reps.item" + i, repsToAdd.get(i).toASCIIString());
}
properties.put(prefix + ".reqs.count", Integer.toString(reqsToAdd.size()));
for (int i = 0; i < reqsToAdd.size(); i++) {
properties.put(prefix + ".reqs.item" + i, reqsToAdd.get(i));
}
}
}
saveProperties();
// Call features service
List<Repository> requiredRepos = Arrays.asList(featuresService.listRequiredRepositories());
Set<URI> requiredReposUris = requiredRepos.stream().map(Repository::getURI).collect(Collectors.toSet());
requiredReposUris.removeAll(repsToRemove);
requiredReposUris.addAll(repsToAdd);
Map<String, Set<String>> requirements = featuresService.listRequirements();
requirements.get(ROOT_REGION).removeAll(reqsToRemove);
requirements.get(ROOT_REGION).addAll(reqsToAdd);
if (!reqsToRemove.isEmpty() || !reqsToAdd.isEmpty()) {
featuresService.updateReposAndRequirements(requiredReposUris, requirements, EnumSet.noneOf(FeaturesService.Option.class));
}
} catch (Exception e) {
logger.error("Unable to update deployed features for bundle: " + bundle.getSymbolicName() + " - " + bundle.getVersion(), e);
}
}
use of org.apache.karaf.features.Feature in project karaf by apache.
the class KarafTestSupport method assertFeatureInstalled.
public void assertFeatureInstalled(String featureName, String featureVersion) throws Exception {
Feature featureToAssert = featureService.getFeatures(featureName, featureVersion)[0];
Feature[] features = featureService.listInstalledFeatures();
for (Feature feature : features) {
if (featureToAssert.equals(feature)) {
return;
}
}
Assert.fail("Feature " + featureName + (featureVersion != null ? "/" + featureVersion : "") + " should be installed but is not");
}
use of org.apache.karaf.features.Feature in project karaf by apache.
the class KarafTestSupport method uninstallNewFeatures.
/**
* The feature service does not uninstall feature dependencies when uninstalling a single feature.
* So we need to make sure we uninstall all features that were newly installed.
*
* @param featuresBefore
* @throws Exception
*/
public void uninstallNewFeatures(Set<Feature> featuresBefore) throws Exception {
Feature[] features = featureService.listInstalledFeatures();
Set<String> uninstall = new HashSet<>();
for (Feature curFeature : features) {
if (!featuresBefore.contains(curFeature)) {
uninstall.add(curFeature.getId());
}
}
try {
System.out.println("Uninstalling " + uninstall);
featureService.uninstallFeatures(uninstall, NO_AUTO_REFRESH);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
use of org.apache.karaf.features.Feature in project karaf by apache.
the class FeaturesServiceImplTest method testListFeatureWithoutVersion.
@Test
public void testListFeatureWithoutVersion() throws Exception {
Feature transactionFeature = feature("transaction", "1.0.0");
FeaturesServiceImpl impl = featuresServiceWithFeatures(transactionFeature);
assertNotNull(impl.getFeatures("transaction", null));
assertSame(transactionFeature, impl.getFeatures("transaction", org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION)[0]);
}
use of org.apache.karaf.features.Feature in project karaf by apache.
the class FeaturesServiceImplTest method testGetFeatureStripVersion.
@Test
public void testGetFeatureStripVersion() throws Exception {
Feature transactionFeature = feature("transaction", "1.0.0");
FeaturesServiceImpl impl = featuresServiceWithFeatures(transactionFeature);
Feature[] features = impl.getFeatures("transaction", " 1.0.0 ");
assertEquals(1, features.length);
Feature feature = features[0];
assertNotNull(feature);
assertSame("transaction", feature.getName());
}
Aggregations