Search in sources :

Example 71 with Equinox

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

the class SystemBundleTests method testBug351083DevClassPath.

public void testBug351083DevClassPath() throws InvalidSyntaxException {
    // create/start/stop/start/stop test
    BundleInstaller testBundleInstaller = new BundleInstaller("test_files/devCPTests", OSGiTestsActivator.getContext());
    try {
        // $NON-NLS-1$
        File config = OSGiTestsActivator.getContext().getDataFile(getName());
        Map<String, Object> configuration = new HashMap<String, Object>();
        configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
        configuration.put("osgi.dev", "../devCP");
        Equinox equinox = new Equinox(configuration);
        try {
            equinox.start();
        } catch (BundleException e) {
            // $NON-NLS-1$
            fail("Unexpected exception in init()", e);
        }
        BundleContext systemContext = equinox.getBundleContext();
        // $NON-NLS-1$
        assertNotNull("System context is null", systemContext);
        // try installing a bundle before starting
        Bundle tb1 = null;
        try {
            // $NON-NLS-1$
            tb1 = systemContext.installBundle(testBundleInstaller.getBundleLocation("tb1"));
        } catch (BundleException e1) {
            // $NON-NLS-1$
            fail("failed to install a bundle", e1);
        }
        URL resource = tb1.getResource("tb1/resource.txt");
        assertNotNull("Resource is null", resource);
        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());
    } finally {
        testBundleInstaller.shutdown();
    }
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleException(org.osgi.framework.BundleException) File(java.io.File) URL(java.net.URL) BundleContext(org.osgi.framework.BundleContext)

Example 72 with Equinox

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

the class SystemBundleTests method testURLExternalFormat01.

