2 Replies Latest reply on Jan 6, 2014 6:30 AM by Martin Kouba

    Override producer methods implementation

    Rafael Pestano Novice

      Hi guys,

       

      there is a way to override a producer method logic but keep its metadata(qualifiers)?

       

      here is an example of what i'm trying to do:

       

      @SessionScoped

      public class BaseSecurityContext implements Serializable {

       

       

          @Produces

          @LoggedIn

          @Named

          public Boolean loggedIn() {

              return isLoggedIn();

          }

       

          public Boolean isLoggedIn(){

              return Boolean.FALSE;//by default user is never logged in

          }

      }

       

      and in the subclass i just want to override the isLoggedIn() method but still producing @LoggedIn in BaseSecurityContext.

       

      I know with "standard" CDI features i wont get that but with an extension there is a way to change isLoggedIn() implementation with its subclass one?

        • 1. Re: Override producer methods implementation
          Rafael Pestano Novice

          for now instead of using @producer method im using a common interface plus @Specializes such as:

           

          @SessionScoped

          public class BaseSecurityContext implements Serializable ,SecurityContext{

           

              @Override    

              public Boolean loggedIn() {

                  return Boolean.FALSE;//by default user is never logged in

              }

          }

           

          @SessionScoped

          @Specializes

          AppSecurityContext extends BaseSecurityContext{

           

              @Override    

              public Boolean loggedIn() {

                  //application decide if user logged in

              }

          }

           

          So to track if user is logged in i use SecurityContext interface(which is in a third party library).

          • 2. Re: Override producer methods implementation
            Martin Kouba Master

            Hi Rafael,

            your solution makes sense. You could also specialize the producer method but this would not completely override the BaseSecurityContext bean which is desirable in this scenario...