This project has retired. For details please refer to its Attic page.
TilesFilter xref
View Javadoc

1   /*
2    * $Id: TilesFilter.java 531864 2007-04-24 10:24:30Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.tiles.web.startup;
23  
24  import java.io.IOException;
25  import java.util.Enumeration;
26  
27  import javax.servlet.Filter;
28  import javax.servlet.FilterChain;
29  import javax.servlet.FilterConfig;
30  import javax.servlet.ServletConfig;
31  import javax.servlet.ServletContext;
32  import javax.servlet.ServletException;
33  import javax.servlet.ServletRequest;
34  import javax.servlet.ServletResponse;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.apache.tiles.definition.util.DefinitionsFactoryUtil;
39  
40  /***
41   * Processes Reloadable Tiles Definitions.
42   *
43   * @version $Rev: 531864 $ $Date: 2007-04-24 12:24:30 +0200 (Tue, 24 Apr 2007) $
44   */
45  
46  public class TilesFilter extends TilesServlet implements Filter {
47  
48      /***
49       * The logging object.
50       */
51      @SuppressWarnings("unused")
52      private static final Log LOG = LogFactory.getLog(TilesFilter.class);
53  
54      /***
55       * The filter configuration object we are associated with.  If
56       * this value is null, this filter instance is not currently
57       * configured.
58       */
59      private FilterConfig filterConfig = null;
60  
61  
62      /***
63       * Checks whether Tiles Definitions need to be reloaded.
64       *
65       * @param request  The servlet request we are processing
66       * @param response The servlet response we are creating
67       * @param chain    The filter chain we are processing
68       * @throws IOException      if an input/output error occurs
69       * @throws ServletException if a servlet error occurs
70       */
71      public void doFilter(ServletRequest request, ServletResponse response,
72                           FilterChain chain)
73          throws IOException, ServletException {
74  
75          try {
76              DefinitionsFactoryUtil.reloadDefinitionsFactory(
77                      getServletContext());
78              chain.doFilter(request, response);
79          } catch (Exception e) {
80              throw new ServletException("Error processing request.", e);
81          }
82      }
83  
84      /***
85       * Returns the filter configuration object for this filter.
86       *
87       * @return The filter configuration.
88       */
89      public FilterConfig getFilterConfig() {
90          return (this.filterConfig);
91      }
92  
93      /***
94       * Set the filter configuration object for this filter.
95       *
96       * @param filterConfig The filter configuration object
97       */
98      public void setFilterConfig
99          (FilterConfig
100             filterConfig) {
101 
102         this.filterConfig = filterConfig;
103     }
104 
105     /***
106      * Destroy method for this filter.
107      */
108     public void destroy
109         () {
110         super.destroy();
111     }
112 
113     /*** {@inheritDoc} */
114     public void init(FilterConfig filterConfig) throws ServletException {
115         this.filterConfig = filterConfig;
116         super.init(createServletConfig());
117 
118         if (DEBUG) {
119             log("TilesDecorationFilter:Initializing filter");
120         }
121     }
122 
123     /*** {@inheritDoc} */
124     public void log(String msg) {
125         filterConfig.getServletContext().log(msg);
126     }
127 
128     /***
129      * A DEBUG flag.
130      */
131     // FIXME Is it really used?
132     private static final boolean DEBUG = true;
133 
134     /***
135      * Creates a servlet configuration object from the filter configuration
136      * object.
137      *
138      * @return The servlet configuration object.
139      */
140     private ServletConfig createServletConfig() {
141         return new ServletConfigAdapter(filterConfig);
142     }
143 
144 
145     /***
146      * Adapts a filter configuration object to become a servlet configuration
147      * object.
148      */
149     class ServletConfigAdapter implements ServletConfig {
150 
151         /***
152          * The filter configuration object to use.
153          */
154         private FilterConfig config;
155 
156 
157         /***
158          * Constructor.
159          *
160          * @param config The filter configuration object to use.
161          */
162         public ServletConfigAdapter(FilterConfig config) {
163             this.config = config;
164         }
165 
166         /*** {@inheritDoc} */
167         public String getServletName() {
168             return config.getFilterName();
169         }
170 
171         /*** {@inheritDoc} */
172         public ServletContext getServletContext() {
173             return config.getServletContext();
174         }
175 
176         /*** {@inheritDoc} */
177         public String getInitParameter(String string) {
178             return config.getInitParameter(string);
179         }
180 
181         /*** {@inheritDoc} */
182         @SuppressWarnings("unchecked")
183         public Enumeration getInitParameterNames() {
184             return config.getInitParameterNames();
185         }
186     }
187 
188 }