KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > loadbalancer > policies > createtable > CreateTableRule


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 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): Jean-Bernard van Zuylen
23  */

24
25 package org.objectweb.cjdbc.controller.loadbalancer.policies.createtable;
26
27 import java.util.ArrayList JavaDoc;
28
29 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
30 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
31
32 /**
33  * Defines the policy to adopt when creating a new table.
34  *
35  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
36  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
37  * </a>
38  * @version 1.0
39  */

40 public abstract class CreateTableRule
41 {
42   /** List of backend names to wait for. */
43   protected ArrayList JavaDoc backendList;
44
45   /** Number of nodes that must create the table. */
46   protected int nbOfNodes = 0;
47
48   /**
49    * Table name pattern to which this rule apply (null means it is the default
50    * rule).
51    */

52   protected String JavaDoc tableName = null;
53
54   protected int policy;
55
56   /**
57    * Constructor for CreateTableRule.
58    *
59    * @param policy the implemented policy
60    */

61   public CreateTableRule(int policy)
62   {
63     this.policy = policy;
64     backendList = new ArrayList JavaDoc();
65   }
66
67   /**
68    * Creates a new <code>CreateTableRule</code> instance.
69    *
70    * @param policy the implemented policy
71    * @param backendList the backend list to use
72    */

73   public CreateTableRule(int policy, ArrayList JavaDoc backendList)
74   {
75     if (backendList == null)
76       throw new IllegalArgumentException JavaDoc(
77           "Null backendList in CreateTableRule constructor");
78
79     this.policy = policy;
80     this.backendList = backendList;
81   }
82
83   /**
84    * Add a backend name to the list of backends to wait for.
85    *
86    * @param name backend name
87    */

88   public void addBackendName(String JavaDoc name)
89   {
90     backendList.add(name);
91   }
92
93   /**
94    * Returns the backendList.
95    *
96    * @return ArrayList
97    */

98   public ArrayList JavaDoc getBackendList()
99   {
100     return backendList;
101   }
102
103   /**
104    * Returns the number of nodes.
105    *
106    * @return an <code>int</code> value
107    */

108   public int getNumberOfNodes()
109   {
110     return nbOfNodes;
111   }
112
113   /**
114    * Sets the number of nodes.
115    *
116    * @param numberOfNodes the number of nodes to set
117    */

118   public void setNumberOfNodes(int numberOfNodes)
119   {
120     this.nbOfNodes = numberOfNodes;
121   }
122
123   /**
124    * Returns the table name.
125    *
126    * @return a <code>String</code> value
127    */

128   public String JavaDoc getTableName()
129   {
130     return tableName;
131   }
132
133   /**
134    * Sets the table name.
135    *
136    * @param tableName the table name to set
137    */

138   public void setTableName(String JavaDoc tableName)
139   {
140     this.tableName = tableName;
141   }
142
143   /**
144    * Returns the policy.
145    *
146    * @return an <code>int</code> value
147    */

148   public int getPolicy()
149   {
150     return policy;
151   }
152
153   /**
154    * Sets the policy.
155    *
156    * @param policy the policy to set
157    */

158   public void setPolicy(int policy)
159   {
160     this.policy = policy;
161   }
162
163   /**
164    * Returns <code>true</code> if this rule is the default rule.
165    *
166    * @return <code>boolean</code>
167    */

168   public boolean isDefaultRule()
169   {
170     return this.tableName == null;
171   }
172
173   /**
174    * Pickups backends from the given backends arraylist according to the current
175    * rule policy.
176    *
177    * @param backends backends to choose from
178    * @return <code>Arraylist</code> of choosen <code>DatabaseBackend</code>
179    * @throws CreateTableException in some specific implementations (not this
180    * one)
181    */

182   public ArrayList JavaDoc getBackends(ArrayList JavaDoc backends) throws CreateTableException
183   {
184     ArrayList JavaDoc clonedList;
185
186     int size = backends.size();
187
188     if (backendList.size() > 0)
189     { // Keep only the backends that are affected by this rule
190
clonedList = new ArrayList JavaDoc(size);
191       for (int i = 0; i < size; i++)
192       {
193         DatabaseBackend db = (DatabaseBackend) backends.get(i);
194         if (db.isWriteEnabled() && backendList.contains(db.getName()))
195           clonedList.add(db);
196       }
197     }
198     else
199     { // Take all enabled backends
200
clonedList = new ArrayList JavaDoc(size);
201       for (int i = 0; i < size; i++)
202       {
203         DatabaseBackend db = (DatabaseBackend) backends.get(i);
204         if (db.isWriteEnabled())
205           clonedList.add(db);
206       }
207     }
208
209     return clonedList;
210   }
211
212   /**
213    * Gives information about the current policy.
214    *
215    * @return a <code>String</code> value
216    */

217   public abstract String JavaDoc getInformation();
218
219   /**
220    * Gives information about the current policy in xml
221    *
222    * @return a <code>String</code> value in xml
223    */

224   public String JavaDoc getXml()
225
226   {
227     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
228     info.append("<" + DatabasesXmlTags.ELT_CreateTable + " "
229         + DatabasesXmlTags.ATT_tableName + "=\"" + tableName + "\" "
230         + DatabasesXmlTags.ATT_policy + "=\""
231         + CreateTablePolicy.getXmlValue(policy) + "\" "
232         + DatabasesXmlTags.ATT_numberOfNodes + "=\"" + nbOfNodes + "\">");
233     ArrayList JavaDoc list = this.getBackendList();
234     int count = list.size();
235     for (int i = 0; i < count; i++)
236     {
237       info
238           .append("<" + DatabasesXmlTags.ELT_BackendName + " "
239               + DatabasesXmlTags.ATT_name + "=\"" + ((String JavaDoc) list.get(i))
240               + "\"/>");
241     }
242     info.append("</" + DatabasesXmlTags.ELT_CreateTable + ">");
243     return info.toString();
244   }
245
246 }
247
Popular Tags