KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > webmanager > WebManagerPortlet


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.console.webmanager;
19
20 import org.apache.geronimo.console.BasePortlet;
21 import org.apache.geronimo.console.util.PortletManager;
22 import org.apache.geronimo.management.geronimo.WebContainer;
23 import org.apache.geronimo.management.geronimo.WebManager;
24 import org.apache.geronimo.management.geronimo.stats.WebContainerStats;
25 import org.apache.geronimo.management.StatisticsProvider;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import javax.portlet.ActionRequest;
30 import javax.portlet.ActionResponse;
31 import javax.portlet.PortletConfig;
32 import javax.portlet.PortletException;
33 import javax.portlet.PortletRequestDispatcher;
34 import javax.portlet.RenderRequest;
35 import javax.portlet.RenderResponse;
36 import javax.portlet.WindowState;
37 import java.io.IOException JavaDoc;
38
39 /**
40  * Basic portlet showing statistics for a web container
41  *
42  * @version $Rev: 476321 $ $Date: 2006-11-17 16:18:49 -0500 (Fri, 17 Nov 2006) $
43  */

44 public class WebManagerPortlet extends BasePortlet {
45     private final static Log log = LogFactory.getLog(WebManagerPortlet.class);
46
47     private PortletRequestDispatcher normalView;
48
49     private PortletRequestDispatcher maximizedView;
50
51     private PortletRequestDispatcher helpView;
52
53     public void processAction(ActionRequest actionRequest,
54                               ActionResponse actionResponse) throws PortletException, IOException JavaDoc {
55         try {
56             WebManager[] managers = PortletManager.getCurrentServer(actionRequest).getWebManagers();
57             if (managers != null) {
58                 WebManager manager = managers[0]; //todo: handle multiple
59
WebContainer[] containers = (WebContainer[]) manager.getContainers();
60                 if (containers != null) {
61                     WebContainer container = containers[0]; //todo: handle multiple
62
String JavaDoc server = getWebServerType(container.getClass());
63                     String JavaDoc action = actionRequest.getParameter("stats");
64                     if (action != null) {
65                         boolean stats = action.equals("true");
66                         if(server.equals(WEB_SERVER_JETTY)) {
67                             setProperty(container, "collectStatistics", stats ? Boolean.TRUE : Boolean.FALSE);
68                         }
69                         else if (server.equals(WEB_SERVER_TOMCAT)) {
70                             //todo: Any Tomcat specific processing?
71
}
72                         else {
73                             log.error("Unrecognized Web Container");
74                         }
75                     }
76                     if (actionRequest.getParameter("resetStats") != null) {
77                         if(server.equals(WEB_SERVER_JETTY)) {
78                             callOperation(container, "resetStatistics", null);
79                         }
80                         else if (server.equals(WEB_SERVER_TOMCAT)) {
81                             //todo: Any Tomcat specific processing?
82
}
83                         else {
84                             log.error("Unrecognized Web Container");
85                         }
86                     }
87                 }
88                 else {
89                     log.error("Error attempting to retrieve the web containers");
90                 }
91             }
92             else {
93                 log.error("Error attempting to retrieve the web managers");
94             }
95         } catch (Exception JavaDoc e) {
96             throw new PortletException(e);
97         }
98     }
99
100     protected void doView(RenderRequest renderRequest,
101                           RenderResponse renderResponse) throws IOException JavaDoc, 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 JavaDoc(webStats.getTotalRequestCount().getCount()));
117                             renderRequest.setAttribute("totalConnectionCount", new Long JavaDoc(webStats.getTotalConnectionCount().getCount()));
118                             renderRequest.setAttribute("totalErrorCount", new Long JavaDoc(webStats.getTotalErrorCount().getCount()));
119                             renderRequest.setAttribute("activeRequestCountCurrent", new Long JavaDoc(webStats.getActiveRequestCount().getCurrent()));
120                             renderRequest.setAttribute("activeRequestCountLow", new Long JavaDoc(webStats.getActiveRequestCount().getLowWaterMark()));
121                             renderRequest.setAttribute("activeRequestCountHigh", new Long JavaDoc(webStats.getActiveRequestCount().getHighWaterMark()));
122                             renderRequest.setAttribute("connectionRequestCountCurrent", new Long JavaDoc(webStats.getConnectionRequestCount().getCurrent()));
123                             renderRequest.setAttribute("connectionRequestCountLow", new Long JavaDoc(webStats.getConnectionRequestCount().getLowWaterMark()));
124                             renderRequest.setAttribute("connectionRequestCountHigh", new Long JavaDoc(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 JavaDoc(webStats.getOpenConnectionCount().getCurrent()));
127                             renderRequest.setAttribute("openConnectionCountLow", new Long JavaDoc(webStats.getOpenConnectionCount().getLowWaterMark()));
128                             renderRequest.setAttribute("openConnectionCountHigh", new Long JavaDoc(webStats.getOpenConnectionCount().getHighWaterMark()));
129                             renderRequest.setAttribute("requestDurationCount", new Long JavaDoc(webStats.getRequestDuration().getCount()));
130                             renderRequest.setAttribute("requestDurationMinTime", new Long JavaDoc(webStats.getRequestDuration().getMinTime()));
131                             renderRequest.setAttribute("requestDurationMaxTime", new Long JavaDoc(webStats.getRequestDuration().getMaxTime()));
132                             renderRequest.setAttribute("requestDurationTotalTime", new Long JavaDoc(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 JavaDoc(webStats.getConnectionDuration().getCount()));
135                             renderRequest.setAttribute("connectionDurationMinTime", new Long JavaDoc(webStats.getConnectionDuration().getMinTime()));
136                             renderRequest.setAttribute("connectionDurationMaxTime", new Long JavaDoc(webStats.getConnectionDuration().getMaxTime()));
137                             renderRequest.setAttribute("connectionDurationTotalTime", new Long JavaDoc(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 JavaDoc 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 JavaDoc {
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 }
187
Popular Tags