Hi all

The ContextHelper utility class [1] contains the following Method

    public static <T> T getServiceFromContext(Class<T> clazz,
ServletContext context) {
        BundleContext bundleContext = (BundleContext)
context.getAttribute(BundleContext.class.getName());
        ServiceReference reference =
bundleContext.getServiceReference(clazz.getName());
        return (T) bundleContext.getService(reference);
    }

This is typically called by JAX-RS Providers and Resources to lookup
required OSGI services via the servlet context. Such resources
(typically) do have a score that is limited to a single request (so
that implementers do not have to worry about threading issues).
Therefore one need to expect a lot of calls to this method.

I am not worried about the frequent lookup of services, but I am
worried about that with this there are no calls to the ungetService
[2] method.

If this is a problem I see two alternatives:
 (1)  add an additional method to WebFragment that allows to specify
the services this Fragment needs. Than it would be possible to add
such services to the Servlet context as soon as the WebFragment starts
and remove it when the fragment is removed. The above method would
than just return the service already present within the servlet
context
 (2) remove this method altogether and replace it with one that just
returns the BundleContext. Callers would than need to lookup, get and
unget the services themselves.

WDYT
Rupert

[1] 
http://svn.apache.org/repos/asf/incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/ContextHelper.java
[2] 
http://www.osgi.org/javadoc/r4v43/org/osgi/framework/BundleContext.html#ungetService(org.osgi.framework.ServiceReference)


-- 
| Rupert Westenthaler             [email protected]
| Bodenlehenstraße 11                             ++43-699-11108907
| A-5500 Bischofshofen

Reply via email to