Search in sources :

Example 31 with WeavingHook

use of org.osgi.framework.hooks.weaving.WeavingHook in project aries by apache.

the class DynamicWeavingActivator method start.

@Override
public synchronized void start(BundleContext context) throws Exception {
    WeavingHook wh = new ClientWeavingHook(context, this);
    weavingHookService = context.registerService(WeavingHook.class.getName(), wh, null);
    super.start(context, SpiFlyConstants.SPI_CONSUMER_HEADER);
}
Also used : WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook)

Example 32 with WeavingHook

use of org.osgi.framework.hooks.weaving.WeavingHook in project aries by apache.

the class DynamicImportTest method testDynamicPackageImportsAddedToSharingPolicyWhenNoImportPackageHeader.

/*
	 * Dynamic package imports added by a weaver to a woven class should be
	 * added to the region's sharing policy even if the subsystem has no
	 * Import-Package header.
	 */
@SuppressWarnings("rawtypes")
@Test
public void testDynamicPackageImportsAddedToSharingPolicyWhenNoImportPackageHeader() throws Exception {
    final AtomicBoolean weavingHookCalled = new AtomicBoolean(false);
    ServiceRegistration reg = bundleContext.registerService(WeavingHook.class, new WeavingHook() {

        @Override
        public void weave(WovenClass wovenClass) {
            if (BUNDLE_A.equals(wovenClass.getBundleWiring().getBundle().getSymbolicName())) {
                wovenClass.getDynamicImports().add("org.osgi.framework");
                weavingHookCalled.set(true);
            }
        }
    }, null);
    try {
        Subsystem s = installSubsystemFromFile(APPLICATION_A);
        try {
            assertNull("Import-Package header should not exist", s.getSubsystemHeaders(null).get(Constants.IMPORT_PACKAGE));
            Bundle a = getConstituentAsBundle(s, BUNDLE_A, null, null);
            // Force the class load so the weaving hook gets called.
            a.loadClass("Empty");
            assertTrue("Weaving hook not called", weavingHookCalled.get());
            try {
                // Try to load a class from the dynamically imported package.
                a.loadClass("org.osgi.framework.Bundle");
            } catch (Exception e) {
                fail("Woven dynamic package import not added to the region's sharing policy");
            }
        } finally {
            try {
                s.uninstall();
            } catch (Exception e) {
            }
        }
    } finally {
        try {
            reg.unregister();
        } catch (Exception e) {
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Bundle(org.osgi.framework.Bundle) Subsystem(org.osgi.service.subsystem.Subsystem) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) BundleException(org.osgi.framework.BundleException) SubsystemException(org.osgi.service.subsystem.SubsystemException) IOException(java.io.IOException) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 33 with WeavingHook

use of org.osgi.framework.hooks.weaving.WeavingHook in project aries by apache.

the class Aries1435Test method registerWeavingHook.

private void registerWeavingHook(final String... dynamicImport) {
    serviceRegistrations.add(bundleContext.registerService(WeavingHook.class, new WeavingHook() {

        @Override
        public void weave(WovenClass wovenClass) {
            Bundle bundle = wovenClass.getBundleWiring().getBundle();
            String symbolicName = bundle.getSymbolicName();
            if (BUNDLE_A.equals(symbolicName)) {
                weavingHookCalled.set(true);
                List<String> dynamicImports = wovenClass.getDynamicImports();
                dynamicImports.addAll(Arrays.asList(dynamicImport));
            }
        }
    }, null));
}
Also used : Bundle(org.osgi.framework.Bundle) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook)

Example 34 with WeavingHook

use of org.osgi.framework.hooks.weaving.WeavingHook 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 35 with WeavingHook

use of org.osgi.framework.hooks.weaving.WeavingHook 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)

Aggregations

WeavingHook (org.osgi.framework.hooks.weaving.WeavingHook)35 Bundle (org.osgi.framework.Bundle)32 WovenClass (org.osgi.framework.hooks.weaving.WovenClass)32 Test (org.junit.Test)27 Hashtable (java.util.Hashtable)26 Method (java.lang.reflect.Method)24 URL (java.net.URL)24 HashSet (java.util.HashSet)9 HashMap (java.util.HashMap)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 ArrayList (java.util.ArrayList)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 BundleContext (org.osgi.framework.BundleContext)4 ServiceRegistration (org.osgi.framework.ServiceRegistration)4 File (java.io.File)3 URLClassLoader (java.net.URLClassLoader)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 Equinox (org.eclipse.osgi.launch.Equinox)3