001: package net.sourceforge.squirrel_sql.client.gui.db;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANT_Y; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.beans.PropertyChangeListener;
022: import java.io.Serializable;
023:
024: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
025: import net.sourceforge.squirrel_sql.fw.persist.ValidationException;
026: import net.sourceforge.squirrel_sql.fw.sql.ISQLAlias;
027: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverProperty;
028: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverPropertyCollection;
029: import net.sourceforge.squirrel_sql.fw.util.PropertyChangeReporter;
030: import net.sourceforge.squirrel_sql.fw.util.StringManager;
031: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
032: import net.sourceforge.squirrel_sql.fw.util.Utilities;
033:
034: /**
035: * This represents a Database alias which is a description of the means
036: * required to connect to a JDBC complient database.<P>
037: * This class is a <CODE>JavaBean</CODE>.
038: *
039: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
040: */
041: @SuppressWarnings("serial")
042: public class SQLAlias implements Cloneable, Serializable, ISQLAliasExt,
043: Comparable<Object> {
044: /** Internationalized strings for this class. */
045: private static final StringManager s_stringMgr = StringManagerFactory
046: .getStringManager(SQLAlias.class);
047:
048: private interface IStrings {
049: String ERR_BLANK_NAME = s_stringMgr
050: .getString("SQLAlias.error.blankname");
051: String ERR_BLANK_DRIVER = s_stringMgr
052: .getString("SQLAlias.error.blankdriver");
053: String ERR_BLANK_URL = s_stringMgr
054: .getString("SQLAlias.error.blankurl");
055: }
056:
057: private interface IPropNames extends ISQLAlias.IPropertyNames {
058: // Empty block.
059: }
060:
061: /** The <CODE>IIdentifier</CODE> that uniquely identifies this object. */
062: private IIdentifier _id;
063:
064: /** The name of this alias. */
065: private String _name;
066:
067: /**
068: * The <CODE>IIdentifier</CODE> that identifies the <CODE>ISQLDriver</CODE>
069: * that this <CODE>ISQLAlias</CODE> uses.
070: */
071: private IIdentifier _driverId;
072:
073: /** The URL required to access the database. */
074: private String _url;
075:
076: /** Name of user for connection. */
077: private String _userName;
078:
079: /** Password of user for connection. */
080: private String _password;
081:
082: /** <TT>true</TT> if this alias should be logged on automatically. */
083: private boolean _autoLogon;
084:
085: /** Should this alias be connected when the application is started up. */
086: private boolean _connectAtStartup;
087:
088: /** If <TT>true</TT> then use drver properties. */
089: private boolean _useDriverProperties = false;
090:
091: /** Collection of <TT>SQLDriverProperty</TT> objects for this alias. */
092: private SQLDriverPropertyCollection _driverProps = new SQLDriverPropertyCollection();
093:
094: /** Object to handle property change events. */
095: private transient PropertyChangeReporter _propChgReporter;
096:
097: private SQLAliasSchemaProperties _schemaProperties = new SQLAliasSchemaProperties();
098:
099: /**
100: * Default ctor.
101: */
102: public SQLAlias() {
103: super ();
104: }
105:
106: /**
107: * Ctor specifying the identifier.
108: *
109: * @param id Uniquely identifies this object.
110: */
111: public SQLAlias(IIdentifier id) {
112: super ();
113: _id = id;
114: _name = "";
115: _driverId = null;
116: _url = "";
117: _userName = "";
118: _password = "";
119: }
120:
121: /**
122: * Assign data from the passed <CODE>ISQLAlias</CODE> to this one.
123: *
124: * This Alias becomes a clone of rhs.
125: *
126: * @param rhs <CODE>ISQLAlias</CODE> to copy data from.
127: *
128: * @param b
129: * @exception ValidationException
130: * Thrown if an error occurs assigning data from
131: * <CODE>rhs</CODE>.
132: */
133: public synchronized void assignFrom(SQLAlias rhs,
134: boolean withIdentifier) throws ValidationException {
135: if (withIdentifier) {
136: setIdentifier(rhs.getIdentifier());
137: }
138:
139: setName(rhs.getName());
140: setDriverIdentifier(rhs.getDriverIdentifier());
141: setUrl(rhs.getUrl());
142: setUserName(rhs.getUserName());
143: setPassword(rhs.getPassword());
144: setAutoLogon(rhs.isAutoLogon());
145: setUseDriverProperties(rhs.getUseDriverProperties());
146: setDriverProperties(rhs.getDriverPropertiesClone());
147: _schemaProperties = (SQLAliasSchemaProperties) Utilities
148: .cloneObject(rhs._schemaProperties, getClass()
149: .getClassLoader());
150: }
151:
152: /**
153: * Returns <TT>true</TT> if this objects is equal to the passed one. Two
154: * <TT>ISQLAlias</TT> objects are considered equal if they have the same
155: * identifier.
156: */
157: public boolean equals(Object rhs) {
158: boolean rc = false;
159: if (rhs != null && rhs.getClass().equals(getClass())) {
160: rc = ((ISQLAlias) rhs).getIdentifier().equals(
161: getIdentifier());
162: }
163: return rc;
164: }
165:
166: // /**
167: // * Return a clone of this object.
168: // */
169: // public Object clone()
170: // {
171: // try
172: // {
173: // final SQLAlias alias = (SQLAlias)super.clone();
174: // alias._propChgReporter = null;
175: // alias.setDriverProperties(getDriverPropertiesClone());
176: // alias._schemaProperties = (SQLAliasSchemaProperties) Utilities.cloneObject(alias._schemaProperties, getClass().getClassLoader());
177: // return alias;
178: // }
179: // catch (CloneNotSupportedException ex)
180: // {
181: // throw new InternalError(ex.getMessage()); // Impossible.
182: // }
183: // }
184:
185: /**
186: * Returns a hash code value for this object.
187: */
188: public synchronized int hashCode() {
189: return getIdentifier().hashCode();
190: }
191:
192: /**
193: * Returns the name of this <TT>ISQLAlias</TT>.
194: */
195: public String toString() {
196: return getName();
197: }
198:
199: /**
200: * Compare this <TT>SQLAlias</TT> to another object. If the passed object
201: * is a <TT>SQLAlias</TT>, then the <TT>getName()</TT> functions of the two
202: * <TT>SQLAlias</TT> objects are used to compare them. Otherwise, it throws a
203: * ClassCastException (as <TT>SQLAlias</TT> objects are comparable only to
204: * other <TT>SQLAlias</TT> objects).
205: */
206: public int compareTo(Object rhs) {
207: return _name.compareTo(((ISQLAlias) rhs).getName());
208: }
209:
210: public void addPropertyChangeListener(
211: PropertyChangeListener listener) {
212: getPropertyChangeReporter().addPropertyChangeListener(listener);
213: }
214:
215: public void removePropertyChangeListener(
216: PropertyChangeListener listener) {
217: getPropertyChangeReporter().removePropertyChangeListener(
218: listener);
219: }
220:
221: /**
222: * Returns <CODE>true</CODE> if this object is valid.<P>
223: * Implementation for <CODE>IPersistable</CODE>.
224: */
225: public synchronized boolean isValid() {
226: return _name != null && _name.length() > 0 && _driverId != null
227: && _url != null && _url.length() > 0;
228: }
229:
230: public IIdentifier getIdentifier() {
231: return _id;
232: }
233:
234: public String getName() {
235: return _name;
236: }
237:
238: public IIdentifier getDriverIdentifier() {
239: return _driverId;
240: }
241:
242: public String getUrl() {
243: return _url;
244: }
245:
246: public String getUserName() {
247: return _userName;
248: }
249:
250: /**
251: * Retrieve the saved password.
252: *
253: * @return The saved password.
254: */
255: public String getPassword() {
256: return _password;
257: }
258:
259: /**
260: * Set the password for this alias.
261: *
262: * @param password The new password.
263: */
264: public void setPassword(String password) {
265: String data = getString(password);
266: if (_password != data) {
267: final String oldValue = _password;
268: _password = data;
269: getPropertyChangeReporter().firePropertyChange(
270: IPropNames.PASSWORD, oldValue, _password);
271: }
272: }
273:
274: /**
275: * Should this alias be logged on automatically.
276: *
277: * @return <TT>true</TT> is this alias should be logged on automatically
278: * else <TT>false</TT>.
279: */
280: public boolean isAutoLogon() {
281: return _autoLogon;
282: }
283:
284: /**
285: * Set whether this alias should be logged on automatically.
286: *
287: * @param value <TT>true</TT> if alias should be autologged on
288: * else <TT>false</TT.
289: */
290: public void setAutoLogon(boolean value) {
291: if (_autoLogon != value) {
292: _autoLogon = value;
293: getPropertyChangeReporter().firePropertyChange(
294: IPropNames.AUTO_LOGON, !_autoLogon, _autoLogon);
295: }
296: }
297:
298: /**
299: * Should this alias be connected when the application is started up.
300: *
301: * @return <TT>true</TT> if this alias should be connected when the
302: * application is started up.
303: */
304: public boolean isConnectAtStartup() {
305: return _connectAtStartup;
306: }
307:
308: /**
309: * Set whether alias should be connected when the application is started up.
310: *
311: * @param value <TT>true</TT> if alias should be connected when the
312: * application is started up.
313: */
314: public void setConnectAtStartup(boolean value) {
315: if (_connectAtStartup != value) {
316: _connectAtStartup = value;
317: getPropertyChangeReporter().firePropertyChange(
318: IPropNames.CONNECT_AT_STARTUP, !_connectAtStartup,
319: _connectAtStartup);
320: }
321: }
322:
323: /**
324: * Returns whether this alias uses driver properties.
325: */
326: public boolean getUseDriverProperties() {
327: return _useDriverProperties;
328: }
329:
330: public void setIdentifier(IIdentifier id) {
331: _id = id;
332: }
333:
334: public void setName(String name) throws ValidationException {
335: String data = getString(name);
336: if (data.length() == 0) {
337: throw new ValidationException(IStrings.ERR_BLANK_NAME);
338: }
339: if (_name != data) {
340: final String oldValue = _name;
341: _name = data;
342: getPropertyChangeReporter().firePropertyChange(
343: IPropNames.NAME, oldValue, _name);
344: }
345: }
346:
347: public void setDriverIdentifier(IIdentifier data)
348: throws ValidationException {
349: if (data == null) {
350: throw new ValidationException(IStrings.ERR_BLANK_DRIVER);
351: }
352: if (_driverId != data) {
353: final IIdentifier oldValue = _driverId;
354: _driverId = data;
355: getPropertyChangeReporter().firePropertyChange(
356: IPropNames.DRIVER, oldValue, _driverId);
357: }
358: }
359:
360: public void setUrl(String url) throws ValidationException {
361: String data = getString(url);
362: if (data.length() == 0) {
363: throw new ValidationException(IStrings.ERR_BLANK_URL);
364: }
365: if (_url != data) {
366: final String oldValue = _url;
367: _url = data;
368: getPropertyChangeReporter().firePropertyChange(
369: IPropNames.URL, oldValue, _url);
370: }
371: }
372:
373: public void setUserName(String userName) {
374: String data = getString(userName);
375: if (_userName != data) {
376: final String oldValue = _userName;
377: _userName = data;
378: getPropertyChangeReporter().firePropertyChange(
379: IPropNames.USER_NAME, oldValue, _userName);
380: }
381: }
382:
383: public void setUseDriverProperties(boolean value) {
384: if (_useDriverProperties != value) {
385: final boolean oldValue = _useDriverProperties;
386: _useDriverProperties = value;
387: getPropertyChangeReporter().firePropertyChange(
388: IPropNames.USE_DRIVER_PROPERTIES, oldValue,
389: _useDriverProperties);
390: }
391: }
392:
393: /**
394: * Retrieve a copy of the SQL driver properties.
395: *
396: * @return the SQL driver properties.
397: */
398: public synchronized SQLDriverPropertyCollection getDriverPropertiesClone() {
399: final int count = _driverProps.size();
400: SQLDriverProperty[] newar = new SQLDriverProperty[count];
401: for (int i = 0; i < count; ++i) {
402: newar[i] = (SQLDriverProperty) _driverProps
403: .getDriverProperty(i).clone();
404: }
405: SQLDriverPropertyCollection coll = new SQLDriverPropertyCollection();
406: coll.setDriverProperties(newar);
407: return coll;
408: }
409:
410: public synchronized void setDriverProperties(
411: SQLDriverPropertyCollection value) {
412: _driverProps.clear();
413: if (value != null) {
414: synchronized (value) {
415: final int count = value.size();
416: SQLDriverProperty[] newar = new SQLDriverProperty[count];
417: for (int i = 0; i < count; ++i) {
418: newar[i] = (SQLDriverProperty) value
419: .getDriverProperty(i).clone();
420:
421: }
422: _driverProps.setDriverProperties(newar);
423: }
424: }
425: }
426:
427: private synchronized PropertyChangeReporter getPropertyChangeReporter() {
428: if (_propChgReporter == null) {
429: _propChgReporter = new PropertyChangeReporter(this );
430: }
431: return _propChgReporter;
432: }
433:
434: private String getString(String data) {
435: return data != null ? data.trim() : "";
436: }
437:
438: public SQLAliasSchemaProperties getSchemaProperties() {
439: return _schemaProperties;
440: }
441:
442: public void setSchemaProperties(
443: SQLAliasSchemaProperties schemaProperties) {
444: _schemaProperties = schemaProperties;
445: }
446:
447: }
|