Search in sources :

Example 36 with Equinox

use of org.eclipse.osgi.launch.Equinox in project rt.equinox.framework by eclipse.

the class SystemBundleTests method doMRUBundleFileList.

// public void testMRUBundleFileListExpectedToFail() {
// doMRUBundleFileList(0);
// }
private void doMRUBundleFileList(int limit) {
    // create/start/stop/start/stop test
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    // $NON-NLS-1$//$NON-NLS-2$
    configuration.put("osgi.bundlefile.limit", Integer.toString(limit));
    final Equinox equinox = new Equinox(configuration);
    try {
        equinox.init();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected exception in init()", e);
    }
    // should be in the STARTING state
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.STARTING, equinox.getState());
    BundleContext systemContext = equinox.getBundleContext();
    // $NON-NLS-1$
    assertNotNull("System context is null", systemContext);
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Failed to start the framework", e);
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.ACTIVE, equinox.getState());
    File[] testBundles = null;
    try {
        // $NON-NLS-1$
        testBundles = createBundles(new File(config, "bundles"), 3000);
    } catch (IOException e) {
        // $NON-NLS-1$
        fail("Unexpected error creating budnles", e);
    }
    for (int i = 0; i < testBundles.length; i++) {
        try {
            // $NON-NLS-1$
            systemContext.installBundle("reference:file:///" + testBundles[i].getAbsolutePath());
        } catch (BundleException e) {
            // $NON-NLS-1$
            fail("Unexpected install error", e);
        }
    }
    // put the framework back to the RESOLVED state
    try {
        equinox.stop();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected erorr stopping framework", e);
    }
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Failed to start the framework", e);
    }
    openAllBundleFiles(equinox.getBundleContext());
    final Exception[] failureException = new BundleException[1];
    final FrameworkEvent[] success = new FrameworkEvent[] { null };
    Thread waitForUpdate = new Thread(new Runnable() {

        public void run() {
            try {
                success[0] = equinox.waitForStop(10000);
            } catch (InterruptedException e) {
                failureException[0] = e;
            }
        }
    }, // $NON-NLS-1$
    "test waitForStop thread");
    waitForUpdate.start();
    try {
        // delay hack to allow waitForUpdate thread to block on waitForStop before we update.
        Thread.sleep(100);
    } catch (InterruptedException e) {
        fail("unexpected interuption", e);
    }
    try {
        equinox.update();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Failed to update the framework", e);
    }
    try {
        waitForUpdate.join();
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("unexpected interuption", e);
    }
    if (failureException[0] != null)
        // $NON-NLS-1$
        fail("Error occurred while waiting", failureException[0]);
    // or we can just try to start it and race with the update() call above
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Failed to start the framework", e);
    }
    openAllBundleFiles(equinox.getBundleContext());
    try {
        equinox.stop();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected erorr stopping framework", e);
    }
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox.getState());
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FrameworkEvent(org.osgi.framework.FrameworkEvent) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) FileNotFoundException(java.io.FileNotFoundException) NoSuchElementException(java.util.NoSuchElementException) MalformedURLException(java.net.MalformedURLException) Equinox(org.eclipse.osgi.launch.Equinox) BundleException(org.osgi.framework.BundleException) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 37 with Equinox

use of org.eclipse.osgi.launch.Equinox in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testBootDelegationConfigIni.

