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();
}
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();
}
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));
}
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();
}
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());
}
Aggregations