use of org.ops4j.pax.web.resources.api.OsgiResourceLocator in project org.ops4j.pax.web by ops4j.
the class WebresourcesExtender method start.
@Override
public void start(BundleContext context) throws Exception {
this.bundleContext = context;
IndexedOsgiResourceLocator indexedRegistryService = new IndexedOsgiResourceLocator(context);
trackerResourceLocator = new ServiceTracker<>(context, OsgiResourceLocator.class.getName(), new ServiceTrackerCustomizer<OsgiResourceLocator, OsgiResourceLocator>() {
@Override
public OsgiResourceLocator addingService(ServiceReference<OsgiResourceLocator> reference) {
OsgiResourceLocator service = context.getService(reference);
if (service != null) {
osgiResourceLocatorServices.add(service);
logger.info("OsgiResourceLocator-Service available from bundle '{}'.", reference.getBundle().getSymbolicName());
fullBundleScan(service);
return service;
}
return null;
}
@Override
public void modifiedService(ServiceReference<OsgiResourceLocator> reference, OsgiResourceLocator service) {
// not interesting
}
@Override
public void removedService(ServiceReference<OsgiResourceLocator> reference, OsgiResourceLocator service) {
logger.info("OsgiResourceLocator from bundle '{}' removed.", reference.getBundle().getSymbolicName());
osgiResourceLocatorServices.remove(service);
context.ungetService(reference);
}
});
trackerResourceLocator.open();
// register service
Dictionary<String, Object> props = new Hashtable<>(1);
props.put(Constants.SERVICE_RANKING, -1);
context.registerService(OsgiResourceLocator.class, indexedRegistryService, props);
context.addBundleListener(WebresourcesExtender.this);
}
use of org.ops4j.pax.web.resources.api.OsgiResourceLocator in project org.ops4j.pax.web by ops4j.
the class OsgiResourceHandler method getServiceAndExecute.
/**
* Gets a {@link OsgiResourceLocator}-service, applies the given function,
* and ungets the service.
*
* @param function the function to apply against the {@link OsgiResourceLocator}
* @return a {@link Resource}, {@link ViewResource} depending on the
* functions or {@code null}.
*/
private <T> T getServiceAndExecute(Function<OsgiResourceLocator, T> function) {
// hook into OSGi-Framework
final BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
// get-service, execute function, and unget-service
ServiceReference<OsgiResourceLocator> serviceRef = context.getServiceReference(OsgiResourceLocator.class);
T resourceQueryResult = null;
if (serviceRef != null) {
final OsgiResourceLocator resourceLocatorService = context.getService(serviceRef);
if (resourceLocatorService != null) {
resourceQueryResult = function.apply(resourceLocatorService);
}
context.ungetService(serviceRef);
}
return resourceQueryResult;
}
use of org.ops4j.pax.web.resources.api.OsgiResourceLocator in project org.ops4j.pax.web by ops4j.
the class AbstractWarJsfResourcehandlerIntegrationTest method testResourceUnavailble.
/**
* After a JSF thread received a resource, the bundle with the resource might be uninstalled
* anyway. This can happen before the actual bytes are served.
* <p>
* <ol>
* <li>createResource</li>
* <li>resourcebundle uninstalled</li>
* <li>resource.getInputStream</li>
* </ol>
* <p>
* According to the spec, IOException is the only one catched later on.
*/
@Test(expected = IOException.class)
public void testResourceUnavailble() throws Exception {
ServiceReference<OsgiResourceLocator> sr = bundleContext.getServiceReference(OsgiResourceLocator.class);
OsgiResourceLocator resourceLocator = bundleContext.getService(sr);
ResourceInfo resourceInfo = resourceLocator.locateResource("default/2_0/images/iceland.jpg");
Resource resource = new OsgiResource(resourceInfo.getUrl(), null, "iceland.jpg", null, "default", "2_0", resourceInfo.getLastModified());
// uninstall bundle
Arrays.stream(bundleContext.getBundles()).filter(bundle -> bundle.getSymbolicName().equals("jsf-resourcehandler-resourcebundle")).findFirst().orElseThrow(() -> new AssertionError("Bundle 'jsf-resourcehandler-resourcebundle' not found")).uninstall();
// to fast for tests, resource isn't fully gone yet
Thread.sleep(1000);
try {
resource.getInputStream();
fail("IOException expected due to missing resource!");
} finally {
bundleContext.ungetService(sr);
}
}
use of org.ops4j.pax.web.resources.api.OsgiResourceLocator in project org.ops4j.pax.web by ops4j.
the class AbstractWarJsfResourcehandlerIntegrationTest method testServiceOverride.
/**
* The default implementation {@link IndexedOsgiResourceLocator} is
* registered with {@link Constants#SERVICE_RANKING} of -1, so when
* registering a new implementation, this new class must be served.
*/
@Test
public void testServiceOverride() throws Exception {
OsgiResourceLocatorForTest expectedService = new OsgiResourceLocatorForTest();
bundleContext.registerService(OsgiResourceLocator.class, new OsgiResourceLocatorForTest(), null);
ServiceReference<OsgiResourceLocator> ref = bundleContext.getServiceReference(OsgiResourceLocator.class);
if (ref != null) {
OsgiResourceLocator service = bundleContext.getService(ref);
if (service != null) {
assertThat("'OsgiResourceLocatorForTest' must be found due to higher service-ranking!", service.getClass().getName(), serviceName -> expectedService.getClass().getName().equals(serviceName));
} else {
fail("Service could not be retrieved");
}
} else {
fail("Service-Reference could not be retrieved");
}
}
Aggregations