Search in sources :

Example 96 with BundleWiring

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();
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) LoggerContextFactory(org.apache.logging.log4j.spi.LoggerContextFactory) BundleWire(org.osgi.framework.wiring.BundleWire)

Example 97 with BundleWiring

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);
    }
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException) FinalModifierException(org.apache.aries.proxy.FinalModifierException) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException) FinalModifierException(org.apache.aries.proxy.FinalModifierException)

Example 98 with BundleWiring

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);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) Before(org.junit.Before)

Example 99 with 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());
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) BundleWiring(org.osgi.framework.wiring.BundleWiring) ArrayList(java.util.ArrayList) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 100 with BundleWiring

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);
        }
    }
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleWire(org.osgi.framework.wiring.BundleWire)

Aggregations

BundleWiring (org.osgi.framework.wiring.BundleWiring)110 Bundle (org.osgi.framework.Bundle)61 BundleWire (org.osgi.framework.wiring.BundleWire)39 ArrayList (java.util.ArrayList)23 BundleRevision (org.osgi.framework.wiring.BundleRevision)23 BundleCapability (org.osgi.framework.wiring.BundleCapability)19 Test (org.junit.Test)15 HashMap (java.util.HashMap)13 Hashtable (java.util.Hashtable)12 List (java.util.List)12 BundleContext (org.osgi.framework.BundleContext)12 URL (java.net.URL)10 Dictionary (java.util.Dictionary)10 HashSet (java.util.HashSet)9 LinkedHashMap (java.util.LinkedHashMap)8 Version (org.osgi.framework.Version)7 BundleRevisions (org.osgi.framework.wiring.BundleRevisions)7 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)6 IAnswer (org.easymock.IAnswer)6 ServiceReference (org.osgi.framework.ServiceReference)6