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

The following examples show how to use org.osgi.framework.launch.Framework#start() . 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: EmbeddedFelixFramework.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
public static Framework newFrameworkStarted(String felixCacheDir, boolean clean, Map<?,?> extraStartupConfig) {
    Map<Object,Object> cfg = MutableMap.copyOf(extraStartupConfig);
    if (clean) cfg.put(Constants.FRAMEWORK_STORAGE_CLEAN, "onFirstInit");
    if (felixCacheDir!=null) cfg.put(Constants.FRAMEWORK_STORAGE, felixCacheDir);
    cfg.put(Constants.FRAMEWORK_BSNVERSION, Constants.FRAMEWORK_BSNVERSION_MULTIPLE);
    FrameworkFactory factory = newFrameworkFactory();

    Stopwatch timer = Stopwatch.createStarted();
    Framework framework = factory.newFramework(cfg);
    try {
        framework.init();
        installBootBundles(framework);
        framework.start();
    } catch (Exception e) {
        // framework bundle start exceptions are not interesting to caller...
        throw Exceptions.propagate(e);
    }
    LOG.debug("System bundles are: "+SYSTEM_BUNDLES);
    LOG.debug("OSGi framework started in " + Duration.of(timer));
    return framework;
}
 
Example 2
Source File: TestOsgi.java    From sensorhub with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void test2InstallBundle() throws Exception
{
    Framework fw = getFramework();
    fw.start();
    
    // install bundle w/o dependency
    Bundle newBundle = installBundle(fw, getClass().getResource("/test-nodep.jar").toString(), "org.sensorhub.test");
            
    // attempt to start it
    newBundle.start();
    assertEquals("Bundle should be in ACTIVE state", Bundle.ACTIVE, newBundle.getState());
    
    fw.stop();
    fw.waitForStop(0);
}
 
Example 3
Source File: TestOsgi.java    From sensorhub with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void test3BundleDependencies() throws Exception
{
    Framework fw = getFramework();
    fw.start();
    
    assertEquals("Wrong number of loaded bundles", 1, fw.getBundleContext().getBundles().length);
    
    // install 1st bundle
    installBundle(fw, getClass().getResource("/test-nodep.jar").toString(), "org.sensorhub.test");
    
    // install 2nd bundle
    Bundle bundle2 = installBundle(fw, getClass().getResource("/test-withdep.jar").toString(), "org.sensorhub.test2");
    
    bundle2.start();
    assertEquals("Bundle " + bundle2.getSymbolicName() + " should be in ACTIVE state", Bundle.ACTIVE, bundle2.getState());
    
    fw.stop();
    fw.waitForStop(0);
}
 
Example 4
Source File: CarbonServer.java    From carbon-kernel with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes and start framework. Framework will try to resolve all the bundles if their requirements
 * can be satisfied.
 *
 * @param framework osgiFramework
 * @throws BundleException
 */
private void initAndStartOSGiFramework(Framework framework) throws BundleException {
    if (logger.isLoggable(Level.FINE)) {
        logger.log(Level.FINE, "Initializing the OSGi framework.");
    }

    framework.init();

    // Starts the framework.
    if (logger.isLoggable(Level.FINE)) {
        logger.log(Level.FINE, "Starting the OSGi framework.");
    }

    framework.start();

    if (logger.isLoggable(Level.FINE)) {
        logger.log(Level.FINE, "Started the OSGi framework.");
    }
}
 
Example 5
Source File: JrtUrlTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Test
public void initFrameWorkAndThenCreateTheUrl() throws Exception {
    Framework framework;
    for (FrameworkFactory ff : ServiceLoader.load(FrameworkFactory.class)) {
        Map<String, String> config = new HashMap<String, String>();
        framework = ff.newFramework(config);
        framework.init();
        framework.start();
        break;
    }

    URL test = new URL("jrt://java.compiler/");
    assertEquals("jrt", test.getProtocol());
}
 
