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.management.geronimo.WebContainer;
023    import org.apache.geronimo.management.geronimo.WebManager;
024    import org.apache.geronimo.management.geronimo.stats.WebContainerStats;
025    import org.apache.geronimo.management.StatisticsProvider;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    import javax.portlet.ActionRequest;
030    import javax.portlet.ActionResponse;
031    import javax.portlet.PortletConfig;
032    import javax.portlet.PortletException;
033    import javax.portlet.PortletRequestDispatcher;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    import javax.portlet.WindowState;
037    import java.io.IOException;
038    
039    /**
040     * Basic portlet showing statistics for a web container
041     *
042     * @version $Rev: 476321 $ $Date: 2006-11-17 16:18:49 -0500 (Fri, 17 Nov 2006) $
043     */
044    public class WebManagerPortlet extends BasePortlet {
045        private final static Log log = LogFactory.getLog(WebManagerPortlet.class);
046    
047        private PortletRequestDispatcher normalView;
048    
049        private PortletRequestDispatcher maximizedView;
050    
051        private PortletRequestDispatcher helpView;
052    
053        public void processAction(ActionRequest actionRequest,
054                                  ActionResponse actionResponse) throws PortletException, IOException {
055            try {
056                WebManager[] managers = PortletManager.getCurrentServer(actionRequest).getWebManagers();
057                if (managers != null) {
058                    WebManager manager = managers[0];  //todo: handle multiple
059                    WebContainer[] containers = (WebContainer[]) manager.getContainers();
060                    if (containers != null) {
061                        WebContainer container = containers[0];  //todo: handle multiple
062                        String server = getWebServerType(container.getClass());
063                        String action = actionRequest.getParameter("stats");
064                        if (action != null) {
065                            boolean stats = action.equals("true");
066                            if(server.equals(WEB_SERVER_JETTY)) {
067                                setProperty(container, "collectStatistics", stats ? Boolean.TRUE : Boolean.FALSE);
068                            }
069                            else if (server.equals(WEB_SERVER_TOMCAT)) {
070                                //todo:   Any Tomcat specific processing?
071                            }
072                            else {
073                                log.error("Unrecognized Web Container");
074                            }
075                        }
076                        if (actionRequest.getParameter("resetStats") != null) {
077                            if(server.equals(WEB_SERVER_JETTY)) {
078                                callOperation(container, "resetStatistics", null);
079                            }
080                            else if (server.equals(WEB_SERVER_TOMCAT)) {
081                                //todo:   Any Tomcat specific processing?
082                            }
083                            else {
084                                log.error("Unrecognized Web Container");
085                            }
086                        }
087                    }
088                    else {
089                        log.error("Error attempting to retrieve the web containers");
090                    }
091                }
092                else {
093                    log.error("Error attempting to retrieve the web managers");
094                }
095            } catch (Exception e) {
096                throw new PortletException(e);
097            }
098        }
099    
100        protected void doView(RenderRequest renderRequest,
101                              RenderResponse renderResponse) throws IOException, PortletException {
102            if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
103                return;
104            }
105            try {
106                WebManager[] managers = PortletManager.getCurrentServer(renderRequest).getWebManagers();
107                if (managers != null) {
108                    WebManager manager = managers[0];  //todo: handle multiple
109                    WebContainer[] containers = (WebContainer[]) manager.getContainers();
110                    if (containers != null) {
111                        WebContainer container = containers[0];  //todo: handle multiple
112                        if(container.isStatisticsProvider()) {
113                            WebContainerStats webStats = (WebContainerStats) ((StatisticsProvider)container).getStats();
114                            if (webStats.isStatsOn()) {
115                                renderRequest.setAttribute("statsOn", Boolean.TRUE);
116                                renderRequest.setAttribute("totalRequestCount", new Long(webStats.getTotalRequestCount().getCount()));
117                                renderRequest.setAttribute("totalConnectionCount", new Long(webStats.getTotalConnectionCount().getCount()));
118                                renderRequest.setAttribute("totalErrorCount", new Long(webStats.getTotalErrorCount().getCount()));
119                                renderRequest.setAttribute("activeRequestCountCurrent", new Long(webStats.getActiveRequestCount().getCurrent()));
120                                renderRequest.setAttribute("activeRequestCountLow", new Long(webStats.getActiveRequestCount().getLowWaterMark()));
121                                renderRequest.setAttribute("activeRequestCountHigh", new Long(webStats.getActiveRequestCount().getHighWaterMark()));
122                                renderRequest.setAttribute("connectionRequestCountCurrent", new Long(webStats.getConnectionRequestCount().getCurrent()));
123                                renderRequest.setAttribute("connectionRequestCountLow", new Long(webStats.getConnectionRequestCount().getLowWaterMark()));
124                                renderRequest.setAttribute("connectionRequestCountHigh", new Long(webStats.getConnectionRequestCount().getHighWaterMark()));
125        //                          renderRequest.setAttribute("connectionRequestsAve", new Long(0));   /* Can't really compute this for a range ... do we still need it (from old portlet) */
126                                renderRequest.setAttribute("openConnectionCountCurrent", new Long(webStats.getOpenConnectionCount().getCurrent()));
127                                renderRequest.setAttribute("openConnectionCountLow", new Long(webStats.getOpenConnectionCount().getLowWaterMark()));
128                                renderRequest.setAttribute("openConnectionCountHigh", new Long(webStats.getOpenConnectionCount().getHighWaterMark()));
129                                renderRequest.setAttribute("requestDurationCount", new Long(webStats.getRequestDuration().getCount()));
130                                renderRequest.setAttribute("requestDurationMinTime", new Long(webStats.getRequestDuration().getMinTime()));
131                                renderRequest.setAttribute("requestDurationMaxTime", new Long(webStats.getRequestDuration().getMaxTime()));
132                                renderRequest.setAttribute("requestDurationTotalTime", new Long(webStats.getRequestDuration().getTotalTime()));
133        //                          renderRequest.setAttribute("requestDurationAve", new Long(0));  /* Would this be valuable to calculate?  We used to show this in the old jetty only portlet */
134                                renderRequest.setAttribute("connectionDurationCount", new Long(webStats.getConnectionDuration().getCount()));
135                                renderRequest.setAttribute("connectionDurationMinTime", new Long(webStats.getConnectionDuration().getMinTime()));
136                                renderRequest.setAttribute("connectionDurationMaxTime", new Long(webStats.getConnectionDuration().getMaxTime()));
137                                renderRequest.setAttribute("connectionDurationTotalTime", new Long(webStats.getConnectionDuration().getTotalTime()));
138        //                          renderRequest.setAttribute("connectionDurationAve", new Long(0));   /* Wouldl this be valueable to calculate?  We used to show this in the old jetty only portlet */
139                            } else {
140                                renderRequest.setAttribute("statsSupported", Boolean.TRUE);
141                                renderRequest.setAttribute("statsMessage", "Statistics are not currently being collected.");
142                            }
143                        } else {
144                            renderRequest.setAttribute("statsSupported", Boolean.FALSE);
145                            renderRequest.setAttribute("statsMessage", "Web statistics are not supported for the current web container.");
146                        }
147                    } else {
148                        log.error("Error attempting to retrieve the web containers");
149                    }
150                } else {
151                    log.error("Error attempting to retrieve the web managers");
152                }
153            } catch (Exception e) {
154                throw new PortletException(e);
155            }
156            if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
157                normalView.include(renderRequest, renderResponse);
158            } else {
159                maximizedView.include(renderRequest, renderResponse);
160            }
161        }
162    
163        protected void doHelp(RenderRequest renderRequest,
164                              RenderResponse renderResponse) throws PortletException, IOException {
165            helpView.include(renderRequest, renderResponse);
166        }
167    
168        public void init(PortletConfig portletConfig) throws PortletException {
169            super.init(portletConfig);
170    
171            normalView = portletConfig.getPortletContext().getRequestDispatcher(
172                    "/WEB-INF/view/webmanager/normal.jsp");
173            maximizedView = portletConfig.getPortletContext().getRequestDispatcher(
174                    "/WEB-INF/view/webmanager/maximized.jsp");
175            helpView = portletConfig.getPortletContext().getRequestDispatcher(
176                    "/WEB-INF/view/webmanager/help.jsp");
177        }
178    
179        public void destroy() {
180            helpView = null;
181            normalView = null;
182            maximizedView = null;
183            super.destroy();
184        }
185    
186    }