001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
006: * Contact: sequoia@continuent.org
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: * Initial developer(s): Emmanuel Cecchet.
021: * Contributor(s): Jean-Bernard van Zuylen
022: */package org.continuent.sequoia.controller.loadbalancer.policies.createtable;
023:
024: import java.util.ArrayList;
025:
026: import org.continuent.sequoia.common.xml.DatabasesXmlTags;
027: import org.continuent.sequoia.controller.backend.DatabaseBackend;
028:
029: /**
030: * Defines the policy to adopt when creating a new table.
031: *
032: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
033: * @author <a href="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
034: * </a>
035: * @version 1.0
036: */
037: public abstract class CreateTableRule {
038: /** List of backend names to wait for. */
039: protected ArrayList backendList;
040:
041: /** Number of nodes that must create the table. */
042: protected int nbOfNodes = 0;
043:
044: /**
045: * Table name pattern to which this rule apply (null means it is the default
046: * rule).
047: */
048: protected String tableName = null;
049:
050: protected int policy;
051:
052: /**
053: * Constructor for CreateTableRule.
054: *
055: * @param policy the implemented policy
056: */
057: public CreateTableRule(int policy) {
058: this .policy = policy;
059: backendList = new ArrayList();
060: }
061:
062: /**
063: * Creates a new <code>CreateTableRule</code> instance.
064: *
065: * @param policy the implemented policy
066: * @param backendList the backend list to use
067: */
068: public CreateTableRule(int policy, ArrayList backendList) {
069: if (backendList == null)
070: throw new IllegalArgumentException(
071: "Null backendList in CreateTableRule constructor");
072:
073: this .policy = policy;
074: this .backendList = backendList;
075: }
076:
077: /**
078: * Add a backend name to the list of backends to wait for.
079: *
080: * @param name backend name
081: */
082: public void addBackendName(String name) {
083: backendList.add(name);
084: }
085:
086: /**
087: * Returns the backendList.
088: *
089: * @return ArrayList
090: */
091: public ArrayList getBackendList() {
092: return backendList;
093: }
094:
095: /**
096: * Returns the number of nodes.
097: *
098: * @return an <code>int</code> value
099: */
100: public int getNumberOfNodes() {
101: return nbOfNodes;
102: }
103:
104: /**
105: * Sets the number of nodes.
106: *
107: * @param numberOfNodes the number of nodes to set
108: */
109: public void setNumberOfNodes(int numberOfNodes) {
110: this .nbOfNodes = numberOfNodes;
111: }
112:
113: /**
114: * Returns the table name.
115: *
116: * @return a <code>String</code> value
117: */
118: public String getTableName() {
119: return tableName;
120: }
121:
122: /**
123: * Sets the table name.
124: *
125: * @param tableName the table name to set
126: */
127: public void setTableName(String tableName) {
128: this .tableName = tableName;
129: }
130:
131: /**
132: * Returns the policy.
133: *
134: * @return an <code>int</code> value
135: */
136: public int getPolicy() {
137: return policy;
138: }
139:
140: /**
141: * Sets the policy.
142: *
143: * @param policy the policy to set
144: */
145: public void setPolicy(int policy) {
146: this .policy = policy;
147: }
148:
149: /**
150: * Returns <code>true</code> if this rule is the default rule.
151: *
152: * @return <code>boolean</code>
153: */
154: public boolean isDefaultRule() {
155: return this .tableName == null;
156: }
157:
158: /**
159: * Pickups backends from the given backends arraylist according to the current
160: * rule policy.
161: *
162: * @param backends backends to choose from
163: * @return <code>Arraylist</code> of choosen <code>DatabaseBackend</code>
164: * @throws CreateTableException in some specific implementations (not this
165: * one)
166: */
167: public ArrayList getBackends(ArrayList backends)
168: throws CreateTableException {
169: ArrayList clonedList;
170:
171: int size = backends.size();
172:
173: if (backendList.size() > 0) { // Keep only the backends that are affected by this rule
174: clonedList = new ArrayList(size);
175: for (int i = 0; i < size; i++) {
176: DatabaseBackend db = (DatabaseBackend) backends.get(i);
177: if (db.isWriteEnabled()
178: && backendList.contains(db.getName()))
179: clonedList.add(db);
180: }
181: } else { // Take all enabled backends
182: clonedList = new ArrayList(size);
183: for (int i = 0; i < size; i++) {
184: DatabaseBackend db = (DatabaseBackend) backends.get(i);
185: if (db.isWriteEnabled())
186: clonedList.add(db);
187: }
188: }
189:
190: return clonedList;
191: }
192:
193: /**
194: * Gives information about the current policy.
195: *
196: * @return a <code>String</code> value
197: */
198: public abstract String getInformation();
199:
200: /**
201: * Gives information about the current policy in xml
202: *
203: * @return a <code>String</code> value in xml
204: */
205: public String getXml()
206:
207: {
208: StringBuffer info = new StringBuffer();
209: info.append("<" + DatabasesXmlTags.ELT_CreateTable + " "
210: + DatabasesXmlTags.ATT_tableName + "=\"" + tableName
211: + "\" " + DatabasesXmlTags.ATT_policy + "=\""
212: + CreateTablePolicy.getXmlValue(policy) + "\" "
213: + DatabasesXmlTags.ATT_numberOfNodes + "=\""
214: + nbOfNodes + "\">");
215: ArrayList list = this .getBackendList();
216: int count = list.size();
217: for (int i = 0; i < count; i++) {
218: info.append("<" + DatabasesXmlTags.ELT_BackendName + " "
219: + DatabasesXmlTags.ATT_name + "=\""
220: + ((String) list.get(i)) + "\"/>");
221: }
222: info.append("</" + DatabasesXmlTags.ELT_CreateTable + ">");
223: return info.toString();
224: }
225:
226: }
|