1   // ========================================================================
2   // $Id: PojoFilter.java 3353 2008-07-22 10:39:41Z janb $
3   // Copyright 2008 Mort Bay Consulting Pty. Ltd.
4   // ------------------------------------------------------------------------
5   // Licensed under the Apache License, Version 2.0 (the "License");
6   // you may not use this file except in compliance with the License.
7   // You may obtain a copy of the License at 
8   // http://www.apache.org/licenses/LICENSE-2.0
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  // ========================================================================
15  
16  package org.mortbay.jetty.plus.annotation;
17  
18  import java.io.IOException;
19  import java.lang.reflect.Method;
20  
21  
22  import javax.servlet.Filter;
23  import javax.servlet.FilterChain;
24  import javax.servlet.FilterConfig;
25  import javax.servlet.ServletException;
26  import javax.servlet.ServletRequest;
27  import javax.servlet.ServletResponse;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  public class PojoFilter implements Filter, PojoWrapper
32  {
33      private Object _pojo;
34      private Method _doFilterMethod;
35      private static final Class[] __params = new Class[] {HttpServletRequest.class, HttpServletResponse.class, FilterChain.class};
36      
37      public PojoFilter (Object pojo)
38      {
39          if (pojo == null)
40              throw new IllegalArgumentException ("Pojo is null");
41          
42          _pojo=pojo;
43          
44          try
45          {
46              _doFilterMethod = _pojo.getClass().getDeclaredMethod("doFilter", __params);
47          }
48          catch (Exception e)
49          {
50              throw new IllegalStateException (e);
51          }
52  
53      }
54      
55      public Object getPojo()
56      {
57          return _pojo;
58      }
59      
60      public void destroy()
61      {
62         //TODO???? Should try to find a destroy method on the pojo?
63      }
64  
65      
66      public void doFilter(ServletRequest req, ServletResponse resp,
67              FilterChain chain) throws IOException, ServletException
68      {   
69          try
70          {
71              _doFilterMethod.invoke(_pojo, new Object[]{req, resp, chain});
72          }
73          catch (Exception e)
74          {
75              throw new ServletException(e);
76          }
77      }
78  
79      public void init(FilterConfig arg0) throws ServletException
80      {
81          // TODO ???? Should try to find an init() method on the pojo?
82      }
83  
84  
85  }