00001
00025
package org.objectweb.cjdbc.controller.loadbalancer.policies.errorchecking;
00026
00027
import java.util.ArrayList;
00028
00029
import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
00030
00038 public class ErrorCheckingRoundRobin extends ErrorCheckingPolicy
00039 {
00041 private int index = 0;
00042
00048 public ErrorCheckingRoundRobin(
int numberOfNodes)
00049 {
00050 super(
ErrorCheckingPolicy.ROUND_ROBIN, numberOfNodes);
00051 }
00052
00056 public ArrayList
getBackends(ArrayList backends)
00057
throws ErrorCheckingException
00058 {
00059
int size = backends.size();
00060
00061
if (nbOfNodes == 0)
00062
return null;
00063
else if (nbOfNodes == size)
00064
return backends;
00065
00066 ArrayList result =
new ArrayList(nbOfNodes);
00067 ArrayList clonedList =
new ArrayList(size);
00068
for (
int i = 0; i < size; i++)
00069 {
00070
DatabaseBackend db = (
DatabaseBackend) backends.get(i);
00071
if (db.
isReadEnabled() || db.
isWriteEnabled())
00072 clonedList.add(db);
00073 }
00074
00075
int clonedSize = clonedList.size();
00076
00077
if (nbOfNodes == clonedSize)
00078
return backends;
00079
else if (nbOfNodes > clonedSize)
00080
throw new ErrorCheckingException(
00081
"Asking for more backends ("
00082 + nbOfNodes
00083 +
") than available ("
00084 + clonedSize
00085 +
")");
00086
00087
synchronized (
this)
00088 {
00089
for (
int i = 0; i < nbOfNodes; i++)
00090 {
00091
index = (
index + 1) % clonedSize;
00092 result.add(clonedList.remove(
index));
00093 }
00094 }
00095
00096
return result;
00097 }
00098
00102 public String
getInformation()
00103 {
00104
return "Error checking using "
00105 + nbOfNodes
00106 +
" nodes choosen using a round-robin algorithm";
00107 }
00108 }