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