KickJava   Java API By Example, From Geeks To Geeks.

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


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

22
23 package org.continuent.sequoia.controller.loadbalancer.policies.createtable;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.continuent.sequoia.common.xml.DatabasesXmlTags;
29 import org.continuent.sequoia.common.xml.XmlComponent;
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  * @version 1.0
36  */

37 public class CreateTablePolicy implements XmlComponent
38 {
39   /** Pickup a backend name randomly in the backend list. */
40   public static final int RANDOM = 0;
41
42   /** Backends are chosen using a round-robin algorithm. */
43   public static final int ROUND_ROBIN = 1;
44
45   /** Table is created on all backends in the backend list. */
46   public static final int ALL = 2;
47
48   /** List of backends to wait for. */
49   private HashMap JavaDoc ruleList = new HashMap JavaDoc();
50
51   /**
52    * Adds a rule to this policy. <br>
53    * If the rule's table name is <code>null</code>, the rule is considered as
54    * the default rule
55    *
56    * @param rule rule to add
57    */

58   public void addRule(CreateTableRule rule)
59   {
60     ruleList.put(rule.getTableName(), rule);
61   }
62
63   /**
64    * Returns the rule Hashmap(table name,rule).
65    *
66    * @return Hashmap
67    */

68   public HashMap JavaDoc getRuleList()
69   {
70     return ruleList;
71   }
72
73   /**
74    * Gets the rule corresponding to a table name.
75    *
76    * @param tableName table name of the rule
77    * @return the rule or <code>null</code> if no specific rule has been
78    * defined for this table
79    */

80   public CreateTableRule getTableRule(String JavaDoc tableName)
81   {
82     return (CreateTableRule) ruleList.get(tableName);
83   }
84
85   /**
86    * Returns the default rule or <code>null</code> if no default rule has been
87    * defined.
88    *
89    * @return a <code>CreateTableRule</code>
90    */

91   public CreateTableRule getDefaultRule()
92   {
93     return (CreateTableRule) ruleList.get(null);
94   }
95
96   /**
97    * Returns the xml attribute value for the given policy
98    *
99    * @param policy the policy to convert
100    * @return xml attribute value or "" if not found
101    */

102   public static final String JavaDoc getXmlValue(int policy)
103   {
104     switch (policy)
105     {
106       case RANDOM :
107         return DatabasesXmlTags.VAL_random;
108       case ROUND_ROBIN :
109         return DatabasesXmlTags.VAL_roundRobin;
110       case ALL :
111         return DatabasesXmlTags.VAL_all;
112       default :
113         return "";
114     }
115   }
116
117   /**
118    * Returns xml formatted string containing information on all rules of the
119    * system
120    *
121    * @return xml formatted string.
122    */

123   public String JavaDoc getXml()
124   {
125     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
126     for (Iterator JavaDoc iterator = ruleList.keySet().iterator(); iterator.hasNext();)
127       info.append(((CreateTableRule) ruleList.get(iterator.next())).getXml());
128     return info.toString();
129   }
130
131 }
132
Popular Tags