001 /*
002 * $Id: ConnectionMonitor.java,v 1.5 2014/05/03 20:02:39 oboehm Exp $
003 *
004 * Copyright (c) 2012 by Oliver Boehm
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *
018 * (c)reated 07.10.2012 by oliver (ob@oasd.de)
019 */
020
021 package patterntesting.runtime.monitor.db;
022
023 import java.sql.Connection;
024 import java.util.*;
025 import java.util.concurrent.CopyOnWriteArrayList;
026
027 import javax.management.JMException;
028 import javax.management.openmbean.*;
029
030 import org.slf4j.*;
031
032 import patterntesting.runtime.jmx.MBeanHelper;
033
034
035 /**
036 * This is the monitor class for the {@link ProxyConnection} which monitors the
037 * different newInstance() and close() call. So you can ask this class how many
038 * and which connections are open.
039 * <p>
040 * The instance of this class is automatically registered as MBean as soon as a
041 * {@link ProxyConnection} is used.
042 * </p>
043 * <p>
044 * Note: Since 1.4.2 this class was moved from package
045 * "patterntesting.runtime.db" to here.
046 * </p>
047 *
048 * @author oliver (ob@aosd.de)
049 * @since 1.3 (07.10.2012)
050 * @version $Revision: 1.5 $
051 */
052 public class ConnectionMonitor implements ConnectionMonitorMBean {
053
054 private static final Logger log = LoggerFactory.getLogger(ConnectionMonitor.class);
055 private static final ConnectionMonitorMBean instance;
056 private static final List<ProxyConnection> openConnections = new CopyOnWriteArrayList<ProxyConnection>();
057
058 private static int sumOfConnections = 0;
059
060 static {
061 instance = new ConnectionMonitor();
062 log.debug("{} created and registered as MBean.", instance);
063 try {
064 MBeanHelper.registerMBean(instance);
065 } catch (JMException e) {
066 log.info("{} can't be registered as MBean ({})", instance, e);
067 }
068 }
069
070 /**
071 * No need to instantiate it - we provide only some services. The
072 * constructor is protected because it is still used by the (deprecated)
073 * ConnectionMonitor in patterntesting.runtime.db.
074 */
075 protected ConnectionMonitor() {
076 log.trace("{} created.", this);
077 }
078
079 /**
080 * Yes, it is a Singleton because it offers only some services. So we don't
081 * need the object twice.
082 *
083 * @return the only instance
084 */
085 public static ConnectionMonitorMBean getInstance() {
086 return instance;
087 }
088
089 /**
090 * If you want to monitor the connection use this method.
091 *
092 * @param connection the original connection
093 * @return a proxy for the connection
094 */
095 public static Connection getMonitoredConnection(final Connection connection) {
096 return ProxyConnection.newInstance(connection);
097 }
098
099 /**
100 * Adds the connection.
101 *
102 * @param proxyConnection the proxy connection
103 */
104 public static void addConnection(final ProxyConnection proxyConnection) {
105 openConnections.add(proxyConnection);
106 sumOfConnections++;
107 }
108
109 /**
110 * Removes the connection.
111 *
112 * @param proxyConnection the proxy connection
113 */
114 public static void removeConnection(final ProxyConnection proxyConnection) {
115 openConnections.remove(proxyConnection);
116 }
117
118 /**
119 * Gets the caller of the given connection.
120 *
121 * @param connection the connection
122 * @return the caller of
123 */
124 public static StackTraceElement getCallerOf(final Connection connection) {
125 for (ProxyConnection proxy : openConnections) {
126 if (proxy.getConnection().equals(connection)) {
127 return proxy.getCaller()[0];
128 }
129 }
130 throw new IllegalArgumentException("not monitored or closed: " + connection);
131 }
132
133 /**
134 * Gets the callers.
135 *
136 * @return the callers
137 * @see ConnectionMonitorMBean#getCallers()
138 */
139 public StackTraceElement[] getCallers() {
140 StackTraceElement[] callers = new StackTraceElement[openConnections.size()];
141 int i = 0;
142 for (ProxyConnection proxy : openConnections) {
143 callers[i] = proxy.getCaller()[0];
144 i++;
145 }
146 return callers;
147 }
148
149 /**
150 * Gets the stacktrace of the caller which opens the last connection.
151 *
152 * @return the all caller
153 */
154 public StackTraceElement[] getLastCallerStacktrace() {
155 ProxyConnection lastConnection = openConnections.get(openConnections.size() - 1);
156 return lastConnection.getCaller();
157 }
158
159 /**
160 * Gets the caller stacktraces of all connections.
161 *
162 * @return stacktraces of all callers
163 * @throws OpenDataException the open data exception
164 */
165 public TabularData getCallerStacktraces() throws OpenDataException {
166 String[] itemNames = { "Caller", "Stacktrace" };
167 String[] itemDescriptions = { "caller name", "stacktrace" };
168 try {
169 OpenType<?>[] itemTypes = { SimpleType.STRING,
170 new ArrayType<String>(1, SimpleType.STRING) };
171 CompositeType rowType = new CompositeType("propertyType",
172 "property entry", itemNames, itemDescriptions, itemTypes);
173 TabularDataSupport data = MBeanHelper.createTabularDataSupport(rowType, itemNames);
174 for (ProxyConnection proxy : openConnections) {
175 StackTraceElement[] stacktrace = proxy.getCaller();
176 Map<String, Object> map = new HashMap<String, Object>();
177 map.put("Caller", stacktrace[0].toString());
178 map.put("Stacktrace", toStringArray(stacktrace));
179 CompositeDataSupport compData = new CompositeDataSupport(
180 rowType, map);
181 data.put(compData);
182 }
183 return data;
184 } catch (OpenDataException ex) {
185 log.warn("Cannot get caller stacktraces of " + openConnections.size()
186 + " open connections.", ex);
187 throw ex;
188 }
189 }
190
191 private static String[] toStringArray(final StackTraceElement[] stacktrace) {
192 String[] array = new String[stacktrace.length];
193 for (int i = 0; i < stacktrace.length; i++) {
194 array[i] = stacktrace[i].toString();
195 }
196 return array;
197 }
198
199 /**
200 * Gets the caller which opens the last connection.
201 *
202 * @return the all caller
203 */
204 public StackTraceElement getLastCaller() {
205 StackTraceElement[] callers = this.getCallers();
206 if (callers.length == 0) {
207 log.debug("No open connections - last caller is null.");
208 return null;
209 }
210 return callers[callers.length-1];
211 }
212
213 /**
214 * Gets the number of open connections.
215 *
216 * @return the open count
217 * @see ConnectionMonitorMBean#getOpenConnections()
218 */
219 public int getOpenConnections() {
220 return ConnectionMonitor.openConnections.size();
221 }
222
223 /**
224 * Gets the closed connections.
225 *
226 * @return the closed connections
227 * @since 1.4.1
228 */
229 public int getClosedConnections() {
230 return this.getSumOfConnections() - this.getOpenConnections();
231 }
232
233 /**
234 * Gets the total sum of open and closed connections.
235 *
236 * @return the sum of connections
237 * @since 1.4.1
238 */
239 public int getSumOfConnections() {
240 return sumOfConnections;
241 }
242
243 /**
244 * Assert that all connections are closed.
245 */
246 public static void assertConnectionsClosed() {
247 int count = instance.getOpenConnections();
248 if (count > 0) {
249 AssertionError error = new AssertionError(count + " connection(s) not closed");
250 error.setStackTrace(openConnections.iterator().next().getCaller());
251 throw error;
252 }
253 }
254
255 /**
256 * To string.
257 *
258 * @return the string
259 * @see java.lang.Object#toString()
260 */
261 @Override
262 public String toString() {
263 int n = this.getOpenConnections();
264 if (n < 1) {
265 return this.getClass().getSimpleName();
266 }
267 return this.getClass().getSimpleName() + " watching " + n + " connection(s)";
268 }
269
270 }
271