001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Emmanuel Cecchet.
020: * Contributor(s): _______________________
021: */package org.continuent.sequoia.controller.loadbalancer.policies.createtable;
022:
023: import java.util.HashMap;
024: import java.util.Iterator;
025:
026: import org.continuent.sequoia.common.xml.DatabasesXmlTags;
027: import org.continuent.sequoia.common.xml.XmlComponent;
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: * @version 1.0
034: */
035: public class CreateTablePolicy implements XmlComponent {
036: /** Pickup a backend name randomly in the backend list. */
037: public static final int RANDOM = 0;
038:
039: /** Backends are chosen using a round-robin algorithm. */
040: public static final int ROUND_ROBIN = 1;
041:
042: /** Table is created on all backends in the backend list. */
043: public static final int ALL = 2;
044:
045: /** List of backends to wait for. */
046: private HashMap ruleList = new HashMap();
047:
048: /**
049: * Adds a rule to this policy. <br>
050: * If the rule's table name is <code>null</code>, the rule is considered as
051: * the default rule
052: *
053: * @param rule rule to add
054: */
055: public void addRule(CreateTableRule rule) {
056: ruleList.put(rule.getTableName(), rule);
057: }
058:
059: /**
060: * Returns the rule Hashmap(table name,rule).
061: *
062: * @return Hashmap
063: */
064: public HashMap getRuleList() {
065: return ruleList;
066: }
067:
068: /**
069: * Gets the rule corresponding to a table name.
070: *
071: * @param tableName table name of the rule
072: * @return the rule or <code>null</code> if no specific rule has been
073: * defined for this table
074: */
075: public CreateTableRule getTableRule(String tableName) {
076: return (CreateTableRule) ruleList.get(tableName);
077: }
078:
079: /**
080: * Returns the default rule or <code>null</code> if no default rule has been
081: * defined.
082: *
083: * @return a <code>CreateTableRule</code>
084: */
085: public CreateTableRule getDefaultRule() {
086: return (CreateTableRule) ruleList.get(null);
087: }
088:
089: /**
090: * Returns the xml attribute value for the given policy
091: *
092: * @param policy the policy to convert
093: * @return xml attribute value or "" if not found
094: */
095: public static final String getXmlValue(int policy) {
096: switch (policy) {
097: case RANDOM:
098: return DatabasesXmlTags.VAL_random;
099: case ROUND_ROBIN:
100: return DatabasesXmlTags.VAL_roundRobin;
101: case ALL:
102: return DatabasesXmlTags.VAL_all;
103: default:
104: return "";
105: }
106: }
107:
108: /**
109: * Returns xml formatted string containing information on all rules of the
110: * system
111: *
112: * @return xml formatted string.
113: */
114: public String getXml() {
115: StringBuffer info = new StringBuffer();
116: for (Iterator iterator = ruleList.keySet().iterator(); iterator
117: .hasNext();)
118: info.append(((CreateTableRule) ruleList
119: .get(iterator.next())).getXml());
120: return info.toString();
121: }
122:
123: }
|