Search in sources :

Example 6 with FrameworkFactory

use of org.osgi.framework.launch.FrameworkFactory in project roboguice by roboguice.

the class OSGiContainerTest method testGuiceWorksInOSGiContainer.

//This test may fail if it is not run from ant, or if it is not run after ant has
//compiled & built jars. This is because the test is validating that the Guice jar
//is properly setup to load in an OSGi container
public void testGuiceWorksInOSGiContainer() throws Throwable {
    // ask framework to clear cache on startup
    Properties properties = new Properties();
    properties.setProperty("org.osgi.framework.storage", BUILD_TEST_DIR + "/bundle.cache");
    properties.setProperty("org.osgi.framework.storage.clean", "onFirstInit");
    // test each available OSGi framework in turn
    Iterator<FrameworkFactory> f = ServiceRegistry.lookupProviders(FrameworkFactory.class);
    while (f.hasNext()) {
        Framework framework = f.next().newFramework(properties);
        framework.start();
        BundleContext systemContext = framework.getBundleContext();
        // load all the necessary bundles and start the OSGi test bundle
        /*if[AOP]*/
        systemContext.installBundle("reference:file:" + BUILD_TEST_DIR + "/aopalliance.jar");
        /*end[AOP]*/
        systemContext.installBundle("reference:file:" + BUILD_TEST_DIR + "/javax.inject.jar");
        systemContext.installBundle("reference:file:" + BUILD_TEST_DIR + "/guava.jar");
        systemContext.installBundle("reference:file:" + GUICE_JAR);
        systemContext.installBundle("reference:file:" + BUILD_TEST_DIR + "/osgitests.jar").start();
        framework.stop();
    }
}
Also used : Properties(java.util.Properties) Framework(org.osgi.framework.launch.Framework) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) BundleContext(org.osgi.framework.BundleContext)

Example 7 with FrameworkFactory

use of org.osgi.framework.launch.FrameworkFactory in project voltdb by VoltDB.

the class TestVoltCSVFormatter method setUp.

@Override
@Before
public void setUp() throws Exception {
    List<String> packages = ImmutableList.<String>builder().add("org.voltcore.network").add("org.voltcore.logging").add("org.voltdb.importer").add("org.voltdb.importer.formatter").add("org.apache.log4j").add("org.voltdb.client").add("org.slf4j").add("org.voltcore.utils").add("com.google_voltpatches.common.base").add("com.google_voltpatches.common.collect").add("com.google_voltpatches.common.net").add("com.google_voltpatches.common.io").add("com.google_voltpatches.common.util.concurrent").build();
    String tmpFilePath = System.getProperty(VOLT_TMP_DIR, System.getProperty("java.io.tmpdir"));
    //Create a directory in temp + username
    File f = new File(tmpFilePath, System.getProperty("user.name"));
    String systemPackagesSpec = FluentIterable.from(packages).transform(appendVersion).join(COMMA_JOINER);
    Map<String, String> m_frameworkProps = ImmutableMap.<String, String>builder().put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, systemPackagesSpec).put("org.osgi.framework.storage.clean", "onFirstInit").put("felix.cache.rootdir", f.getAbsolutePath()).put("felix.cache.locking", Boolean.FALSE.toString()).build();
    FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
    m_framework = frameworkFactory.newFramework(m_frameworkProps);
    m_framework.start();
    m_bundle = m_framework.getBundleContext().installBundle("file:" + System.getProperty("user.dir") + "/bundles/voltcsvformatter.jar");
    m_bundle.start();
}
Also used : File(java.io.File) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) Before(org.junit.Before)

Example 8 with FrameworkFactory

use of org.osgi.framework.launch.FrameworkFactory in project aries by apache.

the class AriesRepositoryGenerator method startFramework.

/**
   * Start OSGi framework and install the necessary bundles
   * @return
   * @throws BundleException
   */
