3 Replies Latest reply on Aug 15, 2008 7:00 PM by Vijay Koppuravuri

    Proper way to clear or recompute a @Factory generated value?

    Steve Tynor Newbie

      Seam 2.0.3.CR1


      I'm attempting to adapt Pete's example CurrentUserHome to our application (UsingEntityHomeForEntitiesInLongrunningContexts).


      The problem is that in our application, the currentUser factory method might be invoked before we have authenticated the user and outjected the currentUserId key.  Once authenticated, we need to reset the component value - or otherwise force the factory method to be be recomputed.


      The following works, but feels wrong. I can't find a documented way to do what I need to do. Is there a cleaner / more acceptable way, or am I right to poke around directly in the Context?


      // reset any factories which may have been invoked before we knew who was logging in
      currentUserHome.clearInstance();
      Contexts.getConversationContext().set("currentUser", null);
      



      Thanks,
      Steve

        • 1. Re: Proper way to clear or recompute a @Factory generated value?
          hasan Newbie

          The proper way to recompute a Factory is to set the associated object to NULL. For Example


          @Out
          private Object currentUser;
          
          public void myMethod(){
            currentUser = null;
          }
          
          


          Calling the method myMethod would automaticaly provoke the factory.



          The problem is that in our application, the currentUser factory method might be invoked before we have authenticated the user and outjected the currentUserId key

          I think the Factory currentUser should not be provoked at the first place


          • 2. Re: Proper way to clear or recompute a @Factory generated value?
            Steve Tynor Newbie

            FWIW, outjection does not work for this case - it appears to happen too late. In our case, we are trying to reset the user inside Authenticator.authenticate() as soon as we know who the authenticated user is. If we don't explicitly clear the contexts as I described (and in fact, also clear the currentUserId), then the view rendered after the authentication still sees the pre outjected value for currentUserId and the factory recomputes the same 'anonymous' User object for the view.



                public boolean authenticate()
                {
            ...
                 // HACK: this needs to be in the session before normal outjection makes it available, so we do it explicitly.
                 Contexts.getSessionContext().set("currentUserId", currentUserId);
                    // Reset the CurrentUserHome to recompute the factory method for the user:
                 currentUserHome.clearInstance();
                 Contexts.getConversationContext().set("currentUser", null);
                 return true;
                }
            


            I still think this feels wrong, but it's the only thing we've found to work.


            Thanks,
            Steve


            • 3. Re: Proper way to clear or recompute a @Factory generated value?
              Vijay Koppuravuri Newbie

              How about using events
              '
              @In
              private Events events;


              @Factory
              @Observer(xxx)
              public void getxxx(){       


              }


              public void myMethod(){
                currentUser = null;
                events.raiseXXXEvent(...);
              }
              '