Search in sources :

Example 1 with OsgiResourceLocator

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);
}
Also used : OsgiResourceLocator(org.ops4j.pax.web.resources.api.OsgiResourceLocator) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with OsgiResourceLocator

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;
}
Also used : IndexedOsgiResourceLocator(org.ops4j.pax.web.resources.extender.internal.IndexedOsgiResourceLocator) OsgiResourceLocator(org.ops4j.pax.web.resources.api.OsgiResourceLocator) BundleContext(org.osgi.framework.BundleContext)

Example 3 with OsgiResourceLocator

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);
    }
}
Also used : Arrays(java.util.Arrays) Constants(org.osgi.framework.Constants) URL(java.net.URL) ResourceQueryResult(org.ops4j.pax.web.resources.api.query.ResourceQueryResult) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) IndexedOsgiResourceLocator(org.ops4j.pax.web.resources.extender.internal.IndexedOsgiResourceLocator) StringUtils(org.apache.commons.lang3.StringUtils) ResourceInfo(org.ops4j.pax.web.resources.api.ResourceInfo) Resource(javax.faces.application.Resource) FacesContext(javax.faces.context.FacesContext) OsgiResource(org.ops4j.pax.web.resources.jsf.OsgiResource) HttpTestClientFactory(org.ops4j.pax.web.itest.base.client.HttpTestClientFactory) Assert.fail(org.junit.Assert.fail) Bundle(org.osgi.framework.Bundle) BundleMatchers(org.ops4j.pax.web.itest.base.assertion.BundleMatchers) ServiceReference(org.osgi.framework.ServiceReference) MalformedURLException(java.net.MalformedURLException) Collection(java.util.Collection) Test(org.junit.Test) IOException(java.io.IOException) Assert.assertThat(org.ops4j.pax.web.itest.base.assertion.Assert.assertThat) OsgiResourceLocator(org.ops4j.pax.web.resources.api.OsgiResourceLocator) ZoneId(java.time.ZoneId) CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) WaitCondition2(org.ops4j.pax.web.itest.base.WaitCondition2) ResourceQueryMatcher(org.ops4j.pax.web.resources.api.query.ResourceQueryMatcher) DateTimeFormatter(java.time.format.DateTimeFormatter) FrameworkUtil(org.osgi.framework.FrameworkUtil) ResourceInfo(org.ops4j.pax.web.resources.api.ResourceInfo) IndexedOsgiResourceLocator(org.ops4j.pax.web.resources.extender.internal.IndexedOsgiResourceLocator) OsgiResourceLocator(org.ops4j.pax.web.resources.api.OsgiResourceLocator) OsgiResource(org.ops4j.pax.web.resources.jsf.OsgiResource) Resource(javax.faces.application.Resource) OsgiResource(org.ops4j.pax.web.resources.jsf.OsgiResource) Test(org.junit.Test)

Example 4 with OsgiResourceLocator

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");
    }
}
Also used : IndexedOsgiResourceLocator(org.ops4j.pax.web.resources.extender.internal.IndexedOsgiResourceLocator) OsgiResourceLocator(org.ops4j.pax.web.resources.api.OsgiResourceLocator) Test(org.junit.Test)

Aggregations

OsgiResourceLocator (org.ops4j.pax.web.resources.api.OsgiResourceLocator)4 IndexedOsgiResourceLocator (org.ops4j.pax.web.resources.extender.internal.IndexedOsgiResourceLocator)3 Test (org.junit.Test)2 ServiceReference (org.osgi.framework.ServiceReference)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 LocalDateTime (java.time.LocalDateTime)1 ZoneId (java.time.ZoneId)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Hashtable (java.util.Hashtable)1 Resource (javax.faces.application.Resource)1 FacesContext (javax.faces.context.FacesContext)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Assert.fail (org.junit.Assert.fail)1 CoreOptions.mavenBundle (org.ops4j.pax.exam.CoreOptions.mavenBundle)1 WaitCondition2 (org.ops4j.pax.web.itest.base.WaitCondition2)1