Search in sources :

Example 81 with Equinox

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

the class SystemBundleTests method testWeavingPersistence.

public void testWeavingPersistence() {
    // $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);
    }
    long testID1 = test1.getBundleId();
    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);
    try {
        testFinal1.loadClass("substitutes.x.Ax");
        testFinal1.loadClass("org.osgi.framework.hooks.bundle.FindHook");
    } catch (Throwable t) {
        fail("Unexpected testing bundle", t);
    } finally {
        reg.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);
    }
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected exception in start()", e);
    }
    systemContext = equinox.getBundleContext();
    test1 = systemContext.getBundle(testID1);
    Bundle test2 = null;
    try {
        test2 = systemContext.installBundle(installer.getBundleLocation("exporter.importer1"));
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected error installing bundle", e);
    }
    long testID2 = test2.getBundleId();
    final Bundle testFinal2 = test2;
    reg = systemContext.registerService(WeavingHook.class, new WeavingHook() {

        public void weave(WovenClass wovenClass) {
            if (!testFinal2.equals(wovenClass.getBundleWiring().getBundle()))
                return;
            if (!"exporter.importer.test.Test1".equals(wovenClass.getClassName()))
                return;
            List dynamicImports = wovenClass.getDynamicImports();
            dynamicImports.add("*");
        }
    }, null);
    try {
        testFinal2.loadClass("exporter.importer.test.Test1");
        testFinal2.loadClass("org.osgi.framework.hooks.service.FindHook");
    } catch (Throwable t) {
        fail("Unexpected testing bundle", t);
    } finally {
        reg.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);
    }
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected exception in start()", e);
    }
    systemContext = equinox.getBundleContext();
    test1 = systemContext.getBundle(testID1);
    test2 = systemContext.getBundle(testID2);
    BundleRevision rev1 = test1.adapt(BundleRevision.class);
    BundleRevision rev2 = test2.adapt(BundleRevision.class);
    BundleWiring wiring1 = rev1.getWiring();
    BundleWiring wiring2 = rev2.getWiring();
    assertNotNull("wiring1 is null", wiring1);
    assertNotNull("wiring2 is null", wiring2);
    List packages1 = wiring1.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
    List packages2 = wiring2.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
    // could make this a more complete check, but with the bug the dynamic wires
    // are missing altogether because we fail to save the resolver state cache.
    assertEquals("Wrong number of wires for wiring1", 1, packages1.size());
    assertEquals("Wrong number of wires for wiring2", 1, packages2.size());
    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);
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) Equinox(org.eclipse.osgi.launch.Equinox) BundleRevision(org.osgi.framework.wiring.BundleRevision) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BundleException(org.osgi.framework.BundleException) File(java.io.File) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 82 with Equinox

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

the class SystemBundleTests method testAllNullConfigurationValues.

