jersey
  1. jersey
  2. JERSEY-2358

Jersey HK2 injections using @Inject do not work for CDI beans on Glassfish 4.0.1

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: 2.5.1
    • Fix Version/s: 2.6
    • Component/s: containers
    • Labels:
      None
    • Environment:

      Glassfish 4.0.1 night build (glassfish-4.0.1-b04-01_22_2014-ml.zip), Jersey 2.5.1

      Description

      Consider these classes:

      Resource class:

      @Path("helloworld")
      public class HelloWorldResource {
          @Inject
          private MyInjection myInjection;
      
          @GET
          @Produces("text/plain")
          public String getHello() {
              return "Hello World! This is my injection: " + myInjection.getName();
          }
      }
      

      MyInjection

      public class MyInjection {
          private final String name;
      
          public MyInjection(String name) {
              this.name = name;
          }
      
          public String getName() {
              return name;
          }
      }
      

      and Application class:

      @ApplicationPath("/")
      public class MyApplication extends Application {
          @Override
          public Set<Class<?>> getClasses() {
              final Set<Class<?>> classes = new HashSet<Class<?>>();
              classes.add(HelloWorldResource.class);
              return classes;
          }
      
          @Override
          public Set<Object> getSingletons() {
              final HashSet<Object> singletons = new HashSet<Object>();
              singletons.add(new AbstractBinder() {
                  @Override
                  protected void configure() {
                      bind(new MyInjection("hello")).to(MyInjection.class);
                  }
              });
              return singletons;
          }
      
      }
      

      When this is part of webapp which has in WEB-INF file beans.xml, then injection into HelloWorldResource does not work and the exception is thrown during the deployment.

      If @Inject is replaced by @Context then the injection works well. Also if beans.xml is removed the @Inject works fine.

      When beans.xml is present in the war then all beans are considered as CDI beans. Jersey CDI integration is trying to inject all @Inject fields but it fails as it cannot inject objects bound into HK2.

      This is the stack trace of the exception thrown during the deployment to GF:

      [2014-01-22T19:23:32.689+0100] [glassfish 4.0] [SEVERE] [] [javax.enterprise.web] [tid: _ThreadID=41 _ThreadName=admin-listener(3)] [timeMillis: 1390415012689] [levelValue: 1000] [[
        WebModule[/helloworld-webapp-c]Servlet /helloworld-webapp-c threw load() exception
      org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [MyInjection] with qualifiers [@Default] at injection point [[BackedAnnotatedField] @Inject private org.glassfish.jersey.examples.helloworld.webapp.HelloWorldResource.myInjectio
      n]
              at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:405)
              at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:327)
              at org.jboss.weld.bootstrap.Validator.validateProducer(Validator.java:433)
              at org.jboss.weld.injection.producer.InjectionTargetService.validateProducer(InjectionTargetService.java:35)
              at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:88)
              at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:77)
              at org.jboss.weld.manager.BeanManagerImpl.createInjectionTarget(BeanManagerImpl.java:1061)
              at org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider$CdiFactory$2.<init>(CdiComponentProvider.java:213)
              at org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider$CdiFactory.<init>(CdiComponentProvider.java:192)
              at org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider.bind(CdiComponentProvider.java:340)
              at org.glassfish.jersey.server.ApplicationHandler.bindWithComponentProvider(ApplicationHandler.java:874)
              at org.glassfish.jersey.server.ApplicationHandler.bindProvidersAndResources(ApplicationHandler.java:804)
              at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:414)
              at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:162)
              at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:304)
              at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
              at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
              at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
              at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
              at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
              at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:301)
              at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
              at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:169)
              at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:359)
              at javax.servlet.GenericServlet.init(GenericServlet.java:244)
              at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1583)
              at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1382)
              at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5704)
              at org.apache.catalina.core.StandardContext.start(StandardContext.java:5946)
              at com.sun.enterprise.web.WebModule.start(WebModule.java:691)
              at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1041)
              at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:1024)
              at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
              at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2286)
              at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1932)
              at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
              at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
              at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:291)
              at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:352)
              at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:500)
              at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
              at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
              at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
              at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
              at java.security.AccessController.doPrivileged(Native Method)
              at javax.security.auth.Subject.doAs(Subject.java:356)
              at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
              at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
              at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
              at java.security.AccessController.doPrivileged(Native Method)
              at javax.security.auth.Subject.doAs(Subject.java:356)
              at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
              at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
              at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
              at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
              at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
              at org.glassfish.admin.rest.resources.admin.CommandResource.executeCommand(CommandResource.java:404)
              at org.glassfish.admin.rest.resources.admin.CommandResource.execCommandSimpInMultOut(CommandResource.java:234)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
              at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151)
              at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
              at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)
              at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
              at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:402)
              at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:349)
              at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106)
              at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259)
              at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
              at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
              at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
              at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
              at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
              at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
              at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
              at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010)
              at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:363)
              at org.glassfish.admin.rest.adapter.JerseyContainerCommandService$3.service(JerseyContainerCommandService.java:173)
              at org.glassfish.admin.rest.adapter.RestAdapter.service(RestAdapter.java:179)
              at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
              at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
              at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:199)
              at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:174)
              at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:212)
              at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
              at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
              at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
              at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
              at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
              at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
              at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:547)
              at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
              at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
              at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
              at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
              at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
              at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
              at java.lang.Thread.run(Thread.java:724)
      ]]
      

        Issue Links

          Activity

          Hide
          Miroslav Fuksa added a comment -

          This JERSEY-2358 issue was created based on not working OAuth1 support in Glassfish. See GLASSFISH-20953. OAuth support contains internal Resource classes (e.g. RequestTokenResource) which needs to Inject for example OAuth1Provider using @Inject. When a deployed application is a CDI app (contains beans.xml) then injections into these resources do not work.

          Show
          Miroslav Fuksa added a comment - This JERSEY-2358 issue was created based on not working OAuth1 support in Glassfish. See GLASSFISH-20953 . OAuth support contains internal Resource classes (e.g. RequestTokenResource) which needs to Inject for example OAuth1Provider using @Inject. When a deployed application is a CDI app (contains beans.xml) then injections into these resources do not work.
          Hide
          jwelzel added a comment - - edited

          I'm still seeing this same behavior when trying to use the OAuth1 Server in my Jersey app with the latest GF (4.0.1 nightly from 03/24) and Jersey (2.7.0) versions. Was this part left out of the fix?

          Show
          jwelzel added a comment - - edited I'm still seeing this same behavior when trying to use the OAuth1 Server in my Jersey app with the latest GF (4.0.1 nightly from 03/24) and Jersey (2.7.0) versions. Was this part left out of the fix?
          Hide
          Jakub Podlesak added a comment - - edited

          The fix is there, but because of a temporary workaround used in GF to cover another bug,
          you would need to either

          Show
          Jakub Podlesak added a comment - - edited The fix is there, but because of a temporary workaround used in GF to cover another bug, you would need to either remove jersey-gf-cdi-ban-custom-hk2-binding.jar from glassfish/modules or allow OAuth injection in an application provided Hk2CustomBoundTypesProvider (see https://jersey.java.net/documentation/latest/deployment.html#deployment.appservers ) or try a newer GF containing Jersey 2.8.

            People

            • Assignee:
              Jakub Podlesak
              Reporter:
              Miroslav Fuksa
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 1 day, 7 hours
                1d 7h