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

CreateTableRule.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2005 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): Jean-Bernard van Zuylen
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.loadbalancer.policies.createtable;
00026 
00027 import java.util.ArrayList;
00028 
00029 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00030 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
00031 
00032 /**
00033  * Defines the policy to adopt when creating a new table.
00034  * 
00035  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
00036  * @author <a href="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
00037  *         </a>
00038  * @version 1.0
00039  */
00040 public abstract class CreateTableRule
00041 {
00042   /** List of backend names to wait for. */
00043   protected ArrayList backendList;
00044 
00045   /** Number of nodes that must create the table. */
00046   protected int nbOfNodes = 0;
00047 
00048   /**
00049    * Table name pattern to which this rule apply (null means it is the default
00050    * rule).
00051    */
00052   protected String tableName = null;
00053 
00054   protected int policy;
00055 
00056   /**
00057    * Constructor for CreateTableRule.
00058    * 
00059    * @param policy the implemented policy
00060    */
00061   public CreateTableRule(int policy)
00062   {
00063     this.policy = policy;
00064     backendList = new ArrayList();
00065   }
00066 
00067   /**
00068    * Creates a new <code>CreateTableRule</code> instance.
00069    * 
00070    * @param policy the implemented policy
00071    * @param backendList the backend list to use
00072    */
00073   public CreateTableRule(int policy, ArrayList backendList)
00074   {
00075     if (backendList == null)
00076       throw new IllegalArgumentException("Null backendList in CreateTableRule constructor");
00077 
00078     this.policy = policy;
00079     this.backendList = backendList;
00080   }
00081 
00082   /**
00083    * Add a backend name to the list of backends to wait for.
00084    * 
00085    * @param name backend name
00086    */
00087   public void addBackendName(String name)
00088   {
00089     backendList.add(name);
00090   }
00091 
00092   /**
00093    * Returns the backendList.
00094    * 
00095    * @return ArrayList
00096    */
00097   public ArrayList getBackendList()
00098   {
00099     return backendList;
00100   }
00101 
00102   /**
00103    * Returns the number of nodes.
00104    * 
00105    * @return an <code>int</code> value
00106    */
00107   public int getNumberOfNodes()
00108   {
00109     return nbOfNodes;
00110   }
00111 
00112   /**
00113    * Sets the number of nodes.
00114    * 
00115    * @param numberOfNodes the number of nodes to set
00116    */
00117   public void setNumberOfNodes(int numberOfNodes)
00118   {
00119     this.nbOfNodes = numberOfNodes;
00120   }
00121 
00122   /**
00123    * Returns the table name.
00124    * 
00125    * @return a <code>String</code> value
00126    */
00127   public String getTableName()
00128   {
00129     return tableName;
00130   }
00131 
00132   /**
00133    * Sets the table name.
00134    * 
00135    * @param tableName the table name to set
00136    */
00137   public void setTableName(String tableName)
00138   {
00139     this.tableName = tableName;
00140   }
00141 
00142   /**
00143    * Returns the policy.
00144    * 
00145    * @return an <code>int</code> value
00146    */
00147   public int getPolicy()
00148   {
00149     return policy;
00150   }
00151 
00152   /**
00153    * Sets the policy.
00154    * 
00155    * @param policy the policy to set
00156    */
00157   public void setPolicy(int policy)
00158   {
00159     this.policy = policy;
00160   }
00161 
00162   /**
00163    * Returns <code>true</code> if this rule is the default rule.
00164    * 
00165    * @return <code>boolean</code>
00166    */
00167   public boolean isDefaultRule()
00168   {
00169     return this.tableName == null;
00170   }
00171 
00172   /**
00173    * Pickups backends from the given backends arraylist according to the
00174    * current rule policy.
00175    * 
00176    * @param backends backends to choose from
00177    * @return <code>Arraylist</code> of choosen <code>DatabaseBackend</code>
00178    * @exception CreateTableException if the rule cannot be applied
00179    */
00180   public ArrayList getBackends(ArrayList backends) throws CreateTableException
00181   {
00182     ArrayList clonedList;
00183 
00184     int size = backends.size();
00185 
00186     if (backendList.size() > 0)
00187     { // Keep only the backends that are affected by this rule
00188       clonedList = new ArrayList(size);
00189       for (int i = 0; i < size; i++)
00190       {
00191         DatabaseBackend db = (DatabaseBackend) backends.get(i);
00192         if (db.isWriteEnabled() && backendList.contains(db.getName()))
00193           clonedList.add(db);
00194       }
00195     }
00196     else
00197     { // Take all enabled backends
00198       clonedList = new ArrayList(size);
00199       for (int i = 0; i < size; i++)
00200       {
00201         DatabaseBackend db = (DatabaseBackend) backends.get(i);
00202         if (db.isWriteEnabled())
00203           clonedList.add(db);
00204       }
00205     }
00206     
00207     return clonedList;
00208   }
00209 
00210   /**
00211    * Gives information about the current policy.
00212    * 
00213    * @return a <code>String</code> value
00214    */
00215   public abstract String getInformation();
00216 
00217   /**
00218    * Gives information about the current policy in xml
00219    * 
00220    * @return a <code>String</code> value in xml
00221    */
00222   public String getXml()
00223   
00224   {
00225     StringBuffer info = new StringBuffer();
00226     info.append(
00227       "<"
00228         + DatabasesXmlTags.ELT_CreateTable
00229         + " "
00230         + DatabasesXmlTags.ATT_tableName
00231         + "=\""
00232         + tableName
00233         + "\" "
00234         + DatabasesXmlTags.ATT_policy
00235         + "=\""
00236         + CreateTablePolicy.getXmlValue(policy)
00237         + "\" "
00238         + DatabasesXmlTags.ATT_numberOfNodes
00239         + "=\""
00240         + nbOfNodes
00241         + "\">");
00242     ArrayList list = this.getBackendList();
00243     int count = list.size();
00244     for (int i = 0; i < count; i++)
00245     {
00246       info.append(
00247         "<"
00248           + DatabasesXmlTags.ELT_BackendName
00249           + " "
00250           + DatabasesXmlTags.ATT_name
00251           + "=\""
00252           + ((String) list.get(i))
00253           + "\"/>");
00254     }
00255     info.append("</" + DatabasesXmlTags.ELT_CreateTable + ">");
00256     return info.toString();
00257   }
00258 
00259 }

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