static

in analysis/heap-dump/provider/src/main/java/org/eclipse/jifa/hdp/provider/HeapDumpAnalysisApiExecutor.java [46:151]


    static {
        Map<String, String> config = new HashMap<>();
        config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);

        String apiBase = "org.eclipse.jifa.hda.api";
        String commonBase = "org.eclipse.jifa.common";
        String analysisBase = "org.eclipse.jifa.analysis";
        String[] extras = {
                apiBase,
                commonBase,
                commonBase + ".annotation",
                commonBase + ".domain.exception",
                commonBase + ".domain.request",
                commonBase + ".domain.vo",
                commonBase + ".enums",
                commonBase + ".util",
                analysisBase,
                analysisBase + ".annotation",
                analysisBase + ".cache",
                analysisBase + ".listener",
                analysisBase + ".support",
                analysisBase + ".util",
                "net.sf.cglib.beans",
                "net.sf.cglib.core",
                "net.sf.cglib.core.internal",
                "net.sf.cglib.proxy",
                "net.sf.cglib.reflect",
                "net.sf.cglib.transform",
                "net.sf.cglib.transform.impl",
                "net.sf.cglib.util",
                "org.apache.commons.lang3"
        };
        config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, String.join(",", extras));
        try {
            String path = System.getProperty("java.io.tmpdir");
            File dir = new File(path);
            if (!dir.exists()) {
                Files.createDirectory(Path.of(path));
            }

            String osgiWorkspace = path + "/osgi_workspace";
            String osgiConfiguration = path + "/osgi_configuration";

            if (Files.exists(Path.of(osgiWorkspace))) {
                FileUtils.deleteQuietly(new File(osgiWorkspace));
            }
            if (Files.exists(Path.of(osgiConfiguration))) {
                FileUtils.deleteQuietly(new File(osgiConfiguration));
            }

            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                FileUtils.deleteQuietly(new File(osgiWorkspace));
                FileUtils.deleteQuietly(new File(osgiConfiguration));
            }));

            config.put(EquinoxLocations.PROP_INSTANCE_AREA_DEFAULT, osgiWorkspace);
            config.put(Location.CONFIGURATION_AREA_TYPE, osgiConfiguration);

            Framework framework = ServiceLoader.load(FrameworkFactory.class).iterator().next().newFramework(config);
            framework.start();

            List<String> dependencies;
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(Objects.requireNonNull(HeapDumpAnalysisApiExecutor.class.getClassLoader().getResourceAsStream("mat-deps/list"))))) {
                dependencies = Arrays.asList(br.readLine().split(","));
            }

            List<Bundle> bundles = new ArrayList<>();

            for (String dependency : dependencies) {
                // org.eclipse.osgi is the system bundle
                if (!dependency.startsWith("org.eclipse.osgi-")) {
                    URL resource = HeapDumpAnalysisApiExecutor.class.getClassLoader().getResource("mat-deps/" + dependency);
                    try {
                        assert resource != null;
                        bundles.add(framework.getBundleContext().installBundle(resource.toString()));
                    } catch (Throwable t) {
                        log.error("Failed to install bundle: {}", dependency);
                        throw  t;
                    }
                }
            }

            List<String> validNames = new ArrayList<>();
            validNames.add("org.apache.felix.scr");
            validNames.add("org.eclipse.equinox.event");
            validNames.add("org.eclipse.jifa.hda.implementation");

            for (Bundle bundle : bundles) {
                if (validNames.contains(bundle.getSymbolicName())) {
                    log.debug("starting bundle: {}", bundle);
                    bundle.start();
                }
            }

            PROVIDER = framework.getBundleContext()
                                .getService(framework.getBundleContext()
                                                     .getServiceReference(HeapDumpAnalyzer.Provider.class));

        } catch (Throwable t) {
            if (t instanceof RuntimeException rt) {
                throw rt;
            }
            throw new RuntimeException(t);
        }
    }