0 Replies Latest reply on Nov 23, 2009 9:11 AM by Nicklas Karlsson

    JSF Phase Event to CDI bridge

    Nicklas Karlsson Master

      Here is a quickie for JSF phase event propagation


      public class PhaseEventListener implements PhaseListener {
      
           private BeanManager beanManager;
      
           private enum When {
                BEFORE, AFTER
           };
      
           public PhaseEventListener() {
                try {
                     beanManager = (BeanManager) new InitialContext().lookup("java:app/BeanManager");
                } catch (NamingException e) {
                     e.printStackTrace();
                }
           }
      
           @SuppressWarnings("serial")
           private void handlePhase(When when, PhaseEvent e) {
                Annotation whenAnnotation = null;
                Annotation phaseAnnotation = null;
                switch (when) {
                case BEFORE:
                     whenAnnotation = new AnnotationLiteral<Before>() {
                     };
                     break;
                case AFTER:
                     whenAnnotation = new AnnotationLiteral<After>() {
                     };
                     break;
                }
                if (e.getPhaseId() == PhaseId.APPLY_REQUEST_VALUES) {
                     phaseAnnotation = new AnnotationLiteral<ApplyRequestValues>() {
                     };
                } else if (e.getPhaseId() == PhaseId.INVOKE_APPLICATION) {
                     phaseAnnotation = new AnnotationLiteral<InvokeApplication>() {
                     };
                } else if (e.getPhaseId() == PhaseId.PROCESS_VALIDATIONS) {
                     phaseAnnotation = new AnnotationLiteral<ProcessValidations>() {
                     };
                } else if (e.getPhaseId() == PhaseId.RENDER_RESPONSE) {
                     phaseAnnotation = new AnnotationLiteral<RenderResponse>() {
                     };
                } else if (e.getPhaseId() == PhaseId.RESTORE_VIEW) {
                     phaseAnnotation = new AnnotationLiteral<RestoreView>() {
                     };
                } else if (e.getPhaseId() == PhaseId.UPDATE_MODEL_VALUES) {
                     phaseAnnotation = new AnnotationLiteral<UpdateModelValues>() {
                     };
                }
                beanManager.fireEvent(e, new Annotation[] { whenAnnotation,     phaseAnnotation });
           }
      
           public void afterPhase(PhaseEvent e) {
                handlePhase(When.AFTER, e);
           }
      
           public void beforePhase(PhaseEvent e) {
                handlePhase(When.BEFORE, e);
           }
      
           public PhaseId getPhaseId() {
                return PhaseId.ANY_PHASE;
           }
      
      }
      



      and the qualifiers


      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface Before {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface After {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface ApplyRequestValues {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface InvokeApplication {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface ProcessValidations {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface RenderResponse {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface RestoreView {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface UpdateModelValues {}
      



      Then you should be able to do


           public void jsf(@Observes @Before @RenderResponse PhaseEvent e) {
                System.out.println("before response " + e);
           }
      



      Notes:



      1. Register it yourself in faces-config

      2. Use correct JNDI name for bean manager if not on JBoss 5.2+ ;-)

      3. System events not included yet