Search in sources :

Example 1 with ResolverImpl

use of org.apache.felix.resolver.ResolverImpl in project karaf by apache.

the class Builder method doGenerateAssembly.

private void doGenerateAssembly() throws Exception {
    systemDirectory = homeDirectory.resolve("system");
    etcDirectory = homeDirectory.resolve("etc");
    LOGGER.info("Generating karaf assembly: " + homeDirectory);
    //
    // Create download manager
    //
    Dictionary<String, String> props = new Hashtable<>();
    if (offline) {
        props.put(ORG_OPS4J_PAX_URL_MVN_PID + "offline", "true");
    }
    if (localRepository != null) {
        props.put(Builder.ORG_OPS4J_PAX_URL_MVN_PID + ".localRepository", localRepository);
    }
    if (mavenRepositories != null) {
        props.put(Builder.ORG_OPS4J_PAX_URL_MVN_PID + ".repositories", mavenRepositories);
    }
    MavenResolver resolver = MavenResolvers.createMavenResolver(props, ORG_OPS4J_PAX_URL_MVN_PID);
    executor = Executors.newScheduledThreadPool(8);
    manager = new CustomDownloadManager(resolver, executor, null, translatedUrls);
    this.resolver = new ResolverImpl(new Slf4jResolverLog(LOGGER));
    //
    // Unzip kars
    //
    LOGGER.info("Unzipping kars");
    Map<String, RepositoryInfo> repositories = new LinkedHashMap<>(this.repositories);
    Downloader downloader = manager.createDownloader();
    for (String kar : kars.keySet()) {
        downloader.download(kar, null);
    }
    downloader.await();
    for (String karUri : kars.keySet()) {
        Kar kar = new Kar(manager.getProviders().get(karUri).getFile().toURI());
        kar.extract(systemDirectory.toFile(), homeDirectory.toFile());
        RepositoryInfo info = kars.get(karUri);
        for (URI repositoryUri : kar.getFeatureRepos()) {
            repositories.put(repositoryUri.toString(), info);
        }
    }
    //
    // Propagate feature installation from repositories
    //
    Map<String, Stage> features = new LinkedHashMap<>(this.features);
    Map<String, Features> karRepositories = loadRepositories(manager, repositories.keySet(), false);
    for (String repo : repositories.keySet()) {
        RepositoryInfo info = repositories.get(repo);
        if (info.addAll) {
            for (Feature feature : karRepositories.get(repo).getFeature()) {
                features.put(feature.getId(), info.stage);
            }
        }
    }
    //
    // Load profiles
    //
    LOGGER.info("Loading profiles");
    allProfiles = new HashMap<>();
    for (String profilesUri : profilesUris) {
        String uri = profilesUri;
        if (uri.startsWith("jar:") && uri.contains("!/")) {
            uri = uri.substring("jar:".length(), uri.indexOf("!/"));
        }
        if (!uri.startsWith("file:")) {
            downloader = manager.createDownloader();
            downloader.download(uri, null);
            downloader.await();
            StreamProvider provider = manager.getProviders().get(uri);
            profilesUri = profilesUri.replace(uri, provider.getFile().toURI().toString());
        }
        URI profileURI = URI.create(profilesUri);
        Path profilePath;
        try {
            profilePath = Paths.get(profileURI);
        } catch (FileSystemNotFoundException e) {
            // file system does not exist, try to create it
            FileSystem fs = FileSystems.newFileSystem(profileURI, new HashMap<>(), Builder.class.getClassLoader());
            profilePath = fs.provider().getPath(profileURI);
        }
        allProfiles.putAll(Profiles.loadProfiles(profilePath));
        // Handle blacklisted profiles
        if (!blacklistedProfiles.isEmpty()) {
            if (blacklistPolicy == BlacklistPolicy.Discard) {
                // Override blacklisted profiles with empty ones
                for (String profile : blacklistedProfiles) {
                    allProfiles.put(profile, ProfileBuilder.Factory.create(profile).getProfile());
                }
            } else {
                // Remove profiles completely
                allProfiles.keySet().removeAll(blacklistedProfiles);
            }
        }
    }
    // Generate profiles
    Profile startupProfile = generateProfile(Stage.Startup, profiles, repositories, features, bundles);
    profiles.put(startupProfile.getId(), Stage.Boot);
    Profile bootProfile = generateProfile(Stage.Boot, profiles, repositories, features, bundles);
    Profile installedProfile = generateProfile(Stage.Installed, profiles, repositories, features, bundles);
    //
    // Compute overall profile
    //
    ProfileBuilder builder = ProfileBuilder.Factory.create(UUID.randomUUID().toString()).setParents(Arrays.asList(startupProfile.getId(), bootProfile.getId(), installedProfile.getId()));
    config.forEach((k, v) -> builder.addConfiguration(Profile.INTERNAL_PID, Profile.CONFIG_PREFIX + k, v));
    system.forEach((k, v) -> builder.addConfiguration(Profile.INTERNAL_PID, Profile.SYSTEM_PREFIX + k, v));
    Profile overallProfile = builder.getProfile();
    Profile overallOverlay = Profiles.getOverlay(overallProfile, allProfiles, environment);
    Profile overallEffective = Profiles.getEffective(overallOverlay, false);
    manager = new CustomDownloadManager(resolver, executor, overallEffective, translatedUrls);
    //        Hashtable<String, String> agentProps = new Hashtable<>(overallEffective.getConfiguration(ORG_OPS4J_PAX_URL_MVN_PID));
    //        final Map<String, String> properties = new HashMap<>();
    //        properties.put("karaf.default.repository", "system");
    //        InterpolationHelper.performSubstitution(agentProps, properties::get, false, false, true);
    //
    // Write config and system properties
    //
    Path configPropertiesPath = etcDirectory.resolve("config.properties");
    Properties configProperties = new Properties(configPropertiesPath.toFile());
    configProperties.putAll(overallEffective.getConfig());
    configProperties.save();
    Path systemPropertiesPath = etcDirectory.resolve("system.properties");
    Properties systemProperties = new Properties(systemPropertiesPath.toFile());
    systemProperties.putAll(overallEffective.getSystem());
    systemProperties.save();
    //
    // Download libraries
    //
    // TODO: handle karaf 2.x and 3.x libraries
    LOGGER.info("Downloading libraries");
    downloader = manager.createDownloader();
    downloadLibraries(downloader, configProperties, overallEffective.getLibraries(), "");
    downloadLibraries(downloader, configProperties, libraries, "");
    downloader.await();
    // Reformat clauses
    reformatClauses(configProperties, Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA);
    reformatClauses(configProperties, Constants.FRAMEWORK_BOOTDELEGATION);
    configProperties.save();
    //
    // Write all configuration files
    //
    LOGGER.info("Writing configurations");
    for (Map.Entry<String, byte[]> config : overallEffective.getFileConfigurations().entrySet()) {
        Path configFile = etcDirectory.resolve(config.getKey());
        LOGGER.info("   adding config file: {}", homeDirectory.relativize(configFile));
        Files.createDirectories(configFile.getParent());
        Files.write(configFile, config.getValue());
    }
    // 'improve' configuration files.
    if (propertyEdits != null) {
        KarafPropertiesEditor editor = new KarafPropertiesEditor();
        editor.setInputEtc(etcDirectory.toFile()).setOutputEtc(etcDirectory.toFile()).setEdits(propertyEdits);
        editor.run();
    }
    //
    if (!overallEffective.getOverrides().isEmpty()) {
        Path overrides = etcDirectory.resolve("override.properties");
        List<String> lines = new ArrayList<>();
        lines.add("#");
        lines.add("# Generated by the karaf assembly builder");
        lines.add("#");
        lines.addAll(overallEffective.getOverrides());
        LOGGER.info("Generating {}", homeDirectory.relativize(overrides));
        Files.write(overrides, lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    }
    //
    if (!blacklistedFeatures.isEmpty() || !blacklistedBundles.isEmpty()) {
        Path blacklist = etcDirectory.resolve("blacklisted.properties");
        List<String> lines = new ArrayList<>();
        lines.add("#");
        lines.add("# Generated by the karaf assembly builder");
        lines.add("#");
        if (!blacklistedFeatures.isEmpty()) {
            lines.add("");
            lines.add("# Features");
            lines.addAll(blacklistedFeatures);
        }
        if (!blacklistedBundles.isEmpty()) {
            lines.add("");
            lines.add("# Bundles");
            lines.addAll(blacklistedBundles);
        }
        LOGGER.info("Generating {}", homeDirectory.relativize(blacklist));
        Files.write(blacklist, lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    }
    //
    // Startup stage
    //
    Profile startupEffective = startupStage(startupProfile);
    //
    // Boot stage
    //
    Set<Feature> allBootFeatures = bootStage(bootProfile, startupEffective);
    //
    // Installed stage
    //
    installStage(installedProfile, allBootFeatures);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Downloader(org.apache.karaf.features.internal.download.Downloader) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Properties(org.apache.felix.utils.properties.Properties) URI(java.net.URI) Feature(org.apache.karaf.features.internal.model.Feature) ProfileBuilder(org.apache.karaf.profile.ProfileBuilder) Profile(org.apache.karaf.profile.Profile) LinkedHashMap(java.util.LinkedHashMap) MavenResolver(org.ops4j.pax.url.mvn.MavenResolver) Features(org.apache.karaf.features.internal.model.Features) StreamProvider(org.apache.karaf.features.internal.download.StreamProvider) Hashtable(java.util.Hashtable) KarafPropertiesEditor(org.apache.karaf.tools.utils.KarafPropertiesEditor) Kar(org.apache.karaf.kar.internal.Kar) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 2 with ResolverImpl

use of org.apache.felix.resolver.ResolverImpl in project karaf by apache.

the class OfflineResolver method resolve.

public static void resolve(String resolutionFile) throws Exception {
    Map<String, Object> resolution;
    try (BufferedReader reader = Files.newBufferedReader(Paths.get(resolutionFile), StandardCharsets.UTF_8)) {
        resolution = (Map<String, Object>) JsonReader.read(reader);
    }
    final Repository globalRepository;
    if (resolution.containsKey("globalRepository")) {
        globalRepository = readRepository(resolution.get("globalRepository"));
    } else {
        globalRepository = null;
    }
    final Repository repository = readRepository(resolution.get("repository"));
    Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_ERROR));
    Map<Resource, List<Wire>> wiring = resolver.resolve(new ResolveContext() {

        private final CandidateComparator candidateComparator = new CandidateComparator(r -> 0);

        @Override
        public Collection<Resource> getMandatoryResources() {
            List<Resource> resources = new ArrayList<>();
            Requirement req = new RequirementImpl(null, IDENTITY_NAMESPACE, Collections.emptyMap(), Collections.emptyMap(), SimpleFilter.parse("(" + IDENTITY_NAMESPACE + "=root)"));
            Collection<Capability> identities = repository.findProviders(Collections.singleton(req)).get(req);
            for (Capability identity : identities) {
                resources.add(identity.getResource());
            }
            return resources;
        }

        @Override
        public List<Capability> findProviders(Requirement requirement) {
            List<Capability> caps = new ArrayList<>();
            Map<Requirement, Collection<Capability>> resMap = repository.findProviders(Collections.singleton(requirement));
            Collection<Capability> res = resMap != null ? resMap.get(requirement) : null;
            if (res != null && !res.isEmpty()) {
                caps.addAll(res);
            } else if (globalRepository != null) {
                // Only bring in external resources for non optional requirements
                if (!RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(RESOLUTION_DIRECTIVE))) {
                    resMap = globalRepository.findProviders(Collections.singleton(requirement));
                    res = resMap != null ? resMap.get(requirement) : null;
                    if (res != null && !res.isEmpty()) {
                        caps.addAll(res);
                    }
                }
            }
            // Sort caps
            Collections.sort(caps, candidateComparator);
            return caps;
        }

        @Override
        public int insertHostedCapability(List<Capability> capabilities, HostedCapability hostedCapability) {
            int idx = Collections.binarySearch(capabilities, hostedCapability, candidateComparator);
            if (idx < 0) {
                idx = Math.abs(idx + 1);
            }
            capabilities.add(idx, hostedCapability);
            return idx;
        }

        @Override
        public boolean isEffective(Requirement requirement) {
            return true;
        }

        @Override
        public Map<Resource, Wiring> getWirings() {
            return Collections.emptyMap();
        }
    });
}
Also used : ResolveContext(org.osgi.service.resolver.ResolveContext) RequirementImpl(org.apache.karaf.features.internal.resolver.RequirementImpl) Resolver(org.osgi.service.resolver.Resolver) HostedCapability(org.osgi.service.resolver.HostedCapability) Capability(org.osgi.resource.Capability) Resource(org.osgi.resource.Resource) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Logger(org.apache.felix.resolver.Logger) HostedCapability(org.osgi.service.resolver.HostedCapability) Requirement(org.osgi.resource.Requirement) BaseRepository(org.apache.karaf.features.internal.repository.BaseRepository) Repository(org.osgi.service.repository.Repository) BufferedReader(java.io.BufferedReader) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 3 with ResolverImpl

