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