Search in sources :

Example 21 with FrameworkFactory

use of org.osgi.framework.launch.FrameworkFactory in project karaf by apache.

the class Main method launch.

public void launch() throws Exception {
    boolean clean = Arrays.asList(args).contains("clean");
    boolean cleanall = Arrays.asList(args).contains("cleanall");
    if (clean || cleanall) {
        // clean instance
        final Path dataDir = new File(System.getProperty(ConfigProperties.PROP_KARAF_DATA)).toPath();
        final Path logDir = new File(System.getProperty(ConfigProperties.PROP_KARAF_LOG)).toPath();
        if (Files.exists(dataDir)) {
            try {
                Files.walkFileTree(dataDir, new SimpleFileVisitor<Path>() {

                    @Override
                    public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException {
                        if (cleanall || (clean && !file.startsWith(logDir))) {
                            Files.delete(file);
                        }
                        return super.visitFile(file, attributes);
                    }

                    @Override
                    public FileVisitResult postVisitDirectory(final Path dir, final IOException exception) throws IOException {
                        if (cleanall || (clean && dir.compareTo(logDir) != 0)) {
                            Files.delete(dir);
                        }
                        return super.postVisitDirectory(dir, exception);
                    }
                });
            } catch (final IOException ioException) {
                LOG.log(Level.WARNING, "Can't delete " + dataDir + " (" + ioException.getMessage() + ")", ioException);
            }
            Files.createDirectories(dataDir.resolve("tmp"));
        }
    }
    if (config == null) {
        config = new ConfigProperties();
    }
    config.performInit();
    if (config.delayConsoleStart) {
        System.out.println(config.startupMessage);
    }
    String log4jConfigPath = System.getProperty("karaf.etc") + "/org.ops4j.pax.logging.cfg";
    BootstrapLogManager.setProperties(config.props, log4jConfigPath);
    /* KARAF-5798: write the PID whether or not the lock has been acquired */
    InstanceHelper.writePid(config.pidFile);
    BootstrapLogManager.configureLogger(LOG);
    for (String provider : config.securityProviders) {
        addSecurityProvider(provider);
    }
    List<File> bundleDirs = getBundleRepos();
    ArtifactResolver resolver = new SimpleMavenResolver(bundleDirs);
    // Start up the OSGI framework
    classLoader = createClassLoader(resolver);
    FrameworkFactory factory = loadFrameworkFactory(classLoader);
    framework = factory.newFramework(config.props);
    setLogger();
    framework.init();
    framework.getBundleContext().addFrameworkListener(lockCallback);
    framework.start();
    FrameworkStartLevel sl = framework.adapt(FrameworkStartLevel.class);
    sl.setInitialBundleStartLevel(config.defaultBundleStartlevel);
    // If we have a clean state, install everything
    if (framework.getBundleContext().getBundles().length == 1) {
        LOG.info("Installing and starting initial bundles");
        File startupPropsFile = new File(config.karafEtc, STARTUP_PROPERTIES_FILE_NAME);
        List<BundleInfo> bundles = readBundlesFromStartupProperties(startupPropsFile);
        installAndStartBundles(resolver, framework.getBundleContext(), bundles);
        LOG.info("All initial bundles installed and set to start");
    }
    ServerInfo serverInfo = new ServerInfoImpl(args, config);
    framework.getBundleContext().registerService(ServerInfo.class, serverInfo, null);
    activatorManager = new KarafActivatorManager(classLoader, framework);
    activatorManager.startKarafActivators();
    setStartLevel(config.lockStartLevel);
    // Progress bar
    if (config.delayConsoleStart) {
        new StartupListener(LOG, framework.getBundleContext());
    }
    monitorThread = monitor();
    registerSignalHandler();
    watchdog();
}
Also used : Path(java.nio.file.Path) ServerInfo(org.apache.karaf.info.ServerInfo) FileVisitResult(java.nio.file.FileVisitResult) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) ArtifactResolver(org.apache.karaf.main.util.ArtifactResolver) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) SimpleMavenResolver(org.apache.karaf.main.util.SimpleMavenResolver) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

FrameworkFactory (org.osgi.framework.launch.FrameworkFactory)21 Framework (org.osgi.framework.launch.Framework)9 File (java.io.File)7 HashMap (java.util.HashMap)6 IOException (java.io.IOException)5 BundleContext (org.osgi.framework.BundleContext)5 BundleException (org.osgi.framework.BundleException)5 Bundle (org.osgi.framework.Bundle)4 FrameworkEvent (org.osgi.framework.FrameworkEvent)4 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 FrameworkListener (org.osgi.framework.FrameworkListener)3 MalformedURLException (java.net.MalformedURLException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 Version (aQute.bnd.version.Version)1 MiniFramework (aQute.launcher.minifw.MiniFramework)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1