Search in sources :

Example 26 with WeavingHook

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

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

Example 28 with WeavingHook

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

the class ClientWeavingHookTest method testClientSpecifyingDifferentMethodsLimitedToDifferentProviders.

@Test
public void testClientSpecifyingDifferentMethodsLimitedToDifferentProviders() throws Exception {
    Dictionary<String, String> headers1 = new Hashtable<String, String>();
    headers1.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "javax.xml.parsers.DocumentBuilderFactory#newInstance();bundle=impl3," + "java.util.ServiceLoader#load(java.lang.Class[org.apache.aries.mytest.MySPI]);bundle=impl4");
    Dictionary<String, String> headers2 = new Hashtable<String, String>();
    headers2.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "javax.xml.parsers.DocumentBuilderFactory#newInstance();bundle=system.bundle," + "java.util.ServiceLoader#load;bundle=impl1");
    Dictionary<String, String> headers3 = new Hashtable<String, String>();
    headers3.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "org.acme.blah#someMethod();bundle=mybundle");
    Bundle providerBundle1 = mockProviderBundle("impl1", 1);
    Bundle providerBundle2 = mockProviderBundle("impl2", 2);
    Bundle providerBundle3 = mockProviderBundle("impl3", 3);
    Bundle providerBundle4 = mockProviderBundle("impl4", 4);
    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.AltSPI", providerBundle2, new HashMap<String, Object>());
    activator.registerProviderBundle("javax.xml.parsers.DocumentBuilderFactory", providerBundle3, new HashMap<String, Object>());
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle4, new HashMap<String, Object>());
    activator.registerProviderBundle("org.apache.aries.mytest.AltSPI", providerBundle4, new HashMap<String, Object>());
    Bundle consumerBundle1 = mockConsumerBundle(headers1, providerBundle1, providerBundle2, providerBundle3, providerBundle4);
    activator.addConsumerWeavingData(consumerBundle1, SpiFlyConstants.SPI_CONSUMER_HEADER);
    Bundle consumerBundle2 = mockConsumerBundle(headers2, providerBundle1, providerBundle2, providerBundle3, providerBundle4);
    activator.addConsumerWeavingData(consumerBundle2, SpiFlyConstants.SPI_CONSUMER_HEADER);
    Bundle consumerBundle3 = mockConsumerBundle(headers3, providerBundle1, providerBundle2, providerBundle3, providerBundle4);
    activator.addConsumerWeavingData(consumerBundle3, SpiFlyConstants.SPI_CONSUMER_HEADER);
    Bundle spiFlyBundle = mockSpiFlyBundle(consumerBundle1, consumerBundle2, consumerBundle3, providerBundle1, providerBundle2, providerBundle3, providerBundle4);
    WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator);
    testConsumerBundleWeaving(consumerBundle1, wh, Collections.singleton("impl4"), "org.apache.aries.spifly.dynamic.impl3.MyAltDocumentBuilderFactory");
    testConsumerBundleWeaving(consumerBundle2, wh, Collections.singleton("olleh"), thisJVMsDBF);
    testConsumerBundleWeaving(consumerBundle3, wh, Collections.<String>emptySet(), thisJVMsDBF);
}
Also used : Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) Test(org.junit.Test)

Example 29 with WeavingHook

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

the class ClientWeavingHookTest method testClientSpecificProviderLoadArgument.

@Test
public void testClientSpecificProviderLoadArgument() throws Exception {
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "java.util.ServiceLoader#load(java.lang.Class[org.apache.aries.mytest.MySPI])," + "java.util.ServiceLoader#load(java.lang.Class[org.apache.aries.mytest.AltSPI]);bundle=impl4");
    Bundle providerBundle1 = mockProviderBundle("impl1", 1);
    Bundle providerBundle2 = mockProviderBundle("impl2", 2);
    Bundle providerBundle4 = mockProviderBundle("impl4", 4);
    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.AltSPI", providerBundle2, new HashMap<String, Object>());
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle4, new HashMap<String, Object>());
    activator.registerProviderBundle("org.apache.aries.mytest.AltSPI", providerBundle4, new HashMap<String, Object>());
    Bundle consumerBundle = mockConsumerBundle(headers, providerBundle1, providerBundle2, providerBundle4);
    activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.SPI_CONSUMER_HEADER);
    Bundle spiFlyBundle = mockSpiFlyBundle(consumerBundle, providerBundle1, providerBundle2, providerBundle4);
    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("olleh", "impl4", "HELLO", "5"));
    Assert.assertEquals("All providers should be selected for this one", expected, result);
    // Weave the AltTestClient class.
    URL cls2Url = getClass().getResource("AltTestClient.class");
    WovenClass wc2 = new MyWovenClass(cls2Url, "org.apache.aries.spifly.dynamic.AltTestClient", consumerBundle);
    wh.weave(wc2);
    // Invoke the AltTestClient
    Class<?> cls2 = wc2.getDefinedClass();
    Method method2 = cls2.getMethod("test", new Class[] { long.class });
    Object result2 = method2.invoke(cls2.newInstance(), 4096);
    Assert.assertEquals("Only the services from bundle impl4 should be selected", -4096L, result2);
}
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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 30 with WeavingHook

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

the class ClientWeavingHookTest method testJAXPClientWantsAltImplementation1.

// If there is an alternate implementation it should always be favoured over the JRE one
@Test
public void testJAXPClientWantsAltImplementation1() 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 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", "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)

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