Refine search
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 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) {
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)); }
private void assertThatPluginWithThisExtensionClassLoadsSuccessfully(Class<?> extensionClass) throws IOException, InvalidSyntaxException { BundleContext installedBundleContext = bundleContext(installBundleWithClasses(extensionClass)); ServiceReference<?>[] references = installedBundleContext.getServiceReferences(GoPlugin.class.getName(), null); assertEquals("No service registered for GoPlugin class", 1, references.length); assertEquals("Symbolic Name property should be present", GO_TEST_DUMMY_SYMBOLIC_NAME, references[0].getProperty(Constants.BUNDLE_SYMBOLICNAME)); assertEquals(extensionClass.getName(), installedBundleContext.getService(references[0]).getClass().getName()); }
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()); } }
@Test public void shouldLoadAValidPluginWithMultipleExtensions_ImplementingDifferentExtensions() throws Exception { Bundle bundle = pluginOSGiFramework.loadPlugin(new GoPluginDescriptor(null, null, null, null, validMultipleExtensionPluginBundleDir, true)); assertThat(bundle.getState(), is(Bundle.ACTIVE)); BundleContext context = bundle.getBundleContext(); String taskExtensionFilter = String.format("(&(%s=%s)(%s=%s))", Constants.BUNDLE_SYMBOLICNAME, "valid-plugin-with-multiple-extensions", Constants.BUNDLE_CATEGORY, "task"); String analyticsExtensionFilter = String.format("(&(%s=%s)(%s=%s))", Constants.BUNDLE_SYMBOLICNAME, "valid-plugin-with-multiple-extensions", Constants.BUNDLE_CATEGORY, "analytics"); ServiceReference<?>[] taskExtensionServiceReferences = context.getServiceReferences(GoPlugin.class.getCanonicalName(), taskExtensionFilter); assertThat(taskExtensionServiceReferences.length, is(1)); assertThat(((GoPlugin) context.getService(taskExtensionServiceReferences[0])).pluginIdentifier().getExtension(), is("task")); ServiceReference<?>[] analyticsExtensionServiceReferences = context.getServiceReferences(GoPlugin.class.getCanonicalName(), analyticsExtensionFilter); assertThat(analyticsExtensionServiceReferences.length, is(1)); assertThat(((GoPlugin) context.getService(analyticsExtensionServiceReferences[0])).pluginIdentifier().getExtension(), is("analytics")); }
@Test public void shouldLoadAValidGoPluginOSGiBundleAndShouldBeDiscoverableThroughSymbolicNameFilter() throws Exception { Bundle bundle = pluginOSGiFramework.loadPlugin(new GoPluginDescriptor(null, null, null, null, descriptorBundleDir, true)); assertThat(bundle.getState(), is(Bundle.ACTIVE)); String filterBySymbolicName = String.format("(%s=%s)", Constants.BUNDLE_SYMBOLICNAME, "testplugin.descriptorValidator"); BundleContext context = bundle.getBundleContext(); ServiceReference<?>[] allServiceReferences = context.getServiceReferences(GoPlugin.class.getCanonicalName(), filterBySymbolicName); assertThat(allServiceReferences.length, is(1)); try { GoPlugin service = (GoPlugin) context.getService(allServiceReferences[0]); service.pluginIdentifier(); } catch (Exception e) { fail(String.format("pluginIdentifier should have been called. Exception: %s", e.getMessage())); } }
@Test public void shouldLoadAValidGoPluginOSGiBundle() throws Exception { Bundle bundle = pluginOSGiFramework.loadPlugin(new GoPluginDescriptor(null, null, null, null, descriptorBundleDir, true)); assertThat(bundle.getState(), is(Bundle.ACTIVE)); BundleContext context = bundle.getBundleContext(); ServiceReference<?>[] allServiceReferences = context.getServiceReferences(GoPlugin.class.getCanonicalName(), null); assertThat(allServiceReferences.length, is(1)); try { GoPlugin service = (GoPlugin) context.getService(allServiceReferences[0]); service.pluginIdentifier(); assertThat("@Load should have been called", getIntField(service, "loadCalled"), is(1)); } catch (Exception e) { fail(String.format("pluginIdentifier should have been called. Exception: %s", e.getMessage())); } }
@Test public void shouldRegisterOneInstanceForEachExtensionPointAnExtensionImplements() throws Exception { BundleContext installedBundledContext = bundleContext(installBundleWithClasses(TestGoPluginExtensionThatImplementsTwoExtensionPoints.class, DummyTestPlugin.class)); ServiceReference<?>[] references = installedBundledContext.getServiceReferences(GoPlugin.class.getName(), null); String[] services = toSortedServiceClassNames(installedBundledContext, references); assertEquals(Arrays.toString(services), 2, services.length); assertEquals(DummyTestPlugin.class.getName(), services[0]); assertEquals(TestGoPluginExtensionThatImplementsTwoExtensionPoints.class.getName(), services[1]); references = installedBundledContext.getServiceReferences(TestGoPluginExtensionPoint.class.getName(), null); assertEquals(1, references.length); assertEquals(TestGoPluginExtensionThatImplementsTwoExtensionPoints.class.getName(), installedBundledContext.getService(references[0]).getClass().getName()); Object testExtensionImplementation = getImplementationOfType(installedBundledContext, references, TestGoPluginExtensionThatImplementsTwoExtensionPoints.class); references = installedBundledContext.getServiceReferences(GoPlugin.class.getName(), null); assertEquals(2, references.length); Object testPluginImplementation = getImplementationOfType(installedBundledContext, references, TestGoPluginExtensionThatImplementsTwoExtensionPoints.class); assertSame(testExtensionImplementation, testPluginImplementation); }
@Test public void shouldUnloadALoadedPlugin() throws Exception { GoPluginDescriptor pluginDescriptor = new GoPluginDescriptor(null, null, null, null, descriptorBundleDir, true); Bundle bundle = pluginOSGiFramework.loadPlugin(pluginDescriptor); BundleContext context = bundle.getBundleContext(); ServiceReference<?>[] allServiceReferences = context.getServiceReferences(GoPlugin.class.getCanonicalName(), null); assertThat(allServiceReferences.length, is(1)); GoPlugin service = (GoPlugin) context.getService(allServiceReferences[0]); assertThat("@Load should have been called", getIntField(service, "loadCalled"), is(1)); pluginDescriptor.setBundle(bundle); pluginOSGiFramework.unloadPlugin(pluginDescriptor); assertThat(bundle.getState(), is(Bundle.UNINSTALLED)); assertThat("@UnLoad should have been called", getIntField(service, "unloadCalled"), is(1)); }
private Subsystem getSubsystem(long id) { try { for (ServiceReference<Subsystem> ref : bundleContext.getServiceReferences(Subsystem.class, "(subsystem.id=" + id + ")")) { Subsystem svc = bundleContext.getService(ref); if (svc != null) return svc; } } catch (InvalidSyntaxException e) { throw new RuntimeException(e); } throw new RuntimeException("Unable to find subsystem " + id); }
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; }
public Collection<PersistenceProvider> lookupProviders() { debug("Persistence class - lookupProviders"); Collection<PersistenceProvider> providers = new HashSet<PersistenceProvider>(); ServiceReference[] refs = null; try { refs = ctx.getServiceReferences(PersistenceProvider.class.getName(), null); } catch (InvalidSyntaxException invEx) {} // Can't happen since filter is null if (refs != null) { for (ServiceReference ref : refs) { providers.add((PersistenceProvider)ctx.getService(ref)); } } return providers; }
/** * 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); } }
/** * Actual check which delegate to one or more restrictor services if available. * * @param pCheck a function object for performing the actual check * @param args arguments passed through to the check * @return true if all checks return true */ private boolean checkRestrictorService(RestrictorCheck pCheck, Object ... args) { try { ServiceReference[] serviceRefs = bundleContext.getServiceReferences(Restrictor.class.getName(),null); if (serviceRefs != null) { boolean ret = true; boolean found = false; for (ServiceReference serviceRef : serviceRefs) { Restrictor restrictor = (Restrictor) bundleContext.getService(serviceRef); if (restrictor != null) { ret = ret && pCheck.check(restrictor,args); found = true; } } return found && ret; } else { return false; } } catch (InvalidSyntaxException e) { // Will not happen, since we dont use a filter here throw new IllegalArgumentException("Impossible exception (we don't use a filter for fetching the services)",e); } }
= bundleContext.getServiceReferences(ComponentImpl.class, null); : serviceReferences) Object service = bundleContext.getService(serviceReference);
public void list() throws InvalidSyntaxException { Map<Long, String> subsystems = new TreeMap<Long, String>(); for (ServiceReference<Subsystem> ref : bundleContext .getServiceReferences(Subsystem.class, null)) { Subsystem s = bundleContext.getService(ref); if (s != null) { subsystems.put( s.getSubsystemId(), String.format("%d\t%s\t%s %s", s.getSubsystemId(), s.getState(), s.getSymbolicName(), s.getVersion())); } } for (String entry : subsystems.values()) { LOGGER.log(Level.INFO, entry); } }
serviceReferences = bundleContext.getServiceReferences( TransformChainManipulator.class, null); bundleContext.getService(serviceReference); if (transformChainManipulators != null)
Collection<ServiceReference<Location>> refs = null; try { refs = PlatformActivator.getContext().getServiceReferences(Location.class, Location.CONFIGURATION_FILTER); if (refs == null || refs.isEmpty()) return true; Location configurationLocation = PlatformActivator.getContext().getService(serviceRef); if (configurationLocation == null) return true;
/** * Look up the Entity Manager Factory service based on the unit name. */ public EntityManagerFactory lookupEMF(String unitName) { debug("Persistence class - lookupEMF, punit=", unitName); String filter = null; ServiceReference[] refs = null; try { filter = "(osgi.unit.name="+ unitName +")"; refs = ctx.getServiceReferences(EntityManagerFactory.class.getName(), filter); } catch (InvalidSyntaxException isEx) { new PersistenceException("Implementation error - incorrect filter specified while looking up EMF", isEx); } if ((refs != null) && (refs.length != 0)) { debug("Persistence class - lookupEMF, found service ", unitName, " in registry"); // Take the first one return (EntityManagerFactory)ctx.getService(refs[0]); } // Didn't find any EMF service under the given name debug("Persistence class - lookupEMF, *** EMF service ", unitName, " not found in registry ***"); return null; }