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: */package org.apache.geronimo.console.webmanager;
017:
018: import org.apache.geronimo.console.BasePortlet;
019: import org.apache.geronimo.console.util.PortletManager;
020: import org.apache.geronimo.console.util.TimeUtils;
021: import org.apache.geronimo.management.geronimo.WebContainer;
022: import org.apache.geronimo.management.geronimo.WebManager;
023: import org.apache.geronimo.management.geronimo.stats.WebContainerStats;
024: import org.apache.geronimo.management.StatisticsProvider;
025: import org.apache.geronimo.management.LazyStatisticsProvider;
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: 605403 $ $Date: 2007-12-18 19:27:20 -0800 (Tue, 18 Dec 2007) $
043: */
044: public class WebManagerPortlet extends BasePortlet {
045: private final static Log log = LogFactory
046: .getLog(WebManagerPortlet.class);
047:
048: private PortletRequestDispatcher normalView;
049:
050: private PortletRequestDispatcher maximizedView;
051:
052: private PortletRequestDispatcher helpView;
053:
054: public void processAction(ActionRequest actionRequest,
055: ActionResponse actionResponse) throws PortletException,
056: IOException {
057: try {
058: WebManager[] managers = PortletManager.getCurrentServer(
059: actionRequest).getWebManagers();
060: if (managers != null) {
061: WebManager manager = managers[0]; //todo: handle multiple
062: WebContainer[] containers = (WebContainer[]) manager
063: .getContainers();
064: if (containers != null) {
065: WebContainer container = containers[0]; //todo: handle multiple
066: String server = getWebServerType(container
067: .getClass());
068: if (actionRequest.getParameter("stats") != null) {
069: Boolean stats = actionRequest.getParameter(
070: "stats").equals("true") ? Boolean.TRUE
071: : Boolean.FALSE;
072: if (server.equals(WEB_SERVER_JETTY)) {
073: setProperty(container, "statsOn", stats);
074: } else if (server.equals(WEB_SERVER_TOMCAT)) {
075: //todo: Any Tomcat specific processing?
076: } else {
077: log.error("Unrecognized Web Container");
078: }
079: }
080: if (actionRequest.getParameter("resetStats") != null) {
081: if (server.equals(WEB_SERVER_JETTY)) {
082: callOperation(container, "resetStats", null);
083: } else if (server.equals(WEB_SERVER_TOMCAT)) {
084: //todo: Any Tomcat specific processing?
085: } else {
086: log.error("Unrecognized Web Container");
087: }
088: }
089: } else {
090: log
091: .error("Error attempting to retrieve the web containers");
092: }
093: } else {
094: log
095: .error("Error attempting to retrieve the web managers");
096: }
097: } catch (Exception e) {
098: throw new PortletException(e);
099: }
100: }
101:
102: protected void doView(RenderRequest renderRequest,
103: RenderResponse renderResponse) throws IOException,
104: PortletException {
105: if (WindowState.MINIMIZED
106: .equals(renderRequest.getWindowState())) {
107: return;
108: }
109: try {
110: WebManager[] managers = PortletManager.getCurrentServer(
111: renderRequest).getWebManagers();
112: if (managers != null) {
113: WebManager manager = managers[0]; //todo: handle multiple
114: WebContainer[] containers = (WebContainer[]) manager
115: .getContainers();
116: if (containers != null) {
117: WebContainer container = containers[0]; //todo: handle multiple
118: if (container.isStatisticsProvider()) {
119: boolean populateStats = false;
120: renderRequest.setAttribute("statsSupported",
121: Boolean.TRUE); // indicate that statistics are supported for this container
122:
123: if (container instanceof LazyStatisticsProvider) {
124: renderRequest.setAttribute("statsLazy",
125: Boolean.TRUE); // indicate that enable/disable should be shown for this container
126:
127: if (((LazyStatisticsProvider) container)
128: .isStatsOn()) {
129: renderRequest.setAttribute("statsOn",
130: Boolean.TRUE); // indicate that stats are to be displayed
131: populateStats = true; // this is a Lazy provider and stats are enabled so populate the stats
132: } else {
133: renderRequest.setAttribute("statsOn",
134: Boolean.FALSE); // indicate that stats are currently disabled
135: renderRequest
136: .setAttribute("statsMessage",
137: "Statistics are not currently being collected.");
138: }
139: } else {
140: renderRequest.setAttribute("statsLazy",
141: Boolean.FALSE); // indicate that enable/disable should not be shown for this container
142: renderRequest.setAttribute("statsOn",
143: Boolean.TRUE); // indicate that stats are to be displayed
144: populateStats = true; // this is not a lazy provider so just populate the stats
145: }
146:
147: if (populateStats) {
148: // get the detailed stats
149: WebContainerStats webStats = (WebContainerStats) ((StatisticsProvider) container)
150: .getStats();
151: //renderRequest.setAttribute("totalRequestCount", new Long(webStats.getTotalRequestCount().getCount()));
152: renderRequest.setAttribute(
153: "activeRequestCountCurrent",
154: new Long(webStats
155: .getActiveRequestCount()
156: .getCurrent()));
157: renderRequest.setAttribute(
158: "activeRequestCountLow",
159: new Long(webStats
160: .getActiveRequestCount()
161: .getLowWaterMark()));
162: renderRequest.setAttribute(
163: "activeRequestCountHigh",
164: new Long(webStats
165: .getActiveRequestCount()
166: .getHighWaterMark()));
167:
168: Long count = new Long(webStats
169: .getRequestDuration().getCount());
170: Long totalTime = new Long(webStats
171: .getRequestDuration()
172: .getTotalTime());
173: renderRequest.setAttribute(
174: "totalRequestCount", count);
175: renderRequest.setAttribute(
176: "requestDurationMinTime",
177: new Long(webStats
178: .getRequestDuration()
179: .getMinTime()));
180: renderRequest.setAttribute(
181: "requestDurationMaxTime",
182: new Long(webStats
183: .getRequestDuration()
184: .getMaxTime()));
185: renderRequest.setAttribute(
186: "requestDurationTotalTime",
187: totalTime);
188: // renderRequest.setAttribute("requestDurationAvg", new Long(webStats.getRequestDurationAvg().getCount()));
189: Long avg = count == 0 ? 0 : new Long(
190: totalTime / count);
191: renderRequest.setAttribute(
192: "requestDurationAvg", avg);
193: renderRequest.setAttribute("response1xx",
194: new Long(webStats.getResponses1xx()
195: .getCount()));
196: renderRequest.setAttribute("response2xx",
197: new Long(webStats.getResponses2xx()
198: .getCount()));
199: renderRequest.setAttribute("response3xx",
200: new Long(webStats.getResponses3xx()
201: .getCount()));
202: renderRequest.setAttribute("response4xx",
203: new Long(webStats.getResponses4xx()
204: .getCount()));
205: renderRequest.setAttribute("response5xx",
206: new Long(webStats.getResponses5xx()
207: .getCount()));
208: renderRequest
209: .setAttribute(
210: "elapsedTime",
211: TimeUtils
212: .formatDuration(webStats
213: .getStatsOnMs()
214: .getCount()));
215: }
216: } else {
217: renderRequest.setAttribute("statsSupported",
218: Boolean.FALSE); // indicate that statistics are not supported for this container
219: renderRequest
220: .setAttribute("statsMessage",
221: "Web statistics are not supported for the current web container.");
222: }
223: } else {
224: log
225: .error("Error attempting to retrieve the web containers");
226: }
227: } else {
228: log
229: .error("Error attempting to retrieve the web managers");
230: }
231: } catch (Exception e) {
232: throw new PortletException(e);
233: }
234: if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
235: normalView.include(renderRequest, renderResponse);
236: } else {
237: maximizedView.include(renderRequest, renderResponse);
238: }
239: }
240:
241: protected void doHelp(RenderRequest renderRequest,
242: RenderResponse renderResponse) throws PortletException,
243: IOException {
244: helpView.include(renderRequest, renderResponse);
245: }
246:
247: public void init(PortletConfig portletConfig)
248: throws PortletException {
249: super .init(portletConfig);
250:
251: normalView = portletConfig.getPortletContext()
252: .getRequestDispatcher(
253: "/WEB-INF/view/webmanager/normal.jsp");
254: maximizedView = portletConfig.getPortletContext()
255: .getRequestDispatcher(
256: "/WEB-INF/view/webmanager/maximized.jsp");
257: helpView = portletConfig.getPortletContext()
258: .getRequestDispatcher(
259: "/WEB-INF/view/webmanager/help.jsp");
260: }
261:
262: public void destroy() {
263: helpView = null;
264: normalView = null;
265: maximizedView = null;
266: super.destroy();
267: }
268:
269: }
|