static Xnio doGetOsgiService() { Bundle bundle = FrameworkUtil.getBundle(Xnio.class); BundleContext context = bundle.getBundleContext(); if (context == null) { throw new IllegalStateException("Bundle not started"); } ServiceReference<Xnio> sr = context.getServiceReference(Xnio.class); if (sr == null) { return null; } return context.getService(sr); }
private void sendAdminEvent() { ServiceReference eaRef = bc.getServiceReference(EventAdmin.class.getName()); if (eaRef != null) { EventAdmin ea = (EventAdmin) bc.getService(eaRef); ea.sendEvent(new Event("jersey/test/DEPLOYED", new HashMap<String, String>() { { put("context-path", "/"); } })); bc.ungetService(eaRef); } }
private void registerService(SomeInterface someInterface, String pluginID, String extension) throws InvalidSyntaxException { ServiceReference<SomeInterface> reference = mock(ServiceReference.class); when(reference.getBundle()).thenReturn(bundle); when(bundle.getSymbolicName()).thenReturn(pluginID); when(bundleContext.getService(reference)).thenReturn(someInterface); when(registry.getPlugin(pluginID)).thenReturn(buildExpectedDescriptor(pluginID)); String propertyFormat = String.format("(&(%s=%s)(%s=%s))", Constants.BUNDLE_SYMBOLICNAME, pluginID, Constants.BUNDLE_CATEGORY, extension); when(bundleContext.getServiceReferences(SomeInterface.class, propertyFormat)).thenReturn(Collections.singletonList(reference)); when(bundleContext.getServiceReferences(SomeInterface.class, null)).thenReturn(Collections.singletonList(reference)); }
public StartLevel getStartLevel() { // Get all Services implement StartLevel interface try { ServiceReference<?>[] references = this.context.getAllServiceReferences(StartLevel.class.getName(), null); for (ServiceReference<?> ref : references) { return (StartLevel) this.context.getService(ref); } return null; } catch (InvalidSyntaxException e) { LOGGER.warning("Cannot load StartLevel on DefaultProcessManager."); return null; } }
private IApplicationContext getApplicationContext() { Collection<ServiceReference<IApplicationContext>> references; try { references = context.getServiceReferences(IApplicationContext.class, "(eclipse.application.type=main.thread)"); //$NON-NLS-1$ } catch (InvalidSyntaxException e) { return null; } if (references == null || references.isEmpty()) return null; // assumes the application context is available as a service ServiceReference<IApplicationContext> firstRef = references.iterator().next(); IApplicationContext result = context.getService(firstRef); if (result != null) { context.ungetService(firstRef); return result; } return null; }
private void addRef(ServiceReference<T> ref) { T service = ctx.getService(ref); synchronized (refs) { if (!refs.containsKey(ref)) { refs.put(ref, service); return; } } ctx.ungetService(ref); }
public void start(final BundleContext context) throws Exception { startLevelServiceReference = context.getServiceReference(StartLevel.class.getName()); startLevel = (StartLevel) context.getService(startLevelServiceReference); for (final Bundle bundle : context.getBundles()) { final String value = bundle.getHeaders().get("Service-Component"); final ServiceReference[] srs = context.getServiceReferences(correspondingInterface, null); if (srs != null) { for (final ServiceReference sr : srs) {
/** * @throws Exception If failed. */ @Test public void testServiceExposedAndCallbacksInvoked() throws Exception { assertNotNull(ignite); assertEquals("testGrid", ignite.name()); TestOsgiFlags flags = (TestOsgiFlags) bundleCtx.getService( bundleCtx.getAllServiceReferences(TestOsgiFlags.class.getName(), null)[0]); assertNotNull(flags); assertEquals(Boolean.TRUE, flags.getOnBeforeStartInvoked()); assertEquals(Boolean.TRUE, flags.getOnAfterStartInvoked()); // The bundle is still not stopped, therefore these callbacks cannot be tested. assertNull(flags.getOnBeforeStopInvoked()); assertNull(flags.getOnAfterStopInvoked()); // No exceptions. assertNull(flags.getOnAfterStartThrowable()); assertNull(flags.getOnAfterStopThrowable()); }
public ProcessManager getProcessManager() { // Get all components implement ProcessManager interface try { ServiceReference<?>[] references = this.context.getAllServiceReferences(ProcessManager.class.getName(), null); for (ServiceReference<?> ref : references) { return (ProcessManager) this.context.getService(ref); } return null; } catch (InvalidSyntaxException e) { LOGGER.warning("Cannot load ProcessManager on ProcessManagerDiagnosticListener."); return null; } }
@Override public void configAdminUpdate(String pid, Map<String, String> data) { ServiceReference sref = bundleContext.getServiceReference(ConfigurationAdmin.class.getName()); if (sref == null) { throw new IllegalStateException("The configuration admin service cannot be found."); } try { ConfigurationAdmin ca = (ConfigurationAdmin) bundleContext.getService(sref); if (ca == null) { throw new IllegalStateException("The configuration admin service cannot be found."); } Configuration config = ca.getConfiguration(pid, null); config.update(new Hashtable<String, String>(data)); } catch (IOException ioe) { throw new RuntimeException(ioe); } finally { bundleContext.ungetService(sref); } } }
@Override public void start(BundleContext context) throws Exception { bc = context; eaRef = bc.getServiceReference(EventAdmin.class.getName()); if (eaRef != null) { setEa((EventAdmin) bc.getService(eaRef)); } }
private void registerServicesWithSameSymbolicName(String symbolicName, String extensionType, SomeInterface... someInterfaces) throws InvalidSyntaxException { ArrayList<ServiceReference<SomeInterface>> references = new ArrayList<>(); for (SomeInterface someInterface : someInterfaces) { ServiceReference<SomeInterface> reference = mock(ServiceReference.class); Bundle bundle = mock(Bundle.class); when(reference.getBundle()).thenReturn(bundle); when(bundle.getSymbolicName()).thenReturn(symbolicName); when(bundleContext.getService(reference)).thenReturn(someInterface); references.add(reference); } String propertyFormat = String.format("(&(%s=%s)(%s=%s))", Constants.BUNDLE_SYMBOLICNAME, symbolicName, Constants.BUNDLE_CATEGORY, extensionType); when(bundleContext.getServiceReferences(SomeInterface.class, propertyFormat)).thenReturn(references); when(registry.getPlugin(symbolicName)).thenReturn(buildExpectedDescriptor(symbolicName)); }
/** * Stops this bundle. */ @Override public void stop(BundleContext bundleContext) throws Exception { try { // Unregister this instance as an OSGi service. Collection<ServiceReference<ComponentImpl>> serviceReferences = bundleContext.getServiceReferences(ComponentImpl.class, null); if (serviceReferences != null) { for (ServiceReference<ComponentImpl> serviceReference : serviceReferences) { Object service = bundleContext.getService(serviceReference); if (service == this) bundleContext.ungetService(serviceReference); } } } finally { common.stop(bundleContext); } }
public JdbcConnector(final BundleContext bundleContext, final ServiceReference<?> reference) { this.datasource = (CommonDataSource)bundleContext.getService(reference); this.resources = new LinkedList<>(); this.resources.addFirst(() -> bundleContext.ungetService(reference)); }
public NotifiableFileMonitorService getFileMonitorService() { // Get all Services implement NotifiableFileMonitorService interface try { ServiceReference<?>[] references = this.context.getAllServiceReferences(NotifiableFileMonitorService.class.getName(), null); for (ServiceReference<?> ref : references) { return (NotifiableFileMonitorService) this.context.getService(ref); } return null; } catch (InvalidSyntaxException e) { LOGGER.warning("Cannot load NotifiableFileMonitorService on DefaultFileManager."); return null; } }
/** * Log error to a logging service (if available), otherwise log to std error * * @param pBundleContext bundle context to lookup LogService * @param pMessage message to log * @param pThrowable an exception to log */ public static void logError(BundleContext pBundleContext, String pMessage, Throwable pThrowable) { final ServiceReference lRef = pBundleContext.getServiceReference(LogService.class.getName()); if (lRef != null) { try { final LogService logService = (LogService) pBundleContext.getService(lRef); if (logService != null) { logService.log(LogService.LOG_ERROR, pMessage, pThrowable); return; } } finally { pBundleContext.ungetService(lRef); } } System.err.println("Jolokia-Error: " + pMessage + " : " + pThrowable.getMessage()); } }
@Override public void start(BundleContext bundleContext) throws Exception { Bundle bundle = bundleContext.getBundle(); pluginId = bundle.getSymbolicName(); pluginHealthService = bundleContext.getService(bundleContext.getServiceReference(PluginHealthService.class)); LoggingService loggingService = bundleContext.getService(bundleContext.getServiceReference(LoggingService.class)); Logger.initialize(loggingService); getImplementersAndRegister(bundleContext, bundle); reportErrorsToHealthService(); }
private void loadProvider(final BundleContext bundleContext, final BundleWiring bundleWiring) { final String filter = "(APIVersion>=2.6.0)"; try { final Collection<ServiceReference<Provider>> serviceReferences = bundleContext.getServiceReferences(Provider.class, filter); Provider maxProvider = null; for (final ServiceReference<Provider> serviceReference : serviceReferences) { final Provider provider = bundleContext.getService(serviceReference); if (maxProvider == null || provider.getPriority() > maxProvider.getPriority()) { maxProvider = provider; } } if (maxProvider != null) { ProviderUtil.addProvider(maxProvider); } } catch (final InvalidSyntaxException ex) { LOGGER.error("Invalid service filter: " + filter, ex); } final List<URL> urls = bundleWiring.findEntries("META-INF", "log4j-provider.properties", 0); for (final URL url : urls) { ProviderUtil.loadProvider(url, bundleWiring.getClassLoader()); } }
= bundleContext.getServiceReferences(ComponentImpl.class, null); : serviceReferences) Object service = bundleContext.getService(serviceReference); bundleContext.ungetService(serviceReference);
private JMSContext createContext(String name, String username, String password, int sessionMode) { ServiceReference<ConnectionFactory> sr = lookupConnectionFactory(name); ConnectionFactory cf = bundleContext.getService(sr); try { return cf.createContext(username, password, sessionMode); } finally { bundleContext.ungetService(sr); } }