Search in sources :

Example 66 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class ServiceExporter method stop.

/**
 * Stop an exporter.
 * Remove the service listener
 * Unregister all exported services.
 */
public void stop() {
    super.stop();
    Set refs = m_registrations.keySet();
    Iterator iterator = refs.iterator();
    while (iterator.hasNext()) {
        ServiceReference ref = (ServiceReference) iterator.next();
        ServiceRegistration reg = (ServiceRegistration) m_registrations.get(ref);
        try {
            reg.unregister();
        } catch (IllegalStateException e) {
        // The service is already unregistered
        // Do nothing silently
        }
    }
    m_registrations.clear();
}
Also used : Set(java.util.Set) Iterator(java.util.Iterator) ServiceReference(org.osgi.framework.ServiceReference) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 67 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class WhiteboardManager method start.

/**
 * Start the whiteboard manager
 * @param containerContext The servlet context
 */
public void start(final ServletContext containerContext, @Nonnull final Dictionary<String, Object> httpServiceProps) {
    // runtime service gets the same props for now
    this.serviceRuntime.setAllAttributes(httpServiceProps);
    this.serviceRuntime.setAttribute(HttpServiceRuntimeConstants.HTTP_SERVICE_ID, Collections.singletonList(this.httpServiceFactory.getHttpServiceServiceId()));
    this.runtimeServiceReg = this.httpBundleContext.registerService(HttpServiceRuntime.class, serviceRuntime, this.serviceRuntime.getAttributes());
    this.serviceRuntime.setServiceReference(this.runtimeServiceReg.getReference());
    this.webContext = containerContext;
    // add context for http service
    final List<WhiteboardContextHandler> list = new ArrayList<>();
    final ServletContextHelperInfo info = new ServletContextHelperInfo(Integer.MAX_VALUE, HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID, HttpServiceFactory.HTTP_SERVICE_CONTEXT_NAME, "/", null);
    list.add(new HttpServiceContextHandler(info, registry.getRegistry(HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID), httpServiceFactory, webContext, this.httpBundleContext.getBundle()));
    this.contextMap.put(HttpServiceFactory.HTTP_SERVICE_CONTEXT_NAME, list);
    // add default context
    final Dictionary<String, Object> props = new Hashtable<>();
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, HttpWhiteboardConstants.HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME);
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/");
    props.put(Constants.SERVICE_RANKING, Integer.MIN_VALUE);
    this.defaultContextRegistration = httpBundleContext.registerService(ServletContextHelper.class, new ServiceFactory<ServletContextHelper>() {

        @Override
        public ServletContextHelper getService(final Bundle bundle, final ServiceRegistration<ServletContextHelper> registration) {
            return new ServletContextHelper(bundle) {
            };
        }

        @Override
        public void ungetService(final Bundle bundle, final ServiceRegistration<ServletContextHelper> registration, final ServletContextHelper service) {
        // nothing to do
        }
    }, props);
    addTracker(new FilterTracker(this.httpBundleContext, this));
    addTracker(new ListenersTracker(this.httpBundleContext, this));
    addTracker(new PreprocessorTracker(this.httpBundleContext, this));
    addTracker(new ResourceTracker(this.httpBundleContext, this));
    addTracker(new ServletContextHelperTracker(this.httpBundleContext, this));
    addTracker(new ServletTracker(this.httpBundleContext, this));
    this.plugin.register();
}
Also used : ListenersTracker(org.apache.felix.http.base.internal.whiteboard.tracker.ListenersTracker) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) HttpServiceFactory(org.apache.felix.http.base.internal.service.HttpServiceFactory) ServiceFactory(org.osgi.framework.ServiceFactory) Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) ResourceTracker(org.apache.felix.http.base.internal.whiteboard.tracker.ResourceTracker) ArrayList(java.util.ArrayList) ServletTracker(org.apache.felix.http.base.internal.whiteboard.tracker.ServletTracker) ServletContextHelper(org.osgi.service.http.context.ServletContextHelper) FilterTracker(org.apache.felix.http.base.internal.whiteboard.tracker.FilterTracker) ServletContextHelperTracker(org.apache.felix.http.base.internal.whiteboard.tracker.ServletContextHelperTracker) ServletContextHelperInfo(org.apache.felix.http.base.internal.runtime.ServletContextHelperInfo) PreprocessorTracker(org.apache.felix.http.base.internal.whiteboard.tracker.PreprocessorTracker) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 68 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class HttpJettyConnectorTest method testRegisterConnectorFactoryOk.

