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.webmanager;
019    
020    import org.apache.geronimo.console.BasePortlet;
021    import org.apache.geronimo.console.util.PortletManager;
022    import org.apache.geronimo.console.util.TimeUtils;
023    import org.apache.geronimo.management.geronimo.WebContainer;
024    import org.apache.geronimo.management.geronimo.WebManager;
025    import org.apache.geronimo.management.geronimo.stats.WebContainerStats;
026    import org.apache.geronimo.management.StatisticsProvider;
027    import org.apache.geronimo.management.LazyStatisticsProvider;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.PortletException;
035    import javax.portlet.PortletRequestDispatcher;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    import javax.portlet.WindowState;
039    import java.io.IOException;
040    
041    /**
042     * Basic portlet showing statistics for a web container
043     *
044     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
045     */
046    public class WebManagerPortlet extends BasePortlet {
047        private final static Log log = LogFactory.getLog(WebManagerPortlet.class);
048    
049        private PortletRequestDispatcher normalView;
050    
051        private PortletRequestDispatcher maximizedView;
052    
053        private PortletRequestDispatcher helpView;
054    
055        public void processAction(ActionRequest actionRequest,
056                                  ActionResponse actionResponse) throws PortletException, IOException {
057            try {
058                WebManager[] managers = PortletManager.getCurrentServer(actionRequest).getWebManagers();
059                if (managers != null) {
060                    WebManager manager = managers[0];  //todo: handle multiple
061                    WebContainer[] containers = (WebContainer[]) manager.getContainers();
062                    if (containers != null) {
063                        WebContainer container = containers[0];  //todo: handle multiple
064                        String server = getWebServerType(container.getClass());
065                        if (actionRequest.getParameter("stats") != null) {
066                            Boolean stats = actionRequest.getParameter("stats").equals("true") ? Boolean.TRUE : Boolean.FALSE;
067                            if(server.equals(WEB_SERVER_JETTY)) {
068                                setProperty(container, "statsOn", stats);
069                            }
070                            else if (server.equals(WEB_SERVER_TOMCAT)) {
071                                //todo:   Any Tomcat specific processing?
072                            }
073                            else {
074                                log.error("Unrecognized Web Container");
075                            }
076                        }
077                        if (actionRequest.getParameter("resetStats") != null) {
078                            if(server.equals(WEB_SERVER_JETTY)) {
079                                callOperation(container, "resetStats", null);
080                            }
081                            else if (server.equals(WEB_SERVER_TOMCAT)) {
082                                //todo:   Any Tomcat specific processing?
083                            }
084                            else {
085                                log.error("Unrecognized Web Container");
086                            }
087                        }
088                    }
089                    else {
090                        log.error("Error attempting to retrieve the web containers");
091                    }
092                }
093                else {
094                    log.error("Error attempting to retrieve the web managers");
095                }
096            } catch (Exception e) {
097                throw new PortletException(e);
098            }
099        }
100    
101        protected void doView(RenderRequest renderRequest,
102                              RenderResponse renderResponse) throws IOException, PortletException {
103            if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
104                return;
105            }
106            try {
107                WebManager[] managers = PortletManager.getCurrentServer(renderRequest).getWebManagers();
108                if (managers != null) {
109                    WebManager manager = managers[0];  //todo: handle multiple
110                    WebContainer[] containers = (WebContainer[]) manager.getContainers();
111                    if (containers != null) {
112                        WebContainer container = containers[0];  //todo: handle multiple
113                        if(container.isStatisticsProvider()) {
114                            boolean populateStats = false;
115                            renderRequest.setAttribute("statsSupported", Boolean.TRUE);  // indicate that statistics are supported for this container
116    
117                            if (container instanceof LazyStatisticsProvider) {
118                                renderRequest.setAttribute("statsLazy", Boolean.TRUE);   // indicate that enable/disable should be shown for this container
119    
120                                if (((LazyStatisticsProvider)container).isStatsOn()) {
121                                    renderRequest.setAttribute("statsOn", Boolean.TRUE); // indicate that stats are to be displayed
122                                    populateStats = true;      // this is a Lazy provider and stats are enabled so populate the stats
123                                } else {
124                                    renderRequest.setAttribute("statsOn", Boolean.FALSE);  // indicate that stats are currently disabled
125                                    renderRequest.setAttribute("statsMessage", "Statistics are not currently being collected.");
126                                }
127                            } else {
128                                renderRequest.setAttribute("statsLazy", Boolean.FALSE);  // indicate that enable/disable should not be shown for this container
129                                renderRequest.setAttribute("statsOn", Boolean.TRUE);     // indicate that stats are to be displayed
130                                populateStats=true;     // this is not a lazy provider so just populate the stats
131                            }
132    
133                            if (populateStats) {
134                                // get the detailed stats
135                                WebContainerStats webStats = (WebContainerStats) ((StatisticsProvider)container).getStats();
136                                //renderRequest.setAttribute("totalRequestCount", new Long(webStats.getTotalRequestCount().getCount()));
137                                renderRequest.setAttribute("activeRequestCountCurrent", new Long(webStats.getActiveRequestCount().getCurrent()));
138                                renderRequest.setAttribute("activeRequestCountLow", new Long(webStats.getActiveRequestCount().getLowWaterMark()));
139                                renderRequest.setAttribute("activeRequestCountHigh", new Long(webStats.getActiveRequestCount().getHighWaterMark()));
140                                
141                                Long count = new Long(webStats.getRequestDuration().getCount());
142                                Long totalTime = new Long(webStats.getRequestDuration().getTotalTime());
143                                renderRequest.setAttribute("totalRequestCount", count);
144                                renderRequest.setAttribute("requestDurationMinTime", new Long(webStats.getRequestDuration().getMinTime()));
145                                renderRequest.setAttribute("requestDurationMaxTime", new Long(webStats.getRequestDuration().getMaxTime()));
146                                renderRequest.setAttribute("requestDurationTotalTime", totalTime);
147                                // renderRequest.setAttribute("requestDurationAvg", new Long(webStats.getRequestDurationAvg().getCount()));
148                                Long avg = count == 0 ? 0: new Long(totalTime/count);
149                                renderRequest.setAttribute("requestDurationAvg", avg);
150                                renderRequest.setAttribute("response1xx", new Long(webStats.getResponses1xx().getCount()));
151                                renderRequest.setAttribute("response2xx", new Long(webStats.getResponses2xx().getCount()));
152                                renderRequest.setAttribute("response3xx", new Long(webStats.getResponses3xx().getCount()));
153                                renderRequest.setAttribute("response4xx", new Long(webStats.getResponses4xx().getCount()));
154                                renderRequest.setAttribute("response5xx", new Long(webStats.getResponses5xx().getCount()));
155                                renderRequest.setAttribute("elapsedTime", TimeUtils.formatDuration(webStats.getStatsOnMs().getCount()));
156                            }
157                        } else {
158                            renderRequest.setAttribute("statsSupported", Boolean.FALSE);   // indicate that statistics are not supported for this container
159                            renderRequest.setAttribute("statsMessage", "Web statistics are not supported for the current web container.");
160                        }
161                    } else {
162                        log.error("Error attempting to retrieve the web containers");
163                    }
164                } else {
165                    log.error("Error attempting to retrieve the web managers");
166                }
167            } catch (Exception e) {
168                throw new PortletException(e);
169            }
170            if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
171                normalView.include(renderRequest, renderResponse);
172            } else {
173                maximizedView.include(renderRequest, renderResponse);
174            }
175        }
176    
177        protected void doHelp(RenderRequest renderRequest,
178                              RenderResponse renderResponse) throws PortletException, IOException {
179            helpView.include(renderRequest, renderResponse);
180        }
181    
182        public void init(PortletConfig portletConfig) throws PortletException {
183            super.init(portletConfig);
184    
185            normalView = portletConfig.getPortletContext().getRequestDispatcher(
186                    "/WEB-INF/view/webmanager/normal.jsp");
187            maximizedView = portletConfig.getPortletContext().getRequestDispatcher(
188                    "/WEB-INF/view/webmanager/maximized.jsp");
189            helpView = portletConfig.getPortletContext().getRequestDispatcher(
190                    "/WEB-INF/view/webmanager/help.jsp");
191        }
192    
193        public void destroy() {
194            helpView = null;
195            normalView = null;
196            maximizedView = null;
197            super.destroy();
198        }
199    
200    }