Search in sources :

Example 1 with FrameworkStartLevel

use of org.osgi.framework.startlevel.FrameworkStartLevel in project karaf by apache.

the class BundleInstallSupportImpl method getInfo.

@Override
public FrameworkInfo getInfo() {
    FrameworkInfo info = new FrameworkInfo();
    info.ourBundle = ourBundle;
    FrameworkStartLevel fsl = systemBundleContext.getBundle().adapt(FrameworkStartLevel.class);
    info.initialBundleStartLevel = fsl.getInitialBundleStartLevel();
    info.currentStartLevel = fsl.getStartLevel();
    info.bundles = new HashMap<>();
    for (Bundle bundle : systemBundleContext.getBundles()) {
        info.bundles.put(bundle.getBundleId(), bundle);
    }
    info.systemBundle = info.bundles.get(0);
    return info;
}
Also used : Bundle(org.osgi.framework.Bundle) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel)

Example 2 with FrameworkStartLevel

use of org.osgi.framework.startlevel.FrameworkStartLevel in project aries by apache.

the class StartAction method startBundleResource.

private void startBundleResource(Resource resource, Coordination coordination) throws BundleException {
    if (target.isRoot())
        // root region.
        return;
    if (Utils.isRegionContextBundle(resource))
        // The region context bundle was persistently started elsewhere.
        return;
    final Bundle bundle = ((BundleRevision) resource).getBundle();
    if ((bundle.getState() & (Bundle.STARTING | Bundle.ACTIVE)) != 0)
        return;
    if (logger.isDebugEnabled()) {
        int bundleStartLevel = bundle.adapt(BundleStartLevel.class).getStartLevel();
        Bundle systemBundle = Activator.getInstance().getBundleContext().getBundle(0);
        int fwStartLevel = systemBundle.adapt(FrameworkStartLevel.class).getStartLevel();
        logger.debug("StartAction: starting bundle " + bundle.getSymbolicName() + " " + bundle.getVersion().toString() + " bundleStartLevel=" + bundleStartLevel + " frameworkStartLevel=" + fwStartLevel);
    }
    bundle.start(Bundle.START_TRANSIENT | Bundle.START_ACTIVATION_POLICY);
    if (logger.isDebugEnabled()) {
        logger.debug("StartAction: bundle " + bundle.getSymbolicName() + " " + bundle.getVersion().toString() + " started correctly");
    }
    if (coordination == null)
        return;
    coordination.addParticipant(new Participant() {

        public void ended(Coordination coordination) throws Exception {
        // noop
        }

        public void failed(Coordination coordination) throws Exception {
            bundle.stop();
        }
    });
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) Participant(org.osgi.service.coordinator.Participant) Coordination(org.osgi.service.coordinator.Coordination) Bundle(org.osgi.framework.Bundle) BundleRevision(org.osgi.framework.wiring.BundleRevision) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) BundleException(org.osgi.framework.BundleException) SubsystemException(org.osgi.service.subsystem.SubsystemException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) CoordinationException(org.osgi.service.coordinator.CoordinationException)

Example 3 with FrameworkStartLevel

use of org.osgi.framework.startlevel.FrameworkStartLevel in project karaf by apache.

the class Main method launch.

public void launch() throws Exception {
    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);
    lockCallback = new KarafLockCallback();
    InstanceHelper.updateInstancePid(config.karafHome, config.karafBase, true);
    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 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());
    }
    monitor();
    registerSignalHandler();
    watchdog();
}
Also used : ServerInfo(org.apache.karaf.info.ServerInfo) 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) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 4 with FrameworkStartLevel

use of org.osgi.framework.startlevel.FrameworkStartLevel in project karaf by apache.

the class MainLockingTest method testLostMasterLock.

@Test
public void testLostMasterLock() throws Exception {
    File basedir = new File(getClass().getClassLoader().getResource("foo").getPath()).getParentFile();
    File home = new File(basedir, "test-karaf-home");
    File data = new File(home, "data");
    Utils.deleteDirectory(data);
    String[] args = new String[0];
    System.setProperty("karaf.home", home.toString());
    System.setProperty("karaf.data", data.toString());
    System.setProperty("karaf.framework.factory", "org.apache.felix.framework.FrameworkFactory");
    System.setProperty("karaf.lock", "true");
    System.setProperty("karaf.lock.delay", "1000");
    System.setProperty("karaf.lock.class", "org.apache.karaf.main.MockLock");
    Main main = new Main(args);
    main.launch();
    Framework framework = main.getFramework();
    String activatorName = TimeoutShutdownActivator.class.getName().replace('.', '/') + ".class";
    Bundle bundle = framework.getBundleContext().installBundle("foo", TinyBundles.bundle().set(Constants.BUNDLE_ACTIVATOR, TimeoutShutdownActivator.class.getName()).add(activatorName, getClass().getClassLoader().getResourceAsStream(activatorName)).build(withBnd()));
    bundle.start();
    FrameworkStartLevel sl = framework.adapt(FrameworkStartLevel.class);
    MockLock lock = (MockLock) main.getLock();
    Assert.assertEquals(100, sl.getStartLevel());
    // simulate losing a lock
    lock.setIsAlive(false);
    lock.setLock(false);
    // lets wait until the start level change is complete
    lock.waitForLock();
    Assert.assertEquals(1, sl.getStartLevel());
    Thread.sleep(1000);
    // get lock back
    lock.setIsAlive(true);
    lock.setLock(true);
    Thread.sleep(1000);
    // exit framework + lock loop
    main.destroy();
}
Also used : Bundle(org.osgi.framework.Bundle) File(java.io.File) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) Framework(org.osgi.framework.launch.Framework) Test(org.junit.Test)

