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