KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > sql > AbstractDatabase


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.sql;
22
23 import java.util.Properties JavaDoc;
24
25 import net.sf.hajdbc.Database;
26
27 /**
28  * @author Paul Ferraro
29  * @version $Revision: 1042 $
30  * @param <T>
31  * @since 1.0
32  */

33 /**
34  * @author Paul Ferraro
35  *
36  * @param <T>
37  */

38 /**
39  * @author Paul Ferraro
40  *
41  * @param <T>
42  */

43 public abstract class AbstractDatabase<T> implements Database<T>
44 {
45     protected String JavaDoc id;
46     protected String JavaDoc user;
47     protected String JavaDoc password;
48     protected Properties JavaDoc properties = new Properties JavaDoc();
49     protected int weight = 1;
50     protected boolean dirty = false;
51
52     /**
53      * @see net.sf.hajdbc.ActiveDatabaseMBean#getId()
54      */

55     public String JavaDoc getId()
56     {
57         return this.id;
58     }
59     
60     /**
61      * @param id
62      */

63     public void setId(String JavaDoc id)
64     {
65         this.checkDirty(this.id, id);
66         this.id = id;
67     }
68     
69     /**
70      * @see net.sf.hajdbc.ActiveDatabaseMBean#getUser()
71      */

72     public String JavaDoc getUser()
73     {
74         return this.user;
75     }
76     
77     /**
78      * @see net.sf.hajdbc.InactiveDatabaseMBean#setUser(java.lang.String)
79      */

80     public void setUser(String JavaDoc user)
81     {
82         this.checkDirty(this.user, user);
83         this.user = user;
84     }
85     
86     /**
87      * @see net.sf.hajdbc.ActiveDatabaseMBean#getPassword()
88      */

89     public String JavaDoc getPassword()
90     {
91         return this.password;
92     }
93     
94     /**
95      * @see net.sf.hajdbc.InactiveDatabaseMBean#setPassword(java.lang.String)
96      */

97     public void setPassword(String JavaDoc password)
98     {
99         this.checkDirty(this.password, password);
100         this.password = password;
101     }
102
103     /**
104      * @see net.sf.hajdbc.ActiveDatabaseMBean#getWeight()
105      */

106     public int getWeight()
107     {
108         return this.weight;
109     }
110     
111     /**
112      * @see net.sf.hajdbc.InactiveDatabaseMBean#setWeight(int)
113      */

114     public void setWeight(int weight)
115     {
116         if (weight < 0)
117         {
118             throw new IllegalArgumentException JavaDoc();
119         }
120         
121         this.checkDirty(this.weight, weight);
122         this.weight = weight;
123     }
124     
125     /**
126      * @see java.lang.Object#hashCode()
127      */

128     @Override JavaDoc
129     public int hashCode()
130     {
131         return this.id.hashCode();
132     }
133     
134     /**
135      * @see java.lang.Object#equals(java.lang.Object)
136      */

137     @Override JavaDoc
138     public boolean equals(Object JavaDoc object)
139     {
140         if ((object == null) || !Database.class.isInstance(object))
141         {
142             return false;
143         }
144         
145         Database database = (Database) object;
146         
147         return this.id.equals(database.getId());
148     }
149     
150     /**
151      * @see java.lang.Object#toString()
152      */

153     @Override JavaDoc
154     public String JavaDoc toString()
155     {
156         return this.id;
157     }
158     
159     /**
160      * @see net.sf.hajdbc.ActiveDatabaseMBean#getProperties()
161      */

162     public Properties JavaDoc getProperties()
163     {
164         return this.properties;
165     }
166     
167     /**
168      * @param properties
169      */

170     public void setProperties(Properties JavaDoc properties)
171     {
172         this.checkDirty(this.properties, properties);
173         this.properties = properties;
174     }
175
176     /**
177      * @see net.sf.hajdbc.InactiveDatabaseMBean#removeProperty(java.lang.String)
178      */

179     public void removeProperty(String JavaDoc name)
180     {
181         this.dirty |= this.properties.containsKey(name);
182         this.properties.remove(name);
183     }
184
185     /**
186      * @see net.sf.hajdbc.InactiveDatabaseMBean#setProperty(java.lang.String, java.lang.String)
187      */

188     public void setProperty(String JavaDoc name, String JavaDoc value)
189     {
190         this.checkDirty(this.properties.getProperty(name), value);
191         this.properties.setProperty(name, value);
192     }
193
194     /**
195      * @see net.sf.hajdbc.Database#clean()
196      */

197     public void clean()
198     {
199         this.dirty = false;
200     }
201
202     /**
203      * @see net.sf.hajdbc.Database#isDirty()
204      */

205     public boolean isDirty()
206     {
207         return this.dirty;
208     }
209     
210     /**
211      * Set the dirty flag if the new value differs from the old value.
212      * @param oldValue
213      * @param newValue
214      */

215     protected void checkDirty(Object JavaDoc oldValue, Object JavaDoc newValue)
216     {
217         this.dirty |= ((oldValue != null) && (newValue != null)) ? !oldValue.equals(newValue) : (oldValue != newValue);
218     }
219
220     /**
221      * @see java.lang.Comparable#compareTo(Object)
222      */

223     public int compareTo(Database database)
224     {
225         return this.id.compareTo(database.getId());
226     }
227 }
228
Popular Tags