public void testURLExternalFormat01() {
    // create multiple instances test
    // $NON-NLS-1$
    File config1 = OSGiTestsActivator.getContext().getDataFile("testURLExternalFormat01_1");
    Map<String, Object> configuration1 = new HashMap<String, Object>();
    configuration1.put(Constants.FRAMEWORK_STORAGE, config1.getAbsolutePath());
    Equinox equinox1 = new Equinox(configuration1);
    try {
        equinox1.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, equinox1.getState());
    // $NON-NLS-1$
    File config2 = OSGiTestsActivator.getContext().getDataFile("testURLExternalFormat01_2");
    Map<String, Object> configuration2 = new HashMap<String, Object>();
    configuration2.put(Constants.FRAMEWORK_STORAGE, config2.getAbsolutePath());
    Equinox equinox2 = new Equinox(configuration2);
    try {
        equinox2.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, equinox2.getState());
    BundleContext systemContext1 = equinox1.getBundleContext();
    // $NON-NLS-1$
    assertNotNull("System context is null", systemContext1);
    BundleContext systemContext2 = equinox2.getBundleContext();
    // $NON-NLS-1$
    assertNotNull("System context is null", systemContext2);
    assertNotSame(systemContext1, systemContext2);
    Bundle test1 = null;
    Bundle test2 = null;
    try {
        // $NON-NLS-1$
        test1 = systemContext1.installBundle(installer.getBundleLocation("test"));
        // $NON-NLS-1$
        test2 = systemContext2.installBundle(installer.getBundleLocation("test"));
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected error installing bundle", e);
    }
    // $NON-NLS-1$
    URL entry1 = test1.getEntry("data/resource1");
    // $NON-NLS-1$
    assertNotNull("entry1", entry1);
    // $NON-NLS-1$
    URL entry2 = test2.getEntry("data/resource1");
    // $NON-NLS-1$
    assertNotNull("entry2", entry2);
    // $NON-NLS-1$
    assertFalse("External form is equal: " + entry1.toExternalForm(), entry1.toExternalForm().equals(entry2.toExternalForm()));
    // $NON-NLS-1$
    assertFalse("Host is equal: " + entry1.getHost(), entry1.getHost().equals(entry2.getHost()));
    // $NON-NLS-1$
    assertFalse("URL is equal: " + entry1.toExternalForm(), entry1.equals(entry2));
    Bundle substitutes1 = null;
    Bundle substitutes2 = null;
    try {
        // $NON-NLS-1$
        substitutes1 = systemContext1.installBundle(installer.getBundleLocation("substitutes.a"));
        // $NON-NLS-1$
        substitutes2 = systemContext2.installBundle(installer.getBundleLocation("substitutes.a"));
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected error installing bundle", e);
    }
    // $NON-NLS-1$
    entry1 = substitutes1.getResource("data/resource1");
    // $NON-NLS-1$
    assertNotNull("entry1", entry1);
    // $NON-NLS-1$
    entry2 = substitutes2.getResource("data/resource1");
    // $NON-NLS-1$
    assertNotNull("entry2", entry2);
    // $NON-NLS-1$
    assertFalse("External form is equal: " + entry1.toExternalForm(), entry1.toExternalForm().equals(entry2.toExternalForm()));
    // $NON-NLS-1$
    assertFalse("Host is equal: " + entry1.getHost(), entry1.getHost().equals(entry2.getHost()));
    // $NON-NLS-1$
    assertFalse("URL is equal: " + entry1.toExternalForm(), entry1.equals(entry2));
    // put the framework 1 back to the RESOLVED state
    try {
        equinox1.stop();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected error stopping framework", e);
    }
    try {
        equinox1.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox1.getState());
    // put the framework 2 back to the RESOLVED state
    try {
        equinox2.stop();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected erorr stopping framework", e);
    }
    try {
        equinox2.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox2.getState());
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleException(org.osgi.framework.BundleException) File(java.io.File) URL(java.net.URL) BundleContext(org.osgi.framework.BundleContext)

Example 73 with Equinox

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

the class SystemBundleTests method testBug414070.

public void testBug414070() throws BundleException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    Equinox equinox = new Equinox(configuration);
    equinox.init();
    BundleContext systemContext = equinox.getBundleContext();
    Bundle systemBundle = systemContext.getBundle();
    // $NON-NLS-1$
    Bundle chainTest = systemContext.installBundle(installer.getBundleLocation("chain.test"));
    // $NON-NLS-1$
    final Bundle chainTestD = systemContext.installBundle(installer.getBundleLocation("chain.test.d"));
    // $NON-NLS-1$
    Bundle chainTestA = systemContext.installBundle(installer.getBundleLocation("chain.test.a"));
    // $NON-NLS-1$
    Bundle chainTestB = systemContext.installBundle(installer.getBundleLocation("chain.test.b"));
    // $NON-NLS-1$
    Bundle chainTestC = systemContext.installBundle(installer.getBundleLocation("chain.test.c"));
    systemContext.registerService(WeavingHook.class, new WeavingHook() {

        public void weave(WovenClass wovenClass) {
            if (!chainTestD.equals(wovenClass.getBundleWiring().getBundle()))
                return;
            if (!"chain.test.d.DMultipleChain1".equals(wovenClass.getClassName()))
                return;
            List dynamicImports = wovenClass.getDynamicImports();
            dynamicImports.add("*");
        }
    }, null);
    equinox.start();
    // $NON-NLS-1$
    chainTest.loadClass("chain.test.TestMultiChain").newInstance();
    // force a dynamic wire to cause a cycle
    chainTestD.loadClass("chain.test.a.AMultiChain1");
    // make sure all bundles are active now
    assertEquals("A is not active.", Bundle.ACTIVE, chainTestA.getState());
    assertEquals("B is not active.", Bundle.ACTIVE, chainTestB.getState());
    assertEquals("C is not active.", Bundle.ACTIVE, chainTestC.getState());
    assertEquals("D is not active.", Bundle.ACTIVE, chainTestD.getState());
    // record STOPPING order
    final List<Bundle> stoppingOrder = new ArrayList<Bundle>();
    systemContext.addBundleListener(new SynchronousBundleListener() {

        @Override
        public void bundleChanged(BundleEvent event) {
            if (event.getType() == BundleEvent.STOPPING) {
                stoppingOrder.add(event.getBundle());
            }
        }
    });
    equinox.stop();
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        fail("Unexpected interruption.", e);
    }
    List<Bundle> expectedOrder = Arrays.asList(systemBundle, chainTest, chainTestA, chainTestB, chainTestC, chainTestD);
    assertEquals("Wrong stopping order", expectedOrder.toArray(), stoppingOrder.toArray());
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Equinox(org.eclipse.osgi.launch.Equinox) BundleEvent(org.osgi.framework.BundleEvent) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) BundleContext(org.osgi.framework.BundleContext)

