6 Replies Latest reply on Aug 3, 2006 12:12 PM by Gavin King

    Help injecting the SessionContext

    Eric Ray Novice

      I need the SessionContext an this isn't working

      @In(required=false,value="sessionContext")
      private SessionContext sessionContext;

      I get the following error. In fact, I get this error everytime I use @In(required=false) regardless of what property it's annotating.

      javax.ejb.EJBException: java.lang.IllegalArgumentException: could not set field value: packages.sessionContext
       at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:69)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:197)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:81)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:188)
       at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:98)
       at $Proxy410.init(Unknown Source)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.seam.util.Reflections.invoke(Reflections.java:13)
       at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:32)
       at org.jboss.seam.Component.callComponentMethod(Component.java:1334)
       at org.jboss.seam.Component.getInstanceFromFactory(Component.java:1293)
       at org.jboss.seam.Component.getInstance(Component.java:1260)
       at org.jboss.seam.Component.getInstance(Component.java:1246)
       at org.jboss.seam.jsf.SeamVariableResolver.resolveVariable(SeamVariableResolver.java:44)
       at com.sun.facelets.el.LegacyELContext$LegacyELResolver.getValue(LegacyELContext.java:134)
       at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:44)
       at com.sun.el.parser.AstValue.getValue(AstValue.java:85)
       at com.sun.el.parser.AstEqual.getValue(AstEqual.java:20)
       at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:183)
       at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
       at com.sun.facelets.el.LegacyValueBinding.getValue(LegacyValueBinding.java:56)
       at javax.faces.component.UIComponentBase.isRendered(UIComponentBase.java:822)
       at com.sun.facelets.FaceletViewHandler.encodeRecursive(FaceletViewHandler.java:554)
       at com.sun.facelets.FaceletViewHandler.encodeRecursive(FaceletViewHandler.java:562)
       at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:457)
       at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:352)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:107)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
       at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
       at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
       at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
       at java.lang.Thread.run(Thread.java:595)
      Caused by: java.lang.IllegalArgumentException: could not set field value: packages.sessionContext
       at org.jboss.seam.Component.setFieldValue(Component.java:1214)
       at org.jboss.seam.Component.injectFields(Component.java:1024)
       at org.jboss.seam.Component.inject(Component.java:795)
       at org.jboss.seam.interceptors.BijectionInterceptor.bijectTargetComponent(BijectionInterceptor.java:30)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.seam.util.Reflections.invoke(Reflections.java:13)
       at org.jboss.seam.interceptors.Interceptor.aroundInvoke(Interceptor.java:90)
       at org.jboss.seam.interceptors.SeamInvocationContext.proceed(SeamInvocationContext.java:60)
       at org.jboss.seam.interceptors.ConversationInterceptor.endOrBeginLongRunningConversation(ConversationInterceptor.java:82)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.seam.util.Reflections.invoke(Reflections.java:13)
       at org.jboss.seam.interceptors.Interceptor.aroundInvoke(Interceptor.java:90)
       at org.jboss.seam.interceptors.SeamInvocationContext.proceed(SeamInvocationContext.java:60)
       at org.jboss.seam.interceptors.BusinessProcessInterceptor.manageBusinessProcessContext(BusinessProcessInterceptor.java:60)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.seam.util.Reflections.invoke(Reflections.java:13)
       at org.jboss.seam.interceptors.Interceptor.aroundInvoke(Interceptor.java:90)
       at org.jboss.seam.interceptors.SeamInvocationContext.proceed(SeamInvocationContext.java:60)
       at org.jboss.seam.interceptors.RemoveInterceptor.removeIfNecessary(RemoveInterceptor.java:39)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.seam.util.Reflections.invoke(Reflections.java:13)
       at org.jboss.seam.interceptors.Interceptor.aroundInvoke(Interceptor.java:90)
       at org.jboss.seam.interceptors.SeamInvocationContext.proceed(SeamInvocationContext.java:60)
       at org.jboss.seam.ejb.SeamInterceptor.aroundInvokeInContexts(SeamInterceptor.java:73)
       at org.jboss.seam.ejb.SeamInterceptor.aroundInvoke(SeamInterceptor.java:45)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:118)
       at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor.invoke(ExtendedPersistenceContextPropagationInterceptor.java:57)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
       ... 57 more
      Caused by: java.lang.IllegalArgumentException
       at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)
       at java.lang.reflect.Field.set(Field.java:656)
       at org.jboss.seam.Component.setFieldValue(Component.java:1210)
       ... 107 more
      


        • 1. Re: Help injecting the SessionContext
          hdslkf hklsdg Apprentice

          First: You don't need to write value="sessionContext" since the name of the annotated variable is the same, namely sessionContext. You only need to explicitely set the value when the variable's name ist different from the seam component name.

          Ok, now to your problem: The last Exception says that reflection cannot set the property. Maybe there's any security restriction in the Java Virtual Machine (I must confess, I'm not that sure why it's possible to access a private field without accessors at all, maybe someone familiar with that could say something about that). I'd try to implement a getter and setter (getSessionContext(), setSessionContext(...), don't forget to put them in the local interface if this is an EJB) in order to see if the exception still occurs then.

          • 2. Re: Help injecting the SessionContext
            hdslkf hklsdg Apprentice

            Stop, forget what I said. I just had a look at UnsafeObjectFieldAccessorImpl.java and its set-method:

            public void set(Object obj, Object value)
             throws IllegalArgumentException, IllegalAccessException
             {
             ensureObj(obj);
             if (isFinal) {
             throw new IllegalAccessException("Field is final");
             }
             if (value != null) {
             if (!field.getType().isAssignableFrom(value.getClass())) {
             throw new IllegalArgumentException();
             }
             }
             unsafe.putObject(obj, fieldOffset, value);
             }


            As you can see, the IllegalArgumentException (that is the root cause auf your exception) is thrown, if the class of a field (namely sessionContext here) doesn't fit the class of the object that should be injected.

            Or, to put in a nutshell:

            Whatever Seam tries to inject into private SessionContext sessionContext; is not compatible to class SessionContext! Try it like this:

            @In(required=false)
            private Object sessionContext;
            
            ...
            
            log.info("The class of the injected thing is: " + sessionContext.getClass());

            Then you will see what's injected and you can start solving the issue.

            • 3. Re: Help injecting the SessionContext
              hdslkf hklsdg Apprentice

              Forget ALL I said *ggg*.


              @In
              private Context sessionContext;
              


              (as shown on page 117 of the manual ;)).

              • 4. Re: Help injecting the SessionContext
                Eric Ray Novice

                Righto. Because SessionContext is probably package access only and I must use the Context Interface. I'll give it a shot.

                Thanks for the help.
                Eric

                • 5. Re: Help injecting the SessionContext
                  hdslkf hklsdg Apprentice

                  There is something I forgot to tell you: WebSessionContext is NOT serializable. So if you have injected it into your SFSB, and the SFSB gets passivated (i.e. serialized) after some idle time, the sessionContext gets lost. And when reactivating the SFSB and accessing sessionContext you will get:

                  java.lang.RuntimeException: org.jboss.serial.exception.SerializationException: Could not create instance of org.jboss.seam.contexts.WebSessionContext


                  So use:

                  private transient sessionContext;

                  (Didn't have the time to test it yet)

                  • 6. Re: Help injecting the SessionContext
                    Gavin King Master

                    Right, this should be mentioned in the Seam docs.

                    Perhaps we should even make Seam warn about it...