KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > loadbalancer > policies > createtable > CreateTableRule


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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