001: package net.sourceforge.squirrel_sql.fw.sql;
002:
003: /*
004: * Copyright (C) 2001-2002 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 WARRANTY; 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: import java.util.ArrayList;
024: import java.util.List;
025:
026: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
027: import net.sourceforge.squirrel_sql.fw.persist.ValidationException;
028: import net.sourceforge.squirrel_sql.fw.util.PropertyChangeReporter;
029: import net.sourceforge.squirrel_sql.fw.util.StringManager;
030: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
031: import net.sourceforge.squirrel_sql.fw.util.beanwrapper.StringWrapper;
032:
033: /**
034: * This represents a JDBC driver.
035: * This class is a <CODE>JavaBean</CODE>.
036: *
037: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
038: */
039: public class SQLDriver implements ISQLDriver, Cloneable, Serializable {
040: static final long serialVersionUID = 8506401259069527981L;
041:
042: /** Internationalized strings for this class. */
043: private static final StringManager s_stringMgr = StringManagerFactory
044: .getStringManager(SQLDriver.class);
045:
046: private interface IStrings {
047: String ERR_BLANK_NAME = s_stringMgr
048: .getString("SQLDriver.error.blankname");
049: String ERR_BLANK_DRIVER = s_stringMgr
050: .getString("SQLDriver.error.blankdriver");
051: String ERR_BLANK_URL = s_stringMgr
052: .getString("SQLDriver.error.blankurl");
053: }
054:
055: /** The <CODE>IIdentifier</CODE> that uniquely identifies this object. */
056: private IIdentifier _id;
057:
058: /** The name of this driver. */
059: private String _name;
060:
061: /**
062: * File name associated with <CODE>_jarFileURL</CODE>.
063: */
064: private String _jarFileName = null;
065:
066: /** Names for driver jar files. */
067: private List<String> _jarFileNamesList = new ArrayList<String>();
068:
069: /** The class name of the JDBC driver. */
070: private String _driverClassName;
071:
072: /** Default URL required to access the database. */
073: private String _url;
074:
075: /** Is the JDBC driver class for this object loaded? */
076: private boolean _jdbcDriverClassLoaded;
077:
078: /** Object to handle property change events. */
079: private transient PropertyChangeReporter _propChgReporter;
080:
081: /** Default Website URL for more info about the JDBC driver */
082: private String _websiteUrl;
083:
084: /**
085: * Ctor specifying the identifier.
086: *
087: * @param id Uniquely identifies this object.
088: */
089: public SQLDriver(IIdentifier id) {
090: super ();
091: _id = id;
092: _name = "";
093: _jarFileName = null;
094: _driverClassName = null;
095: _url = "";
096: _websiteUrl = "";
097: }
098:
099: /**
100: * Default ctor.
101: */
102: public SQLDriver() {
103: super ();
104: }
105:
106: /**
107: * Assign data from the passed <CODE>ISQLDriver</CODE> to this one. This
108: * does <B>not</B> copy the identifier.
109: *
110: * @param rhs <CODE>ISQLDriver</CODE> to copy data from.
111: *
112: * @exception ValidationException
113: * Thrown if an error occurs assigning data from
114: * <CODE>rhs</CODE>.
115: */
116: public synchronized void assignFrom(ISQLDriver rhs)
117: throws ValidationException {
118: setName(rhs.getName());
119: setJarFileNames(rhs.getJarFileNames());
120: setDriverClassName(rhs.getDriverClassName());
121: setUrl(rhs.getUrl());
122: setJDBCDriverClassLoaded(rhs.isJDBCDriverClassLoaded());
123: setWebSiteUrl(rhs.getWebSiteUrl());
124: }
125:
126: /**
127: * Returns <TT>true</TT> if this objects is equal to the passed one. Two
128: * <TT>ISQLDriver</TT> objects are considered equal if they have the same
129: * identifier.
130: */
131: public boolean equals(Object rhs) {
132: boolean rc = false;
133: if (rhs != null && rhs.getClass().equals(getClass())) {
134: rc = ((ISQLDriver) rhs).getIdentifier().equals(
135: getIdentifier());
136: }
137: return rc;
138: }
139:
140: /**
141: * Returns a hash code value for this object.
142: */
143: public synchronized int hashCode() {
144: return getIdentifier().hashCode();
145: }
146:
147: /**
148: * Returns the name of this <TT>ISQLDriver</TT>.
149: */
150: public String toString() {
151: return getName();
152: }
153:
154: /**
155: * Return a clone of this object.
156: */
157: public Object clone() {
158: try {
159: final SQLDriver driver = (SQLDriver) super .clone();
160: driver._propChgReporter = null;
161: return driver;
162: } catch (CloneNotSupportedException ex) {
163: throw new InternalError(ex.getMessage()); // Impossible.
164: }
165: }
166:
167: /**
168: * Compare this <TT>ISQLDriver</TT> to another <TT>ISQLDriver</TT>. The
169: * <TT>getName()</TT> functions of the two <TT>ISQLDriver</TT> objects are
170: * used to compare them.
171: */
172: public int compareTo(ISQLDriver rhs) {
173: return _name.compareTo(rhs.getName());
174: }
175:
176: public void addPropertyChangeListener(
177: PropertyChangeListener listener) {
178: getPropertyChangeReporter().addPropertyChangeListener(listener);
179: }
180:
181: public void removePropertyChangeListener(
182: PropertyChangeListener listener) {
183: getPropertyChangeReporter().removePropertyChangeListener(
184: listener);
185: }
186:
187: public void setReportPropertyChanges(boolean report) {
188: getPropertyChangeReporter().setNotify(report);
189: }
190:
191: public IIdentifier getIdentifier() {
192: return _id;
193: }
194:
195: public void setIdentifier(IIdentifier id) {
196: _id = id;
197: }
198:
199: public String getDriverClassName() {
200: return _driverClassName;
201: }
202:
203: public void setDriverClassName(String driverClassName)
204: throws ValidationException {
205: String data = getString(driverClassName);
206: if (data.length() == 0) {
207: throw new ValidationException(IStrings.ERR_BLANK_DRIVER);
208: }
209: if (!data.equals(_driverClassName)) {
210: final String oldValue = _driverClassName;
211: _driverClassName = data;
212: getPropertyChangeReporter().firePropertyChange(
213: ISQLDriver.IPropertyNames.DRIVER_CLASS, oldValue,
214: _driverClassName);
215: }
216: }
217:
218: /**
219: * @deprecated Replaced by getJarFileNames().
220: */
221: public String getJarFileName() {
222: return _jarFileName;
223: }
224:
225: public void setJarFileName(String value) {
226: if (value == null) {
227: value = "";
228: }
229: if (_jarFileName == null || !_jarFileName.equals(value)) {
230: final String oldValue = _jarFileName;
231: _jarFileName = value;
232: getPropertyChangeReporter().firePropertyChange(
233: ISQLDriver.IPropertyNames.JARFILE_NAME, oldValue,
234: _jarFileName);
235: }
236: }
237:
238: public synchronized String[] getJarFileNames() {
239: return _jarFileNamesList.toArray(new String[_jarFileNamesList
240: .size()]);
241: }
242:
243: public synchronized void setJarFileNames(String[] values) {
244: String[] oldValue = _jarFileNamesList
245: .toArray(new String[_jarFileNamesList.size()]);
246: _jarFileNamesList.clear();
247:
248: if (values == null) {
249: values = new String[0];
250: }
251:
252: for (int i = 0; i < values.length; ++i) {
253: _jarFileNamesList.add(values[i]);
254: }
255:
256: getPropertyChangeReporter().firePropertyChange(
257: ISQLDriver.IPropertyNames.JARFILE_NAMES, oldValue,
258: values);
259: }
260:
261: public String getUrl() {
262: return _url;
263: }
264:
265: public void setUrl(String url) throws ValidationException {
266: String data = getString(url);
267: if (data.length() == 0) {
268: throw new ValidationException(IStrings.ERR_BLANK_URL);
269: }
270: if (!data.equals(_url)) {
271: final String oldValue = _url;
272: _url = data;
273: getPropertyChangeReporter().firePropertyChange(
274: ISQLDriver.IPropertyNames.URL, oldValue, _url);
275: }
276: }
277:
278: public String getName() {
279: return _name;
280: }
281:
282: public void setName(String name) throws ValidationException {
283: String data = getString(name);
284: if (data.length() == 0) {
285: throw new ValidationException(IStrings.ERR_BLANK_NAME);
286: }
287: if (!data.equals(_name)) {
288: final String oldValue = _name;
289: _name = data;
290: getPropertyChangeReporter().firePropertyChange(
291: ISQLDriver.IPropertyNames.NAME, oldValue, _name);
292: }
293: }
294:
295: public boolean isJDBCDriverClassLoaded() {
296: return _jdbcDriverClassLoaded;
297: }
298:
299: public void setJDBCDriverClassLoaded(boolean cl) {
300: _jdbcDriverClassLoaded = cl;
301: //TODO: Decide whether this should be a bound property or not.
302: // getPropertyChangeReporter().firePropertyChange(ISQLDriver.IPropertyNames.NAME, _name, _name);
303: }
304:
305: public synchronized StringWrapper[] getJarFileNameWrappers() {
306: StringWrapper[] wrappers = new StringWrapper[_jarFileNamesList
307: .size()];
308: for (int i = 0; i < wrappers.length; ++i) {
309: wrappers[i] = new StringWrapper(_jarFileNamesList.get(i));
310: }
311: return wrappers;
312: }
313:
314: public StringWrapper getJarFileNameWrapper(int idx)
315: throws ArrayIndexOutOfBoundsException {
316: return new StringWrapper(_jarFileNamesList.get(idx));
317: }
318:
319: public void setJarFileNameWrappers(StringWrapper[] value) {
320: _jarFileNamesList.clear();
321: if (value != null) {
322: for (int i = 0; i < value.length; ++i) {
323: _jarFileNamesList.add(value[i].getString());
324: }
325: }
326: }
327:
328: public void setJarFileNameWrapper(int idx, StringWrapper value)
329: throws ArrayIndexOutOfBoundsException {
330: _jarFileNamesList.set(idx, value.getString());
331: }
332:
333: private String getString(String data) {
334: return data != null ? data.trim() : "";
335: }
336:
337: private synchronized PropertyChangeReporter getPropertyChangeReporter() {
338: if (_propChgReporter == null) {
339: _propChgReporter = new PropertyChangeReporter(this );
340: }
341: return _propChgReporter;
342: }
343:
344: /* (non-Javadoc)
345: * @see net.sourceforge.squirrel_sql.fw.sql.ISQLDriver#getWebSiteUrl()
346: */
347: public String getWebSiteUrl() {
348: return _websiteUrl;
349: }
350:
351: /* (non-Javadoc)
352: * @see net.sourceforge.squirrel_sql.fw.sql.ISQLDriver#setWebSiteUrl(java.lang.String)
353: */
354: public void setWebSiteUrl(String url) throws ValidationException {
355: String data = getString(url);
356: if (!data.equals(_websiteUrl)) {
357: final String oldValue = _websiteUrl;
358: _websiteUrl = data;
359: PropertyChangeReporter pcr = getPropertyChangeReporter();
360: pcr.firePropertyChange(
361: ISQLDriver.IPropertyNames.WEBSITE_URL, oldValue,
362: _websiteUrl);
363: }
364: }
365:
366: }
|