org.osgi.framework.launch.FrameworkFactory Java Examples

The following examples show how to use org.osgi.framework.launch.FrameworkFactory. 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: Main.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public FrameworkFactory getFrameworkFactory() {
  String factoryClassName = FrameworkFactoryImpl.class.getName();
  try {
    final String factoryS = new String(Util.readResource("/META-INF/services/org.osgi.framework.launch.FrameworkFactory"), "UTF-8");
    final String[] w = Util.splitwords(factoryS, "\n\r");
    for(int i = 0; i < w.length; i++) {
      if(w[i].length() > 0 && !w[i].startsWith("#")) {
        factoryClassName = w[i].trim();
        break;
      }
    }

  } catch (final Exception e) {
    // META-INF/services may be lost when putting framework in Android .apk
    println("failed to get FrameworkFactory, using default", 6, e);
  }
  return getFrameworkFactory(factoryClassName);
}
 
Example #2
Source File: FelixGoPluginOSGiFramework.java    From gocd with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
    List<FrameworkFactory> frameworkFactories = IteratorUtils.toList(ServiceLoader.load(FrameworkFactory.class).iterator());

    if (frameworkFactories.size() != 1) {
        throw new RuntimeException("One OSGi framework expected. Got " + frameworkFactories.size() + ": " + frameworkFactories);
    }

    try {
        framework = getFelixFramework(frameworkFactories);
        framework.start();
        registerInternalServices(framework.getBundleContext());
    } catch (BundleException e) {
        throw new RuntimeException("Failed to initialize OSGi framework", e);
    }
}
 
Example #3
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 #4
Source File: Main.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public FrameworkFactory getFrameworkFactory(String factoryClassName) {
  try {
    println("getFrameworkFactory(" + factoryClassName + ")", 2);
    final Class<?> clazz = Class.forName(factoryClassName);
    final Constructor<?> cons  = clazz.getConstructor(new Class[] { });
    final FrameworkFactory ff = (FrameworkFactory) cons.newInstance(new Object[] { });
    return ff;
  } catch (final Exception e) {
    error("failed to create " + factoryClassName, e);
    throw new RuntimeException("failed to create " + factoryClassName + ": " + e);
  }
}
 
Example #5
Source File: TestOsgi.java    From sensorhub with Mozilla Public License 2.0 5 votes vote down vote up
protected Framework getFramework()
{
    Iterator<FrameworkFactory> it = ServiceLoader.load(org.osgi.framework.launch.FrameworkFactory.class).iterator();
    assertTrue("No OSGI implementation found in classpath", it.hasNext());
    
    Map<String,String> osgiConfig = new HashMap<String,String>();
    //osgiConfig.put(AutoProcessor.AUTO_DEPLOY_DIR_PROPERY, "");
    osgiConfig.put("org.osgi.framework.storage", CACHE_FOLDER);
    osgiConfig.put("org.osgi.framework.storage.clean", "onFirstInit");
    Framework fw = it.next().newFramework(osgiConfig);
    
    return fw;
}
 
Example #6
Source File: CarbonServer.java    From carbon-kernel with Apache License 2.0 5 votes vote down vote up
/**
 * Starts a Carbon server instance. This method returns only after the server instance stops completely.
 *
 * @throws Exception if error occurred
 */