Example 6
Source File: AbstractConciergeTestCase.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
/** Start framework with given settings. */
public void startFramework(final Map<String, String> launchArgs)
		throws Exception {
	Framework frameworkToStart = new Factory().newFramework(launchArgs);
	frameworkToStart.init();
	frameworkToStart.start();
	useFramework(frameworkToStart);
}
 
Example 7
Source File: KfApk.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Create a new framework, initialize and start it. If the init
 * parameter is true, or if there is no stored framework state, bundle
 * Jar file data will be read from the "jars" folder of the provided
 * AssetManager. If the init parameter is false and there is a stored
 * framework state, the framework will be started and its state will
 * be the stored state.
 * 
 * @param localStorage Path to application's storage location in the
 *        file system, framework state will be stored here
 * @param init If true, any stored framework state is ignored.
 * @param am AssetManager for the application, will be used to read
 *        bundle Jar file data when the framework is started without
 *        using a stored state.
 * @return a framework instance or null if the framework could not be
 *         started or initialized.
 * @throws IOException if there was a problem reading bundle Jar file
 *         data or framework/system properties using the AssestManager.
 */
public static Framework newFramework(String localStorage,
                                     boolean init,
                                     AssetManager am)
    throws IOException
{
  config = getConfiguration(am);
  String fwDir = localStorage + File.separator + FWDIR;
  config.put(Constants.FRAMEWORK_STORAGE, fwDir);

  boolean doInit = init || !new File(fwDir).exists();
  
  if (doInit) {
    config.put(Constants.FRAMEWORK_STORAGE_CLEAN,
               Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
  }

  Framework fw = new Main().getFrameworkFactory().newFramework(config);
  try {
    fw.init();
    int runlevel;
    if (doInit) {
      runlevel = scanBundles(fw, am);
      saveRunlevel(runlevel, fwDir);
    } else {
      runlevel = loadRunlevel(fwDir);
    }
    fw.start();
    // set target start level for framework start-up
    final FrameworkStartLevel fsl = fw.adapt(FrameworkStartLevel.class);
    if (fsl!=null) {
      fsl.setStartLevel(runlevel);
    }      
  } catch (BundleException be) {
    Log.e(KfApk.class.getName(), "Failed to init/start framework", be);
    return null;
  }
  
  return fw;
}
 
Example 8
Source File: TestOsgi.java    From sensorhub with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void test1StartStopFramework() throws Exception
{
    Framework fw = getFramework();
    fw.start();
    Thread.sleep(500);
    fw.stop();
    fw.waitForStop(0);
}
 
Example 9
Source File: SimpleLSResourceResolverTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testOSGIBundle () throws BundleException
{
  LSInput aRes;

  // Initializing Apache Felix as OSGI container is required to get the
  // "bundle" URL protocol installed correctly
  // Otherwise the first call would end up as a "file" resource ;-)
  final Framework aOSGI = new FrameworkFactory ().newFramework (new HashMap <String, String> ());
  aOSGI.start ();
  try
  {
    // Bundle 0 is the org.apache.felix.framework bundle
    final Bundle b = aOSGI.getBundleContext ().getBundle (0);
    assertNotNull (b);
    assertEquals (Bundle.ACTIVE, b.getState ());

    // No leading slash is important as the ClassLoader is used!
    assertNotNull (b.getResource ("org/apache/felix/framework/util/Util.class"));

    final LSResourceResolver aRR = new SimpleLSResourceResolver ();

    // No class loader
    aRes = aRR.resolveResource (XMLConstants.W3C_XML_SCHEMA_NS_URI,
                                null,
                                null,
                                "../Felix.class",
                                "bundle://0.0:1/org/apache/felix/framework/util/Util.class");
    assertTrue (aRes instanceof ResourceLSInput);
    final IHasInputStream aISP = ((ResourceLSInput) aRes).getInputStreamProvider ();
    assertTrue (aISP instanceof URLResource);
    // Path maybe a "jar:file:" resource
    assertTrue (((URLResource) aISP).getPath ().endsWith ("org/apache/felix/framework/Felix.class"));
  }
  finally
  {
    aOSGI.stop ();
  }
}