use of org.osgi.framework.wiring.BundleWiring in project logging-log4j2 by apache.
the class Activator method start.
@Override
public void start(final BundleContext bundleContext) throws Exception {
ProviderUtil.STARTUP_LOCK.lock();
lockingProviderUtil = true;
final BundleWiring self = bundleContext.getBundle().adapt(BundleWiring.class);
final List<BundleWire> required = self.getRequiredWires(LoggerContextFactory.class.getName());
for (final BundleWire wire : required) {
loadProvider(bundleContext, wire.getProviderWiring());
}
bundleContext.addBundleListener(this);
final Bundle[] bundles = bundleContext.getBundles();
for (final Bundle bundle : bundles) {
loadProvider(bundle);
}
unlockIfReady();
}
use of org.osgi.framework.wiring.BundleWiring in project aries by apache.
the class InterfaceProxyGenerator method getProxyInstance.
/**
* Generate a new proxy instance implementing the supplied interfaces and using the supplied
* dispatcher and listener
* @param client the bundle that is trying to generate this proxy (can be null)
* @param superclass The superclass to use (or null for Object)
* @param ifaces The set of interfaces to implement (may be empty if superclass is non null)
* @param dispatcher
* @param listener
* @return
* @throws UnableToProxyException
*/
public static Object getProxyInstance(Bundle client, Class<?> superclass, Collection<Class<?>> ifaces, Callable<Object> dispatcher, InvocationListener listener) throws UnableToProxyException {
if (superclass != null && (superclass.getModifiers() & Modifier.FINAL) != 0)
throw new FinalModifierException(superclass);
ProxyClassLoader pcl = null;
SortedSet<Class<?>> interfaces = createSet(ifaces);
synchronized (cache) {
BundleWiring wiring = client == null ? null : (BundleWiring) client.adapt(BundleWiring.class);
WeakReference<ProxyClassLoader> ref = cache.get(wiring);
if (ref != null)
pcl = ref.get();
if (pcl != null && pcl.isInvalid(interfaces)) {
pcl = null;
cache.remove(wiring);
}
if (pcl == null) {
pcl = new ProxyClassLoader(client);
cache.put(wiring, new WeakReference<ProxyClassLoader>(pcl));
}
}
Class<?> c = pcl.createProxyClass(superclass, interfaces);
try {
Constructor<?> con = c.getDeclaredConstructor(Callable.class, InvocationListener.class);
con.setAccessible(true);
return con.newInstance(dispatcher, listener);
} catch (Exception e) {
throw new UnableToProxyException(ifaces.iterator().next(), e);
}
}
use of org.osgi.framework.wiring.BundleWiring in project aries by apache.
the class InterfaceProxyingTest method setup.
@Before
public void setup() {
testBundle = Mockito.mock(Bundle.class);
BundleWiring bundleWiring = Mockito.mock(BundleWiring.class);
Mockito.when(testBundle.adapt(BundleWiring.class)).thenReturn(bundleWiring);
}
use of org.osgi.framework.wiring.BundleWiring in project aries by apache.
the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testCapReqHeadersInFragment.
@Test
public void testCapReqHeadersInFragment() throws Exception {
Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
EasyMock.replay(mediatorBundle);
BaseActivator activator = new BaseActivator() {
@Override
public void start(BundleContext context) throws Exception {
}
};
ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
ServiceRegistration<?> sreg = EasyMock.createNiceMock(ServiceRegistration.class);
EasyMock.replay(sreg);
BundleContext implBC = mockSPIBundleContext(sreg);
Dictionary<String, String> headers = new Hashtable<String, String>();
// A typical requirement that is not for us...
headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, "osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.6))\"");
List<BundleWire> wires = new ArrayList<BundleWire>();
BundleWire wire = EasyMock.createMock(BundleWire.class);
Bundle fragment = EasyMock.createMock(Bundle.class);
BundleRevision frev = EasyMock.createMock(BundleRevision.class);
EasyMock.expect(frev.getBundle()).andReturn(fragment).anyTimes();
EasyMock.replay(frev);
BundleRequirement req = EasyMock.createMock(BundleRequirement.class);
EasyMock.expect(req.getRevision()).andReturn(frev).anyTimes();
EasyMock.replay(req);
EasyMock.expect(wire.getRequirement()).andReturn(req).anyTimes();
EasyMock.replay(wire);
wires.add(wire);
BundleWiring bw = EasyMock.createMock(BundleWiring.class);
EasyMock.expect(bw.getProvidedWires("osgi.wiring.host")).andReturn(wires).anyTimes();
EasyMock.replay(bw);
BundleRevision rev = EasyMock.createMock(BundleRevision.class);
EasyMock.expect(rev.getWiring()).andReturn(bw).anyTimes();
EasyMock.expect(rev.getTypes()).andReturn(0).anyTimes();
EasyMock.replay(rev);
Bundle implBundle = mockSPIBundle(implBC, headers, rev);
Dictionary<String, String> fheaders = new Hashtable<String, String>();
fheaders.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.PROVIDER_REQUIREMENT);
fheaders.put(SpiFlyConstants.PROVIDE_CAPABILITY, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI");
EasyMock.expect(fragment.getHeaders()).andReturn(fheaders).anyTimes();
EasyMock.replay(fragment);
assertEquals("Precondition", 0, activator.findProviderBundles("org.apache.aries.mytest.MySPI").size());
customizer.addingBundle(implBundle, null);
Collection<Bundle> bundles = activator.findProviderBundles("org.apache.aries.mytest.MySPI");
assertEquals(1, bundles.size());
assertSame(implBundle, bundles.iterator().next());
}
use of org.osgi.framework.wiring.BundleWiring in project aries by apache.
the class BundleWiringState method populateTransitiveRevisions.
private void populateTransitiveRevisions(String namespace, BundleRevision rootRevision, Map<BundleRevision, Integer> allRevisions) {
allRevisions.put(rootRevision, System.identityHashCode(rootRevision));
BundleWiring wiring = rootRevision.getWiring();
if (wiring == null)
return;
List<BundleWire> requiredWires = wiring.getRequiredWires(namespace);
for (BundleWire wire : requiredWires) {
BundleRevision revision = wire.getCapability().getRevision();
if (!allRevisions.containsKey(revision)) {
populateTransitiveRevisions(namespace, revision, allRevisions);
}
}
List<BundleWire> providedWires = wiring.getProvidedWires(namespace);
for (BundleWire wire : providedWires) {
BundleRevision revision = wire.getRequirement().getRevision();
if (!allRevisions.containsKey(revision)) {
populateTransitiveRevisions(namespace, revision, allRevisions);
}
}
}
Aggregations