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 */
021package net.sf.hajdbc.sql;
022
023import java.util.Properties;
024
025import 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 */
032public abstract class AbstractDatabase<D> implements Database<D>
033{
034        private String id;
035        private String user;
036        private String password;
037        private Properties properties = new Properties();
038        private int weight = 1;
039        private boolean dirty = false;
040        private boolean local = false;
041        
042        /**
043         * @see net.sf.hajdbc.ActiveDatabaseMBean#getId()
044         */
045        @Override
046        public String getId()
047        {
048                return this.id;
049        }
050        
051        /**
052         * @param id
053         */
054        public void setId(String id)
055        {
056                this.checkDirty(this.id, id);
057                this.id = id;
058        }
059        
060        /**
061         * @see net.sf.hajdbc.ActiveDatabaseMBean#getUser()
062         */
063        @Override
064        public String getUser()
065        {
066                return this.user;
067        }
068        
069        /**
070         * @see net.sf.hajdbc.InactiveDatabaseMBean#setUser(java.lang.String)
071         */
072        @Override
073        public void setUser(String user)
074        {
075                this.checkDirty(this.user, user);
076                this.user = user;
077        }
078        
079        /**
080         * @see net.sf.hajdbc.ActiveDatabaseMBean#getPassword()
081         */
082        @Override
083        public String getPassword()
084        {
085                return this.password;
086        }
087        
088        /**
089         * @see net.sf.hajdbc.InactiveDatabaseMBean#setPassword(java.lang.String)
090         */
091        @Override
092        public void setPassword(String password)
093        {
094                this.checkDirty(this.password, password);
095                this.password = password;
096        }
097
098        /**
099         * @see net.sf.hajdbc.ActiveDatabaseMBean#getWeight()
100         */
101        @Override
102        public int getWeight()
103        {
104                return this.weight;
105        }
106        
107        /**
108         * @see net.sf.hajdbc.InactiveDatabaseMBean#setWeight(int)
109         */
110        @Override
111        public void setWeight(int weight)
112        {
113                if (weight < 0)
114                {
115                        throw new IllegalArgumentException();
116                }
117                
118                this.checkDirty(this.weight, weight);
119                this.weight = weight;
120        }
121        
122        /**
123         * @see java.lang.Object#hashCode()
124         */
125        @Override
126        public int hashCode()
127        {
128                return this.id.hashCode();
129        }
130        
131        /**
132         * @see java.lang.Object#equals(java.lang.Object)
133         */
134        @SuppressWarnings("unchecked")
135        @Override
136        public boolean equals(Object object)
137        {
138                if ((object == null) || !(object instanceof Database)) return false;
139                
140                String id = ((Database) object).getId();
141                
142                return (id != null) && id.equals(this.id);
143        }
144        
145        /**
146         * @see java.lang.Object#toString()
147         */
148        @Override
149        public String toString()
150        {
151                return this.id;
152        }
153        
154        /**
155         * @see net.sf.hajdbc.ActiveDatabaseMBean#getProperties()
156         */
157        @Override
158        public Properties getProperties()
159        {
160                return this.properties;
161        }
162        
163        /**
164         * @param properties
165         */
166        public void setProperties(Properties properties)
167        {
168                this.checkDirty(this.properties, properties);
169                this.properties = properties;
170        }
171
172        /**
173         * @see net.sf.hajdbc.InactiveDatabaseMBean#removeProperty(java.lang.String)
174         */
175        @Override
176        public void removeProperty(String name)
177        {
178                this.dirty |= this.properties.containsKey(name);
179                this.properties.remove(name);
180        }
181
182        /**
183         * @see net.sf.hajdbc.InactiveDatabaseMBean#setProperty(java.lang.String, java.lang.String)
184         */
185        @Override
186        public void setProperty(String name, String value)
187        {
188                if ((name == null) || (value == null))
189                {
190                        throw new IllegalArgumentException();
191                }
192                
193                this.checkDirty(this.properties.getProperty(name), value);
194                this.properties.setProperty(name, value);
195        }
196
197        /**
198         * @see net.sf.hajdbc.InactiveDatabaseMBean#setLocal(boolean)
199         */
200        @Override
201        public void setLocal(boolean local)
202        {
203                this.checkDirty(this.local, local);
204                this.local = local;
205        }
206
207        /**
208         * @see net.sf.hajdbc.ActiveDatabaseMBean#isLocal()
209         */
210        @Override
211        public boolean isLocal()
212        {
213                return this.local;
214        }
215
216        /**
217         * @see net.sf.hajdbc.Database#clean()
218         */
219        @Override
220        public void clean()
221        {
222                this.dirty = false;
223        }
224
225        /**
226         * @see net.sf.hajdbc.Database#isDirty()
227         */
228        @Override
229        public boolean isDirty()
230        {
231                return this.dirty;
232        }
233        
234        /**
235         * Set the dirty flag if the new value differs from the old value.
236         * @param oldValue
237         * @param newValue
238         */
239        protected void checkDirty(Object oldValue, Object newValue)
240        {
241                this.dirty |= ((oldValue != null) && (newValue != null)) ? !oldValue.equals(newValue) : (oldValue != newValue);
242        }
243
244        /**
245         * @see java.lang.Comparable#compareTo(Object)
246         */
247        @Override
248        public int compareTo(Database<D> database)
249        {
250                return this.id.compareTo(database.getId());
251        }
252}