Example 74 with Equinox

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

the class SystemBundleTests method testSystemBundle01.

public void testSystemBundle01() {
    // simple test to create an embedded framework
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile("testSystemBundle01");
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    Equinox equinox = new Equinox(configuration);
    try {
        equinox.init();
        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());
    BundleContext systemContext = equinox.getBundleContext();
    // $NON-NLS-1$
    assertNotNull("SystemBundle context is null", systemContext);
    ServiceReference[] refs = null;
    try {
        // $NON-NLS-1$
        refs = systemContext.getServiceReferences(Location.class.getName(), "(type=osgi.configuration.area)");
    } catch (InvalidSyntaxException e) {
        // $NON-NLS-1$
        fail("Unexpected syntax error", e);
    }
    // $NON-NLS-1$
    assertNotNull("Configuration Location refs is null", refs);
    // $NON-NLS-1$
    assertEquals("config refs length is wrong", 1, refs.length);
    Location configLocation = (Location) systemContext.getService(refs[0]);
    URL configURL = configLocation.getURL();
    // $NON-NLS-1$ //$NON-NLS-2$
    assertTrue("incorrect configuration location", configURL.toExternalForm().endsWith("testSystemBundle01/"));
    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) URL(java.net.URL) ServiceReference(org.osgi.framework.ServiceReference) Equinox(org.eclipse.osgi.launch.Equinox) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleException(org.osgi.framework.BundleException) File(java.io.File) BundleContext(org.osgi.framework.BundleContext) Location(org.eclipse.osgi.service.datalocation.Location)

Example 75 with Equinox

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

the class SystemBundleTests method testStartLevelSorting.

