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

1   /*
2    * $Id: TilesFilter.java 734996 2009-01-16 13:27:28Z 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.tiles.definition.util.DefinitionsFactoryUtil;
37  
38  /***
39   * Processes Reloadable Tiles Definitions.
40   *
41   * @version $Rev: 734996 $ $Date: 2009-01-16 14:27:28 +0100 (ven, 16 gen 2009) $
42   * @deprecated Use {@link TilesServlet} or {@link TilesListener}. If you want a
43   * decoration Filter, use
44   * {@link org.apache.tiles.web.util.TilesDecorationFilter}. Moreover, definition
45   * files reload themselves if the {@link .apache.tiles.definition.dao.DefinitionDAO} implements
46   * {@link org.apache.tiles.definition.Refreshable}.
47   */
48  
49  public class TilesFilter extends TilesServlet implements Filter {
50  
51      /***
52       * The filter configuration object we are associated with.  If
53       * this value is null, this filter instance is not currently
54       * configured.
55       */
56      private FilterConfig filterConfig = null;
57  
58      /***
59       * Checks whether Tiles Definitions need to be reloaded.
60       *
61       * @param request  The servlet request we are processing
62       * @param response The servlet response we are creating
63       * @param chain    The filter chain we are processing
64       * @throws IOException      if an input/output error occurs
65       * @throws ServletException if a servlet error occurs
66       */
67      public void doFilter(ServletRequest request, ServletResponse response,
68                           FilterChain chain)
69          throws IOException, ServletException {
70  
71          try {
72              DefinitionsFactoryUtil.reloadDefinitionsFactory(
73                      getServletContext());
74              chain.doFilter(request, response);
75          } catch (Exception e) {
76              throw new ServletException("Error processing request.", e);
77          }
78      }
79  
80      /***
81       * Returns the filter configuration object for this filter.
82       *
83       * @return The filter configuration.
84       */
85      public FilterConfig getFilterConfig() {
86          return (this.filterConfig);
87      }
88  
89      /***
90       * Set the filter configuration object for this filter.
91       *
92       * @param filterConfig The filter configuration object
93       */
94      public void setFilterConfig
95          (FilterConfig
96              filterConfig) {
97  
98          this.filterConfig = filterConfig;
99      }
100 
101     /***
102      * Destroy method for this filter.
103      */
104     public void destroy
105         () {
106         super.destroy();
107     }
108 
109     /*** {@inheritDoc} */
110     public void init(FilterConfig filterConfig) throws ServletException {
111         this.filterConfig = filterConfig;
112         super.init(createServletConfig());
113 
114         if (DEBUG) {
115             log("TilesDecorationFilter:Initializing filter");
116         }
117     }
118 
119     /*** {@inheritDoc} */
120     public void log(String msg) {
121         filterConfig.getServletContext().log(msg);
122     }
123 
124     /***
125      * A DEBUG flag.
126      */
127     // FIXME Is it really used?
128     private static final boolean DEBUG = true;
129 
130     /***
131      * Creates a servlet configuration object from the filter configuration
132      * object.
133      *
134      * @return The servlet configuration object.
135      */
136     private ServletConfig createServletConfig() {
137         return new ServletConfigAdapter(filterConfig);
138     }
139 
140 
141     /***
142      * Adapts a filter configuration object to become a servlet configuration
143      * object.
144      */
145     class ServletConfigAdapter implements ServletConfig {
146 
147         /***
148          * The filter configuration object to use.
149          */
150         private FilterConfig config;
151 
152 
153         /***
154          * Constructor.
155          *
156          * @param config The filter configuration object to use.
157          */
158         public ServletConfigAdapter(FilterConfig config) {
159             this.config = config;
160         }
161 
162         /*** {@inheritDoc} */
163         public String getServletName() {
164             return config.getFilterName();
165         }
166 
167         /*** {@inheritDoc} */
168         public ServletContext getServletContext() {
169             return config.getServletContext();
170         }
171 
172         /*** {@inheritDoc} */
173         public String getInitParameter(String string) {
174             return config.getInitParameter(string);
175         }
176 
177         /*** {@inheritDoc} */
178         @SuppressWarnings("unchecked")
179         public Enumeration getInitParameterNames() {
180             return config.getInitParameterNames();
181         }
182     }
183 
184 }