View Javadoc

1   /**
2    * This file Copyright (c) 2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.filters;
35  
36  import java.io.IOException;
37  import java.util.concurrent.locks.ReentrantReadWriteLock;
38  import javax.servlet.FilterChain;
39  import javax.servlet.FilterConfig;
40  import javax.servlet.ServletException;
41  import javax.servlet.ServletRequest;
42  import javax.servlet.ServletResponse;
43  import javax.servlet.http.HttpServletRequest;
44  
45  
46  /**
47   * Wrapper for a MgnlFilter that using a read/write lock ensures that the destroy method waits for requests to complete
48   * before destroying the target filter. All use of the wrapper must be done while holding the read lock. The exception
49   * to this is the destroy method that MUST NOT be called while holding the read lock, doing so would result in dead
50   * lock.
51   *
52   * @version $Id$
53   */
54  public class SafeDestroyMgnlFilterWrapper implements MgnlFilter {
55  
56      private final MgnlFilter target;
57      private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
58  
59      public SafeDestroyMgnlFilterWrapper(MgnlFilter target) {
60          if (target == null) {
61              throw new NullPointerException("Target filter must not be null");
62          }
63          this.target = target;
64      }
65  
66      public void acquireReadLock() {
67          lock.readLock().lock();
68      }
69  
70      public void releaseReadLock() {
71          lock.readLock().unlock();
72      }
73  
74      @Override
75      public void init(FilterConfig filterConfig) throws ServletException {
76          target.init(filterConfig);
77      }
78  
79      /**
80       * Read lock must be held when invoking this method.
81       */
82      @Override
83      public String getName() {
84          return target.getName();
85      }
86  
87      /**
88       * Read lock must be held when invoking this method.
89       */
90      @Override
91      public void setName(String name) {
92          target.setName(name);
93      }
94  
95      /**
96       * Read lock must be held when invoking this method.
97       */
98      @Override
99      public boolean matches(HttpServletRequest request) {
100         return target.matches(request);
101     }
102 
103     /**
104      * Read lock must be held when invoking this method.
105      */
106     @Override
107     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
108         target.doFilter(request, response, chain);
109     }
110 
111     /**
112      * Destroys the target filter after waiting for all requests to complete. Calling this method while holding the read
113      * lock will result in dead lock. Therefore a request that has passed through this filter MUST NEVER call this
114      * method.
115      */
116     @Override
117     public void destroy() {
118 
119         // We could employ a ThreadLocal to throw an exception instead of dead locking. There is no other way
120         // to see if the calling thread is holding the read lock.
121 
122         lock.writeLock().lock();
123         try {
124             target.destroy();
125         } finally {
126             lock.writeLock().unlock();
127         }
128     }
129 
130     public MgnlFilter getTargetFilter() {
131         return target;
132     }
133 
134     /**
135      * Construct for keeping a reference to a {@link SafeDestroyMgnlFilterWrapper} and change it with the guarantee that
136      * the returned previous reference will not get more read locks. It's then safe to destroy the returned reference.
137      *
138      * @version $Id$
139      */
140     public static class Switcher {
141 
142         private SafeDestroyMgnlFilterWrapper filter;
143 
144         /**
145          * Replaces the current filter with a new one and returns the previous filter. The returned filter is possibly still
146          * in use by currently executing requests but it will not be used by any more requests after this method returns.
147          * <p/>
148          * Notes about the returned filter:
149          * <ul>
150          * <li>No methods other than destroy() should be called on the returned filter</li>
151          * <li>The destroy() method will wait for all requests to complete before it is destroyed</li>
152          * <li>Calling the destroy() method from a request that has entered the filter WILL RESULT IN DEAD LOCK.</li>
153          * </ul>
154          */
155         public synchronized SafeDestroyMgnlFilterWrapper replaceFilter(SafeDestroyMgnlFilterWrapper newFilter) {
156             SafeDestroyMgnlFilterWrapper oldFilter = filter;
157             filter = newFilter;
158             return oldFilter;
159         }
160 
161         /**
162          * Returns the current filter with a read lock held for the current thread. After use the thread must relinquish the
163          * lock by calling releaseReadLock(). If no filter has been set this method returns null and no lock is taken.
164          */
165         public synchronized SafeDestroyMgnlFilterWrapper getFilterAndAcquireReadLock() {
166             if (filter == null) {
167                 return null;
168             }
169             filter.acquireReadLock();
170             return filter;
171         }
172 
173         /**
174          * This method is provided for introspection only. It is inherently UNSAFE as using this method and then
175          * acquiring the read lock on the returned filter breaks the guarantee that a replaced reference wont get more
176          * read locks.
177          */
178         public synchronized SafeDestroyMgnlFilterWrapper getFilter() {
179             return filter;
180         }
181     }
182 }