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 */ 017package org.apache.wicket.jmx.wrapper; 018 019import java.io.IOException; 020 021import org.apache.wicket.Application; 022import org.apache.wicket.jmx.RequestLoggerMBean; 023import org.apache.wicket.protocol.http.WebApplication; 024 025 026/** 027 * Exposes {@link org.apache.wicket.protocol.http.RequestLogger} for JMX. 028 * 029 * @author eelcohillenius 030 */ 031public class RequestLogger implements RequestLoggerMBean 032{ 033 private final Application application; 034 035 private final WebApplication webApplication; 036 037 /** 038 * Construct. 039 * 040 * @param application 041 * The application 042 */ 043 public RequestLogger(final Application application) 044 { 045 this.application = application; 046 047 // do this so that we don't have to cast all the time 048 if (application instanceof WebApplication) 049 { 050 webApplication = (WebApplication)application; 051 } 052 else 053 { 054 webApplication = null; 055 } 056 } 057 058 /** 059 * @see org.apache.wicket.jmx.RequestLoggerMBean#getNumberOfCreatedSessions() 060 */ 061 @Override 062 public Integer getNumberOfCreatedSessions() throws IOException 063 { 064 org.apache.wicket.protocol.http.IRequestLogger logger = getRequestLogger(); 065 if (logger != null) 066 { 067 return logger.getTotalCreatedSessions(); 068 } 069 return null; 070 } 071 072 /** 073 * @see org.apache.wicket.jmx.RequestLoggerMBean#getNumberOfLiveSessions() 074 */ 075 @Override 076 public Integer getNumberOfLiveSessions() throws IOException 077 { 078 org.apache.wicket.protocol.http.IRequestLogger logger = getRequestLogger(); 079 if (logger != null) 080 { 081 return logger.getLiveSessions().length; 082 } 083 return null; 084 } 085 086 /** 087 * @see org.apache.wicket.jmx.RequestLoggerMBean#getPeakNumberOfSessions() 088 */ 089 @Override 090 public Integer getPeakNumberOfSessions() throws IOException 091 { 092 org.apache.wicket.protocol.http.IRequestLogger logger = getRequestLogger(); 093 if (logger != null) 094 { 095 return logger.getPeakSessions(); 096 } 097 return null; 098 } 099 100 /** 101 * @see org.apache.wicket.jmx.RequestLoggerMBean#getNumberOfCurrentActiveRequests() 102 */ 103 @Override 104 public Integer getNumberOfCurrentActiveRequests() throws IOException 105 { 106 org.apache.wicket.protocol.http.IRequestLogger logger = getRequestLogger(); 107 if (logger != null) 108 { 109 return Integer.valueOf(logger.getCurrentActiveRequestCount()); 110 } 111 return null; 112 } 113 114 /** 115 * @see org.apache.wicket.jmx.RequestLoggerMBean#getPeakNumberOfActiveRequests() 116 */ 117 @Override 118 public Integer getPeakNumberOfActiveRequests() throws IOException 119 { 120 org.apache.wicket.protocol.http.IRequestLogger logger = getRequestLogger(); 121 if (logger != null) 122 { 123 return Integer.valueOf(logger.getPeakActiveRequestCount()); 124 } 125 return null; 126 } 127 128 /** 129 * @see org.apache.wicket.jmx.RequestLoggerMBean#restart() 130 */ 131 @Override 132 public void restart() throws IOException 133 { 134 if (webApplication != null) 135 { 136 webApplication.getRequestLoggerSettings().setRequestLoggerEnabled(false); 137 webApplication.getRequestLogger(); 138 webApplication.getRequestLoggerSettings().setRequestLoggerEnabled(true); 139 } 140 } 141 142 /** 143 * @see org.apache.wicket.jmx.RequestLoggerMBean#stop() 144 */ 145 @Override 146 public void stop() throws IOException 147 { 148 if (webApplication != null) 149 { 150 webApplication.getRequestLoggerSettings().setRequestLoggerEnabled(false); 151 } 152 } 153 154 /** 155 * Gets the request logger for this application. 156 * 157 * @return The request logger or null if no request is active, or if this is not a web 158 * application 159 */ 160 protected org.apache.wicket.protocol.http.IRequestLogger getRequestLogger() 161 { 162 if (application instanceof WebApplication) 163 { 164 return application.getRequestLogger(); 165 } 166 return null; 167 } 168}