001: /*
002: * DatabaseConnection.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.executequery.databasemediators;
023:
024: import java.util.Properties;
025: import org.executequery.Constants;
026: import org.executequery.JDBCProperties;
027: import org.underworldlabs.util.DesEncrypter;
028: import org.underworldlabs.util.MiscUtils;
029:
030: /* ----------------------------------------------------------
031: * CVS NOTE: Changes to the CVS repository prior to the
032: * release of version 3.0.0beta1 has meant a
033: * resetting of CVS revision numbers.
034: * ----------------------------------------------------------
035: */
036:
037: /**
038: * <p>This class maintains the necessary information for each
039: * saved database connection.<br>
040: * Each saved connection appears by name within the
041: * saved connections drop-down box displayed on respective
042: * windows.
043: *
044: * @author Takis Diakoumis
045: * @version $Revision: 1.4 $
046: * @date $Date: 2006/05/14 06:56:54 $
047: */
048: public class DatabaseConnection {
049:
050: private static final String ENCRYPTION_KEY = "yb7UD9jH";
051:
052: /** The unique name for this connection */
053: private String name;
054:
055: /** The user name for this connection */
056: private String userName;
057:
058: /** The password for this connection */
059: private String password;
060:
061: /** The host for this connection */
062: private String host;
063:
064: /** The data source name for this connection */
065: private String sourceName;
066:
067: /** The database vendor's name for this connection */
068: private String databaseType;
069:
070: /** The port number for this connection */
071: private String port;
072:
073: /** The driver specific URL for this connection */
074: private String url;
075:
076: /** The unique name of the JDBC/ODBC driver used with this connection */
077: private String dName;
078:
079: /** The unique ID of the JDBC/ODBC driver used with this connection */
080: private long driverId;
081:
082: /** The JDBC/ODBC Driver used with this connection */
083: private DatabaseDriver driver;
084:
085: /** The advanced Properties for this connection */
086: private Properties jdbcProperties;
087:
088: /** Whether this connection's password is stored */
089: private boolean passwordStored;
090:
091: /** Whether this connection is a new connection */
092: private boolean newConn;
093:
094: /** Whether the password is encrypted */
095: private boolean passwordEncrypted;
096:
097: /** the tx isolation level */
098: private int transactionIsolation;
099:
100: /** the commit mode */
101: private boolean autoCommit;
102:
103: /** Whether this connection is active */
104: transient boolean connected;
105:
106: /**
107: * Creates a new empty <code>DatabaseConnection</code> object.
108: */
109: public DatabaseConnection() {
110: this (null);
111: }
112:
113: /**
114: * Creates a new empty <code>DatabaseConnection</code> object
115: * with the specified name.
116: *
117: * @param A unique name for this connection.
118: */
119: public DatabaseConnection(String name) {
120: this .name = name;
121: autoCommit = true;
122: transactionIsolation = -1;
123: }
124:
125: public boolean isPasswordStored() {
126: return passwordStored;
127: }
128:
129: public void setPasswordStored(boolean storePwd) {
130: this .passwordStored = storePwd;
131: }
132:
133: public void setJdbcProperties(Properties jdbcProperties) {
134: this .jdbcProperties = jdbcProperties;
135: }
136:
137: public Properties getJdbcProperties() {
138: return jdbcProperties;
139: }
140:
141: public boolean hasAdvancedProperties() {
142: return jdbcProperties != null && jdbcProperties.size() > 0;
143: }
144:
145: public DatabaseDriver getJDBCDriver() {
146: if (driver == null) {
147: driver = JDBCProperties.getDatabaseDriver(driverId);
148: }
149: return driver;
150: }
151:
152: public boolean hasURL() {
153: return url != null && url.length() > 0;
154: }
155:
156: public int getPortInt() {
157: return Integer.parseInt(port);
158: }
159:
160: public void setJDBCDriver(DatabaseDriver driver) {
161: this .driver = driver;
162: }
163:
164: public String getDriverName() {
165: return dName;
166: }
167:
168: public void setDriverName(String dName) {
169: this .dName = dName;
170: }
171:
172: public String getPort() {
173: return port == null ? Constants.EMPTY : port;
174: }
175:
176: public boolean hasPort() {
177: return port != null && port.length() > 0;
178: }
179:
180: public void setPort(String port) {
181: this .port = port;
182: }
183:
184: public String getURL() {
185: return url == null ? Constants.EMPTY : url;
186: }
187:
188: public void setURL(String url) {
189: this .url = url;
190: }
191:
192: public String getDatabaseType() {
193: return databaseType;
194: }
195:
196: public void setDatabaseType(String databaseType) {
197: this .databaseType = databaseType;
198: }
199:
200: public String getPassword() {
201: return password;
202: }
203:
204: public String getUnencryptedPassword() {
205: String _password = password;
206: if (passwordEncrypted && !MiscUtils.isNull(password)) {
207: DesEncrypter encrypter = new DesEncrypter();
208: _password = encrypter.decrypt(ENCRYPTION_KEY, password);
209: }
210: return _password;
211: }
212:
213: public void setEncryptedPassword(String password) {
214: this .password = password;
215: }
216:
217: public void setPassword(String password) {
218: if (passwordEncrypted && !MiscUtils.isNull(password)) {
219: DesEncrypter encrypter = new DesEncrypter();
220: this .password = encrypter.encrypt(ENCRYPTION_KEY, password);
221: } else {
222: this .password = password;
223: }
224: }
225:
226: public String getSourceName() {
227: return sourceName == null ? Constants.EMPTY : sourceName;
228: }
229:
230: public void setSourceName(String sourceName) {
231: this .sourceName = sourceName;
232: }
233:
234: public boolean hasHost() {
235: return host != null && host.length() > 0;
236: }
237:
238: public boolean hasSourceName() {
239: return sourceName != null && sourceName.length() > 0;
240: }
241:
242: public String getHost() {
243: return host == null ? Constants.EMPTY : host;
244: }
245:
246: public void setHost(String host) {
247: this .host = host;
248: }
249:
250: public String getUserName() {
251: return userName;
252: }
253:
254: public void setUserName(String userName) {
255: this .userName = userName;
256: }
257:
258: public String getName() {
259: return name;
260: }
261:
262: public void setName(String name) {
263: this .name = name;
264: }
265:
266: public String toString() {
267: return name;
268: }
269:
270: public boolean isPasswordEncrypted() {
271: return passwordEncrypted;
272: }
273:
274: public void setPasswordEncrypted(boolean passwordEncrypted) {
275: this .passwordEncrypted = passwordEncrypted;
276: }
277:
278: public boolean isConnected() {
279: return connected;
280: }
281:
282: public void setConnected(boolean connected) {
283: this .connected = connected;
284: }
285:
286: public int getTransactionIsolation() {
287: return transactionIsolation;
288: }
289:
290: public void setTransactionIsolation(int transactionIsolation) {
291: this .transactionIsolation = transactionIsolation;
292: }
293:
294: public boolean isAutoCommit() {
295: return autoCommit;
296: }
297:
298: public void setAutoCommit(boolean autoCommit) {
299: this .autoCommit = autoCommit;
300: }
301:
302: public long getDriverId() {
303: return driverId;
304: }
305:
306: public void setDriverId(long driverId) {
307: this.driverId = driverId;
308: }
309:
310: }
|