KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > loadbalancer > policies > createtable > CreateTablePolicy


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): _______________________
23  */

24
25 package org.objectweb.cjdbc.controller.loadbalancer.policies.createtable;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
31 import org.objectweb.cjdbc.common.xml.XmlComponent;
32
33 /**
34  * Defines the policy to adopt when creating a new table.
35  *
36  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
37  * @version 1.0
38  */

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

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

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

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

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

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

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