00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00034
00035
00036
00037
00038
00039
00040 public abstract class CreateTableRule
00041 {
00042
00043 protected ArrayList backendList;
00044
00045
00046 protected int nbOfNodes = 0;
00047
00048
00049
00050
00051
00052 protected String tableName = null;
00053
00054 protected int policy;
00055
00056
00057
00058
00059
00060
00061 public CreateTableRule(int policy)
00062 {
00063 this.policy = policy;
00064 backendList = new ArrayList();
00065 }
00066
00067
00068
00069
00070
00071
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
00084
00085
00086
00087 public void addBackendName(String name)
00088 {
00089 backendList.add(name);
00090 }
00091
00092
00093
00094
00095
00096
00097 public ArrayList getBackendList()
00098 {
00099 return backendList;
00100 }
00101
00102
00103
00104
00105
00106
00107 public int getNumberOfNodes()
00108 {
00109 return nbOfNodes;
00110 }
00111
00112
00113
00114
00115
00116
00117 public void setNumberOfNodes(int numberOfNodes)
00118 {
00119 this.nbOfNodes = numberOfNodes;
00120 }
00121
00122
00123
00124
00125
00126
00127 public String getTableName()
00128 {
00129 return tableName;
00130 }
00131
00132
00133
00134
00135
00136
00137 public void setTableName(String tableName)
00138 {
00139 this.tableName = tableName;
00140 }
00141
00142
00143
00144
00145
00146
00147 public int getPolicy()
00148 {
00149 return policy;
00150 }
00151
00152
00153
00154
00155
00156
00157 public void setPolicy(int policy)
00158 {
00159 this.policy = policy;
00160 }
00161
00162
00163
00164
00165
00166
00167 public boolean isDefaultRule()
00168 {
00169 return this.tableName == null;
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
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 {
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 {
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
00212
00213
00214
00215 public abstract String getInformation();
00216
00217
00218
00219
00220
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 }