00001 /** 00002 * C-JDBC: Clustered JDBC. 00003 * Copyright (C) 2002-2004 French National Institute For Research In Computer 00004 * Science And Control (INRIA). 00005 * Contact: c-jdbc@objectweb.org 00006 * 00007 * This library is free software; you can redistribute it and/or modify it 00008 * under the terms of the GNU Lesser General Public License as published by the 00009 * Free Software Foundation; either version 2.1 of the License, or any later 00010 * version. 00011 * 00012 * This library is distributed in the hope that it will be useful, but WITHOUT 00013 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00014 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 00015 * for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public License 00018 * along with this library; if not, write to the Free Software Foundation, 00019 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00020 * 00021 * Initial developer(s): Emmanuel Cecchet. 00022 * Contributor(s): _______________________ 00023 */ 00024 00025 package org.objectweb.cjdbc.controller.loadbalancer.policies.createtable; 00026 00027 import java.util.HashMap; 00028 import java.util.Iterator; 00029 00030 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 00031 import org.objectweb.cjdbc.common.xml.XmlComponent; 00032 00033 /** 00034 * Defines the policy to adopt when creating a new table. 00035 * 00036 * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a> 00037 * @version 1.0 00038 */ 00039 public class CreateTablePolicy implements XmlComponent 00040 { 00041 /** Pickup a backend name randomly in the backend list. */ 00042 public static final int RANDOM = 0; 00043 00044 /** Backends are chosen using a round-robin algorithm. */ 00045 public static final int ROUND_ROBIN = 1; 00046 00047 /** Table is created on all backends in the backend list. */ 00048 public static final int ALL = 2; 00049 00050 /** List of backends to wait for. */ 00051 private HashMap ruleList = new HashMap(); 00052 00053 /** 00054 * Adds a rule to this policy. <br>If the rule's table name is <code>null</code>, 00055 * the rule is considered as the default rule 00056 * 00057 * @param rule rule to add 00058 */ 00059 public void addRule(CreateTableRule rule) 00060 { 00061 ruleList.put(rule.getTableName(), rule); 00062 } 00063 00064 /** 00065 * Returns the rule Hashmap(table name,rule). 00066 * 00067 * @return Hashmap 00068 */ 00069 public HashMap getRuleList() 00070 { 00071 return ruleList; 00072 } 00073 00074 /** 00075 * Gets the rule corresponding to a table name. 00076 * 00077 * @param tableName table name of the rule 00078 * @return the rule or <code>null</code> if no specific rule has been 00079 * defined for this table 00080 */ 00081 public CreateTableRule getTableRule(String tableName) 00082 { 00083 return (CreateTableRule) ruleList.get(tableName); 00084 } 00085 00086 /** 00087 * Returns the default rule or <code>null</code> if no default rule has 00088 * been defined. 00089 * 00090 * @return a <code>CreateTableRule</code> 00091 */ 00092 public CreateTableRule getDefaultRule() 00093 { 00094 return (CreateTableRule) ruleList.get(null); 00095 } 00096 00097 /** 00098 * Returns the xml attribute value for the given policy 00099 * 00100 * @param policy the policy to convert 00101 * @return xml attribute value or "" if not found 00102 */ 00103 public static final String getXmlValue(int policy) 00104 { 00105 switch (policy) 00106 { 00107 case RANDOM : 00108 return DatabasesXmlTags.VAL_random; 00109 case ROUND_ROBIN : 00110 return DatabasesXmlTags.VAL_roundRobin; 00111 case ALL : 00112 return DatabasesXmlTags.VAL_all; 00113 default : 00114 return ""; 00115 } 00116 } 00117 00118 /** 00119 * Returns xml formatted string containing information on all rules of the 00120 * system 00121 * 00122 * @return xml formatted string. 00123 */ 00124 public String getXml() 00125 { 00126 StringBuffer info = new StringBuffer(); 00127 for (Iterator iterator = ruleList.keySet().iterator(); iterator.hasNext();) 00128 info.append(((CreateTableRule) ruleList.get(iterator.next())).getXml()); 00129 return info.toString(); 00130 } 00131 00132 }