0 Replies Latest reply on Nov 23, 2009 8:58 AM by Nicklas Karlsson

    Servlet Event to CDI event bus PE

    Nicklas Karlsson Master

      Here is a trivial class that pushes the Servlet Listener events to the CDI event bus so that you can do a


           public void here(@Observes @Created HttpSessionEvent e) {
                // session created, do work
           }
      



      @WebListener
      public class ServletListener implements ServletContextListener,
                ServletContextAttributeListener, HttpSessionListener,
                HttpSessionAttributeListener, HttpSessionActivationListener,
                HttpSessionBindingListener, ServletRequestAttributeListener {
      
           private BeanManager beanManager;
           
           public ServletListener() {
                try {
                     beanManager = (BeanManager) new InitialContext().lookup("java:app/BeanManager");
                } catch (NamingException e) {
                     e.printStackTrace();
                }
           }
           
           private void fireEvent(Object payload, Annotation... qualifiers) {
                beanManager.fireEvent(payload, qualifiers);
           }
           
           public void contextDestroyed(ServletContextEvent e) {
                fireEvent(e, new AnnotationLiteral<Destroyed>() {});
           }
      
           public void contextInitialized(ServletContextEvent e) {
                fireEvent(e, new AnnotationLiteral<Initialized>() {});
           }
      
           public void attributeAdded(ServletContextAttributeEvent e) {
                fireEvent(e, new AnnotationLiteral<AttributeAdded>() {});
           }
      
           public void attributeRemoved(ServletContextAttributeEvent e) {
                fireEvent(e, new AnnotationLiteral<AttributeRemoved>() {});
           }
      
           public void attributeReplaced(ServletContextAttributeEvent e) {
                fireEvent(e, new AnnotationLiteral<AttributeReplaced>() {});
           }
      
           public void sessionCreated(HttpSessionEvent e) {
                fireEvent(e, new AnnotationLiteral<Created>() {});
           }
      
           public void sessionDestroyed(HttpSessionEvent e) {
                fireEvent(e, new AnnotationLiteral<Destroyed>() {});
           }
      
           public void attributeAdded(HttpSessionBindingEvent e) {
                fireEvent(e, new AnnotationLiteral<AttributeAdded>() {});
           }
      
           public void attributeRemoved(HttpSessionBindingEvent e) {
                fireEvent(e, new AnnotationLiteral<AttributeRemoved>() {});
           }
      
           public void attributeReplaced(HttpSessionBindingEvent e) {
                fireEvent(e, new AnnotationLiteral<AttributeReplaced>() {});
           }
      
           public void sessionDidActivate(HttpSessionEvent e) {
                fireEvent(e, new AnnotationLiteral<DidActivate>() {});
           }
      
           public void sessionWillPassivate(HttpSessionEvent e) {
                fireEvent(e, new AnnotationLiteral<WillPassivate>() {});
           }
      
           public void valueBound(HttpSessionBindingEvent e) {
                fireEvent(e, new AnnotationLiteral<ValueBound>() {});
           }
      
           public void valueUnbound(HttpSessionBindingEvent e) {
                fireEvent(e, new AnnotationLiteral<ValueUnbound>() {});
           }
      
           public void attributeAdded(ServletRequestAttributeEvent e) {
                fireEvent(e, new AnnotationLiteral<AttributeAdded>() {});
           }
      
           public void attributeRemoved(ServletRequestAttributeEvent e) {
                fireEvent(e, new AnnotationLiteral<AttributeRemoved>() {});
           }
      
           public void attributeReplaced(ServletRequestAttributeEvent e) {
                fireEvent(e, new AnnotationLiteral<AttributeReplaced>() {});
           }
      
      }
      



      and the qualifiers


      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface AttributeAdded {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface AttributeRemoved {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface AttributeReplaced {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface Created {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface Destroyed {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface DidActivate {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface Initialized {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface ValueBound {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface ValueUnbound {}
      
      @Qualifier
      @Target({FIELD, PARAMETER})
      @Retention(RUNTIME)
      public @interface WillPassivate {}
      



      Notes:



      1. For a pre-Servlet-3 appserver, register the listener yourself

      2. For a spec-compliant server, use the correct JNDI URL for beanmanager ;-)

      3. Servlet 3 asynch listeners not yet included, didn't find any JBoss branch where they would be present



      This is just a proof-of-concept, I'll package it better and put it on in.relation.to. Perhaps a JSF-CDI event bridge, too(?)


      Hopefully in Java EE 7, the servlet spec would just state what CDI-events it would broadcast and there would be no listeners ;-)