org.osgi.framework.BundleActivator

Here are the examples of the java api class org.osgi.framework.BundleActivator taken from open source projects.

1. SmxKernelPlatform#start()

Project: servicemix4-kernel
File: SmxKernelPlatform.java
public void start() throws Exception {
    Set<String> jars = getJars(Felix.class);
    ClassLoader classLoader = new GuardClassLoader(toURLs(jars.toArray(new String[jars.size()])), null);
    BundleActivator activator = new BundleActivator() {

        private ServiceRegistration registration;

        public void start(BundleContext context) {
            registration = context.registerService(MainService.class.getName(), new MainService() {

                public String[] getArgs() {
                    return new String[0];
                }

                public int getExitCode() {
                    return 0;
                }

                public void setExitCode(int exitCode) {
                }
            }, null);
        }

        public void stop(BundleContext context) {
            registration.unregister();
        }
    };
    List<BundleActivator> activations = new ArrayList<BundleActivator>();
    activations.add(activator);
    Properties props = getConfigurationProperties();
    props.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, activations);
    Thread.currentThread().setContextClassLoader(classLoader);
    Class cl = classLoader.loadClass(Felix.class.getName());
    Constructor cns = cl.getConstructor(Map.class);
    platform = cns.newInstance(props);
    platform.getClass().getMethod("start").invoke(platform);
    Bundle systemBundle = (Bundle) platform;
    // call getBundleContext
    final Method getContext = systemBundle.getClass().getMethod("getBundleContext", null);
    AccessController.doPrivileged(new PrivilegedAction() {

        public Object run() {
            getContext.setAccessible(true);
            return null;
        }
    });
    context = (BundleContext) getContext.invoke(systemBundle, null);
}

2. Main#launch()

Project: servicemix4-kernel
File: Main.java
public void launch() throws Exception {
    servicemixHome = getServiceMixHome();
    servicemixBase = getServiceMixBase(servicemixHome);
    //System.out.println("ServiceMix Home: "+main.servicemixHome.getPath());
    //System.out.println("ServiceMix Base: "+main.servicemixBase.getPath());
    System.setProperty(PROP_SERVICEMIX_HOME, servicemixHome.getPath());
    System.setProperty(PROP_SERVICEMIX_BASE, servicemixBase.getPath());
    // Load system properties.
    loadSystemProperties();
    // Read configuration properties.
    m_configProps = loadConfigProperties();
    // Copy framework properties from the system properties.
    Main.copySystemProperties(m_configProps);
    processSecurityProperties(m_configProps);
    m_configProps.setProperty(BundleCache.CACHE_ROOTDIR_PROP, servicemixBase.getPath() + "/data");
    m_configProps.setProperty(Constants.FRAMEWORK_STORAGE, "cache");
    // Register the Main class so that other bundles can inspect the command line args.
    BundleActivator activator = new BundleActivator() {

        private ServiceRegistration registration;

        public void start(BundleContext context) {
            registration = context.registerService(MainService.class.getName(), Main.this, null);
        }

        public void stop(BundleContext context) {
            registration.unregister();
            shutdown.countDown();
        }
    };
    List<BundleActivator> activations = new ArrayList<BundleActivator>();
    activations.add(this);
    activations.add(activator);
    m_configProps.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, activations);
    try {
        defaultStartLevel = Integer.parseInt(m_configProps.getProperty(Constants.FRAMEWORK_BEGINNING_STARTLEVEL));
        lockStartLevel = Integer.parseInt(m_configProps.getProperty(PROPERTY_LOCK_LEVEL, Integer.toString(lockStartLevel)));
        lockDelay = Integer.parseInt(m_configProps.getProperty(PROPERTY_LOCK_DELAY, Integer.toString(lockDelay)));
        m_configProps.setProperty(Constants.FRAMEWORK_BEGINNING_STARTLEVEL, Integer.toString(lockStartLevel));
        // Start up the OSGI framework
        m_felix = new Felix(new StringMap(m_configProps, false));
        m_felix.start();
        // Start lock monitor
        new Thread() {

            public void run() {
                lock(m_configProps);
            }
        }.start();
    } catch (Exception ex) {
        setExitCode(-1);
        throw new Exception("Could not create framework", ex);
    }
}

3. OakOSGiRepositoryFactory#getApplicationActivator()

Project: jackrabbit-oak
File: OakOSGiRepositoryFactory.java
/**
     * Return the BundleActivator provided by the embedding application
     * @param config config passed to factory for initialization
     * @return BundleActivator instance
     */
private static BundleActivator getApplicationActivator(Map config) {
    BundleActivator activator = (BundleActivator) config.get(BundleActivator.class.getName());
    if (activator == null) {
        activator = NOOP;
    }
    return activator;
}

4. OakOSGiRepositoryFactory#getRepository()