public void testBootDelegationConfigIni() throws BundleException, IOException, InterruptedException {
    String compatBootDelegate = "osgi.compatibility.bootdelegation";
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    config.mkdirs();
    Properties configIni = new Properties();
    // use config.ini to override the default for the embedded case.
    // note that the embedded case the default for this setting is false
    configIni.setProperty(compatBootDelegate, "true");
    configIni.store(new FileWriter(new File(config, "config.ini")), null);
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    Equinox equinox = new Equinox(configuration);
    equinox.start();
    BundleContext systemContext = equinox.getBundleContext();
    assertEquals("Wrong value for: " + compatBootDelegate, "true", systemContext.getProperty(compatBootDelegate));
    File bundleFile = null;
    try {
        File baseDir = new File(config, "bundles");
        baseDir.mkdirs();
        bundleFile = createBundle(baseDir, getName(), true, true);
    } catch (IOException e) {
        fail("Unexpected error creating bundles.", e);
    }
    Bundle b = null;
    try {
        // $NON-NLS-1$
        b = systemContext.installBundle("reference:file:///" + bundleFile.getAbsolutePath());
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected install error", e);
    }
    try {
        b.loadClass(SocketFactory.class.getName());
    } catch (ClassNotFoundException e) {
        fail("Expected to be able to load the class from boot.", e);
    }
    long bId = b.getBundleId();
    equinox.stop();
    equinox.waitForStop(5000);
    // remove the setting to ensure false is used for the embedded case
    configIni.remove(compatBootDelegate);
    configIni.store(new FileWriter(new File(config, "config.ini")), null);
    equinox = new Equinox(configuration);
    equinox.start();
    systemContext = equinox.getBundleContext();
    b = systemContext.getBundle(bId);
    try {
        b.loadClass(SocketFactory.class.getName());
        fail("Expected to fail to load the class from boot.");
    } catch (ClassNotFoundException e) {
    // expected
    }
    equinox.stop();
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) SocketFactory(javax.net.SocketFactory) FileWriter(java.io.FileWriter) IOException(java.io.IOException) Properties(java.util.Properties) Equinox(org.eclipse.osgi.launch.Equinox) BundleException(org.osgi.framework.BundleException) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 38 with Equinox

use of org.eclipse.osgi.launch.Equinox in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testSystemBundle10.

public void testSystemBundle10() {
    // create/start/update/stop test
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile("testSystemBundle10");
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    final Equinox equinox = new Equinox(configuration);
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Failed to start the framework", e);
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.ACTIVE, equinox.getState());
    final Exception[] failureException = new BundleException[1];
    final FrameworkEvent[] success = new FrameworkEvent[] { null };
    Thread t = new Thread(new Runnable() {

        public void run() {
            try {
                success[0] = equinox.waitForStop(10000);
            } catch (InterruptedException e) {
                failureException[0] = e;
            }
        }
    }, // $NON-NLS-1$
    "test waitForStop thread");
    t.start();
    try {
        // delay hack to allow t thread to block on waitForStop before we update.
        Thread.sleep(500);
    } catch (InterruptedException e) {
        fail("unexpected interuption", e);
    }
    try {
        equinox.update();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Failed to update the framework", e);
    }
    try {
        t.join();
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("unexpected interuption", e);
    }
    if (failureException[0] != null)
        // $NON-NLS-1$
        fail("Error occurred while waiting", failureException[0]);
    // $NON-NLS-1$
    assertNotNull("Wait for stop event is null", success[0]);
    // $NON-NLS-1$
    assertEquals("Wait for stop event type is wrong", FrameworkEvent.STOPPED_UPDATE, success[0].getType());
    // TODO delay hack to allow the framework to get started again
    for (int i = 0; i < 5 && Bundle.ACTIVE != equinox.getState(); i++) try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // nothing
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.ACTIVE, equinox.getState());
    try {
        equinox.stop();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected erorr stopping framework", e);
    }
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox.getState());
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FrameworkEvent(org.osgi.framework.FrameworkEvent) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) FileNotFoundException(java.io.FileNotFoundException) NoSuchElementException(java.util.NoSuchElementException) MalformedURLException(java.net.MalformedURLException) Equinox(org.eclipse.osgi.launch.Equinox) BundleException(org.osgi.framework.BundleException) File(java.io.File)

Example 39 with Equinox

use of org.eclipse.osgi.launch.Equinox in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testChangeEE.

