1 2 Previous Next 20 Replies Latest reply on Jan 6, 2010 6:54 PM by gavin.king Go to original post
      • 15. Re: Producer method
        gavin.king

        Before login the session @Produces user produce a instance. It's Anonymous copy

        Ah yes, that sounds like the problem. The error is that the @Produces method is marked @RequestScoped, when it should be @Dependent.

        • 16. Re: Producer method
          nickarls

          Anonymous?

          • 17. Re: Producer method
            mcalpay

            Thanks all for the answers.
            Since this worked in my case :


                @Produces
                @RequestScoped
                @Principal
                @Named("principal")
                private User getPrincipal(Current current) {
                    return current.getUser();
                }



            instead of :


                @Produces
                @RequestScoped
                @Principal
                @Named("principal")
                private User getPrincipal() {
                    return user;
                }



            I thought there was a problem with the instances. But what made it work was current.getUser() method call instead of the field access. So calling this.getUser() returns me the expected value but this.user field access remains uninjected. It simply has thte value of the field (I set it Anonymous so its anonymous, if you assigned null it returns null).

            • 18. Re: Producer method
              aliebrahimi

              I think this issue related to following three part of code:


              part 1:




              private User user = ANONYMOUS;
              
              



              ...



                  public String checkLogin(User user) {
                      if("testus".equals(user.getName())) {
                          this.user = user;
                          return "/index";
                      }
                      return "";
                  }
              
                  @Produces
                  @RequestScoped
                  @Principal
                  @Named("principal")
                  private User getPrincipal() {
                      return user;
                  }
              
              
              




              part 2:





                          <h:commandButton value="Login" action="#{current.checkLogin(login)}"/>






              you passed request scope bean(proxy object) to checkLogin method and assign that to user field that returned by producer method. and so that in each request this proxy bean' s target object refreshed.

              • 19. Re: Producer method
                mcalpay

                Thanks for the reply.


                this.user = user;



                Part was problematic and I changed that to bean I got from the entity manager.
                ${login} is not the 'principal' btw, its just to check users input. This is what It looks now :



                @SessionScoped
                @Named("current")
                public class Current implements Serializable {
                
                    @Inject
                    @Anonymous
                    private User user;
                
                    @Inject    
                    private EntityManager entityManager;
                
                    public User getUser() {
                        return user;
                    }
                
                    public String checkLogin(User user) {
                        try {
                            this.user = (User) entityManager.createNamedQuery(User.Queries.GETUSERBYNAME)
                                .setParameter(1,user.getName())
                                .getSingleResult();
                            return "/index";
                        } catch(NoResultException nre) {
                            return "";
                        } finally {
                            entityManager.close();
                        }
                    }
                
                    @Produces
                    @RequestScoped
                    @Principal
                    @Named("principal")
                    private User getPrincipal() {
                        return this.getUser();
                    }
                
                }
                



                Complete code is here : http://code.google.com/p/teknoatolye/source/browse/#svn/trunk/iwall%3Fstate%3Dclosed
                if anyone wants to check out...
                thanks


                • 20. Re: Producer method
                  gavin.king

                  Ali Ebrahimi wrote on Jan 06, 2010 12:45:


                  you passed request scope bean(proxy object) to checkLogin method and assign that to user field that returned by producer method. and so that in each request this proxy bean' s target object refreshed.



                  Yes, you're right. That's the correct explanation.

                  1 2 Previous Next