Search in sources :

Example 21 with WovenClass

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

the class ClientWeavingHookGenericCapabilityTest method testTCCLResetting.

@Test
public void testTCCLResetting() throws Exception {
    ClassLoader cl = new URLClassLoader(new URL[] {});
    Thread.currentThread().setContextClassLoader(cl);
    Assert.assertSame("Precondition", cl, Thread.currentThread().getContextClassLoader());
    Dictionary<String, String> consumerHeaders = new Hashtable<String, String>();
    consumerHeaders.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.CLIENT_REQUIREMENT);
    // 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.REQUIRE_CAPABILITY);
    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;bundle-symbolic-name=spifly;bundle-version=1.9.4";
    String di2 = "org.apache.aries.spifly;bundle-version=1.9.4;bundle-symbolic-name=spifly";
    String di = wc.getDynamicImports().get(0);
    Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di) || di2.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 });
    method.invoke(cls.newInstance(), "hi there");
    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) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) Test(org.junit.Test)

Example 22 with WovenClass

use of org.osgi.framework.hooks.weaving.WovenClass 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;bundle-symbolic-name=spifly;bundle-version=1.9.4";
    String di2 = "org.apache.aries.spifly;bundle-version=1.9.4;bundle-symbolic-name=spifly";
    String di = wc.getDynamicImports().get(0);
    Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di) || di2.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.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 23 with WovenClass

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

the class ClientWeavingHookTest method testJAXPClientWantsJREImplementation2.

@Test
public void testJAXPClientWantsJREImplementation2() 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();bundleId=0");
    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 JRE", thisJVMsDBF, 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 24 with WovenClass

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

the class ClientWeavingHookGenericCapabilityTest method testFilteringCustomAttribute.

@Test
public void testFilteringCustomAttribute() throws Exception {
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.CLIENT_REQUIREMENT + ", " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; filter:=\"(approval=global)\"");
    Bundle providerBundle1 = mockProviderBundle("impl1", 1);
    Bundle providerBundle2 = mockProviderBundle("impl2", 2);
    Map<String, Object> attrs1 = new HashMap<String, Object>();
    attrs1.put("approval", "local");
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle1, attrs1);
    Map<String, Object> attrs2 = new HashMap<String, Object>();
    attrs2.put("approval", "global");
    attrs2.put("other", "attribute");
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle2, attrs2);
    Bundle consumerBundle = mockConsumerBundle(headers, providerBundle1, providerBundle2);
    activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.REQUIRE_CAPABILITY);
    Bundle spiFlyBundle = mockSpiFlyBundle(consumerBundle, providerBundle1, providerBundle2);
    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.newInstance(), "hello");
    Set<String> expected = new HashSet<String>(Arrays.asList("HELLO", "5"));
    Assert.assertEquals("Only the services from bundle impl2 should be selected", expected, result);
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 25 with WovenClass

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

the class ClientWeavingHookGenericCapabilityTest method testMultipleProviders.

@Test
public void testMultipleProviders() 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);
    activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.REQUIRE_CAPABILITY);
    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);
    Bundle providerBundle1 = mockProviderBundle("impl1", 1);
    Bundle providerBundle2 = mockProviderBundle("impl2", 2);
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle1, new HashMap<String, Object>());
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle2, 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 files from impl1 and impl2 are visible.
    Class<?> cls = wc.getDefinedClass();
    Method method = cls.getMethod("test", new Class[] { String.class });
    Object result = method.invoke(cls.newInstance(), "hello");
    Set<String> expected = new HashSet<String>(Arrays.asList("olleh", "HELLO", "5"));
    Assert.assertEquals("All three services should be invoked", expected, 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) HashSet(java.util.HashSet) 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