public void testStartLevelSorting() throws IOException, InterruptedException {
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    Map configuration = new HashMap();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    Equinox equinox = null;
    final int numBundles = 200;
    final File[] testBundleFiles = createBundles(new File(config, "testBundles"), numBundles);
    try {
        equinox = new Equinox(configuration);
        equinox.start();
        final List<Bundle> testBundles = Collections.synchronizedList(new ArrayList<Bundle>());
        final List<Bundle> startedBundles = Collections.synchronizedList(new ArrayList<Bundle>());
        for (int i = 0; i < numBundles / 2; i++) {
            Bundle b = equinox.getBundleContext().installBundle("reference:file:///" + testBundleFiles[i].getAbsolutePath());
            b.adapt(BundleStartLevel.class).setStartLevel(i + 2);
            testBundles.add(b);
            b.start();
        }
        final Equinox finalEquinox = equinox;
        BundleListener initialListener = new SynchronousBundleListener() {

            AtomicBoolean reverseStartLevel = new AtomicBoolean();

            AtomicBoolean installBundles = new AtomicBoolean();

            @Override
            public void bundleChanged(BundleEvent event) {
                Bundle eBundle = event.getBundle();
                String bsn = eBundle.getSymbolicName();
                if (!bsn.startsWith("bundle-b") || event.getType() != BundleEvent.STARTED) {
                    return;
                }
                startedBundles.add(eBundle);
                if (reverseStartLevel.compareAndSet(false, true)) {
                    for (int i = numBundles / 4, j = numBundles; i < numBundles / 2; i++, j--) {
                        BundleStartLevel tbsl = testBundles.get(i).adapt(BundleStartLevel.class);
                        tbsl.setStartLevel(j + 2 + numBundles);
                    }
                } else if (installBundles.compareAndSet(false, true)) {
                    for (int i = numBundles / 2; i < numBundles; i++) {
                        try {
                            Bundle b = finalEquinox.getBundleContext().installBundle("reference:file:///" + testBundleFiles[i].getAbsolutePath());
                            b.adapt(BundleStartLevel.class).setStartLevel(i + 2);
                            testBundles.add(b);
                            b.start();
                        } catch (BundleException e) {
                        // do nothing
                        }
                    }
                }
            }
        };
        equinox.getBundleContext().addBundleListener(initialListener);
        long startTime = System.currentTimeMillis();
        final CountDownLatch waitForStartLevel = new CountDownLatch(1);
        equinox.adapt(FrameworkStartLevel.class).setStartLevel(numBundles * 3, new FrameworkListener() {

            @Override
            public void frameworkEvent(FrameworkEvent event) {
                waitForStartLevel.countDown();
            }
        });
        waitForStartLevel.await(20, TimeUnit.SECONDS);
        System.out.println("Start time: " + (System.currentTimeMillis() - startTime));
        assertEquals("Did not finish start level setting.", 0, waitForStartLevel.getCount());
        assertEquals("Did not install all the expected bundles.", numBundles, testBundles.size());
        List<Bundle> expectedStartOrder = new ArrayList<Bundle>();
        for (int i = 0; i < numBundles / 4; i++) {
            expectedStartOrder.add(testBundles.get(i));
        }
        for (int i = numBundles / 2; i < numBundles; i++) {
            expectedStartOrder.add(testBundles.get(i));
        }
        for (int i = (numBundles / 2) - 1; i >= numBundles / 4; i--) {
            expectedStartOrder.add(testBundles.get(i));
        }
        assertEquals("Size on expected order is wrong.", numBundles, expectedStartOrder.size());
        for (int i = 0; i < numBundles; i++) {
            assertEquals("Wrong bundle at: " + i, expectedStartOrder.get(i), startedBundles.get(i));
        }
        equinox.getBundleContext().removeBundleListener(initialListener);
        final List<Bundle> stoppedBundles = Collections.synchronizedList(new ArrayList<Bundle>());
        BundleListener shutdownListener = new SynchronousBundleListener() {

            AtomicBoolean reverseStartLevel = new AtomicBoolean();

            AtomicBoolean uninstallBundles = new AtomicBoolean();

            AtomicBoolean inUninstall = new AtomicBoolean();

            @Override
            public void bundleChanged(BundleEvent event) {
                if (inUninstall.get()) {
                    return;
                }
                Bundle eBundle = event.getBundle();
                String bsn = eBundle.getSymbolicName();
                if (!bsn.startsWith("bundle-b") || event.getType() != BundleEvent.STOPPED) {
                    return;
                }
                stoppedBundles.add(eBundle);
                if (reverseStartLevel.compareAndSet(false, true)) {
                    for (int i = numBundles / 2, j = numBundles - 1; i < numBundles; i++, j--) {
                        BundleStartLevel tbsl = testBundles.get(i).adapt(BundleStartLevel.class);
                        tbsl.setStartLevel(j + 2);
                    }
                } else if (uninstallBundles.compareAndSet(false, true)) {
                    for (int i = 0; i < numBundles / 4; i++) {
                        try {
                            inUninstall.set(true);
                            testBundles.get(i).uninstall();
                        } catch (BundleException e) {
                        // do nothing
                        } finally {
                            inUninstall.set(false);
                        }
                    }
                }
            }
        };
        equinox.getBundleContext().addBundleListener(shutdownListener);
        List<Bundle> expectedStopOrder = new ArrayList<Bundle>(expectedStartOrder);
        Collections.reverse(expectedStopOrder);
        Collections.reverse(expectedStopOrder.subList(numBundles / 4, 3 * (numBundles / 4)));
        expectedStopOrder = new ArrayList(expectedStopOrder.subList(0, 3 * (numBundles / 4)));
        long stopTime = System.currentTimeMillis();
        equinox.stop();
        equinox.waitForStop(20000);
        System.out.println("Stop time: " + (System.currentTimeMillis() - stopTime));
        assertEquals("Size on expected order is wrong.", expectedStopOrder.size(), stoppedBundles.size());
        for (int i = 0; i < expectedStopOrder.size(); i++) {
            assertEquals("Wrong bundle at: " + i, expectedStopOrder.get(i), stoppedBundles.get(i));
        }
    } catch (BundleException e) {
        fail("Failed init", e);
    } finally {
        try {
            if (equinox != null) {
                equinox.stop();
                equinox.waitForStop(1000);
            }
        } catch (BundleException e) {
            fail("Failed to stop framework.", e);
        } catch (InterruptedException e) {
            fail("Failed to stop framework.", e);
        }
    }
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FrameworkEvent(org.osgi.framework.FrameworkEvent) Bundle(org.osgi.framework.Bundle) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Equinox(org.eclipse.osgi.launch.Equinox) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) BundleException(org.osgi.framework.BundleException) FrameworkListener(org.osgi.framework.FrameworkListener) File(java.io.File) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener)

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