KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > loadbalancer > policies > errorchecking > ErrorCheckingPolicy


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.errorchecking;
26
27 import java.util.ArrayList JavaDoc;
28
29 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
30
31 /**
32  * Defines the policy to adopt for error checking.
33  *
34  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
35  * @version 1.0
36  */

37 public abstract class ErrorCheckingPolicy
38 {
39   /** Pickup backends randomly. */
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   /** Request is sent to all backends. */
46   public static final int ALL = 2;
47
48   /** Number of nodes that are involved in error-checking per request. */
49   protected int nbOfNodes = 0;
50
51   protected int policy;
52
53   /**
54    * Creates a new <code>CreateTableRule</code>.
55    *
56    * @param policy implemented policy
57    * @param numberOfNodes number of nodes to use to check for errors on a query
58    */

59   public ErrorCheckingPolicy(int policy, int numberOfNodes)
60   {
61     setPolicy(policy);
62     setNumberOfNodes(numberOfNodes);
63   }
64
65   /**
66    * Returns the number of nodes.
67    *
68    * @return an <code>int</code> value
69    * @see #setNumberOfNodes
70    */

71   public int getNumberOfNodes()
72   {
73     return nbOfNodes;
74   }
75
76   /**
77    * Sets the number of nodes.
78    *
79    * @param numberOfNodes the number of nodes to set
80    * @see #getNumberOfNodes
81    */

82   public void setNumberOfNodes(int numberOfNodes)
83   {
84     if (numberOfNodes < 3)
85       throw new IllegalArgumentException JavaDoc(
86           "You must use at least 3 nodes for error checking (" + numberOfNodes
87               + " is not acceptable)");
88     this.nbOfNodes = numberOfNodes;
89   }
90
91   /**
92    * Returns the policy.
93    *
94    * @return an <code>int</code> value
95    * @see #setPolicy
96    */

97   public int getPolicy()
98   {
99     return policy;
100   }
101
102   /**
103    * Sets the policy.
104    *
105    * @param policy the policy to set
106    * @see #getPolicy
107    */

108   public void setPolicy(int policy)
109   {
110     this.policy = policy;
111   }
112
113   /**
114    * Pickups backends from the given backends arraylist according to the current
115    * rule policy.
116    *
117    * @param backends backends to choose from
118    * @return Arraylist of choosen <code>DatabaseBackend</code>
119    * @exception ErrorCheckingException if the rule cannot be applied
120    */

121   public abstract ArrayList JavaDoc getBackends(ArrayList JavaDoc backends)
122       throws ErrorCheckingException;
123
124   /**
125    * Gives information about the current policy.
126    *
127    * @return a <code>String</code> value
128    */

129   public abstract String JavaDoc getInformation();
130
131   /**
132    * Convert this error checking policy to xml
133    *
134    * @return xml formatted string
135    */

136   public String JavaDoc getXml()
137
138   {
139     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
140     info.append("<" + DatabasesXmlTags.ELT_ErrorChecking + " />"
141         + DatabasesXmlTags.ATT_numberOfNodes + "=\"" + this.getNumberOfNodes()
142         + "\" " + DatabasesXmlTags.ATT_policy + "=\"");
143     switch (policy)
144     {
145       case RANDOM :
146         info.append(DatabasesXmlTags.VAL_random);
147         break;
148       case ROUND_ROBIN :
149         info.append(DatabasesXmlTags.VAL_roundRobin);
150         break;
151       case ALL :
152         info.append(DatabasesXmlTags.VAL_all);
153         break;
154       default :
155         break;
156     }
157     info.append("\"/>");
158     return info.toString();
159   }
160 }
161
Popular Tags