1 23 24 package org.objectweb.cjdbc.driver.connectpolicy; 25 26 import java.util.ArrayList ; 27 import java.util.Random ; 28 29 import org.objectweb.cjdbc.common.exceptions.NoMoreControllerException; 30 import org.objectweb.cjdbc.driver.CjdbcUrl; 31 import org.objectweb.cjdbc.driver.ControllerInfo; 32 33 42 public class RandomConnectPolicy extends AbstractControllerConnectPolicy 43 { 44 private Random rand; 45 private ArrayList availableControllerList; 46 47 56 public RandomConnectPolicy(ControllerInfo[] controllerList, 57 long retryIntervalInMs, int debugLevel) 58 { 59 super(controllerList, retryIntervalInMs, debugLevel); 60 availableControllerList = new ArrayList (controllerList.length); 62 for (int i = 0; i < controllerList.length; i++) 63 { 64 if (controllerList[i] == null) 65 throw new RuntimeException ( 66 "Invalid null controller in list while instanciating RandomConnectPolicy"); 67 availableControllerList.add(controllerList[i]); 68 } 69 rand = new Random (System.currentTimeMillis()); 70 } 71 72 75 public void removeControllerFromSuspectList(ControllerInfo controller) 76 { 77 super.removeControllerFromSuspectList(controller); 78 availableControllerList.add(controller); 79 } 80 81 84 public synchronized void suspectControllerOfFailure( 85 ControllerInfo controllerInfo) 86 { 87 super.suspectControllerOfFailure(controllerInfo); 88 availableControllerList.remove(controllerInfo); 89 } 90 91 94 public synchronized ControllerInfo getController() 95 throws NoMoreControllerException 96 { 97 int size = availableControllerList.size(); 98 if (size == 0) 99 throw new NoMoreControllerException(); 100 101 ControllerInfo controllerInfo = (ControllerInfo) availableControllerList 102 .get(rand.nextInt(size)); 103 if (debugLevel == CjdbcUrl.DEBUG_LEVEL_DEBUG) 104 System.out.println("Selected controller " + controllerInfo); 105 return controllerInfo; 106 } 107 108 } 109 | Popular Tags |