001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.sql;
022:
023: import java.util.Properties;
024:
025: import net.sf.hajdbc.Database;
026:
027: /**
028: * @author Paul Ferraro
029: * @param <D> either java.sql.Driver or javax.sql.DataSource
030: * @since 1.0
031: */
032: public abstract class AbstractDatabase<D> implements Database<D> {
033: private String id;
034: protected String user;
035: protected String password;
036: protected Properties properties = new Properties();
037: private int weight = 1;
038: private boolean dirty = false;
039: private boolean local = false;
040:
041: /**
042: * @see net.sf.hajdbc.ActiveDatabaseMBean#getId()
043: */
044: @Override
045: public String getId() {
046: return this .id;
047: }
048:
049: /**
050: * @param id
051: */
052: public void setId(String id) {
053: this .checkDirty(this .id, id);
054: this .id = id;
055: }
056:
057: /**
058: * @see net.sf.hajdbc.ActiveDatabaseMBean#getUser()
059: */
060: @Override
061: public String getUser() {
062: return this .user;
063: }
064:
065: /**
066: * @see net.sf.hajdbc.InactiveDatabaseMBean#setUser(java.lang.String)
067: */
068: @Override
069: public void setUser(String user) {
070: this .checkDirty(this .user, user);
071: this .user = user;
072: }
073:
074: /**
075: * @see net.sf.hajdbc.ActiveDatabaseMBean#getPassword()
076: */
077: @Override
078: public String getPassword() {
079: return this .password;
080: }
081:
082: /**
083: * @see net.sf.hajdbc.InactiveDatabaseMBean#setPassword(java.lang.String)
084: */
085: @Override
086: public void setPassword(String password) {
087: this .checkDirty(this .password, password);
088: this .password = password;
089: }
090:
091: /**
092: * @see net.sf.hajdbc.ActiveDatabaseMBean#getWeight()
093: */
094: @Override
095: public int getWeight() {
096: return this .weight;
097: }
098:
099: /**
100: * @see net.sf.hajdbc.InactiveDatabaseMBean#setWeight(int)
101: */
102: @Override
103: public void setWeight(int weight) {
104: if (weight < 0) {
105: throw new IllegalArgumentException();
106: }
107:
108: this .checkDirty(this .weight, weight);
109: this .weight = weight;
110: }
111:
112: /**
113: * @see java.lang.Object#hashCode()
114: */
115: @Override
116: public int hashCode() {
117: return this .id.hashCode();
118: }
119:
120: /**
121: * @see java.lang.Object#equals(java.lang.Object)
122: */
123: @SuppressWarnings("unchecked")
124: @Override
125: public boolean equals(Object object) {
126: if ((object == null) || !(object instanceof Database))
127: return false;
128:
129: String id = ((Database) object).getId();
130:
131: return (id != null) && id.equals(this .id);
132: }
133:
134: /**
135: * @see java.lang.Object#toString()
136: */
137: @Override
138: public String toString() {
139: return this .id;
140: }
141:
142: /**
143: * @see net.sf.hajdbc.ActiveDatabaseMBean#getProperties()
144: */
145: @Override
146: public Properties getProperties() {
147: return this .properties;
148: }
149:
150: /**
151: * @param properties
152: */
153: public void setProperties(Properties properties) {
154: this .checkDirty(this .properties, properties);
155: this .properties = properties;
156: }
157:
158: /**
159: * @see net.sf.hajdbc.InactiveDatabaseMBean#removeProperty(java.lang.String)
160: */
161: @Override
162: public void removeProperty(String name) {
163: this .dirty |= this .properties.containsKey(name);
164: this .properties.remove(name);
165: }
166:
167: /**
168: * @see net.sf.hajdbc.InactiveDatabaseMBean#setProperty(java.lang.String, java.lang.String)
169: */
170: @Override
171: public void setProperty(String name, String value) {
172: if ((name == null) || (value == null)) {
173: throw new IllegalArgumentException();
174: }
175:
176: this .checkDirty(this .properties.getProperty(name), value);
177: this .properties.setProperty(name, value);
178: }
179:
180: /**
181: * @see net.sf.hajdbc.InactiveDatabaseMBean#setLocal(boolean)
182: */
183: @Override
184: public void setLocal(boolean local) {
185: this .checkDirty(this .local, local);
186: this .local = local;
187: }
188:
189: /**
190: * @see net.sf.hajdbc.ActiveDatabaseMBean#isLocal()
191: */
192: @Override
193: public boolean isLocal() {
194: return this .local;
195: }
196:
197: /**
198: * @see net.sf.hajdbc.Database#clean()
199: */
200: @Override
201: public void clean() {
202: this .dirty = false;
203: }
204:
205: /**
206: * @see net.sf.hajdbc.Database#isDirty()
207: */
208: @Override
209: public boolean isDirty() {
210: return this .dirty;
211: }
212:
213: /**
214: * Set the dirty flag if the new value differs from the old value.
215: * @param oldValue
216: * @param newValue
217: */
218: protected void checkDirty(Object oldValue, Object newValue) {
219: this .dirty |= ((oldValue != null) && (newValue != null)) ? !oldValue
220: .equals(newValue)
221: : (oldValue != newValue);
222: }
223:
224: /**
225: * @see java.lang.Comparable#compareTo(Object)
226: */
227: @Override
228: public int compareTo(Database<D> database) {
229: return this.id.compareTo(database.getId());
230: }
231: }
|