Search in sources :

Example 41 with WeavingHook

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

the class ClientWeavingHookTest method testClientSpecifyingProviderVersion.

@Test
public void testClientSpecifyingProviderVersion() throws Exception {
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "java.util.ServiceLoader#load(java.lang.Class);bundle=impl2:version=1.2.3");
    Bundle providerBundle1 = mockProviderBundle("impl1", 1);
    Bundle providerBundle2 = mockProviderBundle("impl2", 2);
    Bundle providerBundle3 = mockProviderBundle("impl2_123", 3, new Version(1, 2, 3));
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle1, new HashMap<String, Object>());
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle2, new HashMap<String, Object>());
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle3, new HashMap<String, Object>());
    Bundle consumerBundle = mockConsumerBundle(headers, providerBundle1, providerBundle2, providerBundle3);
    activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER);
    Bundle spiFlyBundle = mockSpiFlyBundle(consumerBundle, providerBundle1, providerBundle2, providerBundle3);
    WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator);
    // Weave the TestClient class.
    URL clsUrl = getClass().getResource("TestClient.class");
    WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.TestClient", consumerBundle);
    wh.weave(wc);
    // Invoke the woven class and check that it propertly sets the TCCL so that the
    // META-INF/services/org.apache.aries.mytest.MySPI file from impl2 is visible.
    Class<?> cls = wc.getDefinedClass();
    Method method = cls.getMethod("test", new Class[] { String.class });
    Object result = method.invoke(cls.getDeclaredConstructor().newInstance(), "hello");
    Assert.assertEquals("Only the services from bundle impl2 should be selected", Collections.singleton("Updated!hello!Updated"), result);
}
Also used : Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) Method(java.lang.reflect.Method) URL(java.net.URL) Version(org.osgi.framework.Version) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) Test(org.junit.Test)

Example 42 with WeavingHook

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

the class ClientWeavingHookOSGi43Test method testBasicServiceLoaderUsageWithClassLoaderFromBundleRevision.

@Test
public void testBasicServiceLoaderUsageWithClassLoaderFromBundleRevision() throws Exception {
    Dictionary<String, String> consumerHeaders = new Hashtable<String, String>();
    consumerHeaders.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "*");
    // Register the bundle that provides the SPI implementation.
    Bundle providerBundle = mockProviderBundle("impl1", 1);
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle, new HashMap<String, Object>());
    Bundle consumerBundle = mockConsumerBundle(consumerHeaders, providerBundle);
    activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER);
    Bundle spiFlyBundle = mockSpiFlyBundle("spifly", Version.parseVersion("1.9.4"), consumerBundle, providerBundle);
    WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator);
    // Weave the TestClient class.
    URL clsUrl = getClass().getResource("TestClient.class");
    Assert.assertNotNull("Precondition", clsUrl);
    String clientClassName = "org.apache.aries.spifly.dynamic.TestClient";
    WovenClass wc = new MyWovenClass(clsUrl, clientClassName, consumerBundle);
    Assert.assertEquals("Precondition", 0, wc.getDynamicImports().size());
    wh.weave(wc);
    Assert.assertEquals(1, wc.getDynamicImports().size());
    String di1 = "org.apache.aries.spifly";
    String di = wc.getDynamicImports().get(0);
    Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di));
    // Invoke the woven class and check that it properly sets the TCCL so that the
    // META-INF/services/org.apache.aries.mytest.MySPI file from impl1 is visible.
    Class<?> cls = wc.getDefinedClass();
    Method method = cls.getMethod("test", new Class[] { String.class });
    Object result = method.invoke(cls.getDeclaredConstructor().newInstance(), "hello");
    Assert.assertEquals(Collections.singleton("olleh"), result);
}
Also used : Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) Method(java.lang.reflect.Method) URL(java.net.URL) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) Test(org.junit.Test)

Example 43 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 44 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)

Aggregations

WeavingHook (org.osgi.framework.hooks.weaving.WeavingHook)44 WovenClass (org.osgi.framework.hooks.weaving.WovenClass)41 Bundle (org.osgi.framework.Bundle)38 Test (org.junit.Test)36 Hashtable (java.util.Hashtable)32 Method (java.lang.reflect.Method)30 URL (java.net.URL)30 HashSet (java.util.HashSet)13 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 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 BundleClassLoader (org.apache.felix.framework.BundleWiringImpl.BundleClassLoader)3