Java Code Examples for org.osgi.framework.launch.Framework#getBundleContext()

The following examples show how to use org.osgi.framework.launch.Framework#getBundleContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NetigsoStartLevelTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testActivation() throws Exception {
    ModuleSystem ms = Main.getModuleSystem();
    mgr = ms.getManager();
    mgr.mutexPrivileged().enterWriteAccess();
    try {
        m1 = mgr.createBundle(simpleModule, null, false, false, false, 10);
        mgr.enable(m1);
    } finally {
        mgr.mutexPrivileged().exitWriteAccess();
    }

    Class<?> main = m1.getClassLoader().loadClass("org.activate.Main");
    Object s = main.getField("start").get(null);
    assertNull("Not started yet", s);

    Framework f = NetigsoServicesTest.findFramework();
    final BundleContext fc = f.getBundleContext();
    fc.addFrameworkListener(this);
    ServiceReference sr = fc.getServiceReference(StartLevel.class.getName());
    assertNotNull("Start level service found", sr);
    StartLevel level = (StartLevel) fc.getService(sr);
    assertNotNull("Start level found", level);
    level.setStartLevel(10);
    waitLevelChanged();
        
    s = main.getField("start").get(null);
    assertNotNull("Bundle started, its context provided", s);

    mgr.mutexPrivileged().enterWriteAccess();
    try {
        mgr.disable(m1);

        Object e = main.getField("stop").get(null);
        assertNotNull("Bundle stopped, its context provided", e);
    } finally {
        mgr.mutexPrivileged().exitWriteAccess();
    }
}
 
Example 2
Source File: LogReaderServiceTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testExtendedLogReaderServiceAvailable() throws Exception {
    Framework f = IntegrationTest.findFramework();
    BundleContext bc = f.getBundleContext();
    
    ServiceTracker logReaderTracker = new ServiceTracker(bc, ExtendedLogReaderService.class.getName(), null);
    logReaderTracker.open();
            
    LogReaderService logReader = (ExtendedLogReaderService) logReaderTracker.getService();
    assertNotNull(logReader);
        
}
 
Example 3
Source File: NetigsoHasSAXParserTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testSAXParserAvailable() throws Exception {
    Framework f = IntegrationTest.findFramework();
    BundleContext bc = f.getBundleContext();
    
    ServiceReference sr = bc.getServiceReference(SAXParserFactory.class.getName());
    assertNotNull("SAX Service found", sr);
    Object srvc = bc.getService(sr);
    assertTrue("Instance of the right type: " + srvc, srvc instanceof SAXParserFactory);
        
}
 
Example 4
Source File: EmbeddedFelixFramework.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private static void installBootBundles(Framework framework) {
    Stopwatch timer = Stopwatch.createStarted();
    LOG.debug("Installing OSGi boot bundles from "+EmbeddedFelixFramework.class.getClassLoader()+"...");
    
    Iterator<URL> resources = BOOT_BUNDLES.iterator();
    // previously we evaluated this each time, but lately (discovered in 2019,
    // possibly the case for a long time before) it seems to grow, accessing ad hoc dirs
    // in cache/* made by tests, which get deleted, logging lots of errors.
    // so now we statically populate it at load time.
    
    BundleContext bundleContext = framework.getBundleContext();
    Map<String, Bundle> installedBundles = getInstalledBundlesById(bundleContext);
    while (resources.hasNext()) {
        URL url = resources.next();
        ReferenceWithError<?> installResult = installExtensionBundle(bundleContext, url, installedBundles, OsgiUtils.getVersionedId(framework));
        if (installResult.hasError() && !installResult.masksErrorIfPresent()) {
            // it's reported as a critical error, so warn here
            LOG.warn("Unable to install manifest from "+url+": "+installResult.getError(), installResult.getError());
        } else {
            Object result = installResult.getWithoutError();
            if (result instanceof Bundle) {
                String v = OsgiUtils.getVersionedId( (Bundle)result );
                SYSTEM_BUNDLES.add(v);
                if (installResult.hasError()) {
                    LOG.debug(installResult.getError().getMessage()+(result!=null ? " ("+result+"/"+v+")" : ""));
                } else {
                    LOG.debug("Installed "+v+" from "+url);
                }
            } else if (installResult.hasError()) {
                LOG.debug(installResult.getError().getMessage());
            }
        }
    }
    LOG.debug("Installed OSGi boot bundles in "+Time.makeTimeStringRounded(timer)+": "+Arrays.asList(framework.getBundleContext().getBundles()));
}