public void start() throws Exception {
    if (logger.isLoggable(Level.FINE)) {
        logger.log(Level.FINE, "Starting Carbon server instance.");
    }

    // Sets the server start time.
    System.setProperty(CARBON_START_TIME, Long.toString(System.currentTimeMillis()));

    try {
        // Creates an OSGi framework instance.
        ClassLoader fwkClassLoader = createOSGiFwkClassLoader();
        FrameworkFactory fwkFactory = loadOSGiFwkFactory(fwkClassLoader);
        framework = fwkFactory.newFramework(config.getProperties());

        setServerCurrentStatus(ServerStatus.STARTING);
        // Notify Carbon server start.
        dispatchEvent(CarbonServerEvent.STARTING);

        // Initialize and start OSGi framework.
        initAndStartOSGiFramework(framework);

        // Loads initial bundles listed in the launch.properties file.
        loadInitialBundles(framework.getBundleContext());

        setServerCurrentStatus(ServerStatus.STARTED);
        // This thread waits until the OSGi framework comes to a complete shutdown.
        waitForServerStop(framework);

        setServerCurrentStatus(ServerStatus.STOPPING);
        // Notify Carbon server shutdown.
        dispatchEvent(CarbonServerEvent.STOPPING);

    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example #7
Source File: CarbonServer.java    From carbon-kernel with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new service loader for the given service type and class loader.
 * Load OSGi framework factory for the given class loader.
 *
 * @param classLoader The class loader to be used to load provider-configurations
 * @return framework factory for creating framework instances
 */
private FrameworkFactory loadOSGiFwkFactory(ClassLoader classLoader) {
    if (logger.isLoggable(Level.FINE)) {
        logger.log(Level.FINE, "Loading OSGi FrameworkFactory implementation class from the classpath.");
    }

    ServiceLoader<FrameworkFactory> loader = ServiceLoader.load(FrameworkFactory.class, classLoader);
    if (!loader.iterator().hasNext()) {
        throw new RuntimeException("An implementation of the " + FrameworkFactory.class.getName() +
                " must be available in the classpath");
    }
    return loader.iterator().next();
}
 
Example #8
Source File: Starter.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
public void start ( final String[] args ) throws Exception
{
    if ( this.started )
    {
        return;
    }
    this.started = true;

    this.debug = Boolean.getBoolean ( "org.eclipse.scada.utils.osgi.daemon.debug" ); //$NON-NLS-1$
    if ( this.debug )
    {
        this.logger = new Formatter ( System.out );
    }

    final ServiceLoader<FrameworkFactory> loader = ServiceLoader.load ( FrameworkFactory.class );
    final Iterator<FrameworkFactory> i = loader.iterator ();
    if ( !i.hasNext () )
    {
        throw new IllegalStateException ( "No FrameworkFactory found!" );
    }

    final FrameworkFactory factory = i.next ();

    this.properties = new HashMap<String, String> ();

    for ( final String arg : args )
    {
        final String[] toks = arg.split ( "=", 2 );
        if ( toks.length >= 2 )
        {
            this.properties.put ( toks[0], toks[1] );
        }
        else
        {
            this.properties.put ( toks[0], null );
        }
    }

    this.properties.put ( Constants.FRAMEWORK_BEGINNING_STARTLEVEL, "4" );

    this.framework = factory.newFramework ( this.properties );

    this.framework.init ();

    try
    {
        loadStartBundles ( this.framework, this.properties );
    }
    catch ( final Exception e )
    {
        this.framework.stop ();
        throw e;
    }

    this.framework.start ();
}
 
Example #9
Source File: FelixGoPluginOSGiFramework.java    From gocd with Apache License 2.0 4 votes vote down vote up
Framework getFelixFramework(List<FrameworkFactory> frameworkFactories) {
    return frameworkFactories.get(0).newFramework(generateOSGiFrameworkConfig());
}
 
Example #10
Source File: OsgiTestRule.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public OsgiTestRule(final FrameworkFactory factory) {
    this.factory = factory;
}
 
Example #11
Source File: FelixLoadApiBundleTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
protected FrameworkFactory getFactory() {
    return new org.apache.felix.framework.FrameworkFactory();
}
 
Example #12
Source File: EquinoxLoadApiBundleTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
protected FrameworkFactory getFactory() {
    return new EquinoxFactory();
}
 
Example #13
Source File: AbstractOsgiTest.java    From logging-log4j2 with Apache License 2.0 votes vote down vote up
protected abstract FrameworkFactory getFactory();