Example 5 with FrameworkStartLevel

use of org.osgi.framework.startlevel.FrameworkStartLevel in project karaf by apache.

the class List method doExecute.

@Override
protected Object doExecute(java.util.List<Bundle> bundles) throws Exception {
    if (noFormat) {
        noEllipsis = true;
    }
    determineBundleLevelThreshold();
    // Display active start level.
    FrameworkStartLevel fsl = this.bundleContext.getBundle(0).adapt(FrameworkStartLevel.class);
    if (fsl != null) {
        System.out.println("START LEVEL " + fsl.getStartLevel() + " , List Threshold: " + bundleLevelThreshold);
    }
    ShellTable table = new ShellTable();
    if (!noEllipsis && terminal != null && terminal.getWidth() > 0) {
        table.size(terminal.getWidth());
    }
    table.column("ID").alignRight();
    table.column("State");
    table.column("Lvl").alignRight();
    table.column("Version");
    boolean effectiveShowName = showName || (!showLocation && !showSymbolic && !showUpdate && !showRevisions);
    if (effectiveShowName) {
        table.column("Name");
    }
    if (showLocation) {
        table.column(new Col("Location") {

            @Override
            protected String cut(String value, int size) {
                if (showLocation && value.length() > size) {
                    String[] parts = value.split("/");
                    String cut = "";
                    int c = parts[0].length() + 4;
                    for (int idx = parts.length - 1; idx > 0; idx--) {
                        if (cut.length() + c + parts[idx].length() + 1 < size) {
                            cut = "/" + parts[idx] + cut;
                        } else {
                            break;
                        }
                    }
                    cut = parts[0] + "/..." + cut;
                    return cut;
                } else {
                    return super.cut(value, size);
                }
            }
        });
    }
    if (showSymbolic) {
        table.column("Symbolic name");
    }
    if (showUpdate) {
        table.column("Update location");
    }
    if (showRevisions) {
        table.column("Revisions");
    }
    for (Bundle bundle : bundles) {
        BundleInfo info = this.bundleService.getInfo(bundle);
        if (info.getStartLevel() >= bundleLevelThreshold) {
            String version = info.getVersion();
            ArrayList<Object> rowData = new ArrayList<>();
            rowData.add(info.getBundleId());
            rowData.add(getStateString(info.getState()));
            rowData.add(info.getStartLevel());
            rowData.add(version);
            if (effectiveShowName) {
                String bundleName = (info.getName() == null) ? info.getSymbolicName() : info.getName();
                bundleName = (bundleName == null) ? info.getUpdateLocation() : bundleName;
                String name = bundleName + printFragments(info) + printHosts(info);
                rowData.add(name);
            }
            if (showLocation) {
                rowData.add(info.getUpdateLocation());
            }
            if (showSymbolic) {
                rowData.add(info.getSymbolicName() == null ? "<no symbolic name>" : info.getSymbolicName());
            }
            if (showUpdate) {
                rowData.add(info.getUpdateLocation());
            }
            if (showRevisions) {
                rowData.add(info.getRevisions());
            }
            Row row = table.addRow();
            row.addContent(rowData);
        }
    }
    table.print(System.out, !noFormat);
    return null;
}
Also used : Col(org.apache.karaf.shell.support.table.Col) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) ShellTable(org.apache.karaf.shell.support.table.ShellTable) BundleInfo(org.apache.karaf.bundle.core.BundleInfo) Row(org.apache.karaf.shell.support.table.Row)

Aggregations

FrameworkStartLevel (org.osgi.framework.startlevel.FrameworkStartLevel)5 Bundle (org.osgi.framework.Bundle)4 File (java.io.File)2 IOException (java.io.IOException)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 BundleInfo (org.apache.karaf.bundle.core.BundleInfo)1 ServerInfo (org.apache.karaf.info.ServerInfo)1 ArtifactResolver (org.apache.karaf.main.util.ArtifactResolver)1 SimpleMavenResolver (org.apache.karaf.main.util.SimpleMavenResolver)1 Col (org.apache.karaf.shell.support.table.Col)1 Row (org.apache.karaf.shell.support.table.Row)1 ShellTable (org.apache.karaf.shell.support.table.ShellTable)1 Test (org.junit.Test)1 BundleException (org.osgi.framework.BundleException)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 Framework (org.osgi.framework.launch.Framework)1 FrameworkFactory (org.osgi.framework.launch.FrameworkFactory)1 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)1 BundleRevision (org.osgi.framework.wiring.BundleRevision)1