org.apache.felix.framework.FrameworkFactory Java Examples

The following examples show how to use org.apache.felix.framework.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: EmbeddedFelixFramework.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
public static FrameworkFactory newFrameworkFactory() {
    URL url = EmbeddedFelixFramework.class.getClassLoader().getResource(
            "META-INF/services/org.osgi.framework.launch.FrameworkFactory");
    if (url != null) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            try {
                for (String s = br.readLine(); s != null; s = br.readLine()) {
                    s = s.trim();
                    // load the first non-empty, non-commented line
                    if ((s.length() > 0) && (s.charAt(0) != '#')) {
                        return (FrameworkFactory) Class.forName(s).newInstance();
                    }
                }
            } finally {
                if (br != null) br.close();
            }
        } catch (Exception e) {
            // class creation exceptions are not interesting to caller...
            throw Exceptions.propagate(e);
        }
    }
    throw new IllegalStateException("Could not find framework factory.");
}
 
Example #2
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 #3
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 ();
  }
}