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 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.serviceNoParameter(this)); 062 063 Map parameters = new HashMap(); 064 065 return _linkFactory.constructLink(this, post, parameters, true); 066 } 067 068 public void service(IRequestCycle cycle) throws IOException 069 { 070 HttpSession session = _request.getSession(false); 071 072 if (session != null) 073 { 074 try 075 { 076 session.invalidate(); 077 } 078 catch (IllegalStateException ex) 079 { 080 _log.warn("Exception thrown invalidating HttpSession.", ex); 081 082 // Otherwise, ignore it. 083 } 084 } 085 086 String url = cycle.getAbsoluteURL(_servletPath); 087 088 _response.sendRedirect(url); 089 } 090 091 public String getName() 092 { 093 return Tapestry.RESTART_SERVICE; 094 } 095 096 /** @since 4.0 */ 097 public void setLog(Log log) 098 { 099 _log = log; 100 } 101 102 /** @since 4.0 */ 103 public void setRequest(HttpServletRequest request) 104 { 105 _request = request; 106 } 107 108 /** @since 4.0 */ 109 public void setResponse(HttpServletResponse response) 110 { 111 _response = response; 112 } 113 114 /** @since 4.0 */ 115 public void setLinkFactory(LinkFactory linkFactory) 116 { 117 _linkFactory = linkFactory; 118 } 119 120 /** @since 4.0 */ 121 public void setServletPath(String servletPath) 122 { 123 _servletPath = servletPath; 124 } 125 }