You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2014/04/18 23:35:53 UTC
[3/6] [KARAF-2923] Region support in features service
http://git-wip-us.apache.org/repos/asf/karaf/blob/2705ad88/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
----------------------------------------------------------------------
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 0243dc0..84b9529 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
@@ -48,14 +48,16 @@ import org.apache.karaf.features.FeaturesListener;
import org.apache.karaf.features.FeaturesService;
import org.apache.karaf.features.Repository;
import org.apache.karaf.features.RepositoryEvent;
-import org.apache.karaf.features.internal.deployment.DeploymentBuilder;
-import org.apache.karaf.features.internal.deployment.StreamProvider;
-import org.apache.karaf.features.internal.resolver.FeatureNamespace;
-import org.apache.karaf.features.internal.resolver.UriNamespace;
+import org.apache.karaf.features.internal.download.StreamProvider;
+import org.apache.karaf.features.internal.region.ResourceComparator;
+import org.apache.karaf.features.internal.region.SubsystemResolver;
import org.apache.karaf.features.internal.util.ChecksumUtils;
import org.apache.karaf.features.internal.util.Macro;
import org.apache.karaf.features.internal.util.MultiException;
import org.apache.karaf.util.collections.CopyOnWriteArrayIdentityList;
+import org.eclipse.equinox.region.Region;
+import org.eclipse.equinox.region.RegionDigraph;
+import org.eclipse.equinox.region.RegionFilterBuilder;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
@@ -64,6 +66,7 @@ import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.Version;
+import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.framework.startlevel.BundleStartLevel;
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.framework.wiring.BundleWire;
@@ -76,12 +79,18 @@ import org.slf4j.LoggerFactory;
import static org.apache.felix.resolver.Util.getSymbolicName;
import static org.apache.felix.resolver.Util.getVersion;
+import static org.apache.karaf.features.internal.util.MapUtils.addToMapSet;
+import static org.apache.karaf.features.internal.util.MapUtils.copyMapSet;
+import static org.apache.karaf.features.internal.resolver.ResourceUtils.*;
+import static org.apache.karaf.features.internal.util.MapUtils.removeFromMapSet;
/**
*
*/
public class FeaturesServiceImpl implements FeaturesService {
+ public static final String ROOT_REGION = "root";
+
public static final String UPDATE_SNAPSHOTS_NONE = "none";
public static final String UPDATE_SNAPSHOTS_CRC = "crc";
public static final String UPDATE_SNAPSHOTS_ALWAYS = "always";
@@ -114,6 +123,7 @@ public class FeaturesServiceImpl implements FeaturesService {
private final FeatureFinder featureFinder;
private final EventAdminListener eventAdminListener;
private final FeatureConfigInstaller configInstaller;
+ private final RegionDigraph digraph;
private final String overrides;
/**
* Range to use when a version is specified on a feature dependency.
@@ -150,6 +160,7 @@ public class FeaturesServiceImpl implements FeaturesService {
FeatureFinder featureFinder,
EventAdminListener eventAdminListener,
FeatureConfigInstaller configInstaller,
+ RegionDigraph digraph,
String overrides,
String featureResolutionRange,
String bundleUpdateRange,
@@ -160,6 +171,7 @@ public class FeaturesServiceImpl implements FeaturesService {
this.featureFinder = featureFinder;
this.eventAdminListener = eventAdminListener;
this.configInstaller = configInstaller;
+ this.digraph = digraph;
this.overrides = overrides;
this.featureResolutionRange = featureResolutionRange;
this.bundleUpdateRange = bundleUpdateRange;
@@ -221,7 +233,7 @@ public class FeaturesServiceImpl implements FeaturesService {
Set<String> installedFeatures = new TreeSet<String>();
synchronized (lock) {
repositories.addAll(state.repositories);
- installedFeatures.addAll(state.installedFeatures);
+ installedFeatures.addAll(state.installedFeatures.keySet());
}
for (String uri : repositories) {
Repository repository = new RepositoryImpl(URI.create(uri));
@@ -547,15 +559,17 @@ public class FeaturesServiceImpl implements FeaturesService {
public boolean isInstalled(Feature f) {
String id = normalize(f.getId());
synchronized (lock) {
- return state.installedFeatures.contains(id);
+ Set<String> installed = state.installedFeatures.get(ROOT_REGION);
+ return installed != null && installed.contains(id);
}
}
@Override
public boolean isRequired(Feature f) {
- String id = normalize(f.getId());
+ String id = f.getName() + "/" + new VersionRange(f.getVersion(), true);
synchronized (lock) {
- return state.features.contains(id);
+ Set<String> features = state.features.get(ROOT_REGION);
+ return features != null && features.contains(id);
}
}
@@ -563,27 +577,37 @@ public class FeaturesServiceImpl implements FeaturesService {
// Installation and uninstallation of features
//
+ @Override
public void installFeature(String name) throws Exception {
installFeature(name, EnumSet.noneOf(Option.class));
}
+ @Override
public void installFeature(String name, String version) throws Exception {
installFeature(version != null ? name + "/" + version : name, EnumSet.noneOf(Option.class));
}
+ @Override
public void installFeature(String name, EnumSet<Option> options) throws Exception {
installFeatures(Collections.singleton(name), options);
}
+ @Override
public void installFeature(String name, String version, EnumSet<Option> options) throws Exception {
installFeature(version != null ? name + "/" + version : name, options);
}
+ @Override
public void installFeature(Feature feature, EnumSet<Option> options) throws Exception {
installFeature(feature.getId());
}
@Override
+ public void installFeatures(Set<String> features, EnumSet<Option> options) throws Exception {
+ installFeatures(features, ROOT_REGION, options);
+ }
+
+ @Override
public void uninstallFeature(String name, String version) throws Exception {
uninstallFeature(version != null ? name + "/" + version : name);
}
@@ -603,6 +627,11 @@ public class FeaturesServiceImpl implements FeaturesService {
uninstallFeatures(Collections.singleton(name), options);
}
+ @Override
+ public void uninstallFeatures(Set<String> features, EnumSet<Option> options) throws Exception {
+ uninstallFeatures(features, ROOT_REGION, options);
+ }
+
//
//
@@ -613,18 +642,12 @@ public class FeaturesServiceImpl implements FeaturesService {
//
-
-
-
-
- public void installFeatures(Set<String> features, EnumSet<Option> options) throws Exception {
- Set<String> required;
- Set<String> installed;
- Set<Long> managed;
- synchronized (lock) {
- required = new HashSet<String>(state.features);
- installed = new HashSet<String>(state.installedFeatures);
- managed = new HashSet<Long>(state.managedBundles);
+ @Override
+ public void installFeatures(Set<String> features, String region, EnumSet<Option> options) throws Exception {
+ State state = copyState();
+ Map<String, Set<String>> required = copyMapSet(state.features);
+ if (region == null || region.isEmpty()) {
+ region = ROOT_REGION;
}
List<String> featuresToAdd = new ArrayList<String>();
Map<String, Map<String, Feature>> featuresMap = getFeatures();
@@ -638,7 +661,8 @@ public class FeaturesServiceImpl implements FeaturesService {
throw new IllegalArgumentException("No matching features for " + feature);
}
} else {
- featuresToAdd.add(normalize(f.getId()));
+ String req = f.getName() + "/" + new VersionRange(f.getVersion(), true);
+ featuresToAdd.add(req);
}
}
featuresToAdd = new ArrayList<String>(new LinkedHashSet<String>(featuresToAdd));
@@ -651,18 +675,25 @@ public class FeaturesServiceImpl implements FeaturesService {
sb.append(featuresToAdd.get(i));
}
print(sb.toString(), options.contains(Option.Verbose));
- required.addAll(featuresToAdd);
- doInstallFeaturesInThread(required, installed, managed, options);
+ Set<String> fl = required.get(region);
+ if (fl == null) {
+ fl = new HashSet<String>();
+ required.put(region,fl);
+ }
+ fl.addAll(featuresToAdd);
+ doInstallFeaturesInThread(required, state, options);
}
- public void uninstallFeatures(Set<String> features, EnumSet<Option> options) throws Exception {
- Set<String> required;
- Set<String> installed;
- Set<Long> managed;
- synchronized (lock) {
- required = new HashSet<String>(state.features);
- installed = new HashSet<String>(state.installedFeatures);
- managed = new HashSet<Long>(state.managedBundles);
+ public void uninstallFeatures(Set<String> features, String region, EnumSet<Option> options) throws Exception {
+ State state = copyState();
+ Map<String, Set<String>> required = copyMapSet(state.features);
+ if (region == null || region.isEmpty()) {
+ region = ROOT_REGION;
+ }
+ Set<String> fl = required.get(region);
+ if (fl == null) {
+ fl = new HashSet<String>();
+ required.put(region,fl);
}
List<String> featuresToRemove = new ArrayList<String>();
for (String feature : new HashSet<String>(features)) {
@@ -670,15 +701,18 @@ public class FeaturesServiceImpl implements FeaturesService {
feature = normalize(feature);
if (feature.endsWith("/0.0.0")) {
String nameSep = feature.substring(0, feature.indexOf("/") + 1);
- for (String f : required) {
+ for (String f : fl) {
if (normalize(f).startsWith(nameSep)) {
toRemove.add(f);
}
}
} else {
- toRemove.add(feature);
+ String name = feature.substring(0, feature.indexOf("/"));
+ String version = feature.substring(feature.indexOf("/") + 1);
+ String req = name + "/" + new VersionRange(version, true);
+ toRemove.add(req);
}
- toRemove.retainAll(required);
+ toRemove.retainAll(fl);
if (toRemove.isEmpty()) {
throw new IllegalArgumentException("Feature named '" + feature + "' is not installed");
} else if (toRemove.size() > 1) {
@@ -706,8 +740,17 @@ public class FeaturesServiceImpl implements FeaturesService {
sb.append(featuresToRemove.get(i));
}
print(sb.toString(), options.contains(Option.Verbose));
- required.removeAll(featuresToRemove);
- doInstallFeaturesInThread(required, installed, managed, options);
+ fl.removeAll(featuresToRemove);
+ if (fl.isEmpty()) {
+ required.remove(fl);
+ }
+ doInstallFeaturesInThread(required, state, options);
+ }
+
+ private State copyState() {
+ synchronized (lock) {
+ return this.state.copy();
+ }
}
protected String normalize(String feature) {
@@ -726,16 +769,15 @@ public class FeaturesServiceImpl implements FeaturesService {
* the command may be interrupted while waiting for the refresh to be done, leading
* to bundles not being started after the refresh.
*/
- public void doInstallFeaturesInThread(final Set<String> features,
- final Set<String> installed,
- final Set<Long> managed,
+ public void doInstallFeaturesInThread(final Map<String, Set<String>> features,
+ final State state,
final EnumSet<Option> options) throws Exception {
ExecutorService executor = Executors.newCachedThreadPool();
try {
executor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
- doInstallFeatures(features, installed, managed, options);
+ doInstallFeatures(features, state, options);
return null;
}
}).get();
@@ -755,10 +797,9 @@ public class FeaturesServiceImpl implements FeaturesService {
}
}
- public void doInstallFeatures(Set<String> features, // all request features
- Set<String> installed, // installed features
- Set<Long> managed, // currently managed bundles
- EnumSet<Option> options // installation options
+ public void doInstallFeatures(Map<String, Set<String>> features, // all request features
+ State state, // current state
+ EnumSet<Option> options // installation options
) throws Exception {
boolean noRefreshUnmanaged = options.contains(Option.NoAutoRefreshUnmanagedBundles);
@@ -768,43 +809,56 @@ public class FeaturesServiceImpl implements FeaturesService {
boolean verbose = options.contains(Option.Verbose);
boolean simulate = options.contains(Option.Simulate);
+ Map<String, Set<String>> installed = state.installedFeatures;
+ Map<String, Set<Long>> managed = state.managedBundles;
+
// Get a list of resolved and unmanaged bundles to use as capabilities during resolution
- List<Resource> systemBundles = new ArrayList<Resource>();
+ List<BundleRevision> systemBundles = new ArrayList<BundleRevision>();
Bundle[] bundles = systemBundleContext.getBundles();
for (Bundle bundle : bundles) {
- if (bundle.getState() >= Bundle.RESOLVED && !managed.contains(bundle.getBundleId())) {
- Resource res = bundle.adapt(BundleRevision.class);
- systemBundles.add(res);
+ if (bundle.getState() >= Bundle.RESOLVED) {
+ boolean bm = false;
+ for (Set<Long> m : managed.values()) {
+ bm |= m.contains(bundle.getBundleId());
+ }
+ if (!bm) {
+ BundleRevision res = bundle.adapt(BundleRevision.class);
+ systemBundles.add(res);
+ }
}
}
// Resolve
// TODO: requirements
// TODO: bundles
- // TODO: regions: on isolated regions, we may need different resolution for each region
+ // TODO: overrides
Set<String> overrides = Overrides.loadOverrides(this.overrides);
Repository[] repositories = listRepositories();
- DeploymentBuilder builder = createDeploymentBuilder(repositories);
- builder.setFeatureRange(featureResolutionRange);
- builder.download(features,
- Collections.<String>emptySet(),
- Collections.<String>emptySet(),
- overrides,
- Collections.<String>emptySet());
- Map<Resource, List<Wire>> resolution = builder.resolve(systemBundles);
+
+ if (!installed.containsKey(ROOT_REGION)) {
+ installed.put(ROOT_REGION, new HashSet<String>());
+ }
+
+ SubsystemResolver resolver = new SubsystemResolver();
+ Map<Resource, List<Wire>> resolution = resolver.resolve(
+ Arrays.asList(repositories),
+ features,
+ systemBundles,
+ featureResolutionRange);
Collection<Resource> allResources = resolution.keySet();
- Map<String, StreamProvider> providers = builder.getProviders();
+ Map<String, StreamProvider> providers = resolver.getProviders();
+
+ Map<String, Set<String>> installedFeatures = getInstalledFeatures(resolver);
- // Install conditionals
List<String> installedFeatureIds = getFeatureIds(allResources);
List<String> newFeatures = new ArrayList<String>(installedFeatureIds);
- newFeatures.removeAll(installed);
- List<String> delFeatures = new ArrayList<String>(installed);
+ newFeatures.removeAll(installed.get(ROOT_REGION));
+ List<String> delFeatures = new ArrayList<String>(installed.get(ROOT_REGION));
delFeatures.removeAll(installedFeatureIds);
//
// Compute list of installable resources (those with uris)
//
- List<Resource> resources = getBundles(allResources);
+ Map<Resource, String> resources = resolver.getBundles();
// Compute information for each bundle
Map<String, BundleInfo> bundleInfos = new HashMap<String, BundleInfo>();
@@ -823,14 +877,10 @@ public class FeaturesServiceImpl implements FeaturesService {
// Get all resources that will be used to satisfy the old features set
Set<Resource> resourceLinkedToOldFeatures = new HashSet<Resource>();
if (noStart) {
- for (Resource resource : resolution.keySet()) {
- String name = FeatureNamespace.getName(resource);
- if (name != null) {
- Version version = FeatureNamespace.getVersion(resource);
- String id = version != null ? name + "/" + version : name;
- if (installed.contains(id)) {
- addTransitive(resource, resourceLinkedToOldFeatures, resolution);
- }
+ for (Resource resource : resolver.getFeatures().keySet()) {
+ String id = getFeatureId(resource);
+ if (installed.get(ROOT_REGION).contains(id)) {
+ addTransitive(resource, resourceLinkedToOldFeatures, resolution);
}
}
}
@@ -838,15 +888,10 @@ public class FeaturesServiceImpl implements FeaturesService {
//
// Compute deployment
//
- Map<String, Long> bundleChecksums = new HashMap<String, Long>();
- synchronized (lock) {
- bundleChecksums.putAll(state.bundleChecksums);
- }
- Deployment deployment = computeDeployment(managed, bundles, providers, resources, bundleChecksums);
+ // TODO: compute bundles/region mapping to support multiple bundles and bunde moving into a different region
+ Deployment deployment = computeDeployment(resolver, state);
- if (deployment.toDelete.isEmpty() &&
- deployment.toUpdate.isEmpty() &&
- deployment.toInstall.isEmpty()) {
+ if (deployment.regions.isEmpty()) {
print("No deployment change.", verbose);
return;
}
@@ -859,8 +904,10 @@ public class FeaturesServiceImpl implements FeaturesService {
// Compute the set of bundles to refresh
//
Set<Bundle> toRefresh = new HashSet<Bundle>();
- toRefresh.addAll(deployment.toDelete);
- toRefresh.addAll(deployment.toUpdate.keySet());
+ for (RegionDeployment regionDeployment : deployment.regions.values()) {
+ toRefresh.addAll(regionDeployment.toDelete);
+ toRefresh.addAll(regionDeployment.toUpdate.keySet());
+ }
if (!noRefreshManaged) {
int size;
@@ -907,8 +954,11 @@ public class FeaturesServiceImpl implements FeaturesService {
if (noRefreshUnmanaged) {
Set<Bundle> newSet = new HashSet<Bundle>();
for (Bundle bundle : toRefresh) {
- if (managed.contains(bundle.getBundleId())) {
- newSet.add(bundle);
+ for (Set<Long> m : managed.values()) {
+ if (m.contains(bundle.getBundleId())) {
+ newSet.add(bundle);
+ break;
+ }
}
}
toRefresh = newSet;
@@ -932,12 +982,14 @@ public class FeaturesServiceImpl implements FeaturesService {
//
// TODO: handle update on the features service itself
- if (deployment.toUpdate.containsKey(bundle) ||
- deployment.toDelete.contains(bundle)) {
+ RegionDeployment rootRegionDeployment = deployment.regions.get(ROOT_REGION);
+ if (rootRegionDeployment != null &&
+ (rootRegionDeployment.toUpdate.containsKey(bundle)
+ || rootRegionDeployment.toDelete.contains(bundle))) {
LOGGER.warn("Updating or uninstalling of the FeaturesService is not supported");
- deployment.toUpdate.remove(bundle);
- deployment.toDelete.remove(bundle);
+ rootRegionDeployment.toUpdate.remove(bundle);
+ rootRegionDeployment.toDelete.remove(bundle);
}
@@ -945,10 +997,14 @@ public class FeaturesServiceImpl implements FeaturesService {
// Perform bundle operations
//
+ //
// Stop bundles by chunks
+ //
Set<Bundle> toStop = new HashSet<Bundle>();
- toStop.addAll(deployment.toUpdate.keySet());
- toStop.addAll(deployment.toDelete);
+ for (RegionDeployment regionDeployment : deployment.regions.values()) {
+ toStop.addAll(regionDeployment.toUpdate.keySet());
+ toStop.addAll(regionDeployment.toDelete);
+ }
removeFragmentsAndBundlesInState(toStop, Bundle.UNINSTALLED | Bundle.RESOLVED | Bundle.STOPPING);
if (!toStop.isEmpty()) {
print("Stopping bundles:", verbose);
@@ -961,67 +1017,162 @@ public class FeaturesServiceImpl implements FeaturesService {
}
}
}
- if (!deployment.toDelete.isEmpty()) {
+
+ //
+ // Delete bundles
+ //
+ boolean hasToDelete = false;
+ for (RegionDeployment regionDeployment : deployment.regions.values()) {
+ if ((hasToDelete = !regionDeployment.toDelete.isEmpty())) {
+ break;
+ }
+ }
+ if (hasToDelete) {
print("Uninstalling bundles:", verbose);
- for (Bundle bundle : deployment.toDelete) {
- print(" " + bundle.getSymbolicName() + " / " + bundle.getVersion(), verbose);
- bundle.uninstall();
- managed.remove(bundle.getBundleId());
+ for (Map.Entry<String, RegionDeployment> entry : deployment.regions.entrySet()) {
+ String name = entry.getKey();
+ RegionDeployment regionDeployment = entry.getValue();
+ for (Bundle bundle : regionDeployment.toDelete) {
+ print(" " + bundle.getSymbolicName() + " / " + bundle.getVersion(), verbose);
+ bundle.uninstall();
+ removeFromMapSet(managed, name, bundle.getBundleId());
+ }
}
}
- if (!deployment.toUpdate.isEmpty()) {
- print("Updating bundles:", verbose);
- for (Map.Entry<Bundle, Resource> entry : deployment.toUpdate.entrySet()) {
- Bundle bundle = entry.getKey();
- Resource resource = entry.getValue();
- String uri = UriNamespace.getUri(resource);
- print(" " + uri, verbose);
- InputStream is = getBundleInputStream(resource, providers);
- bundle.update(is);
- toStart.add(bundle);
- BundleInfo bi = bundleInfos.get(uri);
- if (bi != null && bi.getStartLevel() > 0) {
- bundle.adapt(BundleStartLevel.class).setStartLevel(bi.getStartLevel());
+
+ //
+ // Update regions
+ //
+ // TODO: this replace the whole digraph with the computed one
+ // TODO: we need to be smarter and allow user modifications or
+ // TODO: externally managed regions such as the one managed
+ // TODO: by aries subsystems
+ RegionDigraph clone = this.digraph.copy();
+ RegionDigraph computedDigraph = resolver.getDigraph();
+ for (Region r : clone.getRegions()) {
+ clone.removeRegion(r);
+ }
+ Map<String, String> flats = resolver.getFlatSubsystemsMap();
+ for (Region r : computedDigraph.getRegions()) {
+ if (r.getName().equals(flats.get(r.getName()))) {
+ clone.createRegion(r.getName());
+ }
+ }
+ for (Region r : computedDigraph.getRegions()) {
+ for (RegionDigraph.FilteredRegion fr : computedDigraph.getEdges(r)) {
+ String rt = flats.get(r.getName());
+ String rh = flats.get(fr.getRegion().getName());
+ if (!rh.equals(rt)) {
+ Region tail = clone.getRegion(rt);
+ Region head = clone.getRegion(rh);
+ RegionFilterBuilder rfb = clone.createRegionFilterBuilder();
+ for (Map.Entry<String, Collection<String>> entry : fr.getFilter().getSharingPolicy().entrySet()) {
+ // Discard osgi.identity namespace
+ if (!IdentityNamespace.IDENTITY_NAMESPACE.equals(entry.getKey())) {
+ for (String f : entry.getValue()) {
+ rfb.allow(entry.getKey(), f);
+ }
+ }
+ }
+ clone.connect(tail, rfb.build(), head);
}
- // TODO: handle region
}
}
- if (!deployment.toInstall.isEmpty()) {
- print("Installing bundles:", verbose);
- for (Resource resource : deployment.toInstall) {
- String uri = UriNamespace.getUri(resource);
- print(" " + uri, verbose);
- InputStream is = getBundleInputStream(resource, providers);
- Bundle bundle = systemBundleContext.installBundle(uri, is);
- managed.add(bundle.getBundleId());
- if (!noStart || resourceLinkedToOldFeatures.contains(resource)) {
+ // Spread bundles across regions
+ Region root = clone.getRegion(ROOT_REGION);
+ for (BundleRevision revision : systemBundles) {
+ root.addBundle(revision.getBundle());
+ }
+ for (Resource resource : resources.keySet()) {
+ Bundle bundle = deployment.resToBnd.get(resource);
+ if (bundle != null) {
+ String region = resources.get(resource);
+ clone.getRegion(region).addBundle(bundle);
+ }
+ }
+ this.digraph.replace(clone);
+
+ //
+ // Update bundles
+ //
+ boolean hasToUpdate = false;
+ for (RegionDeployment regionDeployment : deployment.regions.values()) {
+ if ((hasToUpdate = !regionDeployment.toUpdate.isEmpty())) {
+ break;
+ }
+ }
+ if (hasToUpdate) {
+ print("Updating bundles:", verbose);
+ for (RegionDeployment regionDeployment : deployment.regions.values()) {
+ for (Map.Entry<Bundle, Resource> entry : regionDeployment.toUpdate.entrySet()) {
+ Bundle bundle = entry.getKey();
+ Resource resource = entry.getValue();
+ String uri = getUri(resource);
+ print(" " + uri, verbose);
+ InputStream is = getBundleInputStream(resource, providers);
+ bundle.update(is);
toStart.add(bundle);
+ BundleInfo bi = bundleInfos.get(uri);
+ if (bi != null && bi.getStartLevel() > 0) {
+ bundle.adapt(BundleStartLevel.class).setStartLevel(bi.getStartLevel());
+ }
}
- deployment.resToBnd.put(resource, bundle);
- // save a checksum of installed snapshot bundle
- if (UPDATE_SNAPSHOTS_CRC.equals(updateSnaphots)
- && isUpdateable(resource) && !deployment.newCheckums.containsKey(bundle.getLocation())) {
- deployment.newCheckums.put(bundle.getLocation(), ChecksumUtils.checksum(getBundleInputStream(resource, providers)));
- }
- BundleInfo bi = bundleInfos.get(uri);
- if (bi != null && bi.getStartLevel() > 0) {
- bundle.adapt(BundleStartLevel.class).setStartLevel(bi.getStartLevel());
+ }
+ }
+ //
+ // Install bundles
+ //
+ boolean hasToInstall = false;
+ for (RegionDeployment regionDeployment : deployment.regions.values()) {
+ if ((hasToInstall = !regionDeployment.toInstall.isEmpty())) {
+ break;
+ }
+ }
+ if (hasToInstall) {
+ print("Installing bundles:", verbose);
+ for (Map.Entry<String, RegionDeployment> entry : deployment.regions.entrySet()) {
+ String name = entry.getKey();
+ Region region = digraph.getRegion(name);
+ RegionDeployment regionDeployment = entry.getValue();
+ for (Resource resource : regionDeployment.toInstall) {
+ String uri = getUri(resource);
+ print(" " + uri, verbose);
+ InputStream is = getBundleInputStream(resource, providers);
+ Bundle bundle;
+ if (ROOT_REGION.equals(name)) {
+ bundle = region.installBundleAtLocation(uri, is);
+ } else {
+ bundle = region.installBundle(uri, is);
+ }
+ addToMapSet(managed, name, bundle.getBundleId());
+ if (!noStart || resourceLinkedToOldFeatures.contains(resource)) {
+ toStart.add(bundle);
+ }
+ deployment.resToBnd.put(resource, bundle);
+ // save a checksum of installed snapshot bundle
+ if (UPDATE_SNAPSHOTS_CRC.equals(updateSnaphots)
+ && isUpdateable(resource) && !deployment.newCheckums.containsKey(bundle.getBundleId())) {
+ deployment.newCheckums.put(bundle.getBundleId(), ChecksumUtils.checksum(getBundleInputStream(resource, providers)));
+ }
+ BundleInfo bi = bundleInfos.get(uri);
+ if (bi != null && bi.getStartLevel() > 0) {
+ bundle.adapt(BundleStartLevel.class).setStartLevel(bi.getStartLevel());
+ }
}
- // TODO: handle region
}
}
- //
+ //
// Update and save state
//
synchronized (lock) {
- state.bundleChecksums.putAll(deployment.newCheckums);
- state.features.clear();
- state.features.addAll(features);
- state.installedFeatures.clear();
- state.installedFeatures.addAll(installedFeatureIds);
- state.managedBundles.clear();
- state.managedBundles.addAll(managed);
+ this.state.bundleChecksums.putAll(deployment.newCheckums);
+ this.state.features.clear();
+ this.state.features.putAll(features);
+ this.state.installedFeatures.clear();
+ this.state.installedFeatures.putAll(installedFeatures);
+ this.state.managedBundles.clear();
+ this.state.managedBundles.putAll(managed);
saveState();
}
@@ -1134,122 +1285,145 @@ public class FeaturesServiceImpl implements FeaturesService {
}
}
- protected void logDeployment(Deployment deployment, boolean verbose) {
+ protected void logDeployment(Deployment overallDeployment, boolean verbose) {
print("Changes to perform:", verbose);
- if (!deployment.toDelete.isEmpty()) {
- print(" Bundles to uninstall:", verbose);
- for (Bundle bundle : deployment.toDelete) {
- print(" " + bundle.getSymbolicName() + " / " + bundle.getVersion(), verbose);
+ for (Map.Entry<String, RegionDeployment> region : overallDeployment.regions.entrySet()) {
+ RegionDeployment deployment = region.getValue();
+ print(" Region: " + region.getKey(), verbose);
+ if (!deployment.toDelete.isEmpty()) {
+ print(" Bundles to uninstall:", verbose);
+ for (Bundle bundle : deployment.toDelete) {
+ print(" " + bundle.getSymbolicName() + " / " + bundle.getVersion(), verbose);
+ }
}
- }
- if (!deployment.toUpdate.isEmpty()) {
- print(" Bundles to update:", verbose);
- for (Map.Entry<Bundle, Resource> entry : deployment.toUpdate.entrySet()) {
- print(" " + entry.getKey().getSymbolicName() + " / " + entry.getKey().getVersion() + " with " + UriNamespace.getUri(entry.getValue()), verbose);
+ if (!deployment.toUpdate.isEmpty()) {
+ print(" Bundles to update:", verbose);
+ for (Map.Entry<Bundle, Resource> entry : deployment.toUpdate.entrySet()) {
+ print(" " + entry.getKey().getSymbolicName() + " / " + entry.getKey().getVersion() + " with " + getUri(entry.getValue()), verbose);
+ }
}
- }
- if (!deployment.toInstall.isEmpty()) {
- print(" Bundles to install:", verbose);
- for (Resource resource : deployment.toInstall) {
- print(" " + UriNamespace.getUri(resource), verbose);
+ if (!deployment.toInstall.isEmpty()) {
+ print(" Bundles to install:", verbose);
+ for (Resource resource : deployment.toInstall) {
+ print(" " + getUri(resource), verbose);
+ }
}
}
}
protected Deployment computeDeployment(
- Set<Long> managed,
- Bundle[] bundles,
- Map<String, StreamProvider> providers,
- List<Resource> resources,
- Map<String, Long> bundleChecksums) throws IOException {
- Deployment deployment = new Deployment();
-
- // TODO: regions
- List<Resource> toDeploy = new ArrayList<Resource>(resources);
-
- // First pass: go through all installed bundles and mark them
- // as either to ignore or delete
- for (Bundle bundle : bundles) {
- if (bundle.getSymbolicName() != null && bundle.getBundleId() != 0) {
- Resource resource = null;
- for (Resource res : toDeploy) {
- if (bundle.getSymbolicName().equals(getSymbolicName(res))) {
- if (bundle.getVersion().equals(getVersion(res))) {
- resource = res;
- break;
+ SubsystemResolver resolver,
+ State state) throws IOException {
+
+ Deployment result = new Deployment();
+
+ Map<String, Set<Resource>> bundlesPerRegions = resolver.getBundlesPerRegions();
+
+ // Gather all regions, including old ones and new ones
+ Set<String> regions = new HashSet<String>();
+ regions.addAll(state.managedBundles.keySet());
+ regions.addAll(bundlesPerRegions.keySet());
+
+ for (String region : regions) {
+
+ RegionDeployment deployment = new RegionDeployment();
+
+ // Get the list of bundles currently assigned in the region
+ Set<Long> managed = state.managedBundles.get(region);
+ if (managed == null) {
+ managed = Collections.emptySet();
+ }
+
+ // Compute the list of resources to deploy in the region
+ Set<Resource> bundlesInRegion = bundlesPerRegions.get(region);
+ List<Resource> toDeploy = bundlesInRegion != null
+ ? new ArrayList<Resource>(bundlesInRegion) : new ArrayList<Resource>();
+
+ // First pass: go through all installed bundles and mark them
+ // as either to ignore or delete
+ for (long bundleId : managed) {
+ // Look for the installed bundle
+ Bundle bundle = systemBundleContext.getBundle(bundleId);
+ // Bundle has been manually uninstalled ?
+ if (bundle != null) {
+ // Look for a matching resource
+ Resource resource = null;
+ for (Resource res : toDeploy) {
+ if (bundle.getSymbolicName().equals(getSymbolicName(res))) {
+ if (bundle.getVersion().equals(getVersion(res))) {
+ resource = res;
+ break;
+ }
}
}
- }
- // We found a matching bundle
- if (resource != null) {
- // In case of snapshots, check if the snapshot is out of date
- // and flag it as to update
- if (managed.contains(bundle.getBundleId()) && isUpdateable(resource)) {
- // Always update snapshots
- if (UPDATE_SNAPSHOTS_ALWAYS.equalsIgnoreCase(updateSnaphots)) {
- LOGGER.debug("Update snapshot for " + bundle.getLocation());
- deployment.toUpdate.put(bundle, resource);
- }
- else if (UPDATE_SNAPSHOTS_CRC.equalsIgnoreCase(updateSnaphots)) {
- // if the checksum are different
- InputStream is = null;
- try {
- is = getBundleInputStream(resource, providers);
- long newCrc = ChecksumUtils.checksum(is);
- long oldCrc = bundleChecksums.containsKey(bundle.getLocation()) ? bundleChecksums.get(bundle.getLocation()) : 0l;
- if (newCrc != oldCrc) {
- LOGGER.debug("New snapshot available for " + bundle.getLocation());
- deployment.toUpdate.put(bundle, resource);
- deployment.newCheckums.put(bundle.getLocation(), newCrc);
- }
- } finally {
- if (is != null) {
- is.close();
+ // We found a matching bundle
+ if (resource != null) {
+ // In case of snapshots, check if the snapshot is out of date
+ // and flag it as to update
+ if (isUpdateable(resource)) {
+ // Always update snapshots
+ if (UPDATE_SNAPSHOTS_ALWAYS.equalsIgnoreCase(updateSnaphots)) {
+ LOGGER.debug("Update snapshot for " + bundle.getLocation());
+ deployment.toUpdate.put(bundle, resource);
+ } else if (UPDATE_SNAPSHOTS_CRC.equalsIgnoreCase(updateSnaphots)) {
+ // if the checksum are different
+ InputStream is = null;
+ try {
+ is = getBundleInputStream(resource, resolver.getProviders());
+ long newCrc = ChecksumUtils.checksum(is);
+ long oldCrc = state.bundleChecksums.containsKey(bundle.getBundleId()) ? state.bundleChecksums.get(bundle.getBundleId()) : 0l;
+ if (newCrc != oldCrc) {
+ LOGGER.debug("New snapshot available for " + bundle.getLocation());
+ deployment.toUpdate.put(bundle, resource);
+ result.newCheckums.put(bundle.getBundleId(), newCrc);
+ }
+ } finally {
+ if (is != null) {
+ is.close();
+ }
}
}
}
+ // We're done for this resource
+ toDeploy.remove(resource);
+ result.resToBnd.put(resource, bundle);
+ // There's no matching resource
+ // If the bundle is managed, we need to delete it
+ } else if (managed.contains(bundle.getBundleId())) {
+ deployment.toDelete.add(bundle);
}
- // We're done for this resource
- toDeploy.remove(resource);
- deployment.resToBnd.put(resource, bundle);
- // There's no matching resource
- // If the bundle is managed, we need to delete it
- } else if (managed.contains(bundle.getBundleId())) {
- deployment.toDelete.add(bundle);
}
}
- }
- // Second pass on remaining resources
- for (Resource resource : toDeploy) {
- TreeMap<Version, Bundle> matching = new TreeMap<Version, Bundle>();
- VersionRange range = new VersionRange(Macro.transform(bundleUpdateRange, getVersion(resource).toString()));
- for (Bundle bundle : deployment.toDelete) {
- if (bundle.getSymbolicName().equals(getSymbolicName(resource)) && range.contains(bundle.getVersion())) {
- matching.put(bundle.getVersion(), bundle);
+ // Second pass on remaining resources
+ for (Resource resource : toDeploy) {
+ TreeMap<Version, Bundle> matching = new TreeMap<Version, Bundle>();
+ VersionRange range = new VersionRange(Macro.transform(bundleUpdateRange, getVersion(resource).toString()));
+ for (Bundle bundle : deployment.toDelete) {
+ if (bundle.getSymbolicName().equals(getSymbolicName(resource)) && range.contains(bundle.getVersion())) {
+ matching.put(bundle.getVersion(), bundle);
+ }
+ }
+ if (!matching.isEmpty()) {
+ Bundle bundle = matching.lastEntry().getValue();
+ deployment.toUpdate.put(bundle, resource);
+ deployment.toDelete.remove(bundle);
+ result.resToBnd.put(resource, bundle);
+ } else {
+ deployment.toInstall.add(resource);
}
}
- if (!matching.isEmpty()) {
- Bundle bundle = matching.lastEntry().getValue();
- deployment.toUpdate.put(bundle, resource);
- deployment.toDelete.remove(bundle);
- deployment.resToBnd.put(resource, bundle);
- } else {
- deployment.toInstall.add(resource);
- }
- }
- return deployment;
- }
+ Collections.sort(deployment.toInstall, new ResourceComparator());
- protected List<Resource> getBundles(Collection<Resource> allResources) {
- Map<String, Resource> deploy = new TreeMap<String, Resource>();
- for (Resource res : allResources) {
- String uri = UriNamespace.getUri(res);
- if (uri != null) {
- deploy.put(uri, res);
+ // Add this region if there is something to do
+ if (!deployment.toDelete.isEmpty()
+ || !deployment.toUpdate.isEmpty()
+ || !deployment.toInstall.isEmpty()) {
+ result.regions.put(region, deployment);
}
}
- return new ArrayList<Resource>(deploy.values());
+
+ return result;
}
protected List<Feature> getFeatures(Repository[] repositories, List<String> featureIds) throws Exception {
@@ -1265,28 +1439,33 @@ public class FeaturesServiceImpl implements FeaturesService {
return installedFeatures;
}
+ protected Map<String, Set<String>> getInstalledFeatures(SubsystemResolver resolver) {
+ Map<String, Set<String>> installed = new HashMap<String, Set<String>>();
+ Map<String, Set<Resource>> mapping = resolver.getFeaturesPerRegions();
+ for (Map.Entry<String, Set<Resource>> entry : mapping.entrySet()) {
+ for (Resource resource : entry.getValue()) {
+ addToMapSet(installed, entry.getKey(), getFeatureId(resource));
+ }
+ }
+ return installed;
+ }
+
protected List<String> getFeatureIds(Collection<Resource> allResources) {
List<String> installedFeatureIds = new ArrayList<String>();
for (Resource resource : allResources) {
- String name = FeatureNamespace.getName(resource);
- if (name != null) {
- Version version = FeatureNamespace.getVersion(resource);
- String id = version != null ? name + "/" + version : name;
+ String id = getFeatureId(resource);
+ if (id != null) {
installedFeatureIds.add(id);
}
}
return installedFeatureIds;
}
- protected DeploymentBuilder createDeploymentBuilder(Repository[] repositories) {
- return new DeploymentBuilder(new SimpleDownloader(), Arrays.asList(repositories));
- }
-
-
protected boolean isUpdateable(Resource resource) {
- return (getVersion(resource).getQualifier().endsWith(SNAPSHOT) ||
- UriNamespace.getUri(resource).contains(SNAPSHOT) ||
- !UriNamespace.getUri(resource).contains(MAVEN));
+ String uri = getUri(resource);
+ return getVersion(resource).getQualifier().endsWith(SNAPSHOT)
+ || uri.contains(SNAPSHOT)
+ || !uri.contains(MAVEN);
}
protected List<Bundle> getBundlesToStart(Collection<Bundle> bundles) {
@@ -1378,7 +1557,7 @@ public class FeaturesServiceImpl implements FeaturesService {
}
protected InputStream getBundleInputStream(Resource resource, Map<String, StreamProvider> providers) throws IOException {
- String uri = UriNamespace.getUri(resource);
+ String uri = getUri(resource);
if (uri == null) {
throw new IllegalStateException("Resource has no uri");
}
@@ -1406,8 +1585,12 @@ public class FeaturesServiceImpl implements FeaturesService {
static class Deployment {
- Map<String, Long> newCheckums = new HashMap<String, Long>();
+ Map<Long, Long> newCheckums = new HashMap<Long, Long>();
Map<Resource, Bundle> resToBnd = new HashMap<Resource, Bundle>();
+ Map<String, RegionDeployment> regions = new HashMap<String, RegionDeployment>();
+ }
+
+ static class RegionDeployment {
List<Resource> toInstall = new ArrayList<Resource>();
List<Bundle> toDelete = new ArrayList<Bundle>();
Map<Bundle, Resource> toUpdate = new HashMap<Bundle, Resource>();
http://git-wip-us.apache.org/repos/asf/karaf/blob/2705ad88/features/core/src/main/java/org/apache/karaf/features/internal/service/RepositoryImpl.java
----------------------------------------------------------------------
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 56e5102..9c2f2b6 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
@@ -47,7 +47,7 @@ public class RepositoryImpl implements Repository {
return features.getName();
}
- public URI[] getRepositories() throws Exception {
+ public URI[] getRepositories() throws IOException {
load();
URI[] result = new URI[features.getRepository().size()];
for (int i = 0; i < features.getRepository().size(); i++) {
@@ -58,7 +58,7 @@ public class RepositoryImpl implements Repository {
return result;
}
- public org.apache.karaf.features.Feature[] getFeatures() throws Exception {
+ public org.apache.karaf.features.Feature[] getFeatures() throws IOException {
load();
return features.getFeature().toArray(new org.apache.karaf.features.Feature[features.getFeature().size()]);
}
@@ -94,9 +94,5 @@ public class RepositoryImpl implements Repository {
}
}
- @Override
- public boolean isValid() {
- throw new UnsupportedOperationException();
- }
}
http://git-wip-us.apache.org/repos/asf/karaf/blob/2705ad88/features/core/src/main/java/org/apache/karaf/features/internal/service/SimpleDownloader.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/SimpleDownloader.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/SimpleDownloader.java
deleted file mode 100644
index d1f16b9..0000000
--- a/features/core/src/main/java/org/apache/karaf/features/internal/service/SimpleDownloader.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.karaf.features.internal.service;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.MalformedURLException;
-import java.net.URL;
-
-import org.apache.karaf.features.internal.deployment.Downloader;
-import org.apache.karaf.features.internal.deployment.StreamProvider;
-import org.apache.karaf.features.internal.util.MultiException;
-
-public class SimpleDownloader implements Downloader {
-
- private final MultiException exception = new MultiException("Error");
-
- @Override
- public void await() throws InterruptedException, MultiException {
- exception.throwIfExceptions();
- }
-
- @Override
- public void download(final String location, final DownloadCallback downloadCallback) throws MalformedURLException {
- final URL url = new URL(location);
- try {
- downloadCallback.downloaded(new StreamProvider() {
- @Override
- public InputStream open() throws IOException {
- return url.openStream();
- }
- });
- } catch (Exception e) {
- exception.addException(e);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/karaf/blob/2705ad88/features/core/src/main/java/org/apache/karaf/features/internal/service/State.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/State.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/State.java
index c84f4e0..df4d901 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/service/State.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/State.java
@@ -22,13 +22,34 @@ import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicBoolean;
+import static org.apache.karaf.features.internal.util.MapUtils.copyMapSet;
+
public class State {
public final AtomicBoolean bootDone = new AtomicBoolean();
public final Set<String> repositories = new TreeSet<String>();
- public final Set<String> features = new TreeSet<String>();
- public final Set<String> installedFeatures = new TreeSet<String>();
- public final Set<Long> managedBundles = new TreeSet<Long>();
- public final Map<String, Long> bundleChecksums = new HashMap<String, Long>();
+ public final Map<String, Set<String>> features = new HashMap<String, Set<String>>();
+ public final Map<String, Set<String>> installedFeatures = new HashMap<String, Set<String>>();
+ public final Map<String, Set<Long>> managedBundles = new HashMap<String, Set<Long>>();
+ public final Map<Long, Long> bundleChecksums = new HashMap<Long, Long>();
+
+ public State copy() {
+ State state = new State();
+ state.bootDone.set(bootDone.get());
+ copySet(repositories, state.repositories);
+ copyMapSet(features, state.features);
+ copyMapSet(installedFeatures, state.installedFeatures);
+ copyMapSet(managedBundles, state.managedBundles);
+ copyMap(bundleChecksums, state.bundleChecksums);
+ return state;
+ }
+
+ static <T> void copySet(Set<T> from, Set<T> to) {
+ to.addAll(from);
+ }
+
+ static <S, T> void copyMap(Map<S, T> from, Map<S, T> to) {
+ to.putAll(from);
+ }
}
http://git-wip-us.apache.org/repos/asf/karaf/blob/2705ad88/features/core/src/main/java/org/apache/karaf/features/internal/service/StateStorage.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/StateStorage.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/StateStorage.java
index ac54ab0..3b3424f 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/service/StateStorage.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/StateStorage.java
@@ -20,17 +20,14 @@ import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Enumeration;
+import java.util.Collection;
import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
import java.util.Map;
-import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
-import org.apache.karaf.features.Feature;
+import org.apache.karaf.features.internal.util.JsonReader;
+import org.apache.karaf.features.internal.util.JsonWriter;
public abstract class StateStorage {
@@ -42,14 +39,13 @@ public abstract class StateStorage {
InputStream is = getInputStream();
if (is != null) {
try {
- Properties props = new Properties();
- props.load(is);
- state.bootDone.set(loadBool(props, "bootDone"));
- state.repositories.addAll(loadSet(props, "repositories."));
- state.features.addAll(loadSet(props, "features."));
- state.installedFeatures.addAll(loadSet(props, "installed."));
- state.managedBundles.addAll(toLongSet(loadSet(props, "managed.")));
- state.bundleChecksums.putAll(toStringLongMap(loadMap(props, "checksums.")));
+ Map json = (Map) JsonReader.read(is);
+ state.bootDone.set((Boolean) json.get("bootDone"));
+ state.repositories.addAll(toStringSet((Collection) json.get("repositories")));
+ state.features.putAll(toStringStringSetMap((Map) json.get("features")));
+ state.installedFeatures.putAll(toStringStringSetMap((Map) json.get("installed")));
+ state.managedBundles.putAll(toStringLongSetMap((Map) json.get("managed")));
+ state.bundleChecksums.putAll(toLongLongMap((Map) json.get("checksums")));
} finally {
close(is);
}
@@ -60,14 +56,14 @@ public abstract class StateStorage {
OutputStream os = getOutputStream();
if (os != null) {
try {
- Properties props = new Properties();
- saveBool(props, "bootDone", state.bootDone.get());
- saveSet(props, "repositories.", state.repositories);
- saveSet(props, "features.", state.features);
- saveSet(props, "installed.", state.installedFeatures);
- saveSet(props, "managed.", toStringSet(state.managedBundles));
- saveMap(props, "checksums.", toStringStringMap(state.bundleChecksums));
- props.store(os, "FeaturesService State");
+ Map<String, Object> json = new HashMap<String, Object>();
+ json.put("bootDone", state.bootDone.get());
+ json.put("repositories", state.repositories);
+ json.put("features", state.features);
+ json.put("installed", state.installedFeatures);
+ json.put("managed", state.managedBundles);
+ json.put("checksums", toStringLongMap(state.bundleChecksums));
+ JsonWriter.write(os, json);
} finally {
close(os);
}
@@ -77,92 +73,63 @@ public abstract class StateStorage {
protected abstract InputStream getInputStream() throws IOException;
protected abstract OutputStream getOutputStream() throws IOException;
- protected boolean loadBool(Properties props, String key) {
- return Boolean.parseBoolean(props.getProperty(key));
+ protected Map<String, Set<String>> toStringStringSetMap(Map<?,?> map) {
+ Map<String, Set<String>> nm = new HashMap<String, Set<String>>();
+ for (Map.Entry entry : map.entrySet()) {
+ nm.put(entry.getKey().toString(), toStringSet((Collection) entry.getValue()));
+ }
+ return nm;
}
- protected void saveBool(Properties props, String key, boolean val) {
- props.setProperty(key, Boolean.toString(val));
+ protected Map<String, Set<Long>> toStringLongSetMap(Map<?,?> map) {
+ Map<String, Set<Long>> nm = new HashMap<String, Set<Long>>();
+ for (Map.Entry entry : map.entrySet()) {
+ nm.put(entry.getKey().toString(), toLongSet((Collection) entry.getValue()));
+ }
+ return nm;
}
- protected Set<String> toStringSet(Set<Long> set) {
+ protected Set<String> toStringSet(Collection<?> col) {
Set<String> ns = new TreeSet<String>();
- for (long l : set) {
- ns.add(Long.toString(l));
+ for (Object o : col) {
+ ns.add(o.toString());
}
return ns;
}
- protected Set<Long> toLongSet(Set<String> set) {
+ protected Set<Long> toLongSet(Collection<?> set) {
Set<Long> ns = new TreeSet<Long>();
- for (String s : set) {
- ns.add(Long.parseLong(s));
+ for (Object o : set) {
+ ns.add(toLong(o));
}
return ns;
}
- protected void saveSet(Properties props, String prefix, Set<String> set) {
- List<String> l = new ArrayList<String>(set);
- props.put(prefix + "count", Integer.toString(l.size()));
- for (int i = 0; i < l.size(); i++) {
- props.put(prefix + "item." + i, l.get(i));
- }
- }
-
- protected Set<String> loadSet(Properties props, String prefix) {
- Set<String> l = new HashSet<String>();
- String countStr = (String) props.get(prefix + "count");
- if (countStr != null) {
- int count = Integer.parseInt(countStr);
- for (int i = 0; i < count; i++) {
- l.add((String) props.get(prefix + "item." + i));
- }
- }
- return l;
- }
-
- protected Map<String, String> toStringStringMap(Map<String, Long> map) {
- Map<String, String> nm = new HashMap<String, String>();
- for (Map.Entry<String, Long> entry : map.entrySet()) {
- nm.put(entry.getKey(), Long.toString(entry.getValue()));
+ protected Map<Long, Long> toLongLongMap(Map<?,?> map) {
+ Map<Long, Long> nm = new HashMap<Long, Long>();
+ for (Map.Entry entry : map.entrySet()) {
+ nm.put(toLong(entry.getKey()), toLong(entry.getValue()));
}
return nm;
}
- protected Map<String, Long> toStringLongMap(Map<String, String> map) {
+ protected Map<String, Long> toStringLongMap(Map<?,?> map) {
Map<String, Long> nm = new HashMap<String, Long>();
- for (Map.Entry<String, String> entry : map.entrySet()) {
- nm.put(entry.getKey(), Long.parseLong(entry.getValue()));
+ for (Map.Entry entry : map.entrySet()) {
+ nm.put(entry.getKey().toString(), toLong(entry.getValue()));
}
return nm;
}
-
- protected void saveMap(Properties props, String prefix, Map<String, String> map) {
- List<Map.Entry<String, String>> l = new ArrayList<Map.Entry<String, String>>(map.entrySet());
- props.put(prefix + "count", Integer.toString(l.size()));
- for (int i = 0; i < l.size(); i++) {
- props.put(prefix + "key." + i, l.get(i).getKey());
- props.put(prefix + "val." + i, l.get(i).getValue());
- }
- }
-
- protected Map<String, String> loadMap(Properties props, String prefix) {
- Map<String, String> l = new HashMap<String, String>();
- String countStr = (String) props.get(prefix + "count");
- if (countStr != null) {
- int count = Integer.parseInt(countStr);
- for (int i = 0; i < count; i++) {
- String key = (String) props.get(prefix + "key." + i);
- String val = (String) props.get(prefix + "val." + i);
- l.put(key, val);
- }
+ static long toLong(Object o) {
+ if (o instanceof Number) {
+ return ((Number) o).longValue();
+ } else {
+ return Long.parseLong(o.toString());
}
- return l;
}
-
- protected void close(Closeable closeable) {
+ static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
http://git-wip-us.apache.org/repos/asf/karaf/blob/2705ad88/features/core/src/main/java/org/apache/karaf/features/internal/util/JsonWriter.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/util/JsonWriter.java b/features/core/src/main/java/org/apache/karaf/features/internal/util/JsonWriter.java
index cba27fb..78a85dc 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/util/JsonWriter.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/util/JsonWriter.java
@@ -17,6 +17,8 @@
package org.apache.karaf.features.internal.util;
import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Map;
@@ -25,6 +27,12 @@ import java.util.Map;
*/
public class JsonWriter {
+ public static void write(OutputStream stream, Object value) throws IOException {
+ Writer writer = new OutputStreamWriter(stream);
+ write(writer, value);
+ writer.flush();
+ }
+
public static void write(Writer writer, Object value) throws IOException {
if (value instanceof Map) {
writeObject(writer, (Map) value);
http://git-wip-us.apache.org/repos/asf/karaf/blob/2705ad88/features/core/src/main/java/org/apache/karaf/features/internal/util/MapUtils.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/util/MapUtils.java b/features/core/src/main/java/org/apache/karaf/features/internal/util/MapUtils.java
new file mode 100644
index 0000000..455ceda
--- /dev/null
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/util/MapUtils.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.karaf.features.internal.util;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class MapUtils {
+
+ public static <S, T> Map<S, Set<T>> invert(Map<T, S> map) {
+ Map<S, Set<T>> inverted = new HashMap<S, Set<T>>();
+ for (Map.Entry<T, S> entry : map.entrySet()) {
+ addToMapSet(inverted, entry.getValue(), entry.getKey());
+ }
+ return inverted;
+ }
+
+ public static <S, T> Map<S, Set<T>> copyMapSet(Map<S, Set<T>> from) {
+ Map<S, Set<T>> to = new HashMap<S, Set<T>>();
+ copyMapSet(from, to);
+ return to;
+ }
+
+ public static <S, T> void copyMapSet(Map<S, Set<T>> from, Map<S, Set<T>> to) {
+ for (Map.Entry<S, Set<T>> entry : from.entrySet()) {
+ to.put(entry.getKey(), new HashSet<T>(entry.getValue()));
+ }
+ }
+
+ public static <S, T> void addToMapSet(Map<S, Set<T>> map, S key, T value) {
+ Set<T> values = map.get(key);
+ if (values == null) {
+ values = new HashSet<T>();
+ map.put(key, values);
+ }
+ values.add(value);
+ }
+
+ public static <S, T> void removeFromMapSet(Map<S, Set<T>> map, S key, T value) {
+ Set<T> values = map.get(key);
+ if (values != null) {
+ values.remove(value);
+ if (values.isEmpty()) {
+ map.remove(key);
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/karaf/blob/2705ad88/features/core/src/main/java/org/eclipse/equinox/internal/region/DigraphHelper.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/eclipse/equinox/internal/region/DigraphHelper.java b/features/core/src/main/java/org/eclipse/equinox/internal/region/DigraphHelper.java
new file mode 100644
index 0000000..cd874b6
--- /dev/null
+++ b/features/core/src/main/java/org/eclipse/equinox/internal/region/DigraphHelper.java
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.eclipse.equinox.internal.region;
+
+import java.io.DataInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.karaf.features.internal.service.FeaturesServiceImpl;
+import org.apache.karaf.features.internal.util.JsonReader;
+import org.apache.karaf.features.internal.util.JsonWriter;
+import org.eclipse.equinox.region.Region;
+import org.eclipse.equinox.region.RegionDigraph;
+import org.eclipse.equinox.region.RegionFilterBuilder;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.hooks.bundle.CollisionHook;
+
+public class DigraphHelper {
+
+ private static final String DIGRAPH_FILE = "digraph.json";
+
+ private static final String REGIONS = "regions";
+ private static final String EDGES = "edges";
+ private static final String TAIL = "tail";
+ private static final String HEAD = "head";
+ private static final String POLICY = "policy";
+
+ public static StandardRegionDigraph loadDigraph(BundleContext bundleContext) throws BundleException, IOException, InvalidSyntaxException {
+ StandardRegionDigraph digraph;
+ ThreadLocal<Region> threadLocal = new ThreadLocal<Region>();
+ File digraphFile = bundleContext.getDataFile(DIGRAPH_FILE);
+ if (digraphFile == null || !digraphFile.exists()) {
+ digraph = new StandardRegionDigraph(bundleContext, threadLocal);
+ Region root = digraph.createRegion(FeaturesServiceImpl.ROOT_REGION);
+ for (Bundle bundle : bundleContext.getBundles()) {
+ root.addBundle(bundle);
+ }
+ } else {
+ InputStream in = new FileInputStream(digraphFile);
+ try {
+ digraph = readDigraph(new DataInputStream(in), bundleContext, threadLocal);
+ } finally {
+ in.close();
+ }
+ }
+ return digraph;
+ }
+
+ public static void saveDigraph(BundleContext bundleContext, RegionDigraph digraph) throws IOException {
+ File digraphFile = bundleContext.getDataFile(DIGRAPH_FILE);
+ FileOutputStream out = new FileOutputStream(digraphFile);
+ try {
+ saveDigraph(digraph, out);
+ } catch (Exception e) {
+ // Ignore
+ } finally {
+ try {
+ out.close();
+ } catch (IOException e) {
+ // ignore;
+ }
+ }
+ }
+
+ public static CollisionHook getCollisionHook(StandardRegionDigraph digraph) {
+ return (CollisionHook) digraph.getBundleCollisionHook();
+ }
+
+
+ @SuppressWarnings("unchecked")
+ static StandardRegionDigraph readDigraph(InputStream in, BundleContext bundleContext, ThreadLocal<Region> threadLocal) throws IOException, BundleException, InvalidSyntaxException {
+ StandardRegionDigraph digraph = new StandardRegionDigraph(bundleContext, threadLocal);
+ Map json = (Map) JsonReader.read(in);
+ Map<String, Collection<Number>> regions = (Map<String, Collection<Number>>) json.get(REGIONS);
+ for (Map.Entry<String, Collection<Number>> rmap : regions.entrySet()) {
+ String name = rmap.getKey();
+ Region region = digraph.createRegion(name);
+ for (Number b : rmap.getValue()) {
+ region.addBundle(b.longValue());
+ }
+ }
+ List<Map<String, Object>> edges = (List<Map<String, Object>>) json.get(EDGES);
+ for (Map<String, Object> e : edges) {
+ String tail = (String) e.get(TAIL);
+ String head = (String) e.get(HEAD);
+ Map<String, Collection<String>> policy = (Map<String, Collection<String>>) e.get(POLICY);
+ RegionFilterBuilder builder = digraph.createRegionFilterBuilder();
+ for (Map.Entry<String,Collection<String>> rf : policy.entrySet()) {
+ String ns = rf.getKey();
+ for (String f : rf.getValue()) {
+ builder.allow(ns, f);
+ }
+ }
+ digraph.connect(digraph.getRegion(tail), builder.build(), digraph.getRegion(head));
+ }
+ return digraph;
+ }
+
+ static void saveDigraph(RegionDigraph digraph, OutputStream out) throws IOException {
+ Map<String, Object> json = new LinkedHashMap<String, Object>();
+ Map<String, Set<Long>> regions = new LinkedHashMap<String, Set<Long>>();
+ json.put(REGIONS, regions);
+ for (Region region : digraph.getRegions()) {
+ regions.put(region.getName(), region.getBundleIds());
+ }
+ List<Map<String, Object>> edges = new ArrayList<Map<String, Object>>();
+ json.put(EDGES, edges);
+ for (Region tail : digraph.getRegions()) {
+ for (RegionDigraph.FilteredRegion fr : digraph.getEdges(tail)) {
+ Map<String, Object> edge = new HashMap<String, Object>();
+ edge.put(TAIL, tail.getName());
+ edge.put(HEAD, fr.getRegion().getName());
+ edge.put(POLICY, fr.getFilter().getSharingPolicy());
+ edges.add(edge);
+
+ }
+ }
+ JsonWriter.write(out, json);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/karaf/blob/2705ad88/features/core/src/main/resources/org/apache/karaf/features/karaf-features-1.3.0.xsd
----------------------------------------------------------------------
diff --git a/features/core/src/main/resources/org/apache/karaf/features/karaf-features-1.3.0.xsd b/features/core/src/main/resources/org/apache/karaf/features/karaf-features-1.3.0.xsd
index b68b1bb..e28f05f 100644
--- a/features/core/src/main/resources/org/apache/karaf/features/karaf-features-1.3.0.xsd
+++ b/features/core/src/main/resources/org/apache/karaf/features/karaf-features-1.3.0.xsd
@@ -61,23 +61,26 @@ Feature definition.
Definition of the Feature.
]]></xs:documentation>
</xs:annotation>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
- <xs:element name="details" minOccurs="0" type="xs:string">
- <xs:annotation>
- <xs:documentation><![CDATA[
-The help text shown for this feature when using feature:info console command.
- ]]>
- </xs:documentation>
- </xs:annotation>
- </xs:element>
- <xs:element name="config" type="tns:config" />
- <xs:element name="configfile" type="tns:configFile" />
- <xs:element name="feature" type="tns:dependency" />
- <xs:element name="bundle" type="tns:bundle" />
- <xs:element name="conditional" type="tns:conditional" />
- <xs:element name="requirement" type="tns:requirement" />
- <xs:element name="capability" type="tns:capability" />
- </xs:choice>
+ <xs:sequence>
+ <xs:choice minOccurs="0" maxOccurs="unbounded">
+ <xs:element name="details" minOccurs="0" type="xs:string">
+ <xs:annotation>
+ <xs:documentation><![CDATA[
+ The help text shown for this feature when using feature:info console command.
+ ]]>
+ </xs:documentation>
+ </xs:annotation>
+ </xs:element>
+ <xs:element name="config" type="tns:config" />
+ <xs:element name="configfile" type="tns:configFile" />
+ <xs:element name="feature" type="tns:dependency" />
+ <xs:element name="bundle" type="tns:bundle" />
+ <xs:element name="conditional" type="tns:conditional" />
+ <xs:element name="requirement" type="tns:requirement" />
+ <xs:element name="capability" type="tns:capability" />
+ </xs:choice>
+ <xs:element name="scoping" minOccurs="0" maxOccurs="1" type="tns:scoping" />
+ </xs:sequence>
<xs:attribute name="name" type="tns:featureName" use="required" />
<xs:attribute name="version" type="xs:string" default="0.0.0" />
<xs:attribute name="description" type="xs:string" />
@@ -232,6 +235,27 @@ Additional capability of this feature.
</xs:simpleContent>
</xs:complexType>
+ <xs:complexType name="scoping">
+ <xs:annotation>
+ <xs:documentation><![CDATA[
+Scoping definition for this feature.
+ ]]></xs:documentation>
+ </xs:annotation>
+ <xs:choice minOccurs="0" maxOccurs="unbounded">
+ <xs:element name="import" type="tns:scopeFilter"/>
+ <xs:element name="export" type="tns:scopeFilter"/>
+ </xs:choice>
+ <xs:attribute name="acceptDependencies" type="xs:boolean"/>
+ </xs:complexType>
+
+ <xs:complexType name="scopeFilter">
+ <xs:simpleContent>
+ <xs:extension base="xs:string">
+ <xs:attribute name="namespace" type="xs:string" use="required"/>
+ </xs:extension>
+ </xs:simpleContent>
+ </xs:complexType>
+
<xs:simpleType name="featureName">
<xs:annotation>
<xs:documentation><![CDATA[
http://git-wip-us.apache.org/repos/asf/karaf/blob/2705ad88/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
----------------------------------------------------------------------
diff --git a/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java b/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
index 9f7e67b..d94d9b7 100644
--- a/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
+++ b/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
@@ -346,7 +346,7 @@ public class FeaturesServiceTest extends TestBase {
+ " <feature name='f2' version='0.2'><bundle>bundle2</bundle></feature>"
+ "</features>");
- FeaturesServiceImpl svc = new FeaturesServiceImpl(null, null, new Storage(), null, null, null, null, null, null, null);
+ FeaturesServiceImpl svc = new FeaturesServiceImpl(null, null, new Storage(), null, null, null, null, null, null, null, null);
svc.addRepository(uri);
assertEquals(feature("f2", "0.2"), svc.getFeature("f2", "[0.1,0.3)"));
@@ -366,7 +366,7 @@ public class FeaturesServiceTest extends TestBase {
expect(bundleContext.getBundles()).andReturn(new Bundle[0]);
replay(bundleContext);
- FeaturesServiceImpl svc = new FeaturesServiceImpl(null, bundleContext, new Storage(), null, null, null, null, null, null, null);
+ FeaturesServiceImpl svc = new FeaturesServiceImpl(null, bundleContext, new Storage(), null, null, null, null, null, null, null, null);
svc.addRepository(uri);
try {
List<String> features = new ArrayList<String>();
@@ -377,7 +377,8 @@ public class FeaturesServiceTest extends TestBase {
svc.installFeatures(new CopyOnWriteArraySet<String>(features),
EnumSet.noneOf(FeaturesService.Option.class));
fail("Call should have thrown an exception");
- } catch (MalformedURLException e) {
+ } catch (Exception t) {
+ // Expected
}
verify(bundleContext);
}
@@ -390,7 +391,7 @@ public class FeaturesServiceTest extends TestBase {
URI uri = createTempRepo("<features name='test' xmlns='http://karaf.apache.org/xmlns/features/v1.0.0'>"
+ " <featur><bundle>somebundle</bundle></featur></features>");
- FeaturesServiceImpl svc = new FeaturesServiceImpl(null, null, new Storage(), null, null, null, null, null, null, null);
+ FeaturesServiceImpl svc = new FeaturesServiceImpl(null, null, new Storage(), null, null, null, null, null, null, null, null);
try {
svc.addRepository(uri);
fail("exception expected");
@@ -408,7 +409,7 @@ public class FeaturesServiceTest extends TestBase {
+ " <feature name='f1'><bundle>file:bundle1</bundle><bundle>file:bundle2</bundle></feature>"
+ "</features>");
- FeaturesServiceImpl svc = new FeaturesServiceImpl(null, null, new Storage(), null, null, null, null, null, null, null);
+ FeaturesServiceImpl svc = new FeaturesServiceImpl(null, null, new Storage(), null, null, null, null, null, null, null, null);
svc.addRepository(uri);
Feature feature = svc.getFeature("f1");
Assert.assertNotNull("No feature named fi found", feature);