001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.services.impl;
016    
017    import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
018    
019    import java.io.IOException;
020    
021    import org.apache.hivemind.ErrorLog;
022    import org.apache.hivemind.HiveMind;
023    import org.apache.tapestry.services.ResetEventHub;
024    import org.apache.tapestry.services.WebRequestServicer;
025    import org.apache.tapestry.services.WebRequestServicerFilter;
026    import org.apache.tapestry.web.WebRequest;
027    import org.apache.tapestry.web.WebResponse;
028    
029    /**
030     * Filter whose job is to invoke
031     * {@link org.apache.tapestry.services.ResetEventHub#fireResetEvent()} after the request has
032     * been processed. This filter is only contributed into the
033     * tapestry.request.WebRequestServicerPipeline configuration if the
034     * org.apache.tapestry.disable-caching system property is true.
035     * 
036     * @author Howard M. Lewis Ship
037     * @since 4.0
038     */
039    public class DisableCachingFilter implements WebRequestServicerFilter
040    {
041        private final ReentrantLock _lock = new ReentrantLock();
042        
043        private ErrorLog _errorLog;
044        
045        private ResetEventHub _resetEventHub;
046        
047        public void service(WebRequest request, WebResponse response, WebRequestServicer servicer)
048                throws IOException
049        {
050            try
051            {
052                _lock.lock();
053                
054                servicer.service(request, response);
055            }
056            finally
057            {
058                fireResetEvent();
059                
060                _lock.unlock();
061            }
062    
063        }
064    
065        private void fireResetEvent()
066        {
067            try
068            {
069                _resetEventHub.fireResetEvent();
070            }
071            catch (Exception ex)
072            {
073                _errorLog.error(ImplMessages.errorResetting(ex), HiveMind.getLocation(ex), ex);
074            }
075        }
076        
077        public void setResetEventHub(ResetEventHub resetEventCoordinator)
078        {
079            _resetEventHub = resetEventCoordinator;
080        }
081    
082        public void setErrorLog(ErrorLog errorLog)
083        {
084            _errorLog = errorLog;
085        }
086    }