OSGIServiceContext implements ServiceContext { @Override public Collection<URL> getResources(String resource){ LOG.finest("TAMAYA Loading resources: " + resource); List<URL> result = new ArrayList<>(); URL url = osgiServiceLoader.getBundleContext().getBundle() .getEntry(resource); if(url != null) { LOG.finest("TAMAYA Resource: " + resource + " found in unregistered bundle " + osgiServiceLoader.getBundleContext().getBundle().getSymbolicName()); result.add(url); } for(Bundle bundle: osgiServiceLoader.getResourceBundles()) { url = bundle.getEntry(resource); if (url != null && !result.contains(url)) { LOG.finest("TAMAYA Resource: " + resource + " found in registered bundle " + bundle.getSymbolicName()); result.add(url); } } for(Bundle bundle: osgiServiceLoader.getBundleContext().getBundles()) { url = bundle.getEntry(resource); if (url != null && !result.contains(url)) { LOG.finest("TAMAYA Resource: " + resource + " found in unregistered bundle " + bundle.getSymbolicName()); result.add(url); } } return result; } OSGIServiceContext(OSGIServiceLoader osgiServiceLoader); boolean isInitialized(); @Override ClassLoader getClassLoader(); @Override void init(ClassLoader classLoader); @Override int ordinal(); @Override T getService(Class<T> serviceType, Supplier<T> supplier); @Override T create(Class<T> serviceType, Supplier<T> supplier); @Override List<T> getServices(Class<T> serviceType, Supplier<List<T>> supplier); @Override Collection<URL> getResources(String resource); @Override URL getResource(String resource); @Override T register(Class<T> serviceType, T instance, boolean force); @Override List<T> register(Class<T> serviceType, List<T> instances, boolean force); @Override void reset();  }
@Test public void testGetResources() throws IOException { MockBundleContext mockBundleContext = new MockBundleContext(); OSGIServiceLoader loader = new OSGIServiceLoader(mockBundleContext); MockBundle startedBundle = new MockBundle(); startedBundle.setState(Bundle.ACTIVE); startedBundle.setBundleId(1); startedBundle.setBundleContext(mockBundleContext); mockBundleContext.installBundle(startedBundle); OSGIServiceContext instance = new OSGIServiceContext(loader); Collection<URL> resources = instance.getResources("dummy"); assertThat(resources).isNotNull(); URL resource = (URL)resources.iterator().next(); assertThat(resource.toString()).contains("mockbundle.service"); assertThat(resources.size()).isEqualTo(1); }