Search in sources :

Example 21 with Equinox

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

the class SystemBundleTests method testInitialBundleUpdate.

public void testInitialBundleUpdate() throws IOException {
    // test installing bundle with empty manifest
    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);
    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 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 testBundle = null;
    try {
        String location = "reference:file:///" + bundleFile.getAbsolutePath();
        URL url = new URL(null, location, new Handler(systemContext.getProperty(EquinoxLocations.PROP_INSTALL_AREA)));
        // $NON-NLS-1$
        testBundle = systemContext.installBundle("initial@" + location, url.openStream());
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected install error", e);
    }
    try {
        testBundle.update();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected update error", e);
    }
    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) Bundle(org.osgi.framework.Bundle) Handler(org.eclipse.osgi.storage.url.reference.Handler) IOException(java.io.IOException) 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 22 with Equinox

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

the class SystemBundleTests method testBug413879.

public void testBug413879() {
    // $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);
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected exception in start()", e);
    }
    BundleContext systemContext = equinox.getBundleContext();
    // $NON-NLS-1$
    assertNotNull("System context is null", systemContext);
    Bundle test1 = null;
    try {
        test1 = systemContext.installBundle(installer.getBundleLocation("substitutes.a"));
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected error installing bundle", e);
    }
    final Bundle testFinal1 = test1;
    ServiceRegistration reg = systemContext.registerService(WeavingHook.class, new WeavingHook() {

        public void weave(WovenClass wovenClass) {
            if (!testFinal1.equals(wovenClass.getBundleWiring().getBundle()))
                return;
            if (!"substitutes.x.Ax".equals(wovenClass.getClassName()))
                return;
            List dynamicImports = wovenClass.getDynamicImports();
            dynamicImports.add("*");
        }
    }, null);
    ServiceRegistration<ResolverHookFactory> resolverHookReg = systemContext.registerService(ResolverHookFactory.class, new ResolverHookFactory() {

        @Override
        public ResolverHook begin(Collection<BundleRevision> triggers) {
            // just trying to delay the resolve so that we get two threads trying to apply off the same snapshot
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(e);
            }
            return null;
        }
    }, null);
    final Set<Throwable> errors = Collections.newSetFromMap(new ConcurrentHashMap<Throwable, Boolean>());
    try {
        Runnable dynamicLoadClass = new Runnable() {

            @Override
            public void run() {
                try {
                    testFinal1.loadClass("substitutes.x.Ax");
                    testFinal1.loadClass("org.osgi.framework.hooks.bundle.FindHook");
                } catch (Throwable t) {
                    errors.add(t);
                }
            }
        };
        Thread t1 = new Thread(dynamicLoadClass, getName() + "-1");
        Thread t2 = new Thread(dynamicLoadClass, getName() + "-2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    } catch (Throwable t) {
        fail("Unexpected testing bundle", t);
    } finally {
        reg.unregister();
        resolverHookReg.unregister();
    }
    // put the framework back to the RESOLVED state
    try {
        equinox.stop();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected error stopping framework", e);
    }
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
    if (!errors.isEmpty()) {
        fail("Failed to resolve dynamic", errors.iterator().next());
    }
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) BundleRevision(org.osgi.framework.wiring.BundleRevision) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BundleException(org.osgi.framework.BundleException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServiceRegistration(org.osgi.framework.ServiceRegistration) Bundle(org.osgi.framework.Bundle) Equinox(org.eclipse.osgi.launch.Equinox) File(java.io.File) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) BundleContext(org.osgi.framework.BundleContext)

Example 23 with Equinox

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

the class SystemBundleTests method testDynamicSecurityManager.