Project: jackrabbit-oak
File: OakOSGiRepositoryFactory.java
@SuppressWarnings("unchecked")
public Repository getRepository(Map parameters) throws RepositoryException {
    if (parameters == null || !parameters.containsKey(REPOSITORY_HOME)) {
        //Required param missing so repository cannot be created
        return null;
    }
    Map config = new HashMap();
    config.putAll(parameters);
    PojoServiceRegistry registry = initializeServiceRegistry(config);
    BundleActivator activator = getApplicationActivator(config);
    try {
        activator.start(registry.getBundleContext());
    } catch (Exception e) {
        log.warn("Error occurred while starting activator {}", activator.getClass(), e);
    }
    //Future which would be used to notify when repository is ready
    // to be used
    SettableFuture<Repository> repoFuture = SettableFuture.create();
    new RunnableJobTracker(registry.getBundleContext());
    int timeoutInSecs = PropertiesUtil.toInteger(config.get(REPOSITORY_TIMEOUT_IN_SECS), DEFAULT_TIMEOUT);
    //Start the tracker for repository creation
    new RepositoryTracker(registry, activator, repoFuture, timeoutInSecs);
    // where OSGi runtime fails to start due to bugs (like cycles)
    try {
        return repoFuture.get(timeoutInSecs, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RepositoryException("Repository initialization was interrupted");
    } catch (ExecutionException e) {
        throw new RepositoryException(e);
    } catch (TimeoutException e) {
        try {
            if (PropertiesUtil.toBoolean(config.get(REPOSITORY_SHUTDOWN_ON_TIMEOUT), true)) {
                shutdown(registry, timeoutInSecs);
                log.info("OSGi container shutdown after waiting for repository initialization for {} sec", timeoutInSecs);
            } else {
                log.warn("[{}] found to be false. Container is not stopped", REPOSITORY_SHUTDOWN_ON_TIMEOUT);
            }
        } catch (BundleException be) {
            log.warn("Error occurred while shutting down the service registry (due to " + "startup timeout) backing the Repository ", be);
        }
        throw new RepositoryException("Repository could not be started in " + timeoutInSecs + " seconds", e);
    }
}

5. Launcher#activate()

Project: bnd
File: Launcher.java
public int activate() throws Exception {
    active.set(true);
    Policy.setPolicy(new AllPolicy());
    systemBundle = createFramework();
    if (systemBundle == null)
        return LauncherConstants.ERROR;
    doTimeoutHandler();
    doSecurity();
    // Initialize this framework so it becomes STARTING
    systemBundle.start();
    trace("system bundle started ok");
    BundleContext systemContext = systemBundle.getBundleContext();
    systemContext.addServiceListener(this, "(&(|(objectclass=" + Runnable.class.getName() + ")(objectclass=" + Callable.class.getName() + "))(main.thread=true))");
    int result = LauncherConstants.OK;
    // Start embedded activators
    trace("start embedded activators");
    if (parms.activators != null) {
        ClassLoader loader = getClass().getClassLoader();
        for (Object token : parms.activators) {
            try {
                Class<?> clazz = loader.loadClass((String) token);
                BundleActivator activator = (BundleActivator) clazz.newInstance();
                if (isImmediate(activator)) {
                    start(systemContext, result, activator);
                }
                embedded.add(activator);
                trace("adding activator %s", activator);
            } catch (Exception e) {
                throw new IllegalArgumentException("Embedded Bundle Activator incorrect: " + token + ", " + e);
            }
        }
    }
    update(System.currentTimeMillis() + 100);
    if (parms.trace) {
        report(out);
    }
    for (BundleActivator activator : embedded) if (!isImmediate(activator))
        result = start(systemContext, result, activator);
    return result;
}

6. DSTestWiringTest#testSimple()

Project: bnd
File: DSTestWiringTest.java
public void testSimple() throws Exception {
    BundleActivator act = mock(BundleActivator.class);
    TestingLog testlog = new TestingLog().direct().stacktrace();
    DSTestWiring ds = new DSTestWiring();
    // by instance
    ds.add(this);
    ds.add(act);
    // instance
    ds.add(testlog).$("filters", Arrays.asList("skip"));
    // by name
    ds.add(String.class.getName());
    // by class
    ds.add(A.class).$("a", 1);
    ds.add(1);
    ds.add(2);
    ds.add(3);
    ds.add(4);
    ds.wire();
    assertNotNull(log);
    assertNotNull(string);
    assertNotNull(a);
    assertNotNull(a.map);
    assertEquals(1, a.map.get("a"));
    assertEquals(Arrays.asList(1, 2, 3, 4), integers);
    assertEquals(1, integer);
    ds.get(BundleActivator.class).start(null);
    verify(act).start(null);
    verifyNoMoreInteractions(act);
    log.log(LogService.LOG_ERROR, "skip");
    log.log(LogService.LOG_ERROR, "include");
    assertEquals(1, testlog.getEntries().size());
    assertFalse(testlog.check("include"));
}

7. Activator#stop()

Project: aries
File: Activator.java
public void stop(BundleContext bundleContext) throws Exception {
    for (BundleActivator activator : activators) {
        activator.stop(bundleContext);
    }
}

8. Activator#start()

Project: aries
File: Activator.java
public void start(BundleContext bundleContext) throws Exception {
    for (BundleActivator activator : activators) {
        activator.start(bundleContext);
    }
}

9. Activator#stop()

Project: apache-aries
File: Activator.java
public void stop(BundleContext bundleContext) throws Exception {
    for (BundleActivator activator : activators) {
        activator.stop(bundleContext);
    }
}

10. Activator#start()

Project: apache-aries
File: Activator.java
public void start(BundleContext bundleContext) throws Exception {
    for (BundleActivator activator : activators) {
        activator.start(bundleContext);
    }
}