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

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