use of org.apache.felix.resolver.ResolverImpl in project karaf by apache.

the class VerifyMojo method verifyResolution.

private void verifyResolution(DownloadManager manager, final Map<String, Features> repositories, Set<String> features, Hashtable<String, String> properties) throws MojoExecutionException {
    try {
        Bundle systemBundle = getSystemBundle(getMetadata(properties, "metadata#"));
        DummyDeployCallback callback = new DummyDeployCallback(systemBundle, repositories.values());
        Deployer deployer = new Deployer(manager, new ResolverImpl(new MavenResolverLog()), callback, callback);
        // Install framework
        Deployer.DeploymentRequest request = createDeploymentRequest();
        for (String fmwk : framework) {
            MapUtils.addToMapSet(request.requirements, FeaturesService.ROOT_REGION, fmwk);
        }
        try {
            deployer.deploy(callback.getDeploymentState(), request);
        } catch (Exception e) {
            throw new MojoExecutionException("Unable to resolve framework features", e);
        }
        // Install features
        for (String feature : features) {
            MapUtils.addToMapSet(request.requirements, FeaturesService.ROOT_REGION, feature);
        }
        try {
            Set<String> prereqs = new HashSet<>();
            while (true) {
                try {
                    deployer.deploy(callback.getDeploymentState(), request);
                    break;
                } catch (Deployer.PartialDeploymentException e) {
                    if (!prereqs.containsAll(e.getMissing())) {
                        prereqs.addAll(e.getMissing());
                    } else {
                        throw new Exception("Deployment aborted due to loop in missing prerequisites: " + e.getMissing());
                    }
                }
            }
        // TODO: find unused resources ?
        } catch (Exception e) {
            throw new MojoExecutionException("Feature resolution failed for " + features + "\nMessage: " + e.getMessage() + "\nRepositories: " + toString(new TreeSet<>(repositories.keySet())) + "\nResources: " + toString(new TreeSet<>(manager.getProviders().keySet())), e);
        }
    } catch (MojoExecutionException e) {
        throw e;
    } catch (Exception e) {
        throw new MojoExecutionException("Error verifying feature " + features + "\nMessage: " + e.getMessage(), e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Bundle(org.osgi.framework.Bundle) ResolverImpl(org.apache.felix.resolver.ResolverImpl) MultiException(org.apache.karaf.features.internal.util.MultiException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ResolutionException(org.osgi.service.resolver.ResolutionException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) TreeSet(java.util.TreeSet) Deployer(org.apache.karaf.features.internal.service.Deployer) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with ResolverImpl

use of org.apache.felix.resolver.ResolverImpl 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

ResolverImpl (org.apache.felix.resolver.ResolverImpl)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Repository (org.osgi.service.repository.Repository)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 Logger (org.apache.felix.resolver.Logger)1 Properties (org.apache.felix.utils.properties.Properties)1 Downloader (org.apache.karaf.features.internal.download.Downloader)1 StreamProvider (org.apache.karaf.features.internal.download.StreamProvider)1