org.glassfish.jersey
Class ServiceLocatorProvider

java.lang.Object
  extended by org.glassfish.jersey.ServiceLocatorProvider

public class ServiceLocatorProvider
extends Object

Utility class with static methods that extract service locator from various JAX-RS components. This class can be used when no injection is possible by Context or Inject annotation due to character of provider but there is a need to get any service from ServiceLocator.

Injections are not possible for example when a provider is registered as an instance on the client. In this case the runtime will not inject the instance as this instance might be used in other client runtimes too.

Example. This is the class using a standard injection:

     public static class MyWriterInterceptor implements WriterInterceptor {
         @Inject
         MyInjectedService service;

         @Override
         public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
             Something something = service.getSomething();
             ...
         }

     }
 

If this injection is not possible then this construct can be used:

     public static class MyWriterInterceptor implements WriterInterceptor {

         @Override
         public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
             final ServiceLocator serviceLocator = ServiceLocatorProvider.getServiceLocator(context);
             final MyInjectedService service = serviceLocator.getService(MyInjectedService.class);
             Something something = service.getSomething();
             ...
         }
     }
 

Note, that this injection support is intended mostly for injection of custom user types. JAX-RS types are usually available without need for injections in method parameters. However, when injection of custom type is needed it is preferred to use standard injections if it is possible rather than injection support provided by this class.

User returned ServiceLocator only for purpose of getting services (do not change the state of the locator).

Since:
2.6
Author:
Miroslav Fuksa (miroslav.fuksa at oracle.com)

Constructor Summary
ServiceLocatorProvider()
           
 
Method Summary
static org.glassfish.hk2.api.ServiceLocator getServiceLocator(javax.ws.rs.core.FeatureContext featureContext)
          Extract and return service locator from featureContext.
static org.glassfish.hk2.api.ServiceLocator getServiceLocator(javax.ws.rs.ext.ReaderInterceptorContext readerInterceptorContext)
          Extract and return service locator from readerInterceptorContext.
static org.glassfish.hk2.api.ServiceLocator getServiceLocator(javax.ws.rs.ext.WriterInterceptorContext writerInterceptorContext)
          Extract and return service locator from writerInterceptorContext.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServiceLocatorProvider

public ServiceLocatorProvider()
Method Detail

getServiceLocator

public static org.glassfish.hk2.api.ServiceLocator getServiceLocator(javax.ws.rs.ext.WriterInterceptorContext writerInterceptorContext)
Extract and return service locator from writerInterceptorContext. The method can be used to inject custom types into a WriterInterceptor.

Parameters:
writerInterceptorContext - Writer interceptor context.
Returns:
Service locator.
Throws:
IllegalArgumentException - when writerInterceptorContext is not a default Jersey implementation provided by Jersey as argument in the WriterInterceptor.aroundWriteTo(javax.ws.rs.ext.WriterInterceptorContext) method.

getServiceLocator

public static org.glassfish.hk2.api.ServiceLocator getServiceLocator(javax.ws.rs.ext.ReaderInterceptorContext readerInterceptorContext)
Extract and return service locator from readerInterceptorContext. The method can be used to inject custom types into a ReaderInterceptor.

Parameters:
readerInterceptorContext - Reader interceptor context.
Returns:
Service locator.
Throws:
IllegalArgumentException - when readerInterceptorContext is not a default Jersey implementation provided by Jersey as argument in the ReaderInterceptor.aroundReadFrom(javax.ws.rs.ext.ReaderInterceptorContext) method.

getServiceLocator

public static org.glassfish.hk2.api.ServiceLocator getServiceLocator(javax.ws.rs.core.FeatureContext featureContext)
Extract and return service locator from featureContext. The method can be used to inject custom types into a Feature.

Note that features are utilized during initialization phase when not all providers are registered yet. It is undefined which injections are already available in this phase.

Parameters:
featureContext - Feature context.
Returns:
Service locator.
Throws:
IllegalArgumentException - when writerInterceptorContext is not a default Jersey instance provided by Jersey in Feature.configure(javax.ws.rs.core.FeatureContext) method.


Copyright © 2007-2014, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.