KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > scheduler > schema > SchedulerDatabaseSchema


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Emmanuel Cecchet.
20  * Contributor(s): _________________________.
21  */

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

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

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

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

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

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

122   public ArrayList JavaDoc getTables()
123   {
124     return tables;
125   }
126
127   /**
128    * Returns the <code>SchedulerDatabaseTable</code> object matching the given
129    * table name or <code>null</code> if not found. Matching is case
130    * 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