1 24 25 package org.objectweb.cjdbc.controller.loadbalancer.policies.errorchecking; 26 27 import java.util.ArrayList ; 28 29 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 30 31 38 public class ErrorCheckingRoundRobin extends ErrorCheckingPolicy 39 { 40 41 private int index = 0; 42 43 48 public ErrorCheckingRoundRobin(int numberOfNodes) 49 { 50 super(ErrorCheckingPolicy.ROUND_ROBIN, numberOfNodes); 51 } 52 53 56 public ArrayList getBackends(ArrayList backends) 57 throws ErrorCheckingException 58 { 59 int size = backends.size(); 60 61 if (nbOfNodes == 0) 62 return null; 63 else if (nbOfNodes == size) 64 return backends; 65 66 ArrayList result = new ArrayList (nbOfNodes); 67 ArrayList clonedList = new ArrayList (size); 68 for (int i = 0; i < size; i++) 69 { DatabaseBackend db = (DatabaseBackend) backends.get(i); 71 if (db.isReadEnabled() || db.isWriteEnabled()) 72 clonedList.add(db); 73 } 74 75 int clonedSize = clonedList.size(); 76 77 if (nbOfNodes == clonedSize) 78 return backends; 79 else if (nbOfNodes > clonedSize) 80 throw new ErrorCheckingException( 81 "Asking for more backends (" 82 + nbOfNodes 83 + ") than available (" 84 + clonedSize 85 + ")"); 86 87 synchronized (this) 88 { for (int i = 0; i < nbOfNodes; i++) 90 { 91 index = (index + 1) % clonedSize; 92 result.add(clonedList.remove(index)); 93 } 94 } 95 96 return result; 97 } 98 99 102 public String getInformation() 103 { 104 return "Error checking using " 105 + nbOfNodes 106 + " nodes choosen using a round-robin algorithm"; 107 } 108 } 109 | Popular Tags |