001: /*
002: * ==============================================================================
003: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
004: * use this file except in compliance with the License. You may obtain a copy of
005: * the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
011: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
012: * License for the specific language governing permissions and limitations under
013: * the License.
014: */
015: package wicket.jmx;
016:
017: import java.io.IOException;
018:
019: import wicket.protocol.http.WebApplication;
020:
021: /**
022: * Exposes {@link wicket.protocol.http.RequestLogger} for JMX.
023: *
024: * @author eelcohillenius
025: */
026: public class RequestLogger implements RequestLoggerMBean {
027: private final wicket.Application application;
028:
029: private final WebApplication webApplication;
030:
031: /**
032: * Construct.
033: *
034: * @param application
035: * The application
036: */
037: public RequestLogger(wicket.Application application) {
038: this .application = application;
039:
040: // do this so that we don't have to cast all the time
041: if (application instanceof WebApplication) {
042: this .webApplication = (WebApplication) application;
043: } else {
044: this .webApplication = null;
045: }
046: }
047:
048: /**
049: * @see wicket.jmx.RequestLoggerMBean#getNumberOfCreatedSessions()
050: */
051: public Integer getNumberOfCreatedSessions() throws IOException {
052: wicket.protocol.http.RequestLogger logger = getRequestLogger();
053: if (logger != null) {
054: return Integer.valueOf(logger.getTotalCreatedSessions());
055: }
056: return null;
057: }
058:
059: /**
060: * @see wicket.jmx.RequestLoggerMBean#getNumberOfLiveSessions()
061: */
062: public Integer getNumberOfLiveSessions() throws IOException {
063: wicket.protocol.http.RequestLogger logger = getRequestLogger();
064: if (logger != null) {
065: return Integer.valueOf(logger.getLiveSessions().size());
066: }
067: return null;
068: }
069:
070: /**
071: * @see wicket.jmx.RequestLoggerMBean#getPeakNumberOfSessions()
072: */
073: public Integer getPeakNumberOfSessions() throws IOException {
074: wicket.protocol.http.RequestLogger logger = getRequestLogger();
075: if (logger != null) {
076: return Integer.valueOf(logger.getPeakSessions());
077: }
078: return null;
079: }
080:
081: /**
082: * @see wicket.jmx.RequestLoggerMBean#restart()
083: */
084: public void restart() throws IOException {
085: if (webApplication != null) {
086: webApplication
087: .setRequestLogger(new wicket.protocol.http.RequestLogger());
088: }
089: }
090:
091: /**
092: * @see wicket.jmx.RequestLoggerMBean#stop()
093: */
094: public void stop() throws IOException {
095: if (webApplication != null) {
096: webApplication.setRequestLogger(null);
097: }
098: }
099:
100: /**
101: * Gets the request logger for this application.
102: *
103: * @return The request logger or null if no request is active, or if this is
104: * not a web application
105: */
106: protected wicket.protocol.http.RequestLogger getRequestLogger() {
107: if (application instanceof WebApplication) {
108: return ((WebApplication) application).getRequestLogger();
109: }
110: return null;
111: }
112: }
|