Search in sources :

Example 6 with WovenClass

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

the class ClientWeavingHookGenericCapabilityTest method testTCCLResettingOnException.

@Test
public void testTCCLResettingOnException() throws Exception {
    ClassLoader cl = new URLClassLoader(new URL[] {});
    Thread.currentThread().setContextClassLoader(cl);
    Assert.assertSame("Precondition", cl, Thread.currentThread().getContextClassLoader());
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.CLIENT_REQUIREMENT);
    Bundle providerBundle5 = mockProviderBundle("impl5", 1);
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle5, new HashMap<String, Object>());
    Bundle consumerBundle = mockConsumerBundle(headers, providerBundle5);
    activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.REQUIRE_CAPABILITY);
    Bundle spiFlyBundle = mockSpiFlyBundle(consumerBundle, providerBundle5);
    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);
    Class<?> cls = wc.getDefinedClass();
    Method method = cls.getMethod("test", new Class[] { String.class });
    // That implementation throws an exception, after which we are making sure that the TCCL is set back appropriately.
    try {
        method.invoke(cls.newInstance(), "hello");
        Assert.fail("Invocation should have thrown an exception");
    } catch (InvocationTargetException ite) {
        RuntimeException re = (RuntimeException) ite.getCause();
        String msg = re.getMessage();
        Assert.assertEquals("Uh-oh: hello", msg);
        // The TCCL should have been reset correctly
        Assert.assertSame(cl, Thread.currentThread().getContextClassLoader());
    }
}
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) InvocationTargetException(java.lang.reflect.InvocationTargetException) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) Test(org.junit.Test)

Example 7 with WovenClass

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

the class ClientWeavingHookGenericCapabilityTest method testAltServiceLoaderLoadUnprocessed.

@Test
public void testAltServiceLoaderLoadUnprocessed() throws Exception {
    Bundle spiFlyBundle = mockSpiFlyBundle();
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.CLIENT_REQUIREMENT);
    Bundle consumerBundle = mockConsumerBundle(headers, spiFlyBundle);
    WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator);
    // Weave the TestClient class.
    URL clsUrl = getClass().getResource("UnaffectedTestClient.class");
    Assert.assertNotNull("Precondition", clsUrl);
    WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.UnaffectedTestClient", consumerBundle);
    Assert.assertEquals("Precondition", 0, wc.getDynamicImports().size());
    wh.weave(wc);
    Assert.assertEquals("The client is not affected so no additional imports should have been added", 0, wc.getDynamicImports().size());
    // ok the weaving is done, now prepare the registry for the call
    Bundle providerBundle = mockProviderBundle("impl1", 1);
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle, new HashMap<String, Object>());
    // 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 impl1 is visible.
    Class<?> cls = wc.getDefinedClass();
    Method method = cls.getMethod("test", new Class[] { String.class });
    Object result = method.invoke(cls.newInstance(), "hello");
    Assert.assertEquals("impl4", result);
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) 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 8 with WovenClass

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

the class ClientWeavingHookTest method testJAXPClientWantsAltImplementation2.

@Test
public void testJAXPClientWantsAltImplementation2() throws Exception {
    Bundle systembundle = mockSystemBundle();
    Bundle providerBundle = mockProviderBundle("impl3", 1);
    activator.registerProviderBundle("javax.xml.parsers.DocumentBuilderFactory", providerBundle, new HashMap<String, Object>());
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "javax.xml.parsers.DocumentBuilderFactory#newInstance();bundle=impl3");
    Bundle consumerBundle = mockConsumerBundle(headers, providerBundle, systembundle);
    activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER);
    WeavingHook wh = new ClientWeavingHook(mockSpiFlyBundle(consumerBundle, providerBundle, systembundle).getBundleContext(), activator);
    URL clsUrl = getClass().getResource("JaxpClient.class");
    WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.JaxpClient", consumerBundle);
    wh.weave(wc);
    Class<?> cls = wc.getDefinedClass();
    Method method = cls.getMethod("test", new Class[] {});
    Class<?> result = (Class<?>) method.invoke(cls.newInstance());
    Assert.assertEquals("JAXP implementation from alternative bundle", "org.apache.aries.spifly.dynamic.impl3.MyAltDocumentBuilderFactory", result.getName());
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) Method(java.lang.reflect.Method) URL(java.net.URL) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) Test(org.junit.Test)

Example 9 with WovenClass

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

the class ClientWeavingHookTest method testConsumerBundleWeaving.

