summaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/core/config/PlatformBundleLoader.java
blob: 1f788823753c813a184b44bdd4f488311ef82f19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.yahoo.container.core.config;

import com.yahoo.config.FileReference;
import com.yahoo.osgi.Osgi;
import org.osgi.framework.Bundle;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

/**
 * Used to install the bundles that are added as platform bundles by the config-model.
 *
 * All platform bundles reside on disk, and they are never uninstalled.
 * Platform bundles are allowed to pre-install other bundles on disk via the
 * X-JDisc-Preinstall-Bundle manifest header.
 *
 * Attempts to install additional bundles, after the first call, should be a NOP.
 *
 * @author gjoranv
 */
public class PlatformBundleLoader {
    private static final Logger log = Logger.getLogger(PlatformBundleLoader.class.getName());

    private final Osgi osgi;
    private final DiskBundleInstaller installer;

    private Set<Bundle> installedBundles;
    private boolean hasLoadedBundles = false;

    public PlatformBundleLoader(Osgi osgi) {
        this(osgi, new DiskBundleInstaller());
    }

    PlatformBundleLoader(Osgi osgi, DiskBundleInstaller installer) {
        this.osgi = osgi;
        this.installer = installer;
    }

    public void useBundles(List<FileReference> fileReferences) {
        if (hasLoadedBundles) {
            log.fine(() -> "Platform bundles have already been installed." +
                    "\nInstalled bundles: " + installedBundles +
                    "\nGiven files: " + fileReferences);
            return;
        }
        installedBundles = install(fileReferences);
        BundleStarter.startBundles(installedBundles);
        hasLoadedBundles = true;
    }

    private Set<Bundle> install(List<FileReference> bundlesToInstall) {
        var allInstalled = new LinkedHashSet<Bundle>();
        for (FileReference reference : bundlesToInstall) {
            try {
                allInstalled.addAll(installBundleFromDisk(reference));
            }
            catch(Exception e) {
                throw new RuntimeException("Could not install bundle '" + reference + "'", e);
            }
        }
        return allInstalled;
    }

    private List<Bundle> installBundleFromDisk(FileReference reference) {
        log.info("Installing bundle from disk with reference '" + reference.value() + "'");
        List<Bundle> bundles = installer.installBundles(reference, osgi);
        log.fine("Installed " + bundles.size() + " bundles for file reference " + reference);
        return bundles;
    }

}