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