1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * ConnectionEvent.java |
---|
19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.gui.sql.event; |
---|
24 | |
---|
25 | import weka.gui.sql.DbUtils; |
---|
26 | |
---|
27 | import java.util.EventObject; |
---|
28 | |
---|
29 | /** |
---|
30 | * An event that is generated when a connection is established or dropped. |
---|
31 | * |
---|
32 | * @see ConnectionListener |
---|
33 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
34 | * @version $Revision: 1.2 $ |
---|
35 | */ |
---|
36 | public class ConnectionEvent |
---|
37 | extends EventObject { |
---|
38 | |
---|
39 | /** for serialization */ |
---|
40 | private static final long serialVersionUID = 5420308930427835037L; |
---|
41 | |
---|
42 | /** it was a connect try */ |
---|
43 | public final static int CONNECT = 0; |
---|
44 | |
---|
45 | /** it was a disconnect */ |
---|
46 | public final static int DISCONNECT = 1; |
---|
47 | |
---|
48 | /** the type of event, CONNECT or DISCONNECT */ |
---|
49 | protected int m_Type; |
---|
50 | |
---|
51 | /** the databaseutils instance reponsible for the connection */ |
---|
52 | protected DbUtils m_DbUtils; |
---|
53 | |
---|
54 | /** a possible exception that occurred if not successful */ |
---|
55 | protected Exception m_Exception; |
---|
56 | |
---|
57 | /** |
---|
58 | * constructs the event |
---|
59 | * @param source the source that generated this event |
---|
60 | * @param type whether CONNECT or DISCONNECT happened |
---|
61 | * @param utils the DatabaseUtils isntance responsible for the |
---|
62 | * connection |
---|
63 | */ |
---|
64 | public ConnectionEvent(Object source, int type, DbUtils utils) { |
---|
65 | this(source, type, utils, null); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * constructs the event |
---|
70 | * @param source the source that generated this event |
---|
71 | * @param type whether CONNECT or DISCONNECT happened |
---|
72 | * @param utils the DatabaseUtils isntance responsible for the |
---|
73 | * connection |
---|
74 | * @param ex a possible exception, if not successful |
---|
75 | */ |
---|
76 | public ConnectionEvent(Object source, int type, DbUtils utils, Exception ex) { |
---|
77 | super(source); |
---|
78 | |
---|
79 | m_Type = type; |
---|
80 | m_DbUtils = utils; |
---|
81 | m_Exception = ex; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * returns the type of this event, CONNECT or DISCONNECT |
---|
86 | * @return the type of this event |
---|
87 | * @see #CONNECT |
---|
88 | * @see #DISCONNECT |
---|
89 | */ |
---|
90 | public int getType() { |
---|
91 | return m_Type; |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * whether an exception happened and is stored |
---|
96 | * @return whether an exception happened |
---|
97 | */ |
---|
98 | public boolean failed() { |
---|
99 | return (getException() != null); |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * returns whether the connection is still open. |
---|
104 | * @return whether the connection is still open |
---|
105 | */ |
---|
106 | public boolean isConnected() { |
---|
107 | return m_DbUtils.isConnected(); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * returns the stored exception, if any (can be NULL) |
---|
112 | */ |
---|
113 | public Exception getException() { |
---|
114 | return m_Exception; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * returns the DbUtils instance that is responsible for the |
---|
119 | * connect/disconnect. |
---|
120 | * @return the responsible DbUtils instance |
---|
121 | */ |
---|
122 | public DbUtils getDbUtils() { |
---|
123 | return m_DbUtils; |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * returns the event in a string representation |
---|
128 | * @return the event in a string representation |
---|
129 | */ |
---|
130 | public String toString() { |
---|
131 | String result; |
---|
132 | |
---|
133 | result = super.toString(); |
---|
134 | result = result.substring(0, result.length() - 1); // remove "]" |
---|
135 | result += ",url=" + m_DbUtils.getDatabaseURL() |
---|
136 | + ",user=" + m_DbUtils.getUsername() |
---|
137 | + ",password=" + m_DbUtils.getPassword().replaceAll(".", "*") |
---|
138 | + ",connected=" + isConnected() |
---|
139 | + ",exception=" + getException() |
---|
140 | + "]"; |
---|
141 | |
---|
142 | return result; |
---|
143 | } |
---|
144 | } |
---|