001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.console.servermanager;
019    
020    import java.io.IOException;
021    
022    import javax.portlet.ActionRequest;
023    import javax.portlet.ActionResponse;
024    import javax.portlet.PortletConfig;
025    import javax.portlet.PortletException;
026    import javax.portlet.PortletRequestDispatcher;
027    import javax.portlet.RenderRequest;
028    import javax.portlet.RenderResponse;
029    
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    import org.apache.geronimo.console.BasePortlet;
033    import org.apache.geronimo.kernel.Kernel;
034    import org.apache.geronimo.kernel.KernelRegistry;
035    import org.apache.geronimo.system.main.Daemon;
036    
037    public class ServerManagerPortlet extends BasePortlet {
038    
039        private static final Log log = LogFactory.getLog(ServerManagerPortlet.class);
040    
041        private PortletRequestDispatcher normalView;
042    
043        private PortletRequestDispatcher shutdownView;
044    
045        private PortletRequestDispatcher helpView;
046    
047        private Kernel kernel;
048    
049        public void processAction(ActionRequest actionRequest,
050                ActionResponse actionResponse) throws PortletException, IOException {
051            if (actionRequest.getParameter("reboot") != null) {
052                log.info("Reboot initiated by user request: " + actionRequest.getUserPrincipal().getName());
053                new Thread() {
054                    public void run() {
055                        try {
056                            Thread.sleep(2000);
057                        } catch (InterruptedException e) {
058                        }
059                        kernel.shutdown();
060                        Daemon.main(new String[0]);
061                    }
062                }.start();
063            } else if(actionRequest.getParameter("shutdown") != null) {
064                actionResponse.setRenderParameter("shutdown", actionRequest.getParameter("shutdown"));
065            }
066        }
067    
068        protected void doView(RenderRequest request, RenderResponse response)
069                throws PortletException, IOException {
070            if (request.getParameter("shutdown") != null) {
071                log.info("Shutting down by user request: " + request.getUserPrincipal().getName());
072                shutdownView.include(request, response);
073                response.flushBuffer();
074                kernel.shutdown();
075                System.exit(0);
076            } else {
077                normalView.include(request, response);
078            }
079        }
080    
081        protected void doHelp(RenderRequest renderRequest,
082                RenderResponse renderResponse) throws PortletException, IOException {
083            helpView.include(renderRequest, renderResponse);
084        }
085    
086        public void init(PortletConfig portletConfig) throws PortletException {
087            super.init(portletConfig);
088            normalView = portletConfig.getPortletContext().getRequestDispatcher(
089                    "/WEB-INF/view/servermanager/normal.jsp");
090            shutdownView = portletConfig.getPortletContext().getRequestDispatcher(
091                    "/WEB-INF/view/servermanager/shutdown.jsp");
092            helpView = portletConfig.getPortletContext().getRequestDispatcher(
093                    "/WEB-INF/view/servermanager/help.jsp");
094            kernel = KernelRegistry.getSingleKernel();
095        }
096    
097        public void destroy() {
098            normalView = null;
099            shutdownView = null;
100            helpView = null;
101            super.destroy();
102        }
103    
104    }