public BundleContext startFramework() throws BundleException {
    ServiceLoader<FrameworkFactory> factoryLoader = ServiceLoader.load(FrameworkFactory.class, getClass().getClassLoader());
    Iterator<FrameworkFactory> factoryIterator = factoryLoader.iterator();
    //Map<String, String> osgiPropertyMap = new HashMap<String, String>();
    if (!!!factoryIterator.hasNext()) {
        System.out.println("Unable to locate the osgi jar");
    }
    try {
        FrameworkFactory frameworkFactory = factoryIterator.next();
        framework = frameworkFactory.newFramework(Collections.EMPTY_MAP);
    } catch (ServiceConfigurationError sce) {
        sce.printStackTrace();
    }
    framework.init();
    framework.start();
    // install the bundles in the current directory
    Collection<Bundle> installedBundles = new ArrayList<Bundle>();
    File bundleDir = new File(".");
    File[] jars = bundleDir.listFiles();
    try {
        for (File jar : jars) {
            if (jar.isFile() && (jar.getName().endsWith(".jar"))) {
                String location = URLDecoder.decode(jar.toURI().toURL().toExternalForm(), "UTF-8");
                if (shouldInstall(location, getIgnoreList())) {
                    installedBundles.add(framework.getBundleContext().installBundle("reference:" + location));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    ServiceReference paRef = framework.getBundleContext().getServiceReference(PackageAdmin.class.getCanonicalName());
    if (paRef != null) {
        try {
            PackageAdmin admin = (PackageAdmin) framework.getBundleContext().getService(paRef);
            admin.resolveBundles(installedBundles.toArray(new Bundle[installedBundles.size()]));
        } finally {
            framework.getBundleContext().ungetService(paRef);
        }
    } else {
        System.out.println("Unable to find the service reference for package admin");
    }
    //loop through the list of installed bundles to start them
    for (Bundle bundle : installedBundles) {
        try {
            if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) == null) {
                //start the bundle using its activiation policy
                bundle.start(Bundle.START_ACTIVATION_POLICY);
            } else {
            //nothing to start, we have got a fragment
            }
        } catch (BundleException e) {
            e.printStackTrace();
            continue;
        }
    }
    return framework.getBundleContext();
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ServiceConfigurationError(java.util.ServiceConfigurationError) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) ServiceReference(org.osgi.framework.ServiceReference) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) BundleException(org.osgi.framework.BundleException) File(java.io.File)

Example 9 with FrameworkFactory

use of org.osgi.framework.launch.FrameworkFactory in project karaf by apache.

the class Main method launch.

public void launch() throws Exception {
    if (config == null) {
        config = new ConfigProperties();
    }
    config.performInit();
    if (config.delayConsoleStart) {
        System.out.println(config.startupMessage);
    }
    String log4jConfigPath = System.getProperty("karaf.etc") + "/org.ops4j.pax.logging.cfg";
    BootstrapLogManager.setProperties(config.props, log4jConfigPath);
    lockCallback = new KarafLockCallback();
    InstanceHelper.updateInstancePid(config.karafHome, config.karafBase, true);
    BootstrapLogManager.configureLogger(LOG);
    for (String provider : config.securityProviders) {
        addSecurityProvider(provider);
    }
    List<File> bundleDirs = getBundleRepos();
    ArtifactResolver resolver = new SimpleMavenResolver(bundleDirs);
    // Start up the OSGI framework
    ClassLoader classLoader = createClassLoader(resolver);
    FrameworkFactory factory = loadFrameworkFactory(classLoader);
    framework = factory.newFramework(config.props);
    setLogger();
    framework.init();
    framework.getBundleContext().addFrameworkListener(lockCallback);
    framework.start();
    FrameworkStartLevel sl = framework.adapt(FrameworkStartLevel.class);
    sl.setInitialBundleStartLevel(config.defaultBundleStartlevel);
    // If we have a clean state, install everything
    if (framework.getBundleContext().getBundles().length == 1) {
        LOG.info("Installing and starting initial bundles");
        File startupPropsFile = new File(config.karafEtc, STARTUP_PROPERTIES_FILE_NAME);
        List<BundleInfo> bundles = readBundlesFromStartupProperties(startupPropsFile);
        installAndStartBundles(resolver, framework.getBundleContext(), bundles);
        LOG.info("All initial bundles installed and set to start");
    }
    ServerInfo serverInfo = new ServerInfoImpl(args, config);
    framework.getBundleContext().registerService(ServerInfo.class, serverInfo, null);
    activatorManager = new KarafActivatorManager(classLoader, framework);
    activatorManager.startKarafActivators();
    setStartLevel(config.lockStartLevel);
    // Progress bar
    if (config.delayConsoleStart) {
        new StartupListener(LOG, framework.getBundleContext());
    }
    monitor();
    registerSignalHandler();
    watchdog();
}
Also used : ServerInfo(org.apache.karaf.info.ServerInfo) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) ArtifactResolver(org.apache.karaf.main.util.ArtifactResolver) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) SimpleMavenResolver(org.apache.karaf.main.util.SimpleMavenResolver) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 10 with FrameworkFactory

use of org.osgi.framework.launch.FrameworkFactory in project karaf by apache.

the class Main method loadFrameworkFactory.

private FrameworkFactory loadFrameworkFactory(ClassLoader classLoader) throws Exception {
    String factoryClass = config.frameworkFactoryClass;
    if (factoryClass == null) {
        InputStream is = classLoader.getResourceAsStream("META-INF/services/" + FrameworkFactory.class.getName());
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        factoryClass = br.readLine();
        br.close();
    }
    FrameworkFactory factory = (FrameworkFactory) classLoader.loadClass(factoryClass).newInstance();
    return factory;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory)

Aggregations

FrameworkFactory (org.osgi.framework.launch.FrameworkFactory)12 File (java.io.File)7 Framework (org.osgi.framework.launch.Framework)6 BundleContext (org.osgi.framework.BundleContext)5 HashMap (java.util.HashMap)4 Bundle (org.osgi.framework.Bundle)4 BundleException (org.osgi.framework.BundleException)4 IOException (java.io.IOException)3 Properties (java.util.Properties)3 FrameworkListener (org.osgi.framework.FrameworkListener)3 ArrayList (java.util.ArrayList)2 FrameworkEvent (org.osgi.framework.FrameworkEvent)2 Version (aQute.bnd.version.Version)1 MiniFramework (aQute.launcher.minifw.MiniFramework)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URLClassLoader (java.net.URLClassLoader)1