1 | /* |
2 | Copyright (C) 2002-2004 MySQL AB |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of version 2 of the GNU General Public License as |
6 | published by the Free Software Foundation. |
7 | |
8 | There are special exceptions to the terms and conditions of the GPL |
9 | 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 | */ |
25 | package com.mysql.jdbc.profiler; |
26 | |
27 | import com.mysql.jdbc.Connection; |
28 | import com.mysql.jdbc.log.Log; |
29 | |
30 | import java.sql.SQLException; |
31 | import java.util.HashMap; |
32 | import java.util.Map; |
33 | |
34 | /** |
35 | * @author mmatthew |
36 | */ |
37 | public class ProfileEventSink { |
38 | |
39 | private static final Map CONNECTIONS_TO_SINKS = new HashMap(); |
40 | |
41 | private Connection ownerConnection = null; |
42 | |
43 | private Log log = null; |
44 | |
45 | /** |
46 | * Returns the ProfileEventSink that handles profiler events for the given |
47 | * connection. |
48 | * |
49 | * @param conn |
50 | * the connection to handle events for |
51 | * @return the ProfileEventSink that handles profiler events |
52 | */ |
53 | public static synchronized ProfileEventSink getInstance(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("Unable to get logger from connection"); |
90 | } |
91 | } |
92 | |
93 | } |