use of org.osgi.framework.hooks.weaving.WeavingHook in project aries by apache.
the class DynamicWeavingActivator method start.
@Override
public synchronized void start(BundleContext context) throws Exception {
WeavingHook wh = new ClientWeavingHook(context, this);
weavingHookService = context.registerService(WeavingHook.class.getName(), wh, null);
super.start(context, SpiFlyConstants.SPI_CONSUMER_HEADER);
}
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) {
}
}
}
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));
}
use of org.osgi.framework.hooks.weaving.WeavingHook in project rt.equinox.framework by eclipse.
the class SystemBundleTests method testBug414070.
public void testBug414070() throws BundleException, InstantiationException, IllegalAccessException, ClassNotFoundException {
// $NON-NLS-1$
File config = OSGiTestsActivator.getContext().getDataFile(getName());
Map<String, Object> configuration = new HashMap<String, Object>();
configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
Equinox equinox = new Equinox(configuration);
equinox.init();
BundleContext systemContext = equinox.getBundleContext();
Bundle systemBundle = systemContext.getBundle();
// $NON-NLS-1$
Bundle chainTest = systemContext.installBundle(installer.getBundleLocation("chain.test"));
// $NON-NLS-1$
final Bundle chainTestD = systemContext.installBundle(installer.getBundleLocation("chain.test.d"));
// $NON-NLS-1$
Bundle chainTestA = systemContext.installBundle(installer.getBundleLocation("chain.test.a"));
// $NON-NLS-1$
Bundle chainTestB = systemContext.installBundle(installer.getBundleLocation("chain.test.b"));
// $NON-NLS-1$
Bundle chainTestC = systemContext.installBundle(installer.getBundleLocation("chain.test.c"));
systemContext.registerService(WeavingHook.class, new WeavingHook() {
public void weave(WovenClass wovenClass) {
if (!chainTestD.equals(wovenClass.getBundleWiring().getBundle()))
return;
if (!"chain.test.d.DMultipleChain1".equals(wovenClass.getClassName()))
return;
List dynamicImports = wovenClass.getDynamicImports();
dynamicImports.add("*");
}
}, null);
equinox.start();
// $NON-NLS-1$
chainTest.loadClass("chain.test.TestMultiChain").newInstance();
// force a dynamic wire to cause a cycle
chainTestD.loadClass("chain.test.a.AMultiChain1");
// make sure all bundles are active now
assertEquals("A is not active.", Bundle.ACTIVE, chainTestA.getState());
assertEquals("B is not active.", Bundle.ACTIVE, chainTestB.getState());
assertEquals("C is not active.", Bundle.ACTIVE, chainTestC.getState());
assertEquals("D is not active.", Bundle.ACTIVE, chainTestD.getState());
// record STOPPING order
final List<Bundle> stoppingOrder = new ArrayList<Bundle>();
systemContext.addBundleListener(new SynchronousBundleListener() {
@Override
public void bundleChanged(BundleEvent event) {
if (event.getType() == BundleEvent.STOPPING) {
stoppingOrder.add(event.getBundle());
}
}
});
equinox.stop();
try {
equinox.waitForStop(10000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
fail("Unexpected interruption.", e);
}
List<Bundle> expectedOrder = Arrays.asList(systemBundle, chainTest, chainTestA, chainTestB, chainTestC, chainTestD);
assertEquals("Wrong stopping order", expectedOrder.toArray(), stoppingOrder.toArray());
}
use of org.osgi.framework.hooks.weaving.WeavingHook in project rt.equinox.framework by eclipse.
the class SystemBundleTests method testWeavingPersistence.
public void testWeavingPersistence() {
// $NON-NLS-1$
File config = OSGiTestsActivator.getContext().getDataFile(getName());
Map<String, Object> configuration = new HashMap<String, Object>();
configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
Equinox equinox = new Equinox(configuration);
try {
equinox.start();
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected exception in start()", e);
}
BundleContext systemContext = equinox.getBundleContext();
// $NON-NLS-1$
assertNotNull("System context is null", systemContext);
Bundle test1 = null;
try {
test1 = systemContext.installBundle(installer.getBundleLocation("substitutes.a"));
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected error installing bundle", e);
}
long testID1 = test1.getBundleId();
final Bundle testFinal1 = test1;
ServiceRegistration reg = systemContext.registerService(WeavingHook.class, new WeavingHook() {
public void weave(WovenClass wovenClass) {
if (!testFinal1.equals(wovenClass.getBundleWiring().getBundle()))
return;
if (!"substitutes.x.Ax".equals(wovenClass.getClassName()))
return;
List dynamicImports = wovenClass.getDynamicImports();
dynamicImports.add("*");
}
}, null);
try {
testFinal1.loadClass("substitutes.x.Ax");
testFinal1.loadClass("org.osgi.framework.hooks.bundle.FindHook");
} catch (Throwable t) {
fail("Unexpected testing bundle", t);
} finally {
reg.unregister();
}
// put the framework back to the RESOLVED state
try {
equinox.stop();
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected error stopping framework", e);
}
try {
equinox.waitForStop(10000);
} catch (InterruptedException e) {
// $NON-NLS-1$
fail("Unexpected interrupted exception", e);
}
try {
equinox.start();
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected exception in start()", e);
}
systemContext = equinox.getBundleContext();
test1 = systemContext.getBundle(testID1);
Bundle test2 = null;
try {
test2 = systemContext.installBundle(installer.getBundleLocation("exporter.importer1"));
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected error installing bundle", e);
}
long testID2 = test2.getBundleId();
final Bundle testFinal2 = test2;
reg = systemContext.registerService(WeavingHook.class, new WeavingHook() {
public void weave(WovenClass wovenClass) {
if (!testFinal2.equals(wovenClass.getBundleWiring().getBundle()))
return;
if (!"exporter.importer.test.Test1".equals(wovenClass.getClassName()))
return;
List dynamicImports = wovenClass.getDynamicImports();
dynamicImports.add("*");
}
}, null);
try {
testFinal2.loadClass("exporter.importer.test.Test1");
testFinal2.loadClass("org.osgi.framework.hooks.service.FindHook");
} catch (Throwable t) {
fail("Unexpected testing bundle", t);
} finally {
reg.unregister();
}
// put the framework back to the RESOLVED state
try {
equinox.stop();
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected error stopping framework", e);
}
try {
equinox.waitForStop(10000);
} catch (InterruptedException e) {
// $NON-NLS-1$
fail("Unexpected interrupted exception", e);
}
try {
equinox.start();
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected exception in start()", e);
}
systemContext = equinox.getBundleContext();
test1 = systemContext.getBundle(testID1);
test2 = systemContext.getBundle(testID2);
BundleRevision rev1 = test1.adapt(BundleRevision.class);
BundleRevision rev2 = test2.adapt(BundleRevision.class);
BundleWiring wiring1 = rev1.getWiring();
BundleWiring wiring2 = rev2.getWiring();
assertNotNull("wiring1 is null", wiring1);
assertNotNull("wiring2 is null", wiring2);
List packages1 = wiring1.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
List packages2 = wiring2.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
// could make this a more complete check, but with the bug the dynamic wires
// are missing altogether because we fail to save the resolver state cache.
assertEquals("Wrong number of wires for wiring1", 1, packages1.size());
assertEquals("Wrong number of wires for wiring2", 1, packages2.size());
try {
equinox.stop();
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected error stopping framework", e);
}
try {
equinox.waitForStop(10000);
} catch (InterruptedException e) {
// $NON-NLS-1$
fail("Unexpected interrupted exception", e);
}
}
Aggregations