use of org.eclipse.equinox.internal.region.StandardRegionDigraph in project karaf by apache.
the class SubsystemResolver method getFlatDigraph.
public RegionDigraph getFlatDigraph() throws BundleException, InvalidSyntaxException {
if (flatDigraph == null) {
flatDigraph = new StandardRegionDigraph(null, null);
Map<String, String> flats = getFlatSubsystemsMap();
if (digraph != null) {
for (Region r : digraph.getRegions()) {
if (r.getName().equals(flats.get(r.getName()))) {
flatDigraph.createRegion(r.getName());
}
}
for (Region r : digraph.getRegions()) {
for (RegionDigraph.FilteredRegion fr : digraph.getEdges(r)) {
String rt = flats.get(r.getName());
String rh = flats.get(fr.getRegion().getName());
if (!rh.equals(rt)) {
Region tail = flatDigraph.getRegion(rt);
Region head = flatDigraph.getRegion(rh);
RegionFilterBuilder rfb = flatDigraph.createRegionFilterBuilder();
for (Map.Entry<String, Collection<String>> entry : fr.getFilter().getSharingPolicy().entrySet()) {
// Discard osgi.identity namespace
if (!IDENTITY_NAMESPACE.equals(entry.getKey())) {
for (String f : entry.getValue()) {
rfb.allow(entry.getKey(), f);
}
}
}
flatDigraph.connect(tail, rfb.build(), head);
}
}
}
}
}
return flatDigraph;
}
use of org.eclipse.equinox.internal.region.StandardRegionDigraph in project karaf by apache.
the class SubsystemResolver method resolve.
public Map<Resource, List<Wire>> resolve(Set<String> overrides, String featureResolutionRange, String serviceRequirements, final Repository globalRepository, String outputFile) throws Exception {
if (root == null) {
return Collections.emptyMap();
}
// Download bundles
RepositoryManager repos = new RepositoryManager();
root.downloadBundles(manager, overrides, featureResolutionRange, serviceRequirements, repos);
// Populate digraph and resolve
digraph = new StandardRegionDigraph(null, null);
populateDigraph(digraph, root);
Downloader downloader = manager.createDownloader();
SubsystemResolveContext context = new SubsystemResolveContext(root, digraph, globalRepository, downloader, serviceRequirements);
if (outputFile != null) {
Map<String, Object> json = new HashMap<>();
if (globalRepository != null) {
json.put("globalRepository", toJson(globalRepository));
}
json.put("repository", toJson(context.getRepository()));
try {
wiring = resolver.resolve(context);
json.put("success", "true");
json.put("wiring", toJson(wiring));
} catch (Exception e) {
json.put("success", "false");
json.put("exception", e.toString());
throw e;
} finally {
try (Writer writer = Files.newBufferedWriter(Paths.get(outputFile), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
JsonWriter.write(writer, json, true);
}
}
} else {
wiring = resolver.resolve(context);
}
downloader.await();
// Remove wiring to the fake environment resource
if (environmentResource != null) {
for (List<Wire> wires : wiring.values()) {
wires.removeIf(wire -> wire.getProvider() == environmentResource);
}
}
// Fragments are always wired to their host only, so create fake wiring to
// the subsystem the host is wired to
associateFragments();
return wiring;
}
use of org.eclipse.equinox.internal.region.StandardRegionDigraph in project karaf by apache.
the class DigraphHelper method loadDigraph.
public static StandardRegionDigraph loadDigraph(BundleContext bundleContext) throws BundleException, IOException, InvalidSyntaxException {
StandardRegionDigraph digraph;
ThreadLocal<Region> threadLocal = new ThreadLocal<>();
File digraphFile = bundleContext.getDataFile(DIGRAPH_FILE);
if (digraphFile == null || !digraphFile.exists()) {
digraph = new StandardRegionDigraph(bundleContext, threadLocal);
} else {
try (InputStream in = new FileInputStream(digraphFile)) {
digraph = readDigraph(new DataInputStream(in), bundleContext, threadLocal);
}
}
// Create default region is missing
Region defaultRegion = digraph.getRegion(FeaturesServiceImpl.ROOT_REGION);
if (defaultRegion == null) {
defaultRegion = digraph.createRegion(FeaturesServiceImpl.ROOT_REGION);
}
// Add all unknown bundle to default region
Set<Long> ids = new HashSet<>();
for (Bundle bundle : bundleContext.getBundles()) {
long id = bundle.getBundleId();
ids.add(id);
if (digraph.getRegion(id) == null) {
defaultRegion.addBundle(id);
}
}
// Clean stalled bundles
for (Region region : digraph) {
Set<Long> bundleIds = new HashSet<>(region.getBundleIds());
bundleIds.removeAll(ids);
for (long id : bundleIds) {
region.removeBundle(id);
}
}
return digraph;
}
use of org.eclipse.equinox.internal.region.StandardRegionDigraph in project karaf by apache.
the class DigraphHelper method readDigraph.
@SuppressWarnings({ "unchecked", "rawtypes" })
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;
}
use of org.eclipse.equinox.internal.region.StandardRegionDigraph in project karaf by apache.
the class Activator method doStart.
protected void doStart() throws Exception {
BundleContext systemBundleContext = bundleContext.getBundle(0).getBundleContext();
ConfigurationAdmin configurationAdmin = getTrackedService(ConfigurationAdmin.class);
Resolver resolver = new ResolverImpl(new Slf4jResolverLog(LoggerFactory.getLogger(ResolverImpl.class)));
URLStreamHandlerService mvnUrlHandler = getTrackedService(URLStreamHandlerService.class);
if (configurationAdmin == null || mvnUrlHandler == null) {
return;
}
StandardRegionDigraph dg = DigraphHelper.loadDigraph(bundleContext);
registerRegionDiGraph(dg);
boolean configCfgStore = getBoolean("configCfgStore", FeaturesService.DEFAULT_CONFIG_CFG_STORE);
FeatureConfigInstaller configInstaller = configurationAdmin != null ? new FeatureConfigInstaller(configurationAdmin, configCfgStore) : null;
installSupport = new BundleInstallSupportImpl(bundleContext.getBundle(), bundleContext, systemBundleContext, configInstaller, dg);
register(RegionDigraphPersistence.class, () -> installSupport.saveState());
FeatureRepoFinder featureFinder = new FeatureRepoFinder();
register(ManagedService.class, featureFinder, FeatureRepoFinder.getServiceProperties());
Repository globalRepository = getGlobalRepository();
FeaturesServiceConfig cfg = getConfig();
StateStorage stateStorage = createStateStorage();
featuresService = new FeaturesServiceImpl(stateStorage, featureFinder, configurationAdmin, resolver, installSupport, globalRepository, cfg);
try {
EventAdminListener eventAdminListener = new EventAdminListener(bundleContext);
featuresService.registerListener(eventAdminListener);
} catch (Throwable t) {
// No EventAdmin support in this case
}
register(FeaturesService.class, featuresService);
featuresListenerTracker = createFeatureListenerTracker();
featuresListenerTracker.open();
FeaturesServiceMBeanImpl featuresServiceMBean = new FeaturesServiceMBeanImpl();
featuresServiceMBean.setBundleContext(bundleContext);
featuresServiceMBean.setFeaturesService(featuresService);
registerMBean(featuresServiceMBean, "type=feature");
String[] featuresRepositories = getStringArray("featuresRepositories", "");
String featuresBoot = getString("featuresBoot", "");
boolean featuresBootAsynchronous = getBoolean("featuresBootAsynchronous", false);
BootFeaturesInstaller bootFeaturesInstaller = new BootFeaturesInstaller(bundleContext, featuresService, featuresRepositories, featuresBoot, featuresBootAsynchronous);
bootFeaturesInstaller.start();
}
Aggregations