@Test
public void testRegisterConnectorFactoryOk() throws Exception {
    final CountDownLatch openLatch = new CountDownLatch(1);
    final CountDownLatch closeLatch = new CountDownLatch(1);
    ConnectorFactory factory = new ConnectorFactory() {

        @Override
        public Connector createConnector(Server server) {
            return new LocalConnector(server) {

                @Override
                public void doStart() throws Exception {
                    openLatch.countDown();
                    super.doStart();
                }

                @Override
                public void doStop() throws Exception {
                    closeLatch.countDown();
                    super.doStop();
                }
            };
        }
    };
    ServiceRegistration reg = m_context.registerService(ConnectorFactory.class.getName(), factory, null);
    // Should be opened automatically when picked up by the Jetty implementation...
    assertTrue("Felix HTTP Jetty did not open the Connection or pick up the registered ConnectionFactory", openLatch.await(5, TimeUnit.SECONDS));
    // Should close our connection...
    reg.unregister();
    assertTrue("Felix HTTP Jetty did not close the Connection", closeLatch.await(5, TimeUnit.SECONDS));
}
Also used : ConnectorFactory(org.apache.felix.http.jetty.ConnectorFactory) Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 69 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class JettyServiceTest method setUp.

@Before
public void setUp() throws Exception {
    // Setup Mocks
    mockBundleContext = mock(BundleContext.class);
    mockBundle = mock(Bundle.class);
    // Setup Behaviors
    when(mockBundleContext.getBundle()).thenReturn(mockBundle);
    final org.osgi.framework.Filter f = mock(org.osgi.framework.Filter.class);
    when(f.toString()).thenReturn("(prop=*)");
    when(mockBundleContext.createFilter(anyString())).thenReturn(f);
    when(mockBundle.getSymbolicName()).thenReturn("main");
    when(mockBundle.getVersion()).thenReturn(new Version("1.0.0"));
    when(mockBundle.getHeaders()).thenReturn(new Hashtable<String, String>());
    final ServiceReference ref = mock(ServiceReference.class);
    when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
    final ServiceRegistration reg = mock(ServiceRegistration.class);
    when(reg.getReference()).thenReturn(ref);
    when(mockBundleContext.registerService((Class<ServletContextHelper>) Matchers.isNotNull(), (ServiceFactory<ServletContextHelper>) Matchers.any(ServiceFactory.class), Matchers.any(Dictionary.class))).thenReturn(reg);
    when(mockBundleContext.registerService(Matchers.<String[]>any(), Matchers.any(ServiceFactory.class), Matchers.any(Dictionary.class))).thenReturn(reg);
    when(mockBundleContext.registerService((Class<HttpServiceRuntime>) Matchers.isNotNull(), Matchers.any(HttpServiceRuntime.class), Matchers.any(Dictionary.class))).thenReturn(reg);
    httpServiceController = new HttpServiceController(mockBundleContext);
    jettyService = new JettyService(mockBundleContext, httpServiceController);
    jettyService.start();
}
Also used : Dictionary(java.util.Dictionary) ServiceFactory(org.osgi.framework.ServiceFactory) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) HttpServiceController(org.apache.felix.http.base.internal.HttpServiceController) Bundle(org.osgi.framework.Bundle) Matchers.anyString(org.mockito.Matchers.anyString) ServiceReference(org.osgi.framework.ServiceReference) ServletContextHelper(org.osgi.service.http.context.ServletContextHelper) Version(org.osgi.framework.Version) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Before(org.junit.Before)

Example 70 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class ServiceRegistryTest method testRegisterFindHookService.

public void testRegisterFindHookService() {
    MockControl control = MockControl.createNiceControl(Bundle.class);
    Bundle b = (Bundle) control.getMock();
    control.replay();
    MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
    BundleContext c = (BundleContext) controlContext.getMock();
    controlContext.expectAndReturn(c.getBundle(), b);
    controlContext.replay();
    ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
    FindHook hook = new FindHook() {

        @Override
        public void find(BundleContext context, String name, String filter, boolean allServices, Collection references) {
        }
    };
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { FindHook.class.getName() }, hook, new Hashtable());
    assertEquals(1, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(FindHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    sr.unregisterService(b, reg);
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
Also used : MockControl(org.easymock.MockControl) FindHook(org.osgi.framework.hooks.service.FindHook) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

ServiceRegistration (org.osgi.framework.ServiceRegistration)365 Test (org.junit.Test)106 Hashtable (java.util.Hashtable)94 Bundle (org.osgi.framework.Bundle)64 BundleContext (org.osgi.framework.BundleContext)64 Dictionary (java.util.Dictionary)62 Ensure (org.apache.felix.dm.itest.util.Ensure)42 Properties (java.util.Properties)39 ServiceReference (org.osgi.framework.ServiceReference)38 ArrayList (java.util.ArrayList)34 HashMap (java.util.HashMap)28 ServiceFactory (org.osgi.framework.ServiceFactory)21 IOException (java.io.IOException)16 Map (java.util.Map)15 List (java.util.List)12 ServiceEvent (org.osgi.framework.ServiceEvent)12 HashSet (java.util.HashSet)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 Collection (java.util.Collection)9