1 2 Previous Next 17 Replies Latest reply on Nov 16, 2009 8:47 AM by gavin.king Go to original post
      • 15. Re: Weld: Give me all the java.lang.Class with annotation XXX

        Francisco Peredo wrote on Nov 16, 2009 03:10:



        Gavin King wrote on Nov 16, 2009 01:02:


        In a Java EE environment, servlet filters can inject beans (including Event), as required by the CDI spec. They are defined as a Java EE component class supporting injection. Not sure what restrictions exist in the case of our Tomcat integration. Do you know that it doesn't work, are are you just assuming? Did you try it?


        No... did not try it... going to try... but I am suspect that it might not work because I did not copy weld-tomcat-support.jar in to Tomcat lib (I had it at WEB-INF\lib but it was silly to have it there, that is not its place, so I deleted it).



        Did not work:


        class WeldFilter extends Filter {
           @Inject @Any
           def setPageEvent(newPageEvent:Event[PageContext]){
             pageEvent=newPageEvent;
           }
                  
           def doFilter(request:ServletRequest, response:ServletResponse, chain:FilterChain){     
             
             var requestURL:StringBuffer = request.asInstanceOf[HttpServletRequest].getRequestURL();
             var contextPath:String = request.asInstanceOf[HttpServletRequest].getContextPath();
             var indexOf:Int=requestURL.lastIndexOf(contextPath);
             indexOf=indexOf+1;
             var page:String=requestURL.substring(indexOf);
                  
             pageEvent.select(new PageBinding(page)).fire(new PageContext(request,response));
                  
             chain.doFilter(request,response);
           }
        }
        



        Got null pointer exception at pageEvent.select(new PageBinding(page)).fire(new PageContext(request,response)); Guess I will need to ask for the BeanManager

        • 16. Re: Weld: Give me all the java.lang.Class with annotation XXX

          Its alive! Its alive! I mean, it works! my filter fires the event:


          class WeldFilter extends Filter {
          
             private var beanManager:BeanManager=null;
            
             def init(filterConfig:FilterConfig){
               var initialContext:Context = new InitialContext();
               var comp:Context= initialContext.lookup("java:comp/env/").asInstanceOf[Context];
               beanManager = comp.lookup("BeanManager").asInstanceOf[BeanManager];
                   
             }    
                    
             def doFilter(request:ServletRequest, response:ServletResponse, chain:FilterChain){     
               
               var requestURL:StringBuffer = request.asInstanceOf[HttpServletRequest].getRequestURL();
               var contextPath:String = request.asInstanceOf[HttpServletRequest].getContextPath();
               var indexOf:Int=requestURL.lastIndexOf(contextPath);
               indexOf=indexOf+contextPath.length;
               var page:String=requestURL.substring(indexOf);
               
               beanManager.fireEvent(new PageContext(request.asInstanceOf[HttpServletRequest],response.asInstanceOf[HttpServletResponse]),new PageBinding(page));               
                    
               chain.doFilter(request,response);
             }
                
             
             def      destroy(){
               
             }
                    
           
          }
          



          And Game.load gets called (and the values of the form get to the console):


          @Named
          @SessionScoped
          class Game extends Serializable(){
          
          /*
          Here there are lots of other stuff...
          */
          
          def load(@Observes @Page{val name="/home.jsp"} pageContext:PageContext ){
              var parametersSet = pageContext.request.getParameterMap.entrySet();    
              var currentLogger:Logger=logger;
              currentLogger.info("Parameter Set: "+parametersSet);
              for (parameterEntry <- Conversions.convertSet(parametersSet)){      
                  currentLogger.info("Key/Value: {}/{}.",parameterEntry.getKey(),parameterEntry.getValue());        
              }
            }
          }
          
          



          I am starting to really like this!

          • 17. Re: Weld: Give me all the java.lang.Class with annotation XXX
            gavin.king

            Francisco Peredo wrote on Nov 16, 2009 03:12:



            Gavin King wrote on Nov 16, 2009 02:49:


            You don't have to download anything, it's part of CDI. All CDI implementations have to provide this. You already have it, as part of Weld.


            Thanks again! Any step by step tutorials ;-) ?


            Here's a couple of examples.

            1 2 Previous Next