01: /*
02: Copyright (C) 2002-2007 MySQL AB
03:
04: This program is free software; you can redistribute it and/or modify
05: it under the terms of version 2 of the GNU General Public License as
06: published by the Free Software Foundation.
07:
08: There are special exceptions to the terms and conditions of the GPL
09: as it is applied to this software. View the full text of the
10: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11: software distribution.
12:
13: This program is distributed in the hope that it will be useful,
14: but WITHOUT ANY WARRANTY; without even the implied warranty of
15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: GNU General Public License for more details.
17:
18: You should have received a copy of the GNU General Public License
19: along with this program; if not, write to the Free Software
20: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21:
22: */
23:
24: package com.mysql.jdbc.profiler;
25:
26: import com.mysql.jdbc.Connection;
27: import com.mysql.jdbc.log.Log;
28:
29: import java.sql.SQLException;
30: import java.util.HashMap;
31: import java.util.Map;
32:
33: /**
34: * @author mmatthew
35: */
36: public class ProfileEventSink {
37:
38: private static final Map CONNECTIONS_TO_SINKS = new HashMap();
39:
40: private Connection ownerConnection = null;
41:
42: private Log log = null;
43:
44: /**
45: * Returns the ProfileEventSink that handles profiler events for the given
46: * connection.
47: *
48: * @param conn
49: * the connection to handle events for
50: * @return the ProfileEventSink that handles profiler events
51: */
52: public static synchronized ProfileEventSink getInstance(
53: Connection conn) {
54: ProfileEventSink sink = (ProfileEventSink) CONNECTIONS_TO_SINKS
55: .get(conn);
56:
57: if (sink == null) {
58: sink = new ProfileEventSink(conn);
59: CONNECTIONS_TO_SINKS.put(conn, sink);
60: }
61:
62: return sink;
63: }
64:
65: /**
66: * Process a profiler event
67: *
68: * @param evt
69: * the event to process
70: */
71: public void consumeEvent(ProfilerEvent evt) {
72: if (evt.eventType == ProfilerEvent.TYPE_WARN) {
73: this .log.logWarn(evt);
74: } else {
75: this .log.logInfo(evt);
76: }
77: }
78:
79: public static synchronized void removeInstance(Connection conn) {
80: CONNECTIONS_TO_SINKS.remove(conn);
81: }
82:
83: private ProfileEventSink(Connection conn) {
84: this .ownerConnection = conn;
85:
86: try {
87: this .log = this .ownerConnection.getLog();
88: } catch (SQLException sqlEx) {
89: throw new RuntimeException(
90: "Unable to get logger from connection");
91: }
92: }
93: }
|