public void execute()

in tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/RunMojo.java [150:275]


    public void execute() throws MojoExecutionException, MojoFailureException {
        // reset system properties after the execution to ensure not not pollute the maven build
        final Properties originalProperties = new Properties();
        originalProperties.putAll(System.getProperties());

        // before any mkdir or so since "clean" is handled
        final String[] args = handleArgs(karafDirectory, mainArgs == null ? new String[0] : mainArgs);

        if (karafDirectory.exists()) {
            getLog().info("Using Karaf container located " + karafDirectory.getAbsolutePath());
        } else {
            getLog().info("Extracting Karaf container");
            try {
                File karafArchiveFile = resolveFile(karafDistribution);
                extract(karafArchiveFile, karafDirectory);
            } catch (Exception e) {
                throw new MojoFailureException("Can't extract Karaf container", e);
            }
        }

        getLog().info("Starting Karaf container");
        System.setProperty("karaf.home", karafDirectory.getAbsolutePath());
        System.setProperty("karaf.base", karafDirectory.getAbsolutePath());
        System.setProperty("karaf.data", karafDirectory.getAbsolutePath() + "/data");
        System.setProperty("karaf.etc", karafDirectory.getAbsolutePath() + "/etc");
        System.setProperty("karaf.log", karafDirectory.getAbsolutePath() + "/data/log");
        System.setProperty("karaf.instances", karafDirectory.getAbsolutePath() + "/instances");
        if (System.getProperty("karaf.startLocalConsole") == null) {
            System.setProperty("karaf.startLocalConsole", "false");
        }
        System.setProperty("karaf.startRemoteShell", startSsh);
        System.setProperty("karaf.lock", "false");
        if (consoleLogLevel != null && !consoleLogLevel.isEmpty()) {
            System.setProperty("karaf.log.console", consoleLogLevel);
        }
        // last to ensure it wins over defaults/shortcuts
        if (systemProperties != null) {
            systemProperties.forEach(System::setProperty);
        }

        String featureBootFinished = BootFinished.class.getName();
        Thread bootThread = Thread.currentThread();
        ClassLoader classRealm = bootThread.getContextClassLoader();
        ClassLoader bootLoader = new ClassLoader(classRealm) {
            @Override // avoids to use that silently and fail later on in the waiting loop
            protected Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
                if (featureBootFinished.equals(name)) {
                    throw new ClassNotFoundException(
                            "avoid to use the classrealm loader which will prevent felix to match its reference");
                }
                if (name != null && forbiddenDelegationPackages != null && forbiddenDelegationPackages.stream().anyMatch(name::startsWith)) {
                    throw new ClassNotFoundException(name);
                }
                return super.loadClass(name, resolve);
            }
        };
        final Main main = newMain(bootLoader, args);

        try {
            long start = System.nanoTime();
            main.launch();
            while (main.getFramework().getState() != Bundle.ACTIVE && checkDurationTimeout(start)) {
                waitForValidState();
            }
            if (main.getFramework().getState() != Bundle.ACTIVE) {
                try {
                    main.destroy();
                } catch (final Throwable e) {
                    // ignore it
                    getLog().debug(e.getMessage(), e);
                }
                throw startupTimeout(start);
            }

            // first find the feature bundle to load the bootfinished class properly,
            // if we use bundleContext0.getBundle() we end up on ClassRealm which will not match in ServiceRegImpl
            BundleContext featureBundleCtx = null;

            Object bootFinished = null;
            while (bootFinished == null && checkDurationTimeout(start)) {
                waitForValidState();
                if (featureBundleCtx == null) {
                    featureBundleCtx = Stream.of(main.getFramework().getBundleContext().getBundles())
                            .filter(b -> b.getSymbolicName().equals("org.apache.karaf.deployer.features"))
                            .findFirst()
                            .map(Bundle::getBundleContext)
                            .orElse(null);
                }
                if (featureBundleCtx == null) {
                    continue;
                }
                ServiceReference<?> ref = featureBundleCtx.getServiceReference(featureBundleCtx.getBundle().loadClass(featureBootFinished));
                if (ref != null) {
                    bootFinished = featureBundleCtx.getService(ref);
                }
            }
            if (bootFinished == null) {
                try {
                    main.destroy();
                } catch (final Throwable e) {
                    // ignore it
                    getLog().debug(e.getMessage(), e);
                }
                throw startupTimeout(start);
            }

            Object featureService = findFeatureService(featureBundleCtx);
            addFeatureRepositories(featureService);
            if (!deployAfterFeatures) {
                deploy(featureBundleCtx, featureService);
            }
            addFeatures(featureService);
            if (deployAfterFeatures) {
                deploy(featureBundleCtx, featureService);
            }
            if (keepRunning)
                main.awaitShutdown();
            main.destroy();
        } catch (Throwable e) {
            throw new MojoExecutionException("Can't start container", e);
        } finally {
            System.gc();
            System.getProperties().clear();
            System.getProperties().putAll(originalProperties);
        }
    }