泽西和Guice管理的资源

时间:2013-11-08 14:06:26

标签: jersey osgi guice jersey-2.0 hk2

我正在开发一个在OSGI环境中动态加载JAX资源的应用程序。 这些资源由Guice在每个Bundle中管理,并且这些资源被加载到在核心包上运行的JAX应用程序中。

ResourceConfig cfg = new ResourceConfig(context.getConfiguration());
for (Provider<?> r : kb.getResources()) {
  if (!cfg.isRegistered(r.get())) {
    cfg.registerInstances(r.get());
  }
}

context.reload(cfg);

其中context是由Guice管理的Jersey ServletContext。

这一切都很完美。 出现此问题的原因是我们希望在加载资源时操纵路径。然后,我们创建了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();
}

错误是无法处理注入资源的所有依赖项。例如:如果资源已注入日期(@Inject Date date),则错误如下:

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]

.... .... ....

我认为错误是新实例(资源r1)由H2K而不是Guice处理,无法满足您的依赖关系。

我们尝试在OSGI环境中使用guice-bridge工件,但这会产生错误:找不到ServiceLocator的实现。

但所有依赖项都在类路径中:

karaf@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@root> la | grep ServiceLo
[ 261] [Active     ] [            ] [   30] ServiceLocator Default Implementation (2.2.0.b21)
karaf@root> 

那么,任何想法如何改变guice管理的资源路径,如果解决方案是使用guice-bridge,这是配置它的最佳方法?

我正在使用运动衫2.4。

由于

1 个答案:

答案 0 :(得分:0)

我找到的解决方案(如果不是最好的)是定义一个CustomServletContext并使用guice-bridge工件,在jersey问题跟踪器中引用问题HK2-121之后。   解决方案:

 public class KratosServletContainer extends ServletContainer {

     private Injector injector;

     @Inject
    KratosServletContainer(Injector injector, ResourceConfig configuration) {
         super(configuration);
         this.injector = injector;
     }

     @Override
     protected void init(WebConfig webConfig) throws ServletException {
         super.init(webConfig);

         ServiceLocator locator;
         try {
             Field webComponentField = getClass().getSuperclass()
                     .getDeclaredField("webComponent");

             webComponentField.setAccessible(true);
             WebComponent webComponent = (WebComponent) webComponentField.get(this);

             Field appHandlerField = webComponent.getClass().getDeclaredField("appHandler");
             appHandlerField.setAccessible(true);
             ApplicationHandler appHandler = (ApplicationHandler) appHandlerField.get(webComponent);
             locator = appHandler.getServiceLocator();

         } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | llegalAccessException e) {
             throw new RuntimeException(e);
         }

         GuiceBridge.getGuiceBridge().initializeGuiceBridge(
                 locator);

         GuiceIntoHK2Bridge guiceBridge = locator
                 .getService(GuiceIntoHK2Bridge.class);
         guiceBridge.bridgeGuiceInjector(injector);
     } 
}

以身作则:   我的资源看起来像这样:

@Path("resource") 
public class ResourceFinder {

     private static final Logger logger = Logger.getLogger(ResourceFinder.class.getName());

     private Date date;

     private final PersistenceManagerService pm;

     @Inject
     public ResourceFinder(Date date, PersistenceManagerService pm) {
         this.date = date;
         this.pm = pm;   
     }

     @GET
     @Produces(MediaType.APPLICATION_JSON)
     @Path("/suma/{value1}/{value2}")
     @PermitAll
     public String getSuma(@PathParam("value1") int value1,
             @PathParam("value2") int value2) {

         User user = new User();

         user.setName("cacho" + value1);



         pm.get().makePersistent(user);

         pm.get().flush();

         //date was injected by guice
         //pm was injected by guice

         return String.valueOf(value1 + value2 + "   "+pm.get()+" "+date);
     } 
}

方法:

     @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);
                    resourceBuilder.path(actualKrundle.getName()+"/"+resource.getPath());
                    Resource r1 = resourceBuilder.build();
                    newResourceModelBuilder.addResource(r1);
                    break;
                 }
             }
         }
         return newResourceModelBuilder.build();
     }

更改资源的路径,重新创建实例,所有内容都会保持注入...