Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

SchedulerDatabaseSchema.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Emmanuel Cecchet.
00022  * Contributor(s): _________________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.scheduler.schema;
00026 
00027 import java.io.Serializable;
00028 import java.util.ArrayList;
00029 
00030 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema;
00031 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable;
00032 
00033 /**
00034  * A <code>SchedulerDatabaseSchema</code> describes all the tables and columns
00035  * of a database and its associated cache entries.
00036  * 
00037  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00038  * @version 1.0
00039  */
00040 public class SchedulerDatabaseSchema implements Serializable
00041 {
00042   /** <code>ArrayList</code> of <code>SchedulerDatabaseTable</code>. */
00043   private ArrayList                tables;
00044 
00045   private TransactionExclusiveLock lock = new TransactionExclusiveLock();
00046 
00047   /**
00048    * Creates a new <code>SchedulerDatabaseSchema</code> instance by cloning an
00049    * existing <code>DatabaseSchema</code>.
00050    * 
00051    * @param schema the database schema to clone
00052    */
00053   public SchedulerDatabaseSchema(DatabaseSchema schema)
00054   {
00055     if (schema == null)
00056     {
00057       tables = new ArrayList();
00058       return;
00059     }
00060 
00061     // Clone the tables
00062     ArrayList origTables = schema.getTables();
00063     int size = origTables.size();
00064     tables = new ArrayList(size);
00065     for (int i = 0; i < size; i++)
00066       tables.add(new SchedulerDatabaseTable((DatabaseTable) origTables.get(i)));
00067   }
00068 
00069   /**
00070    * Adds a <code>SchedulerDatabaseTable</code> describing a table of the
00071    * database.
00072    * 
00073    * @param table the table to add
00074    */
00075   public void addTable(SchedulerDatabaseTable table)
00076   {
00077     tables.add(table);
00078   }
00079 
00080   /**
00081    * Removes a <code>SchedulerDatabaseTable</code> describing a table of the
00082    * database.
00083    * 
00084    * @param table the table to remove
00085    */
00086   public void removeTable(SchedulerDatabaseTable table)
00087   {
00088     tables.remove(table);
00089   }
00090 
00091   /**
00092    * Merge the given schema with the current one. All missing tables are added
00093    * if no conflict is detected. An exception is thrown if the given schema
00094    * definition conflicts with the current one.
00095    * 
00096    * @param databaseSchema the schema to merge
00097    */
00098   public void mergeSchema(SchedulerDatabaseSchema databaseSchema)
00099   {
00100     if (databaseSchema == null)
00101       return;
00102 
00103     ArrayList otherTables = databaseSchema.getTables();
00104     if (otherTables == null)
00105       return;
00106 
00107     int size = otherTables.size();
00108     for (int i = 0; i < size; i++)
00109     {
00110       SchedulerDatabaseTable t = (SchedulerDatabaseTable) otherTables.get(i);
00111       SchedulerDatabaseTable original = getTable(t.getName());
00112       if (original == null)
00113         addTable(t);
00114     }
00115   }
00116 
00117   /**
00118    * Returns an <code>ArrayList</code> of <code>SchedulerDatabaseTable</code>
00119    * objects describing the database.
00120    * 
00121    * @return an <code>ArrayList</code> of <code>SchedulerDatabaseTable</code>
00122    */
00123   public ArrayList getTables()
00124   {
00125     return tables;
00126   }
00127 
00128   /**
00129    * Returns the <code>SchedulerDatabaseTable</code> object matching the given
00130    * table name or <code>null</code> if not found. Matching is case insensitive.
00131    * 
00132    * @param tableName the table name to look for
00133    * @return a <code>SchedulerDatabaseTable</code> value or null
00134    */
00135   public SchedulerDatabaseTable getTable(String tableName)
00136   {
00137     SchedulerDatabaseTable t;
00138     int size = tables.size();
00139     for (int i = 0; i < size; i++)
00140     {
00141       t = (SchedulerDatabaseTable) tables.get(i);
00142       if (tableName.equalsIgnoreCase(t.getName()))
00143         return t;
00144     }
00145     return null;
00146   }
00147 
00148   /**
00149    * Returns <code>true</code> if the given <code>TableName</code> is found
00150    * in this schema.
00151    * 
00152    * @param tableName the name of the table you are looking for
00153    * @return <code>true</code> if the table has been found
00154    */
00155   public boolean hasTable(String tableName)
00156   {
00157     int size = tables.size();
00158     for (int i = 0; i < size; i++)
00159     {
00160       SchedulerDatabaseTable t = (SchedulerDatabaseTable) tables.get(i);
00161       if (tableName.equals(t.getName()))
00162         return true;
00163     }
00164     return false;
00165   }
00166 
00167   /**
00168    * Returns the lock for this table.
00169    * 
00170    * @return a <code>TransactionExclusiveLock</code> instance
00171    * @see TransactionExclusiveLock
00172    */
00173   public TransactionExclusiveLock getLock()
00174   {
00175     return lock;
00176   }
00177 
00178   /**
00179    * Two <code>SchedulerDatabaseSchema</code> are equals if they have the same
00180    * tables.
00181    * 
00182    * @param other the object to compare with
00183    * @return true if the objects are the same
00184    */
00185   public boolean equals(Object other)
00186   {
00187     if (!(other instanceof SchedulerDatabaseSchema))
00188       return false;
00189 
00190     if (tables == null)
00191       return ((SchedulerDatabaseSchema) other).getTables() == null;
00192     else
00193       return tables.equals(((SchedulerDatabaseSchema) other).getTables());
00194   }
00195 
00196   /**
00197    * Returns information about the database schema.
00198    * 
00199    * @param longFormat <code>true</code> for a <code>long</code> format,
00200    *          <code>false</code> for a short summary
00201    * @return a <code>String</code> value
00202    */
00203   public String getInformation(boolean longFormat)
00204   {
00205     StringBuffer result = new StringBuffer();
00206     SchedulerDatabaseTable t;
00207     int size = tables.size();
00208     for (int i = 0; i < size; i++)
00209     {
00210       t = (SchedulerDatabaseTable) tables.get(i);
00211       result.append(t.getInformation(longFormat));
00212       result.append(System.getProperty("line.separator"));
00213     }
00214     return result.toString();
00215   }
00216 }

Generated on Mon Apr 11 22:01:34 2005 for C-JDBC by  doxygen 1.3.9.1