use of org.osgi.framework.hooks.weaving.WovenClass 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);
}
use of org.osgi.framework.hooks.weaving.WovenClass 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());
}
use of org.osgi.framework.hooks.weaving.WovenClass 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) {
}
}
}
use of org.osgi.framework.hooks.weaving.WovenClass 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));
}
Aggregations