14 Replies Latest reply on Aug 28, 2007 10:29 AM by pmuir

    When do I need a @Factory?

    hexxter

      Hi,

      when I try to inject some SessionBean with

      @In( create=true )
      MySessonBean mySessionBean

      a "could not set field value" Exception is thrown.

      The documentation states that "If the context variable is null, an instance of the component is instantiated by Seam.".

      If I create a
      @Factory( "mySessionBean" )
      public MySessionBean makeMySessionBean(){ return new MySessionBean(); }

      The whole stuff works.

      Am I doing something wrong? Or is the documentation wrong. When exactly do I need a @Factory?

      Thanks,

      Florian

        • 1. Re: When do I need a @Factory?
          stephen.friedrich

          Do you have an annotation @Name("mySessionBean") at the class MySessionBean?

          @In does not define the component, you'll need the @Name annotation at the component's class or else a @Factory somewhere else.

          See "3.8. Factory and manager components" in the seam reference.

          • 2. Re: When do I need a @Factory?
            scheintod

            yes I do.

            here is what i have:

            == Authenticator.java ===
            @Stateful
            @Scope(ScopeType.SESSION)
            @Name("authenticator")
            public class Authenticator
            implements Authenticator_L

            @In( create = true )
            MySessionBean mySessionBean;

            ...

            === MySessionBean.java ===
            @Stateful
            @Scope( ScopeType.APPLICATION )
            @Name( "mySessionBean" )
            public class MySessionBean
            implements MySessionBean_L
            {
            public String xyz(){ return "xyz"; }

            @Create
            public void create(){}

            @Destroy @Remove
            public void remove(){}
            }

            === Factories.java ===
            @Stateless
            @Scope( ScopeType.EVENT )
            @Name( "factories" )
            public class Factories
            implements Factories_L
            {

            @Logger Log log;

            @Factory( "mySessionBean" )
            public MySessionBean makeMySessionBean(){

            return new MySessionBean();
            }
            }


            With the Factories it works, without it dont.

            • 3. Re: When do I need a @Factory?
              stephen.friedrich

              No idea :-(
              From my understanding of seam this should work without the factory.

              Maybe the full exception stacktrace could help...

              Just a side note:
              @Stateless
              @Scope( ScopeType.EVENT )
              Does not make sense. Stateless beans always live in the stateless context.

              • 4. Re: When do I need a @Factory?
                christian.douven

                Also not sur, but shouldn't you inject mySessionBean like this:

                @EJB
                MySessionBean mySessionBean;

                ?

                • 5. Re: When do I need a @Factory?
                  scheintod

                  @stephen:

                  just what I thought... seems to be the way to learn seam/jsf/ejb: do what you think should be right and watch the exceptions pop up...

                  @christian:

                  ?

                  (what is @ejb? can't find it in the seam reference nor get google to come up with results (discards the @))

                  • 6. Re: When do I need a @Factory?
                    christian.douven

                    @EJB
                    MySessionBean mySessionBean_L;

                    i meant.

                    The @Factory annotated method is mostly used, when you need to initialize data. It is executed, when the refered variable doesn't have a value.

                    • 7. Re: When do I need a @Factory?
                      christian.douven

                      Well, im new to seam, too.

                      But in your application servers container, a session bean is usually injected via the @EJB annotation. Since it seems to live in the same container, you can use the local interface.

                      I don't know, whether seam has some exceptions here. Maybe you better wait until som professional deals with you post....

                      @EJB is an annattion defined by the EJB 3.0 specification.

                      • 8. Re: When do I need a @Factory?
                        iradix

                        It looks to me like the container is having a problem retrieving your session bean. When you @In(ject) a session bean Seam will retrieve it from JNDI. What you are doing in your factory method is simply instantiating the class, which will not have the Seam or EJB interceptors applied to it. Why don't you remove the factory and post the stack trace.

                        • 9. Re: When do I need a @Factory?
                          scheintod

                          And you shall receive it :)

                          This is without the factory method:

                          00:02:57,234 ERROR [org.jboss.seam.security.jaas.SeamLoginModule] Error invoking login method
                          javax.ejb.EJBTransactionRolledbackException: could not set field value: authenticator.mySessionBean
                          at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:87)
                          at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
                          at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)
                          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:83)
                          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
                          at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
                          at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:106)
                          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
                          at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
                          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:204)
                          at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:100)
                          at $Proxy467.create(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:597)
                          at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
                          at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
                          at org.jboss.seam.intercept.ClientSideInterceptor$1.proceed(ClientSideInterceptor.java:72)
                          at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
                          at org.jboss.seam.ejb.RemoveInterceptor.aroundInvoke(RemoveInterceptor.java:41)
                          at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                          at org.jboss.seam.core.SynchronizationInterceptor.aroundInvoke(SynchronizationInterceptor.java:32)
                          at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                          at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:106)
                          at org.jboss.seam.intercept.ClientSideInterceptor.invoke(ClientSideInterceptor.java:50)
                          at org.javassist.tmp.java.lang.Object_$$_javassist_23.create(Object_$$_javassist_23.java)
                          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:597)
                          at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
                          at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:124)
                          at org.jboss.seam.Component.callComponentMethod(Component.java:1957)
                          at org.jboss.seam.Component.callCreateMethod(Component.java:1880)
                          at org.jboss.seam.Component.newInstance(Component.java:1859)
                          at org.jboss.seam.Component.getInstance(Component.java:1756)
                          at org.jboss.seam.Component.getInstance(Component.java:1723)
                          at org.jboss.seam.Component.getInstance(Component.java:1717)
                          at org.jboss.seam.el.SeamELResolver.getValue(SeamELResolver.java:60)
                          at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
                          at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:64)
                          at org.jboss.el.parser.AstIdentifier.getValue(AstIdentifier.java:44)
                          at org.jboss.el.parser.AstValue.getTarget(AstValue.java:34)
                          at org.jboss.el.parser.AstValue.invoke(AstValue.java:95)
                          at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
                          at org.jboss.seam.core.Expressions$2.invoke(Expressions.java:174)
                          at org.jboss.seam.security.jaas.SeamLoginModule.login(SeamLoginModule.java:108)
                          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:597)
                          at javax.security.auth.login.LoginContext.invoke(LoginContext.java:769)
                          at javax.security.auth.login.LoginContext.access$000(LoginContext.java:186)
                          at javax.security.auth.login.LoginContext$5.run(LoginContext.java:706)
                          at java.security.AccessController.doPrivileged(Native Method)
                          at javax.security.auth.login.LoginContext.invokeCreatorPriv(LoginContext.java:703)
                          at javax.security.auth.login.LoginContext.login(LoginContext.java:575)
                          at org.jboss.seam.security.Identity.authenticate(Identity.java:331)
                          at org.jboss.seam.security.Identity.authenticate(Identity.java:321)
                          at org.jboss.seam.security.Identity.login(Identity.java:237)
                          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:597)
                          at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:325)
                          at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:338)
                          at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58)
                          at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
                          at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
                          at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
                          at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:77)
                          at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
                          at javax.faces.component.UICommand.broadcast(UICommand.java:383)
                          at org.ajax4jsf.framework.ajax.AjaxViewRoot.processEvents(AjaxViewRoot.java:180)
                          at org.ajax4jsf.framework.ajax.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:158)
                          at org.ajax4jsf.framework.ajax.AjaxViewRoot.processApplication(AjaxViewRoot.java:346)
                          at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
                          at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
                          at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
                          at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
                          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                          at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:87)
                          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                          at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:63)
                          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                          at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:46)
                          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                          at org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:127)
                          at org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter. java:277)
                          at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:40)
                          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                          at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:63)
                          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                          at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:140)
                          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                          at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
                          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                          at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
                          at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
                          at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
                          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
                          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
                          at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
                          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
                          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                          at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
                          at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                          at java.lang.Thread.run(Thread.java:619)
                          Caused by: java.lang.IllegalArgumentException: could not set field value: authenticator.mySessionBean
                          at org.jboss.seam.Component.setFieldValue(Component.java:1672)
                          at org.jboss.seam.Component.access$500(Component.java:134)
                          at org.jboss.seam.Component$BijectedField.set(Component.java:2408)
                          at org.jboss.seam.Component.injectAttributes(Component.java:1481)
                          at org.jboss.seam.Component.inject(Component.java:1302)
                          at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:45)
                          at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                          at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
                          at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                          at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:
                          at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:87)
                          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                          at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:63)
                          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                          at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:46)
                          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                          at org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:127)
                          at org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter.java:277)
                          at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:40)
                          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                          at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:63)
                          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                          at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:140)
                          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                          at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
                          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                          at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
                          at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
                          at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValv e.java:84)
                          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
                          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
                          at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
                          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
                          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                          at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
                          at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                          at java.lang.Thread.run(Thread.java:619)
                          Caused by: java.lang.IllegalArgumentException: could not set field value: authenticator.mySessionBean
                          at org.jboss.seam.Component.setFieldValue(Component.java:1672)
                          at org.jboss.seam.Component.access$500(Component.java:134)
                          at org.jboss.seam.Component$BijectedField.set(Component.java:2408)
                          at org.jboss.seam.Component.injectAttributes(Component.java:1481)
                          at org.jboss.seam.Component.inject(Component.java:1302)
                          at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:45)
                          at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                          at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
                          at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                          at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:

                          • 10. Re: When do I need a @Factory?
                            iradix

                            Your field is of the wrong type. When you inject a session bean Seam looks it up from JNDI and returns, not a MySessionBean object, but an actual session bean that implements the MySessionBean_L interface.

                            • 11. Re: When do I need a @Factory?
                              scheintod

                              Cool. Thanks. That works.

                              Would be worth mentioning in the documentation wouldn't it?

                              Thanks,

                              Florian

                              • 12. Re: When do I need a @Factory?
                                iradix

                                It's not really a Seam feature. It comes down to an understanding of EJBs in this case. If your bean had been a POJO, what you were trying to do would have worked.

                                • 13. Re: When do I need a @Factory?
                                  scheintod

                                  nevertheless. If you look at seam from a viewpoint of an experienced ejb/jsf developer many things are clear. If you are (like I am) new to enterprice java but would like to use seam from the beginnen, this are the problems which would scare you away.

                                  • 14. Re: When do I need a @Factory?
                                    pmuir

                                    The Seam documentation documents Seam. A good book on Seam should cover EJB3 basics as well - for example Yuan/Heute