public void testAllNullConfigurationValues() throws BundleException {
    Collection<String> requiredProperties = // prevent bad formatting...
    Arrays.asList(// 
    Constants.FRAMEWORK_EXECUTIONENVIRONMENT, // 
    Constants.FRAMEWORK_LANGUAGE, // 
    Constants.FRAMEWORK_OS_NAME, // 
    Constants.FRAMEWORK_OS_VERSION, // 
    Constants.FRAMEWORK_PROCESSOR, // 
    Constants.FRAMEWORK_STORAGE, // 
    Constants.FRAMEWORK_SYSTEMCAPABILITIES, // 
    Constants.FRAMEWORK_SYSTEMPACKAGES, // 
    Constants.FRAMEWORK_UUID, // 
    Constants.FRAMEWORK_VENDOR, // 
    Constants.FRAMEWORK_VERSION, // 
    Constants.SUPPORTS_FRAMEWORK_EXTENSION, // 
    Constants.SUPPORTS_FRAMEWORK_FRAGMENT, // 
    Constants.SUPPORTS_FRAMEWORK_REQUIREBUNDLE, // 
    EquinoxConfiguration.PROP_FRAMEWORK, // 
    EquinoxConfiguration.PROP_OSGI_ARCH, // 
    EquinoxConfiguration.PROP_OSGI_OS, // 
    EquinoxConfiguration.PROP_OSGI_WS, // 
    EquinoxConfiguration.PROP_OSGI_WS, // 
    EquinoxConfiguration.PROP_OSGI_NL, // 
    EquinoxConfiguration.PROP_STATE_SAVE_DELAY_INTERVAL, // 
    EquinoxConfiguration.PROP_INIT_UUID, // 
    "gosh.args", // 
    EquinoxLocations.PROP_HOME_LOCATION_AREA, // 
    EquinoxLocations.PROP_CONFIG_AREA, // 
    EquinoxLocations.PROP_INSTALL_AREA, // 
    EclipseStarter.PROP_LOGFILE);
    Properties systemProperties = (Properties) System.getProperties().clone();
    Map<String, Object> configuration = new HashMap<String, Object>();
    for (Object key : systemProperties.keySet()) {
        configuration.put((String) key, null);
    }
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    Equinox equinox = new Equinox(configuration);
    equinox.start();
    for (Object key : systemProperties.keySet()) {
        String property = (String) key;
        String value = equinox.getBundleContext().getProperty(property);
        if (requiredProperties.contains(property)) {
            assertNotNull(property + " is null", value);
        } else {
            assertNull(property + " is not null", value);
        }
        String systemValue = System.getProperty(property);
        assertEquals("Wrong system value for " + property, systemProperties.getProperty(property), systemValue);
    }
    assertEquals(systemProperties, System.getProperties());
    equinox.stop();
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Properties(java.util.Properties) File(java.io.File)

Example 83 with Equinox

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

the class SecurityAdminUnitTests method setUp.

protected void setUp() throws Exception {
    previousPolicy = Policy.getPolicy();
    final Permission allPermission = new AllPermission();
    final PermissionCollection allPermissions = new PermissionCollection() {

        private static final long serialVersionUID = 3258131349494708277L;

        // A simple PermissionCollection that only has AllPermission
        public void add(Permission permission) {
        // no adding to this policy
        }

        public boolean implies(Permission permission) {
            return true;
        }

        public Enumeration elements() {
            return new Enumeration() {

                int cur = 0;

                public boolean hasMoreElements() {
                    return cur < 1;
                }

                public Object nextElement() {
                    if (cur == 0) {
                        cur = 1;
                        return allPermission;
                    }
                    throw new NoSuchElementException();
                }
            };
        }
    };
    Policy.setPolicy(new Policy() {

        public PermissionCollection getPermissions(CodeSource codesource) {
            return allPermissions;
        }

        public void refresh() {
        // nothing
        }
    });
    // $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(Constants.FRAMEWORK_SECURITY, Constants.FRAMEWORK_SECURITY_OSGI);
    equinox = new Equinox(configuration);
    try {
        equinox.init();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected exception on init()", e);
    }
    cpa = equinox.getBundleContext().getService(equinox.getBundleContext().getServiceReference(ConditionalPermissionAdmin.class));
    pa = equinox.getBundleContext().getService(equinox.getBundleContext().getServiceReference(PermissionAdmin.class));
    super.setUp();
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) FilePermission(java.io.FilePermission) SocketPermission(java.net.SocketPermission) File(java.io.File)

Example 84 with Equinox

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

the class SecurityManagerTests method testEnableSecurityManager05.

public void testEnableSecurityManager05() 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(Constants.FRAMEWORK_SECURITY, Constants.FRAMEWORK_SECURITY_OSGI);
    Equinox equinox = new Equinox(configuration);
    try {
        equinox.init();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected exception on init()", e);
    }
    // $NON-NLS-1$
    assertNotNull("SecurityManager is null", System.getSecurityManager());
    // 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 installing a bundle to tests bug
    // $NON-NLS-1$
    String locationLinkA = installer.getBundleLocation("test.link.a");
    // $NON-NLS-1$
    String locationLinkAClient = installer.getBundleLocation("test.link.a.client");
    Bundle linkA = systemContext.installBundle(locationLinkA);
    Bundle linkAClient = systemContext.installBundle(locationLinkAClient);
    equinox.start();
    try {
        PackageAdmin pa = (PackageAdmin) systemContext.getService(systemContext.getServiceReference(PackageAdmin.class.getName()));
        assertTrue(pa.resolveBundles(new Bundle[] { linkA, linkAClient }));
        linkA.uninstall();
        linkAClient.uninstall();
        linkA = systemContext.installBundle(locationLinkA);
        linkAClient = systemContext.installBundle(locationLinkAClient);
        assertTrue(pa.resolveBundles(new Bundle[] { linkA, linkAClient }));
    } finally {
        // 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);
        }
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox.getState());
    // $NON-NLS-1$
    assertNull("SecurityManager is not null", System.getSecurityManager());
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) File(java.io.File)

Example 85 with Equinox

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

the class SecurityManagerTests method testLocalization01.

public void testLocalization01() throws BundleException {
    // create/start/stop/start/stop test
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile("testLocalization01");
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    configuration.put(Constants.FRAMEWORK_SECURITY, Constants.FRAMEWORK_SECURITY_OSGI);
    Equinox equinox = new Equinox(configuration);
    try {
        equinox.init();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected exception on init()", e);
    }
    // $NON-NLS-1$
    assertNotNull("SecurityManager is null", System.getSecurityManager());
    // 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 installing host and fragment to test bug 245678
    // $NON-NLS-1$
    String locationSecurityA = installer.getBundleLocation("security.a");
    // $NON-NLS-1$
    String locationSecurityAFragA = installer.getBundleLocation("security.a.frag.a");
    // set the security for the host and fragment
    ConditionalPermissionAdmin ca = (ConditionalPermissionAdmin) systemContext.getService(systemContext.getServiceReference(ConditionalPermissionAdmin.class.getName()));
    ConditionalPermissionUpdate update = ca.newConditionalPermissionUpdate();
    List rows = update.getConditionalPermissionInfos();
    rows.add(ca.newConditionalPermissionInfo(null, null, new PermissionInfo[] { hostFragmentPermission, allPackagePermission }, ConditionalPermissionInfo.ALLOW));
    // $NON-NLS-1$
    assertTrue("Cannot commit rows", update.commit());
    Bundle securityA = systemContext.installBundle(locationSecurityA);
    systemContext.installBundle(locationSecurityAFragA);
    equinox.start();
    try {
        securityA.start();
        // $NON-NLS-1$
        Dictionary headers = securityA.getHeaders("en_US");
        String name = (String) headers.get(Constants.BUNDLE_NAME);
        // $NON-NLS-1$ //$NON-NLS-2$
        assertEquals("Wrong Bundle-Name", "en_US", name);
        // $NON-NLS-1$
        headers = securityA.getHeaders("en");
        name = (String) headers.get(Constants.BUNDLE_NAME);
        // $NON-NLS-1$ //$NON-NLS-2$
        assertEquals("Wrong Bundle-Name", "default", name);
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Failed to start securityA", e);
    } finally {
        // 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);
        }
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox.getState());
    // $NON-NLS-1$
    assertNull("SecurityManager is not null", System.getSecurityManager());
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) PermissionInfo(org.osgi.service.permissionadmin.PermissionInfo) 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