private Map getBundlesToInstall()

in src/main/java/org/apache/sling/installer/provider/installhook/OsgiInstallerHook.java [245:305]


    private Map<String, InstallableResource> getBundlesToInstall(List<BundleInPackage> bundlesInPackage, Session session,
            InstallationState installationState, PackageProperties packageProperties) throws RepositoryException, IOException {
        Map<String, InstallableResource> installableResources = new HashMap<>();
        Iterator<BundleInPackage> bundlesIt = bundlesInPackage.iterator();
        while (bundlesIt.hasNext()) {
            BundleInPackage bundle = bundlesIt.next();

            List<Resource> currentInstallerBundleResources = getBundleResources(installationState, bundle.symbolicName);

            boolean needsInstallation = false;
            if (currentInstallerBundleResources.isEmpty()) {
                needsInstallation = true;
            } else if (currentInstallerBundleResources.size() == 1) {
                Resource resource = currentInstallerBundleResources.get(0);

                if (resource.getState() == ResourceState.INSTALLED) {
                    String currentlyActiveBundleVersion = resource.getVersion().toString();
                    if (!StringUtils.startsWith(currentlyActiveBundleVersion, bundle.version)) {
                        logger.log("Bundle " + bundle.symbolicName + " is installed with version "
                                + currentlyActiveBundleVersion + " but package contains version " + bundle.version);
                        needsInstallation = true;
                    } else {
                        logger.log("Bundle " + bundle.symbolicName + " is already installed with version "
                                + currentlyActiveBundleVersion + " that matches " + bundle.version + " as provided in package");
                    }
                } else {
                    String msg = MessageFormat.format("Bundle {0} is not in state INSTALLED but in {1}", bundle.symbolicName, resource.getState());
                    if (StringUtils.isNotEmpty(resource.getError())) {
                        // related error if there is some
                        msg += " due to error '" + resource.getError() + "'";
                    }
                    logger.log(msg);
                    needsInstallation = true;
                }

            } else {
                logger.log("Bundle " + bundle.symbolicName + " exists with multiple installer resources");
                boolean installedBundleResourceFound = false;
                for (Resource resource : currentInstallerBundleResources) {
                    logger.log("Resource " + resource);
                    if (resource.getState() == ResourceState.INSTALLED
                            && StringUtils.equals(resource.getVersion().toString(), bundle.version)) {
                        installedBundleResourceFound = true;
                    }
                }
                if (!installedBundleResourceFound) {
                    needsInstallation = true;
                }

            }

            if (needsInstallation) {
                logger.log("Bundle " + bundle.symbolicName + " requires installation");
                Node node = session.getNode(bundle.path);
                InstallableResource installableResource = convert(node, bundle.path, packageProperties);
                String bundleUrl = URL_SCHEME + ":" + bundle.path;
                installableResources.put(bundleUrl, installableResource);
            }
        }
        return installableResources;
    }