9 Replies Latest reply on Apr 28, 2010 11:48 AM by pmuir

    producing instance to alternate scope

    asookazian

      So in Seam 2.x we are able to outject to any scope we want (wider spanning or shorter spanning).  So let's say the current Seam component is conversation-scoped, we could outject an object into session scope via:


      @Out(scope=ScopeType.SESSION)


      http://docs.jboss.org/seam/2.2.1.CR1/api/org/jboss/seam/annotations/Out.html


      In Weld, outjection is prohibited but simulated via @Produces annotation.


      http://java.sun.com/javaee/6/docs/api/index.html?javax/enterprise/inject/Produces.html



      A producer method is a method that acts as a source of bean instances. The method declaration itself describes
      the bean and the container invokes the method to obtain an instance of the bean when no instance exists in the
      specified context. A producer method lets the application take full control of the bean instantiation process.

      So what if in a Weld or Seam 3 app we need to outject from conversation-scoped managed bean or session bean to session scope?  Is this strictly prohibited?  If yes, then we'd have to subsequently manually set the object in the HttpSession.  There must be some valid use cases for this...

        • 1. Re: producing instance to alternate scope
          nickarls

          Weld isn't designed for this.


          Even if you would hack the session attributes, the bean itself would still be conversation scoped and looked for in the conversation scope on next reference.


          I guess it's theoretically possible to change the scope of the bean through hacks but in the future beans will probably be immutable in Weld. Then you might still be able to do it through an extension (or reflection hacks) but the Weld-way would probably be to have a separate session-scoped bean for that usage. Contexts in Seam 2 were more like plain hashmaps that held... whatever they happened to hold at the moment.

          • 2. Re: producing instance to alternate scope
            pmuir

            Nik, I think you are talking about changing the scope of the produced bean mid-flight, when I think


            @ConversationScoped
            class Foo {
            
               @Produces @SessionScoped
               Bar bar = new Bar();
            }



            will suffice Arbi's requirements.

            • 3. Re: producing instance to alternate scope
              asookazian

              Yes, PMuir's solution is basically what I was wondering about.  That would work.  And what about an API for determining what scope a particular object is in?  Or at least viewing all the objects in a specified scope?  thx.

              • 4. Re: producing instance to alternate scope
                asookazian

                Let's say in the same session, I produce Foo instance to conversation context and another Foo instance to session context.


                Then later in the same LRC, in a different session-scoped managed bean, session bean, etc. I @Inject Foo foo. But I want to inject the Foo instance in the conversation context.  Is this possible?


                @SessionScoped
                class Foo {
                
                   @Produces @ConversationScoped 
                   Bar getBar1() {
                      return new Bar();
                   }
                
                   @Produces
                   Bar getBar2() {
                      return new Bar();
                   }
                }



                @SessionScoped
                class Boo {
                   
                   @Inject Bar bar;  // how can I inject the instance from conversation context?
                
                }

                • 5. Re: producing instance to alternate scope
                  nickarls

                  Scope isn't considered in typesafe resolves. Therefore you need some additional qualifier, you can't just @Produces @RequestScoped Foo and @Produces @SessionScoped Foo.

                  • 6. Re: producing instance to alternate scope
                    nickarls

                    Hmm, can't think of any clean ways of determining the scope of an instance. Short of doing something really ugly involving iterating over the contexts to see where the instance can be found.

                    • 7. Re: producing instance to alternate scope
                      asookazian

                      how about (via a proxy?) decorating a managed bean with additional behavior like getScope() which allows us to get additional metadata about the managed bean?


                      Every managed bean should know intrinsically what scope it lives in...

                      • 8. Re: producing instance to alternate scope
                        nickarls

                        Every managed bean should know intrinsically what scope it lives in...


                        Every strange idea needs one advocate ;-)

                        • 9. Re: producing instance to alternate scope
                          pmuir

                          Arbi Sookazian wrote on Apr 27, 2010 22:14:


                          Every managed bean should know intrinsically what scope it lives in...


                          Every Managed Bean does know what scope it lives in. However there is no easy way in the CDI API to discover what Bean<> the current instance is created from. This goes back to adding something like Stuart proposed


                          @Inject @This Bean bean;