users@jersey.java.net

[Jersey] Re: Jersey and resources managed by Guice in ModelProcesor

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Tue, 12 Nov 2013 13:52:33 +0100

Please send your question to HK2 forum - they may be better suited to answer & help you.

Thanks,
Marek

On 08 Nov 2013, at 18:06, Cristian Rinaldi <csrinaldi_at_gmail.com> wrote:

> I am developing an application that dynamically load JAX resources in a OSGI enviroment. These resources are managed by Guice , in each Bundle and these are loaded into a JAX application that runs on a core bundle.
>
> ***********************************************************************************
> ResourceConfig cfg = new ResourceConfig(context.getConfiguration());
> for (Provider<?> r : kb.getResources()) {
> if (!cfg.isRegistered(r.get())) {
> cfg.registerInstances(r.get());
> }
> }
>
> context.reload(cfg);
>
> ***********************************************************************************
>
> where context is a Jersey ServletContext managed by Guice.
>
> All this works perfectly!!!!!
>
> The problem occurs because we want manipulate the path when the resources are loaded. Then, we create our ModelProcesor.
>
> ************************************************************************************************************
> @Override
> public ResourceModel processResourceModel(ResourceModel resourceModel, Configuration configuration) {
> logger.info(actualKrundle.getName());
> ResourceModel.Builder newResourceModelBuilder = new ResourceModel.Builder(false);
>
> for (final Provider<?> r : actualKrundle.getResources()) {
> String name = r.get().getClass().getCanonicalName();
> List<Resource> resources = resourceModel.getResources();
> for (final Resource resource : resources) {
> if (resource.getName().endsWith(name)){
> final Resource.Builder resourceBuilder = Resource.builder(resource);
> //add the bundle name to resource path
> resourceBuilder.path(actualKrundle.getName()+"/"+resource.getPath());
>
> Resource r1 = resourceBuilder.build();
> newResourceModelBuilder.addResource(r1);
> break;
> }
> }
> }
> return newResourceModelBuilder.build();
> }
>
> ************************************************************************************************************
>
> The error is that all dependencies injected into the resource can not be handled. By example: if resource has inject a Date (@Inject Date date) the error is the follow:
>
> ************************************************************************************************************
> A MultiException has 1 exceptions. They are:
> 1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=Date,parent=ResourceFinder,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,23999364)
>
> at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)[261:org.glassfish.hk2.locator:2.2.0.b21]
> at org.jvnet.hk2.internal.Utilities.justInject(Utilities.java:803)[261:org.glassfish.hk2.locator:2.2.0.b21]
> at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:832)[261:org.glassfish.hk2.locator:2.2.0.b21]
> at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:822)[261:org.glassfish.hk2.locator:2.2.0.b21]
>
> .... .... ....
> ************************************************************************************************************
>
> I think the error is that the new instance (Resource r1) is handled by H2K and not by Guice and can not satisfy your dependencies.
>
> We tried to use in OSGI enviroment the guice-bridge artifact, but this gives an error: not found an implementation of ServiceLocator.
>
> But all dependencies are in the classpath:
>
> ************************************************************************************************************
> karaf_at_root> la | grep HK2
> [ 260] [Active ] [ ] [ 30] HK2 API module (2.2.0.b21)
> [ 262] [Active ] [ ] [ 30] HK2 Implementation Utilities (2.2.0.b21)
> [ 283] [Active ] [ ] [ 30] HK2 Guice Bridge (2.1.96)
> karaf_at_root> la | grep ServiceLo
> [ 261] [Active ] [ ] [ 30] ServiceLocator Default Implementation (2.2.0.b21)
> karaf_at_root>
> ************************************************************************************************************
>
> So, any idea how to change the path of resource managed by guice, if the solution is to use guice-bridge, which is the best way to configure it?
>
> I'm using jersey 2.4.
>
> Thanks
> A.U.S Cristian Rinaldi
> Logikas - Conectando Ideas
> www.logikas.com