private void testConsumerBundleWeaving(Bundle consumerBundle, WeavingHook wh, Set<String> testClientResult, String jaxpClientResult) throws Exception {
    // Weave the TestClient class.
    URL clsUrl = getClass().getResource("TestClient.class");
    WovenClass wc = new MyWovenClass(clsUrl, TestClient.class.getName(), 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.newInstance(), "hello");
    Assert.assertEquals(testClientResult, result);
    URL clsUrl2 = getClass().getResource("JaxpClient.class");
    WovenClass wc2 = new MyWovenClass(clsUrl2, JaxpClient.class.getName(), consumerBundle);
    wh.weave(wc2);
    Class<?> cls2 = wc2.getDefinedClass();
    Method method2 = cls2.getMethod("test", new Class[] {});
    Class<?> result2 = (Class<?>) method2.invoke(cls2.newInstance());
    Assert.assertEquals(jaxpClientResult, result2.getName());
}
Also used : WovenClass(org.osgi.framework.hooks.weaving.WovenClass) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) Method(java.lang.reflect.Method) URL(java.net.URL)

Example 10 with WovenClass

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

the class Aries1429Test method testMissingParentChildEdgeTolerated.

@Test
public void testMissingParentChildEdgeTolerated() throws Exception {
    final AtomicBoolean weavingHookCalled = new AtomicBoolean();
    final AtomicReference<FrameworkEvent> frameworkEvent = new AtomicReference<FrameworkEvent>();
    bundleContext.registerService(WeavingHook.class, new WeavingHook() {

        @Override
        public void weave(WovenClass wovenClass) {
            Bundle bundle = wovenClass.getBundleWiring().getBundle();
            if (BUNDLE_A.equals(bundle.getSymbolicName())) {
                wovenClass.getDynamicImports().add("com.acme.tnt");
                weavingHookCalled.set(true);
            }
        }
    }, null);
    Subsystem applicationA = installSubsystemFromFile(APPLICATION_A);
    try {
        removeConnectionWithParent(applicationA);
        BundleContext context = applicationA.getBundleContext();
        Bundle bundleA = context.installBundle(BUNDLE_A, TinyBundles.bundle().add(getClass().getClassLoader().loadClass("a.A"), InnerClassStrategy.NONE).set(Constants.BUNDLE_SYMBOLICNAME, BUNDLE_A).build(TinyBundles.withBnd()));
        bundleContext.addFrameworkListener(new FrameworkListener() {

            @Override
            public void frameworkEvent(FrameworkEvent event) {
                if (FrameworkEvent.ERROR == event.getType() && getSubsystemCoreBundle().equals(event.getBundle())) {
                    frameworkEvent.set(event);
                    if (event.getThrowable() != null) {
                        event.getThrowable().printStackTrace();
                    }
                }
            }
        });
        bundleA.loadClass("a.A");
        assertTrue("Weaving hook not called", weavingHookCalled.get());
        Thread.sleep(1000);
        assertNull("An exception was thrown", frameworkEvent.get());
    } finally {
        uninstallSubsystemSilently(applicationA);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FrameworkEvent(org.osgi.framework.FrameworkEvent) Bundle(org.osgi.framework.Bundle) AriesSubsystem(org.apache.aries.subsystem.AriesSubsystem) Subsystem(org.osgi.service.subsystem.Subsystem) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) AtomicReference(java.util.concurrent.atomic.AtomicReference) FrameworkListener(org.osgi.framework.FrameworkListener) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) BundleContext(org.osgi.framework.BundleContext) SubsystemTest(org.apache.aries.subsystem.itests.SubsystemTest) Test(org.junit.Test)

Aggregations

WovenClass (org.osgi.framework.hooks.weaving.WovenClass)29 Bundle (org.osgi.framework.Bundle)28 Test (org.junit.Test)27 WeavingHook (org.osgi.framework.hooks.weaving.WeavingHook)27 Method (java.lang.reflect.Method)25 URL (java.net.URL)25 Hashtable (java.util.Hashtable)24 HashSet (java.util.HashSet)8 URLClassLoader (java.net.URLClassLoader)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Subsystem (org.osgi.service.subsystem.Subsystem)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 WovenProxy (org.apache.aries.proxy.weaving.WovenProxy)1 ProxyWeavingController (org.apache.aries.proxy.weavinghook.ProxyWeavingController)1 WeavingHelper (org.apache.aries.proxy.weavinghook.WeavingHelper)1