Search in sources :

Example 1 with StandardRegionDigraph

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;
}
Also used : StandardRegionDigraph(org.eclipse.equinox.internal.region.StandardRegionDigraph) RegionDigraph(org.eclipse.equinox.region.RegionDigraph) Region(org.eclipse.equinox.region.Region) RegionFilterBuilder(org.eclipse.equinox.region.RegionFilterBuilder) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap) StandardRegionDigraph(org.eclipse.equinox.internal.region.StandardRegionDigraph)

Example 2 with StandardRegionDigraph

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;
}
Also used : Downloader(org.apache.karaf.features.internal.download.Downloader) Wire(org.osgi.resource.Wire) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) JsonWriter(org.apache.karaf.util.json.JsonWriter) Writer(java.io.Writer) StandardRegionDigraph(org.eclipse.equinox.internal.region.StandardRegionDigraph)

Example 3 with StandardRegionDigraph

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;
}
Also used : DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Bundle(org.osgi.framework.Bundle) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) StandardRegionDigraph(org.eclipse.equinox.internal.region.StandardRegionDigraph) Region(org.eclipse.equinox.region.Region) File(java.io.File) HashSet(java.util.HashSet)

Example 4 with StandardRegionDigraph

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;
}
Also used : StandardRegionDigraph(org.eclipse.equinox.internal.region.StandardRegionDigraph) Collection(java.util.Collection) Region(org.eclipse.equinox.region.Region) ArrayList(java.util.ArrayList) List(java.util.List) RegionFilterBuilder(org.eclipse.equinox.region.RegionFilterBuilder) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with StandardRegionDigraph

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();
}
Also used : FeaturesServiceImpl(org.apache.karaf.features.internal.service.FeaturesServiceImpl) BootFeaturesInstaller(org.apache.karaf.features.internal.service.BootFeaturesInstaller) Resolver(org.osgi.service.resolver.Resolver) URLStreamHandlerService(org.osgi.service.url.URLStreamHandlerService) StateStorage(org.apache.karaf.features.internal.service.StateStorage) BundleInstallSupportImpl(org.apache.karaf.features.internal.service.BundleInstallSupportImpl) ResolverImpl(org.apache.felix.resolver.ResolverImpl) FeatureRepoFinder(org.apache.karaf.features.internal.service.FeatureRepoFinder) FeaturesServiceMBeanImpl(org.apache.karaf.features.internal.management.FeaturesServiceMBeanImpl) StandardRegionDigraph(org.eclipse.equinox.internal.region.StandardRegionDigraph) EventAdminListener(org.apache.karaf.features.internal.service.EventAdminListener) FeatureConfigInstaller(org.apache.karaf.features.internal.service.FeatureConfigInstaller) XmlRepository(org.apache.karaf.features.internal.repository.XmlRepository) Repository(org.osgi.service.repository.Repository) AggregateRepository(org.apache.karaf.features.internal.repository.AggregateRepository) JsonRepository(org.apache.karaf.features.internal.repository.JsonRepository) FeaturesServiceConfig(org.apache.karaf.features.internal.service.FeaturesServiceConfig) Slf4jResolverLog(org.apache.karaf.features.internal.resolver.Slf4jResolverLog) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) BundleContext(org.osgi.framework.BundleContext)

Aggregations

StandardRegionDigraph (org.eclipse.equinox.internal.region.StandardRegionDigraph)5 Region (org.eclipse.equinox.region.Region)3 RegionFilterBuilder (org.eclipse.equinox.region.RegionFilterBuilder)2 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 ResolverImpl (org.apache.felix.resolver.ResolverImpl)1 DictionaryAsMap (org.apache.felix.utils.collections.DictionaryAsMap)1 Downloader (org.apache.karaf.features.internal.download.Downloader)1 FeaturesServiceMBeanImpl (org.apache.karaf.features.internal.management.FeaturesServiceMBeanImpl)1 AggregateRepository (org.apache.karaf.features.internal.repository.AggregateRepository)1