public void testChangeEE() throws IOException, BundleException {
    URL javaSE7Profile = OSGiTestsActivator.getContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getEntry("JavaSE-1.7.profile");
    URL javaSE8Profile = OSGiTestsActivator.getContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getEntry("JavaSE-1.8.profile");
    // configure equinox for javaSE 8
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    // $NON-NLS-1$
    configuration.put("osgi.java.profile", javaSE8Profile.toExternalForm());
    Equinox equinox = new Equinox(configuration);
    equinox.start();
    // install a bundle that requires java 8
    BundleContext systemContext = equinox.getBundleContext();
    // $NON-NLS-1$
    assertNotNull("System context is null", systemContext);
    Map<String, String> testHeaders = new HashMap<String, String>();
    testHeaders.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    testHeaders.put(Constants.BUNDLE_SYMBOLICNAME, getName());
    testHeaders.put(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, "JavaSE-1.8");
    File testBundle = createBundle(config, getName(), testHeaders);
    // $NON-NLS-1$
    Bundle b = systemContext.installBundle("reference:file:///" + testBundle.getAbsolutePath());
    long bid = b.getBundleId();
    // should resolve fine
    Assert.assertTrue("Could not resolve bundle.", equinox.adapt(FrameworkWiring.class).resolveBundles(Collections.singleton(b)));
    // put the framework back to the RESOLVED state
    equinox.stop();
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
    // configure equinox for java 7
    configuration.put("osgi.java.profile", javaSE7Profile.toExternalForm());
    equinox = new Equinox(configuration);
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Failed to start the framework", e);
    }
    // bundle should fail to resolve
    b = equinox.getBundleContext().getBundle(bid);
    Assert.assertFalse("Could resolve bundle.", equinox.adapt(FrameworkWiring.class).resolveBundles(Collections.singleton(b)));
    // put the framework back to the RESOLVED state
    equinox.stop();
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
    // move back to java 8
    configuration.put("osgi.java.profile", javaSE8Profile.toExternalForm());
    equinox = new Equinox(configuration);
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Failed to start the framework", e);
    }
    // bundle should succeed to resolve again
    b = equinox.getBundleContext().getBundle(bid);
    Assert.assertTrue("Could not resolve bundle.", equinox.adapt(FrameworkWiring.class).resolveBundles(Collections.singleton(b)));
    // put the framework back to the RESOLVED state
    equinox.stop();
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) URL(java.net.URL) Equinox(org.eclipse.osgi.launch.Equinox) BundleException(org.osgi.framework.BundleException) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 40 with Equinox

use of org.eclipse.osgi.launch.Equinox in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testNullStorageArea.

public void testNullStorageArea() throws BundleException {
    File install = getContext().getDataFile(getName());
    install.mkdirs();
    Equinox equinox = new Equinox(Collections.singletonMap("osgi.install.area", install.getAbsolutePath()));
    try {
        equinox.init();
        String storageArea = equinox.getBundleContext().getProperty(Constants.FRAMEWORK_STORAGE);
        assertNotNull("No storage area set.", storageArea);
    } finally {
        equinox.stop();
    }
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) File(java.io.File)

Aggregations

Equinox (org.eclipse.osgi.launch.Equinox)105 File (java.io.File)88 HashMap (java.util.HashMap)61 LinkedHashMap (java.util.LinkedHashMap)58 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)58 BundleException (org.osgi.framework.BundleException)52 BundleContext (org.osgi.framework.BundleContext)51 Bundle (org.osgi.framework.Bundle)43 IOException (java.io.IOException)18 FileNotFoundException (java.io.FileNotFoundException)11 URL (java.net.URL)10 Location (org.eclipse.osgi.service.datalocation.Location)10 Properties (java.util.Properties)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 FrameworkEvent (org.osgi.framework.FrameworkEvent)8 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)8 BundleWiring (org.osgi.framework.wiring.BundleWiring)8 ArrayList (java.util.ArrayList)7 ExecutionException (java.util.concurrent.ExecutionException)6 FileOutputStream (java.io.FileOutputStream)5