Search in sources :

Example 16 with ServiceReference

use of org.osgi.framework.ServiceReference in project aries by apache.

the class CdiBeanTests method testConstructorInjectedService.

public void testConstructorInjectedService() throws Exception {
    Iterator<ServiceReference<BeanService>> iterator = bundleContext.getServiceReferences(BeanService.class, String.format("(objectClass=*.%s)", "ConstructorInjectedService")).iterator();
    assertTrue(iterator.hasNext());
    ServiceReference<BeanService> serviceReference = iterator.next();
    assertNotNull(serviceReference);
    BeanService bean = bundleContext.getService(serviceReference);
    assertNotNull(bean);
    assertEquals("PREFIXCONSTRUCTOR", bean.doSomething());
}
Also used : BeanService(org.apache.aries.cdi.test.interfaces.BeanService) ServiceReference(org.osgi.framework.ServiceReference)

Example 17 with ServiceReference

use of org.osgi.framework.ServiceReference in project aries by apache.

the class CdiBeanTests method testFieldInjectedService.

public void testFieldInjectedService() throws Exception {
    Iterator<ServiceReference<BeanService>> iterator = bundleContext.getServiceReferences(BeanService.class, String.format("(objectClass=*.%s)", "FieldInjectedService")).iterator();
    assertTrue(iterator.hasNext());
    ServiceReference<BeanService> serviceReference = iterator.next();
    assertNotNull(serviceReference);
    BeanService bean = bundleContext.getService(serviceReference);
    assertNotNull(bean);
    assertEquals("PREFIXFIELD", bean.doSomething());
}
Also used : BeanService(org.apache.aries.cdi.test.interfaces.BeanService) ServiceReference(org.osgi.framework.ServiceReference)

Example 18 with ServiceReference

use of org.osgi.framework.ServiceReference in project aries by apache.

the class BlueprintURLContext method findBPCRef.

/**
   * Look for a BluepintContainer service in a given bundle
   * @param b Bundle to look in
   * @return BlueprintContainer service, or null if none available
   */
private static ServiceReference findBPCRef(Bundle b) {
    ServiceReference[] refs = b.getRegisteredServices();
    ServiceReference result = null;
    if (refs != null) {
        outer: for (ServiceReference r : refs) {
            String[] objectClasses = (String[]) r.getProperty(Constants.OBJECTCLASS);
            for (String objectClass : objectClasses) {
                if (objectClass.equals(BlueprintContainer.class.getName())) {
                    // Arguably we could put an r.isAssignableTo(jndi-url-bundle, BlueprintContainer.class.getName())
                    // check here. But if you've got multiple, class-space inconsistent instances of blueprint in 
                    // your environment, you've almost certainly got other problems.
                    result = r;
                    break outer;
                }
            }
        }
    }
    return result;
}
Also used : ServiceReference(org.osgi.framework.ServiceReference)

Example 19 with ServiceReference

use of org.osgi.framework.ServiceReference in project aries by apache.

the class AbstractJPATransactionTest method getService.

protected <T> T getService(Class<T> clazz, String filter, long timeout) throws InvalidSyntaxException {
    Filter f = FrameworkUtil.createFilter(filter == null ? "(|(foo=bar)(!(foo=bar)))" : filter);
    ServiceTracker<T, T> tracker = new ServiceTracker<T, T>(context, clazz, null) {

        @Override
        public T addingService(ServiceReference<T> reference) {
            return f.match(reference) ? super.addingService(reference) : null;
        }
    };
    tracker.open();
    try {
        T t = tracker.waitForService(timeout);
        if (t == null) {
            throw new NoSuchElementException(clazz.getName());
        }
        return t;
    } catch (InterruptedException e) {
        throw new RuntimeException("Error waiting for service " + clazz.getName(), e);
    } finally {
        trackers.add(tracker);
    }
}
Also used : Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) NoSuchElementException(java.util.NoSuchElementException) ServiceReference(org.osgi.framework.ServiceReference)

Example 20 with ServiceReference

use of org.osgi.framework.ServiceReference in project jackrabbit-oak by apache.

the class OsgiWhiteboard method track.

/**
     * Returns a tracker for services of the given type. The returned tracker
     * is optimized for frequent {@link Tracker#getServices()} calls through
     * the use of a pre-compiled list of services that's atomically updated
     * whenever services are added, modified or removed.
     */
@Override
public <T> Tracker<T> track(final Class<T> type) {
    checkNotNull(type);
    final AtomicReference<List<T>> list = new AtomicReference<List<T>>(Collections.<T>emptyList());
    final ServiceTrackerCustomizer customizer = new ServiceTrackerCustomizer() {

        private final Map<ServiceReference, T> services = newHashMap();

        @Override
        @SuppressWarnings("unchecked")
        public synchronized Object addingService(ServiceReference reference) {
            Object service = context.getService(reference);
            if (type.isInstance(service)) {
                services.put(reference, (T) service);
                list.set(getServiceList(services));
                return service;
            } else {
                context.ungetService(reference);
                return null;
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public synchronized void modifiedService(ServiceReference reference, Object service) {
            // TODO: Figure out if the old reference instance
            // would automatically reflect the updated properties.
            // For now we play it safe by replacing the old key
            // with the new reference instance passed as argument.
            services.remove(reference);
            services.put(reference, (T) service);
            list.set(getServiceList(services));
        }

        @Override
        public synchronized void removedService(ServiceReference reference, Object service) {
            services.remove(reference);
            list.set(getServiceList(services));
            // TODO: Note that the service might still be in use
            // by some client that called getServices() before
            // this method was invoked.
            context.ungetService(reference);
        }
    };
    final ServiceTracker tracker = new ServiceTracker(context, type.getName(), customizer);
    tracker.open();
    return new Tracker<T>() {

        @Override
        public List<T> getServices() {
            return list.get();
        }

        @Override
        public void stop() {
            tracker.close();
        }
    };
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) Tracker(org.apache.jackrabbit.oak.spi.whiteboard.Tracker) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Map(java.util.Map) Maps.newTreeMap(com.google.common.collect.Maps.newTreeMap) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) SortedMap(java.util.SortedMap) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServiceReference (org.osgi.framework.ServiceReference)1687 Test (org.junit.Test)926 Properties (java.util.Properties)396 Architecture (org.apache.felix.ipojo.architecture.Architecture)263 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)233 BundleContext (org.osgi.framework.BundleContext)229 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)227 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)215 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)182 ArrayList (java.util.ArrayList)167 Bundle (org.osgi.framework.Bundle)144 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)141 Hashtable (java.util.Hashtable)124 IOException (java.io.IOException)107 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)92 Dictionary (java.util.Dictionary)82 Configuration (org.osgi.service.cm.Configuration)74 CheckService (org.apache.felix.ipojo.handler.temporal.services.CheckService)70 FooService (org.apache.felix.ipojo.handler.temporal.services.FooService)70 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65