Search in sources :

Example 1 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project jetty.project by eclipse.

the class JettyBootstrapActivator method start.

/* ------------------------------------------------------------ */
/**
     * Setup a new jetty Server, registers it as a service. Setup the Service
     * tracker for the jetty ContextHandlers that are in charge of deploying the
     * webapps. Setup the BundleListener that supports the extender pattern for
     * the jetty ContextHandler.
     * 
     * @param context the bundle context
     */
public void start(final BundleContext context) throws Exception {
    INSTANCE = this;
    // track other bundles and fragments attached to this bundle that we
    // should activate.
    _packageAdminServiceTracker = new PackageAdminServiceTracker(context);
    // track jetty Server instances that we should support as deployment targets
    _jettyServerServiceTracker = new ServiceTracker(context, context.createFilter("(objectclass=" + Server.class.getName() + ")"), new JettyServerServiceTracker());
    _jettyServerServiceTracker.open();
    // Create a default jetty instance right now.
    Server defaultServer = DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context);
}
Also used : Server(org.eclipse.jetty.server.Server) ServiceTracker(org.osgi.util.tracker.ServiceTracker) PackageAdminServiceTracker(org.eclipse.jetty.osgi.boot.utils.internal.PackageAdminServiceTracker) JettyServerServiceTracker(org.eclipse.jetty.osgi.boot.internal.serverfactory.JettyServerServiceTracker) JettyServerServiceTracker(org.eclipse.jetty.osgi.boot.internal.serverfactory.JettyServerServiceTracker) PackageAdminServiceTracker(org.eclipse.jetty.osgi.boot.utils.internal.PackageAdminServiceTracker)

Example 2 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project jersey by jersey.

the class Activator method start.

@Override
public synchronized void start(BundleContext bundleContext) throws Exception {
    this.bc = bundleContext;
    logger.info("STARTING HTTP SERVICE BUNDLE");
    this.tracker = new ServiceTracker(this.bc, HttpService.class.getName(), null) {

        @Override
        public Object addingService(ServiceReference serviceRef) {
            httpService = (HttpService) super.addingService(serviceRef);
            registerServlets();
            return httpService;
        }

        @Override
        public void removedService(ServiceReference ref, Object service) {
            if (httpService == service) {
                unregisterServlets();
                httpService = null;
            }
            super.removedService(ref, service);
        }
    };
    this.tracker.open();
    logger.info("HTTP SERVICE BUNDLE STARTED");
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) HttpService(org.osgi.service.http.HttpService) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project aries by apache.

the class Activator method initServiceTracker.

private ServiceTracker initServiceTracker(BundleContext context, Class<?> type, ServiceTrackerCustomizer custom) {
    ServiceTracker t = new ServiceTracker(context, type.getName(), custom);
    t.open();
    return t;
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker)

Example 4 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project aries by apache.

the class ConfigurationTests method testConfiguration.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testConfiguration() throws Exception {
    Bundle tb3Bundle = installBundle("tb3.jar");
    Configuration configurationA = null, configurationB = null;
    try {
        Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + "))");
        ServiceTracker<CdiContainer, CdiContainer> containerTracker = new ServiceTracker<>(bundleContext, filter, null);
        containerTracker.open();
        containerTracker.waitForService(timeout);
        ServiceReference<CdiContainer> serviceReference = containerTracker.getServiceReference();
        assertNotNull(serviceReference);
        assertEquals(CdiEvent.Type.WAITING_FOR_CONFIGURATIONS, serviceReference.getProperty(CdiConstants.CDI_CONTAINER_STATE));
        configurationA = configurationAdmin.getConfiguration("configA", "?");
        Dictionary<String, Object> properties = new Hashtable<>();
        properties.put("ports", new int[] { 12, 4567 });
        configurationA.update(properties);
        configurationB = configurationAdmin.getConfiguration("configB", "?");
        properties = new Hashtable<>();
        properties.put("color", "green");
        properties.put("ports", new int[] { 80 });
        configurationB.update(properties);
        containerTracker.close();
        filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=CREATED))");
        containerTracker = new ServiceTracker<>(bundleContext, filter, null);
        containerTracker.open();
        containerTracker.waitForService(timeout);
        ServiceTracker<BeanService, BeanService> stA = new ServiceTracker<BeanService, BeanService>(bundleContext, bundleContext.createFilter("(&(objectClass=org.apache.aries.cdi.test.interfaces.BeanService)(bean=A))"), null);
        stA.open(true);
        BeanService<Callable<int[]>> beanService = stA.waitForService(timeout);
        assertNotNull(beanService);
        assertEquals("blue", beanService.doSomething());
        assertArrayEquals(new int[] { 12, 4567 }, beanService.get().call());
        ServiceTracker<BeanService, BeanService> stB = new ServiceTracker<BeanService, BeanService>(bundleContext, bundleContext.createFilter("(&(objectClass=org.apache.aries.cdi.test.interfaces.BeanService)(bean=B))"), null);
        stB.open(true);
        beanService = stB.waitForService(timeout);
        assertNotNull(beanService);
        assertEquals("green", beanService.doSomething());
        assertArrayEquals(new int[] { 80 }, beanService.get().call());
    } finally {
        if (configurationA != null) {
            try {
                configurationA.delete();
            } catch (Exception e) {
            // ignore
            }
        }
        if (configurationB != null) {
            try {
                configurationB.delete();
            } catch (Exception e) {
            // ignore
            }
        }
        tb3Bundle.uninstall();
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Callable(java.util.concurrent.Callable) Filter(org.osgi.framework.Filter) BeanService(org.apache.aries.cdi.test.interfaces.BeanService) CdiContainer(org.osgi.service.cdi.CdiContainer)

Example 5 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project aries by apache.

the class AbstractTestCase method getServiceTracker.

ServiceTracker<CdiContainer, CdiContainer> getServiceTracker(long bundleId) throws InvalidSyntaxException {
    Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + bundleId + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=" + CdiEvent.Type.CREATED + "))");
    ServiceTracker<CdiContainer, CdiContainer> serviceTracker = new ServiceTracker<>(bundleContext, filter, null);
    serviceTracker.open();
    return serviceTracker;
}
Also used : Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) CdiContainer(org.osgi.service.cdi.CdiContainer)

Aggregations

ServiceTracker (org.osgi.util.tracker.ServiceTracker)321 ServiceReference (org.osgi.framework.ServiceReference)65 Filter (org.osgi.framework.Filter)41 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)32 BundleContext (org.osgi.framework.BundleContext)28 Hashtable (java.util.Hashtable)26 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)23 Activate (org.apache.felix.scr.annotations.Activate)20 IContainerManager (org.eclipse.ecf.core.IContainerManager)20 Bundle (org.osgi.framework.Bundle)19 LogService (org.osgi.service.log.LogService)17 Dictionary (java.util.Dictionary)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 IOException (java.io.IOException)11 Test (org.junit.Test)10 ConfigurationException (org.osgi.service.cm.ConfigurationException)10 Map (java.util.Map)7 List (java.util.List)6 Properties (java.util.Properties)6