001    // Copyright 2004, 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.engine;
016    
017    import java.io.IOException;
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import javax.servlet.http.HttpServletRequest;
022    import javax.servlet.http.HttpServletResponse;
023    import javax.servlet.http.HttpSession;
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.tapestry.IRequestCycle;
027    import org.apache.tapestry.Tapestry;
028    import org.apache.tapestry.services.LinkFactory;
029    
030    /**
031     * Restarts the Tapestry application. This is normally reserved for dealing with catastrophic
032     * failures of the application. Discards the {@link javax.servlet.http.HttpSession}, if any, and
033     * redirects to the Tapestry application servlet URL (invoking the {@link HomeService}).
034     * 
035     * @author Howard Lewis Ship
036     * @since 1.0.9
037     */
038    
039    public class RestartService implements IEngineService
040    {
041        /** @since 4.0 */
042        private Log _log;
043    
044        /** @since 4.0 */
045        private HttpServletRequest _request;
046    
047        /** @since 4.0 */
048        private HttpServletResponse _response;
049    
050        /** @since 4.0 */
051        private LinkFactory _linkFactory;
052    
053        /** @since 4.0 */
054        private String _servletPath;
055    
056        public ILink getLink(boolean post, Object parameter)
057        {
058            if (parameter != null)
059                throw new IllegalArgumentException(EngineMessages.serviceNoParameter(this));
060    
061            Map parameters = new HashMap();
062    
063            return _linkFactory.constructLink(this, post, parameters, true);
064        }
065    
066        public void service(IRequestCycle cycle) throws IOException
067        {
068            HttpSession session = _request.getSession(false);
069    
070            if (session != null)
071            {
072                try
073                {
074                    session.invalidate();
075                }
076                catch (IllegalStateException ex)
077                {
078                    _log.warn("Exception thrown invalidating HttpSession.", ex);
079    
080                    // Otherwise, ignore it.
081                }
082            }
083    
084            String url = cycle.getAbsoluteURL(_servletPath);
085    
086            _response.sendRedirect(url);
087        }
088    
089        public String getName()
090        {
091            return Tapestry.RESTART_SERVICE;
092        }
093    
094        /** @since 4.0 */
095        public void setLog(Log log)
096        {
097            _log = log;
098        }
099    
100        /** @since 4.0 */
101        public void setRequest(HttpServletRequest request)
102        {
103            _request = request;
104        }
105    
106        /** @since 4.0 */
107        public void setResponse(HttpServletResponse response)
108        {
109            _response = response;
110        }
111    
112        /** @since 4.0 */
113        public void setLinkFactory(LinkFactory linkFactory)
114        {
115            _linkFactory = linkFactory;
116        }
117    
118        /** @since 4.0 */
119        public void setServletPath(String servletPath)
120        {
121            _servletPath = servletPath;
122        }
123    }