4 Replies Latest reply on May 31, 2012 9:50 PM by hantsy bai

    A litte confused about the CDI event fire and observer.

    hantsy bai Master

      I have an @Authenticated Quanlifier to identify the logged in event in the custom Authenticator(provided by Seam Security)...I have a user management page in the admin console and provide CRUD operations for users, and when the user is saved, will fire a userSavedEnvet(without any Quanlifier) to display message...

       

      But when I logged in, the message display two messags: one for saved event, one for loggedin...

       

      When I added @Default to saved event declaration and observer method, it worked as expected.

       

      I have read realted topic in the weld documetn , I think if there is no custom Quanlifier specified with the event,it should be the same as @Default.

       

      Where I ommited in the document?

       

      Thanks.

        • 1. Re: A litte confused about the CDI event fire and observer.
          zeeman Novice

          Why are you complicating things. Why not just observe Seam LoggedInEvent? It's fired after a succesful login and after your authorize mehtod is called. I don't see why you need to separate login event from saved event.

           

          If you instist on your approach, I would not add @Default to a custom event your fire. If you do, the @Observe method needs to have @Default as well. That's the behavior I know of.

          • 2. Re: A litte confused about the CDI event fire and observer.
            hantsy bai Master

            OK, I pasted some codes to explain the problem...

             

            The login code:

             

             

            @Stateful
            @Named("siorcAuthenticator")
            public class SiorcAuthenticator extends BaseAuthenticator implements
                    Authenticator {
            
                private static final Logger log=LoggerFactory.getLogger(SiorcAuthenticator.class);
            
            
            ....
                @Inject
                @Authenticated
                private Event<UserAccount> loginEventSrc;
            
                public void authenticate() {
                    log.info("Logging in " + credentials.getUsername());
                            loginEventSrc.fire(user);
            
             ......
                }
            
            }
            

             

            The Authenticated is custom Qulifier, I used a @Observes to produce a SessionScoped User and send a messages to screen. The codes are copied from the Seam 3 booking example

             

            But in the user account edit codes, I used savedEvent to display a message to user.

             

             

            @Stateful
            @ConversationScoped
            @Named("userAccountEdit")
            public class UserAccountEditAction {
            
                private static final org.slf4j.Logger log = LoggerFactory
                        .getLogger(UserAccountEditAction.class);
            
                @Inject @Default
                private Event<UserAccount> userAccountSavedEventSrc;
            
            
                // @End
                public void save() {
                    if (log.isDebugEnabled()) {
                        log.debug("call save...");
                    }
            
            
                    this.userAccountSavedEventSrc.fire(this.currentUserAccount);
            
                    if (!conversation.isTransient()) {
                        conversation.end();
                    }
                }
            
                public void onSaved(
                        @Observes(during = TransactionPhase.AFTER_SUCCESS) @Default UserAccount userAccount) {
                    messages.info(new DefaultBundleKey("userAccount_saved"))
                            .defaults("UserAccount saved").params(userAccount.getName());
                }
            
            
            }
            

             

             

            The problem is that if I do not add @Default, when user log in, it will display the saved message....

            • 3. Re: A litte confused about the CDI event fire and observer.
              zeeman Novice

              What you're doing is not safe because you're doing UI actions in Authenticator and you're not supposed to. (event.fire happens in the same thread, control is moving from authenticator to another class)

               

              Since SiorcAuthenticator implements Authenticator. Why don't you make your UserAccount class implement org.picketlink.idm.api.User;

              In SiorcAuthenticator call setUser (from its parent) and set it to user account.

               

              Now in any class you can Inject Identity, use identity.getUser(). It'll give you the logged in user. That's how you're supposed to use Seam Security.

               

              But anyway, to answer your question, you don't @Default since all beans get @Default and @Any. Assuming you only have one UserAcount class.

              • 4. Re: A litte confused about the CDI event fire and observer.
                hantsy bai Master

                I see, in the weld document, we do not need  any extra Quanlifier.

                 

                But the problem is here, if I do not used @ Default(I think other Quanlifier aslo worked), the login action will fire the savedEvent and display two mesages together when user log in.

                 

                UserAccount is an entity class.

                 

                I used Weld 1.1.5.Final (upgraded),  JSF 2.0.2(Mojorra, downgraded due to the ajax problem), JBoss 7.0.1 ... I will try the latest version of weld and have a look if it is missing.