KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > scheduler > schema > SchedulerDatabaseSchema


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): _________________________.
23  */

24
25 package org.objectweb.cjdbc.controller.scheduler.schema;
26
27 import java.io.Serializable JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema;
31 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable;
32
33 /**
34  * A <code>SchedulerDatabaseSchema</code> describes all the tables and columns
35  * of a database and its associated cache entries.
36  *
37  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
38  * @version 1.0
39  */

40 public class SchedulerDatabaseSchema implements Serializable JavaDoc
41 {
42   /** <code>ArrayList</code> of <code>SchedulerDatabaseTable</code>. */
43   private ArrayList JavaDoc tables;
44
45   private TransactionExclusiveLock lock = new TransactionExclusiveLock();
46
47   /**
48    * Creates a new <code>SchedulerDatabaseSchema</code> instance by cloning an
49    * existing <code>DatabaseSchema</code>.
50    *
51    * @param schema the database schema to clone
52    */

53   public SchedulerDatabaseSchema(DatabaseSchema schema)
54   {
55     if (schema == null)
56     {
57       tables = new ArrayList JavaDoc();
58       return;
59     }
60
61     // Clone the tables
62
ArrayList JavaDoc origTables = schema.getTables();
63     int size = origTables.size();
64     tables = new ArrayList JavaDoc(size);
65     for (int i = 0; i < size; i++)
66       tables.add(new SchedulerDatabaseTable((DatabaseTable) origTables.get(i)));
67   }
68
69   /**
70    * Adds a <code>SchedulerDatabaseTable</code> describing a table of the
71    * database.
72    *
73    * @param table the table to add
74    */

75   public void addTable(SchedulerDatabaseTable table)
76   {
77     tables.add(table);
78   }
79
80   /**
81    * Removes a <code>SchedulerDatabaseTable</code> describing a table of the
82    * database.
83    *
84    * @param table the table to remove
85    */

86   public void removeTable(SchedulerDatabaseTable table)
87   {
88     tables.remove(table);
89   }
90
91   /**
92    * Merge the given schema with the current one. All missing tables are added
93    * if no conflict is detected. An exception is thrown if the given schema
94    * definition conflicts with the current one.
95    *
96    * @param databaseSchema the schema to merge
97    */

98   public void mergeSchema(SchedulerDatabaseSchema databaseSchema)
99   {
100     if (databaseSchema == null)
101       return;
102
103     ArrayList JavaDoc otherTables = databaseSchema.getTables();
104     if (otherTables == null)
105       return;
106
107     int size = otherTables.size();
108     for (int i = 0; i < size; i++)
109     {
110       SchedulerDatabaseTable t = (SchedulerDatabaseTable) otherTables.get(i);
111       SchedulerDatabaseTable original = getTable(t.getName());
112       if (original == null)
113         addTable(t);
114     }
115   }
116
117   /**
118    * Returns an <code>ArrayList</code> of <code>SchedulerDatabaseTable</code>
119    * objects describing the database.
120    *
121    * @return an <code>ArrayList</code> of <code>SchedulerDatabaseTable</code>
122    */

123   public ArrayList JavaDoc getTables()
124   {
125     return tables;
126   }
127
128   /**
129    * Returns the <code>SchedulerDatabaseTable</code> object matching the given
130    * table name or <code>null</code> if not found. Matching is case insensitive.
131    *
132    * @param tableName the table name to look for
133    * @return a <code>SchedulerDatabaseTable</code> value or null
134    */

135   public SchedulerDatabaseTable getTable(String JavaDoc tableName)
136   {
137     SchedulerDatabaseTable t;
138     int size = tables.size();
139     for (int i = 0; i < size; i++)
140     {
141       t = (SchedulerDatabaseTable) tables.get(i);
142       if (tableName.equalsIgnoreCase(t.getName()))
143         return t;
144     }
145     return null;
146   }
147
148   /**
149    * Returns <code>true</code> if the given <code>TableName</code> is found
150    * in this schema.
151    *
152    * @param tableName the name of the table you are looking for
153    * @return <code>true</code> if the table has been found
154    */

155   public boolean hasTable(String JavaDoc tableName)
156   {
157     int size = tables.size();
158     for (int i = 0; i < size; i++)
159     {
160       SchedulerDatabaseTable t = (SchedulerDatabaseTable) tables.get(i);
161       if (tableName.equals(t.getName()))
162         return true;
163     }
164     return false;
165   }
166
167   /**
168    * Returns the lock for this table.
169    *
170    * @return a <code>TransactionExclusiveLock</code> instance
171    * @see TransactionExclusiveLock
172    */

173   public TransactionExclusiveLock getLock()
174   {
175     return lock;
176   }
177
178   /**
179    * Two <code>SchedulerDatabaseSchema</code> are equals if they have the same
180    * tables.
181    *
182    * @param other the object to compare with
183    * @return true if the objects are the same
184    */

185   public boolean equals(Object JavaDoc other)
186   {
187     if (!(other instanceof SchedulerDatabaseSchema))
188       return false;
189
190     if (tables == null)
191       return ((SchedulerDatabaseSchema) other).getTables() == null;
192     else
193       return tables.equals(((SchedulerDatabaseSchema) other).getTables());
194   }
195
196   /**
197    * Returns information about the database schema.
198    *
199    * @param longFormat <code>true</code> for a <code>long</code> format,
200    * <code>false</code> for a short summary
201    * @return a <code>String</code> value
202    */

203   public String JavaDoc getInformation(boolean longFormat)
204   {
205     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
206     SchedulerDatabaseTable t;
207     int size = tables.size();
208     for (int i = 0; i < size; i++)
209     {
210       t = (SchedulerDatabaseTable) tables.get(i);
211       result.append(t.getInformation(longFormat));
212       result.append(System.getProperty("line.separator"));
213     }
214     return result.toString();
215   }
216 }
Popular Tags