1 3 package com.sslexplorer.setup.actions; 4 5 import java.text.SimpleDateFormat ; 6 import java.util.Date ; 7 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpServletResponse ; 10 import javax.servlet.http.HttpSession ; 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 import org.apache.struts.Globals; 15 import org.apache.struts.action.ActionForm; 16 import org.apache.struts.action.ActionForward; 17 import org.apache.struts.action.ActionMapping; 18 import org.apache.struts.action.ActionMessage; 19 import org.apache.struts.action.ActionMessages; 20 21 import com.sslexplorer.boot.ContextHolder; 22 import com.sslexplorer.core.BundleActionMessage; 23 import com.sslexplorer.core.CoreServlet; 24 import com.sslexplorer.core.CoreUtil; 25 import com.sslexplorer.core.GlobalWarning; 26 import com.sslexplorer.core.actions.AuthenticatedDispatchAction; 27 import com.sslexplorer.policyframework.Permission; 28 import com.sslexplorer.policyframework.PolicyConstants; 29 import com.sslexplorer.policyframework.PolicyUtil; 30 import com.sslexplorer.security.Constants; 31 import com.sslexplorer.security.SessionInfo; 32 import com.sslexplorer.setup.forms.ShutdownForm; 33 import com.sslexplorer.tasks.shutdown.ShutdownTimerTask; 34 import com.sslexplorer.tasks.timer.StoppableTimer; 35 36 39 public class ShutdownAction extends AuthenticatedDispatchAction { 40 final static Log log = LogFactory.getLog(ShutdownAction.class); 41 42 45 public ShutdownAction() { 46 super(PolicyConstants.SERVICE_CONTROL_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_SHUTDOWN, 47 PolicyConstants.PERM_RESTART }); 48 } 49 50 public ActionForward sendMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request, 51 HttpServletResponse response) throws Exception { 52 return CoreUtil.addParameterToForward(mapping.findForward("message"), "users", "*"); 53 } 54 55 public ActionForward installShutdown(ActionMapping mapping, ActionForm form, HttpServletRequest request, 56 HttpServletResponse response) throws Exception { 57 ShutdownForm shutdownForm = (ShutdownForm) form; 58 performShutdown(request, shutdownForm); 59 return mapping.findForward("installShutdown"); 60 } 61 62 public ActionForward confirmed(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 63 throws Exception { 64 ShutdownForm shutdownForm = (ShutdownForm) form; 65 performShutdown(request, shutdownForm); 66 return mapping.findForward("refresh"); 68 } 69 70 public ActionForward shutdown(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 71 throws Exception { 72 ShutdownForm shutdownForm = (ShutdownForm) form; 73 PolicyUtil.checkPermission(PolicyConstants.SERVICE_CONTROL_RESOURCE_TYPE, shutdownForm.getShutdownOperation() 74 .equals(ShutdownForm.SHUTDOWN) ? PolicyConstants.PERM_SHUTDOWN : PolicyConstants.PERM_RESTART, request); 75 if (shutdownForm.getAlreadyPerforming()) { 76 shutdownForm.setReferer(CoreUtil.getReferer(request)); 77 StoppableTimer timer = (StoppableTimer) CoreServlet.getServlet().getServletContext().getAttribute(StoppableTimer.NAME); 78 timer.cancelTimerTask(ShutdownTimerTask.NAME); 79 CoreUtil.removeGlobalWarning((HttpSession ) null, "shutdown.global.warning.message"); 80 shutdownForm.setAlreadyPerforming(false); 81 return mapping.findForward("refresh"); 82 } else { 83 if (shutdownForm.isImmediate()) { 84 return mapping.findForward("confirmImmediate"); 86 } else { 87 ActionForward fwd = mapping.findForward("confirmTimed"); 88 return new ActionForward(CoreUtil.addParameterToPath(fwd.getPath(), 89 "arg0", 90 new SimpleDateFormat ("HH:mm").format(new Date (System.currentTimeMillis() 91 + (Integer.parseInt(shutdownForm.getShutdownDelay()) * 60 * 1000)))), fwd.getRedirect()); 92 } 93 } 94 } 95 96 private void performShutdown(HttpServletRequest request, ShutdownForm shutdownForm) throws Exception { 97 final boolean restart = "restart".equals(shutdownForm.getShutdownOperation()); 98 if (restart) { 99 if (!ContextHolder.getContext().isRestartAvailableMode()) { 100 ActionMessages msgs = new ActionMessages(); 101 msgs.add(Globals.ERROR_KEY, new ActionMessage("server.not.service")); 102 saveErrors(request, msgs); 103 } else { 104 doShutdown(request, shutdownForm, restart); 105 } 106 } else { 107 doShutdown(request, shutdownForm, restart); 108 } 109 } 110 111 private void doShutdown(HttpServletRequest request, ShutdownForm shutdownForm, final boolean restart) { 112 StoppableTimer timer = (StoppableTimer) CoreServlet.getServlet().getServletContext().getAttribute(StoppableTimer.NAME); 113 ShutdownTimerTask stt = new ShutdownTimerTask(restart, Integer.parseInt(shutdownForm.getShutdownDelay())); 114 timer.schedule(ShutdownTimerTask.NAME, stt, stt.getDelay()); 115 CoreUtil.addMultipleGlobalWarning(GlobalWarning.ALL_USERS, new BundleActionMessage("setup", 116 "shutdown.global.warning.message", 117 stt.getShutDownTimeString())); 118 request.getSession().setAttribute(Constants.RESTARTING, Boolean.valueOf(restart)); 119 } 120 121 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 122 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT | SessionInfo.SETUP_CONSOLE_CONTEXT; 123 } 124 } 125 | Popular Tags |