Search in sources :

Example 16 with FrameworkFactory

use of org.osgi.framework.launch.FrameworkFactory in project Payara by payara.

the class BundleProvisioner method main.

/**
 * A simple main method to test this class
 *
 * @param args
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) throws Exception {
    logger.log(INFO, STARTING_BUNDLEPROVISIONER);
    Properties props = new Properties();
    props.load(new FileInputStream(args[0]));
    Util.substVars(props);
    long t0 = System.currentTimeMillis();
    Framework framework = null;
    Map<String, String> mm = new HashMap<>();
    props.putAll(mm);
    for (FrameworkFactory frameworkFactory : ServiceLoader.load(FrameworkFactory.class)) {
        framework = frameworkFactory.newFramework(mm);
        System.out.println("framework = " + framework);
        break;
    }
    if (framework == null) {
        throw new RuntimeException("no OSGi framework in classpath");
    }
    long t1 = System.currentTimeMillis();
    logger.log(Level.INFO, LogFacade.OSGI_LOCATE_TIME, (t1 - t0));
    framework.init();
    long t2 = System.currentTimeMillis();
    logger.log(Level.INFO, LogFacade.OSGI_INIT_TIME, (t2 - t1));
    BundleProvisioner bundleProvisioner = createBundleProvisioner(framework.getBundleContext(), props);
    bundleProvisioner.installBundles();
    long t3 = System.currentTimeMillis();
    logger.log(Level.INFO, LogFacade.BUNDLE_INSTALLATION_TIME, (t3 - t2));
    int installed = bundleProvisioner.getNoOfInstalledBundles();
    int updated = bundleProvisioner.getNoOfUpdatedBundles();
    int uninstalled = bundleProvisioner.getNoOfUninstalledBundles();
    System.out.printf("installed = %d, updated = %d, uninstalled = %d\n", installed, updated, uninstalled);
    if (bundleProvisioner.hasAnyThingChanged()) {
        System.out.println("Refreshing framework");
        bundleProvisioner.refresh();
    }
    bundleProvisioner.startBundles();
    framework.start();
    long t4 = System.currentTimeMillis();
    logger.log(Level.INFO, LogFacade.BUNDLE_STARTING_TIME, (t4 - t3));
    logger.log(Level.INFO, LogFacade.TOTAL_START_TIME, (t4 - t0));
    if (args.length == 3 && args[2].equalsIgnoreCase("wait-before-stopping")) {
        System.out.println("Hit enter to continue");
        // 
        System.in.read();
    }
    framework.stop();
    framework.waitForStop(0);
    long t5 = System.currentTimeMillis();
    logger.log(Level.INFO, LogFacade.BUNDLE_STOP_TIME, (t5 - t4));
    logger.log(Level.INFO, LogFacade.TOTAL_TIME, (t5 - t0));
    try (PrintStream out = new PrintStream(new FileOutputStream(args[1], true))) {
        out.printf("%d,%d,%d,%d,%d,%d,%d\n", t1 - t0, t2 - t1, t3 - t2, t4 - t3, t4 - t0, t5 - t4, t5 - t0);
    }
}
Also used : FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) Framework(org.osgi.framework.launch.Framework)

Example 17 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 18 with FrameworkFactory

use of org.osgi.framework.launch.FrameworkFactory in project guice by google.

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
@Test
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
    for (FrameworkFactory frameworkFactory : ServiceLoader.load(FrameworkFactory.class)) {
        Framework framework = frameworkFactory.newFramework(properties);
        framework.start();
        BundleContext systemContext = framework.getBundleContext();
        // load all the necessary bundles and start the OSGi test bundle
        systemContext.installBundle("reference:file:" + BUILD_TEST_DIR + "/aopalliance.jar");
        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) Test(org.junit.Test)

Example 19 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 20 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, StandardCharsets.UTF_8));
        factoryClass = br.readLine();
        br.close();
    }
    FrameworkFactory factory = (FrameworkFactory) classLoader.loadClass(factoryClass).newInstance();
    return factory;
}
Also used : FrameworkFactory(org.osgi.framework.launch.FrameworkFactory)

Aggregations

FrameworkFactory (org.osgi.framework.launch.FrameworkFactory)21 Framework (org.osgi.framework.launch.Framework)9 File (java.io.File)7 HashMap (java.util.HashMap)6 IOException (java.io.IOException)5 BundleContext (org.osgi.framework.BundleContext)5 BundleException (org.osgi.framework.BundleException)5 Bundle (org.osgi.framework.Bundle)4 FrameworkEvent (org.osgi.framework.FrameworkEvent)4 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 FrameworkListener (org.osgi.framework.FrameworkListener)3 MalformedURLException (java.net.MalformedURLException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 Version (aQute.bnd.version.Version)1 MiniFramework (aQute.launcher.minifw.MiniFramework)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1