Search in sources :

Example 1 with Main

use of org.apache.karaf.main.Main in project karaf by apache.

the class RunMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    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.instances", karafDirectory.getAbsolutePath() + "/instances");
    System.setProperty("karaf.startLocalConsole", "false");
    System.setProperty("karaf.startRemoteShell", startSsh);
    System.setProperty("karaf.lock", "false");
    Main main = new Main(new String[0]);
    try {
        main.launch();
        while (main.getFramework().getState() != Bundle.ACTIVE) {
            Thread.sleep(1000);
        }
        BundleContext bundleContext = main.getFramework().getBundleContext();
        Object bootFinished = null;
        while (bootFinished == null) {
            Thread.sleep(1000);
            ServiceReference ref = bundleContext.getServiceReference(BootFinished.class);
            if (ref != null) {
                bootFinished = bundleContext.getService(ref);
            }
        }
        deploy(bundleContext);
        if (keepRunning)
            main.awaitShutdown();
        main.destroy();
    } catch (Throwable e) {
        throw new MojoExecutionException("Can't start container", e);
    } finally {
        System.gc();
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Main(org.apache.karaf.main.Main) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with Main

use of org.apache.karaf.main.Main in project opennms by OpenNMS.

the class WebAppListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    try {
        m_servletContext = sce.getServletContext();
        File karafRoot = new File(m_servletContext.getRealPath("/") + "/WEB-INF/karaf");
        final String opennmsHome = System.getProperty("opennms.home");
        if (opennmsHome != null) {
            karafRoot = new File(opennmsHome);
        }
        /*
            String karafHome = System.getProperty("karaf.home");
            if (karafHome != null) {
                karafRoot = new File(karafHome);
            }
            */
        m_servletContext.log("contextInitialized");
        // log4j class instances will leak into the OSGi classloader so we
        // need to tell log4j to ignore the thread context loader and to use
        // the same classloader for all classloading.
        //
        // @see https://issues.apache.org/jira/browse/FELIX-2108
        // @see http://www.mail-archive.com/announcements@jakarta.apache.org/msg00110.html
        //
        System.setProperty("log4j.ignoreTCL", "true");
        final String root = karafRoot.getAbsolutePath();
        m_servletContext.log("Root: " + root);
        System.setProperty("karaf.home", root);
        System.setProperty("karaf.base", root);
        System.setProperty("karaf.data", root + File.separator + "data");
        System.setProperty("karaf.etc", root + File.separator + "etc");
        System.setProperty("karaf.history", root + File.separator + "data" + File.separator + "history.txt");
        System.setProperty("karaf.instances", root + File.separator + "instances");
        System.setProperty("karaf.startLocalConsole", "false");
        System.setProperty("karaf.startRemoteShell", "true");
        System.setProperty("karaf.lock", "false");
        main = new Main(new String[0]);
        main.launch();
        // get bundle context for registering service
        m_framework = main.getFramework().getBundleContext();
        // add bundle context to servlet context for Proxy Servlet
        m_servletContext.setAttribute(BundleContext.class.getName(), m_framework);
        m_bridge.start(m_framework);
    } catch (final Throwable e) {
        m_servletContext.log("Unexpected exception while starting Karaf", e);
        main = null;
        e.printStackTrace();
    }
}
Also used : File(java.io.File) Main(org.apache.karaf.main.Main) BundleContext(org.osgi.framework.BundleContext)

Example 3 with Main

use of org.apache.karaf.main.Main in project karaf by apache.

the class WebAppListener method contextInitialized.

public void contextInitialized(ServletContextEvent sce) {
    try {
        System.err.println("contextInitialized");
        String root = new File(sce.getServletContext().getRealPath("/") + "/WEB-INF/karaf").getAbsolutePath();
        System.err.println("Root: " + root);
        System.setProperty("karaf.home", root);
        System.setProperty("karaf.base", root);
        System.setProperty("karaf.data", root + "/data");
        System.setProperty("karaf.etc", root + "/etc");
        System.setProperty("karaf.history", root + "/data/history.txt");
        System.setProperty("karaf.instances", root + "/instances");
        System.setProperty("karaf.startLocalConsole", "false");
        System.setProperty("karaf.startRemoteShell", "true");
        System.setProperty("karaf.lock", "false");
        main = new Main(new String[0]);
        main.launch();
    } catch (Exception e) {
        main = null;
        e.printStackTrace();
    }
}
Also used : File(java.io.File) Main(org.apache.karaf.main.Main)

Aggregations

Main (org.apache.karaf.main.Main)3 File (java.io.File)2 BundleContext (org.osgi.framework.BundleContext)2 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)1 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 ServiceReference (org.osgi.framework.ServiceReference)1