public void testDynamicSecurityManager() throws BundleException {
    SecurityManager sm = System.getSecurityManager();
    assertNull("SecurityManager must be null to test.", sm);
    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());
        Equinox equinox = new Equinox(configuration);
        try {
            equinox.start();
        } catch (BundleException e) {
            // $NON-NLS-1$
            fail("Unexpected exception in start()", e);
        }
        Bundle substitutesA = null;
        try {
            // $NON-NLS-1$
            substitutesA = equinox.getBundleContext().installBundle(installer.getBundleLocation("substitutes.a"));
        } catch (BundleException e1) {
            // $NON-NLS-1$
            fail("failed to install a bundle", e1);
        }
        assertTrue("BundleCould not resolve.", equinox.adapt(FrameworkWiring.class).resolveBundles(Collections.singleton(substitutesA)));
        substitutesA.adapt(BundleWiring.class).findEntries("/", null, 0);
        // set security manager after resolving
        System.setSecurityManager(new SecurityManager() {

            @Override
            public void checkPermission(Permission perm, Object context) {
            // do nothing
            }

            @Override
            public void checkPermission(Permission perm) {
            // do nothing
            }
        });
        equinox.stop();
        try {
            FrameworkEvent event = equinox.waitForStop(10000);
            assertEquals("Wrong event.", FrameworkEvent.STOPPED, event.getType());
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            fail("Unexpected interruption.", e);
        }
        // $NON-NLS-1$
        assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox.getState());
    } finally {
        System.setSecurityManager(null);
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FrameworkEvent(org.osgi.framework.FrameworkEvent) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) Equinox(org.eclipse.osgi.launch.Equinox) Permission(java.security.Permission) BundleException(org.osgi.framework.BundleException) File(java.io.File)

Example 24 with Equinox

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

the class SystemBundleTests method testProvideOSGiEEandNative.

public void testProvideOSGiEEandNative() throws BundleException {
    // $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.equinox.allow.restricted.provides", "true");
    Equinox equinox = new Equinox(configuration);
    equinox.start();
    BundleContext systemContext = equinox.getBundleContext();
    Bundle testBundle = systemContext.installBundle(installer.getBundleLocation("test.bug449484"));
    equinox.adapt(FrameworkWiring.class).resolveBundles(Collections.singleton(testBundle));
    assertEquals("Wrong bundle state", Bundle.RESOLVED, testBundle.getState());
    testBundle.uninstall();
    equinox.stop();
    configuration.remove("osgi.equinox.allow.restricted.provides");
    equinox = new Equinox(configuration);
    equinox.start();
    systemContext = equinox.getBundleContext();
    try {
        testBundle = systemContext.installBundle(installer.getBundleLocation("test.bug449484"));
        testBundle.uninstall();
        fail("Expected to fail to install bundle with restricted provide capabilities.");
    } catch (BundleException e) {
    // expected
    }
    equinox.stop();
    configuration.put("osgi.equinox.allow.restricted.provides", "false");
    equinox = new Equinox(configuration);
    equinox.start();
    systemContext = equinox.getBundleContext();
    try {
        testBundle = systemContext.installBundle(installer.getBundleLocation("test.bug449484"));
        testBundle.uninstall();
        fail("Expected to fail to install bundle with restricted provide capabilities.");
    } catch (BundleException e) {
    // expected
    }
    equinox.stop();
}
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) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) BundleException(org.osgi.framework.BundleException) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 25 with Equinox

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

the class SystemBundleTests method testSystemBundle08.

public void testSystemBundle08() {
    // create/start/stop/start/stop test
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile("testSystemBundle08_1");
    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());
    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());
    // $NON-NLS-1$
    config = OSGiTestsActivator.getContext().getDataFile("testSystemBundle08_2");
    configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    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());
    ServiceReference[] refs = null;
    try {
        // $NON-NLS-1$
        refs = equinox.getBundleContext().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) equinox.getBundleContext().getService(refs[0]);
    URL configURL = configLocation.getURL();
    // $NON-NLS-1$ //$NON-NLS-2$
    assertTrue("incorrect configuration location", configURL.toExternalForm().endsWith("testSystemBundle08_2/"));
    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) Location(org.eclipse.osgi.service.datalocation.Location)

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