1 /**
2 * This file Copyright (c) 2011-2015 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 public class SafeDestroyMgnlFilterWrapper implements MgnlFilter {
53
54 private final MgnlFilter target;
55 private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
56
57 public SafeDestroyMgnlFilterWrapper(MgnlFilter target) {
58 if (target == null) {
59 throw new NullPointerException("Target filter must not be null");
60 }
61 this.target = target;
62 }
63
64 public void acquireReadLock() {
65 lock.readLock().lock();
66 }
67
68 public void releaseReadLock() {
69 lock.readLock().unlock();
70 }
71
72 @Override
73 public void init(FilterConfig filterConfig) throws ServletException {
74 target.init(filterConfig);
75 }
76
77 /**
78 * Read lock must be held when invoking this method.
79 */
80 @Override
81 public String getName() {
82 return target.getName();
83 }
84
85 /**
86 * Read lock must be held when invoking this method.
87 */
88 @Override
89 public void setName(String name) {
90 target.setName(name);
91 }
92
93 /**
94 * Read lock must be held when invoking this method.
95 */
96 @Override
97 public boolean matches(HttpServletRequest request) {
98 return target.matches(request);
99 }
100
101 /**
102 * Read lock must be held when invoking this method.
103 */
104 @Override
105 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
106 target.doFilter(request, response, chain);
107 }
108
109 /**
110 * Destroys the target filter after waiting for all requests to complete. Calling this method while holding the read
111 * lock will result in dead lock. Therefore a request that has passed through this filter MUST NEVER call this
112 * method.
113 */
114 @Override
115 public void destroy() {
116
117 // We could employ a ThreadLocal to throw an exception instead of dead locking. There is no other way
118 // to see if the calling thread is holding the read lock.
119
120 lock.writeLock().lock();
121 try {
122 target.destroy();
123 } finally {
124 lock.writeLock().unlock();
125 }
126 }
127
128 public MgnlFilter getTargetFilter() {
129 return target;
130 }
131
132 /**
133 * Construct for keeping a reference to a {@link SafeDestroyMgnlFilterWrapper} and change it with the guarantee that
134 * the returned previous reference will not get more read locks. It's then safe to destroy the returned reference.
135 */
136 public static class Switcher {
137
138 private SafeDestroyMgnlFilterWrapper filter;
139
140 /**
141 * Replaces the current filter with a new one and returns the previous filter. The returned filter is possibly still
142 * in use by currently executing requests but it will not be used by any more requests after this method returns.
143 * <p/>
144 * Notes about the returned filter:
145 * <ul>
146 * <li>No methods other than destroy() should be called on the returned filter</li>
147 * <li>The destroy() method will wait for all requests to complete before it is destroyed</li>
148 * <li>Calling the destroy() method from a request that has entered the filter WILL RESULT IN DEAD LOCK.</li>
149 * </ul>
150 */
151 public synchronized SafeDestroyMgnlFilterWrapper replaceFilter(SafeDestroyMgnlFilterWrapper newFilter) {
152 SafeDestroyMgnlFilterWrapper oldFilter = filter;
153 filter = newFilter;
154 return oldFilter;
155 }
156
157 /**
158 * Returns the current filter with a read lock held for the current thread. After use the thread must relinquish the
159 * lock by calling releaseReadLock(). If no filter has been set this method returns null and no lock is taken.
160 */
161 public synchronized SafeDestroyMgnlFilterWrapper getFilterAndAcquireReadLock() {
162 if (filter == null) {
163 return null;
164 }
165 filter.acquireReadLock();
166 return filter;
167 }
168
169 /**
170 * This method is provided for introspection only. It is inherently UNSAFE as using this method and then
171 * acquiring the read lock on the returned filter breaks the guarantee that a replaced reference wont get more
172 * read locks.
173 */
174 public synchronized SafeDestroyMgnlFilterWrapper getFilter() {
175 return filter;
176 }
177 }
178 }