1 23 24 package org.objectweb.cjdbc.driver.connectpolicy; 25 26 import java.sql.SQLException ; 27 import java.util.ArrayList ; 28 import java.util.StringTokenizer ; 29 30 import org.objectweb.cjdbc.common.exceptions.NoMoreControllerException; 31 import org.objectweb.cjdbc.driver.CjdbcUrl; 32 import org.objectweb.cjdbc.driver.ControllerInfo; 33 34 41 public class PreferredListConnectPolicy extends AbstractControllerConnectPolicy 42 { 43 private int index = -1; 44 private ArrayList preferredControllers; 45 private ArrayList deadPreferredControllers; 46 47 58 public PreferredListConnectPolicy(ControllerInfo[] controllerList, 59 long retryIntervalInMs, String preferredControllerList, int debugLevel) 60 { 61 super(controllerList, retryIntervalInMs, debugLevel); 62 63 StringTokenizer controllers = new StringTokenizer (preferredControllerList, 65 ",", true); 66 int tokenNumber = controllers.countTokens(); 67 preferredControllers = new ArrayList (tokenNumber - 1); 68 deadPreferredControllers = new ArrayList (tokenNumber - 1); 69 int i = 0; 70 String s; 71 boolean lastTokenWasComma = false; 72 while (controllers.hasMoreTokens()) 73 { 74 s = controllers.nextToken().trim(); 75 if (s.equals(",")) 76 { 77 if (lastTokenWasComma || (i == 0) || (i == tokenNumber - 1)) 78 throw new RuntimeException ( 81 "Syntax error in controller list for preferredController attribute '" 82 + preferredControllerList + "'"); 83 else 84 { 85 lastTokenWasComma = true; 86 continue; 87 } 88 } 89 lastTokenWasComma = false; 90 try 91 { 92 preferredControllers.add(CjdbcUrl.parseController(s)); 93 } 94 catch (SQLException e) 95 { 96 throw new RuntimeException ("Invalid controller " + s 97 + " in controller list for preferredController attribute"); 98 } 99 i++; 100 } 101 102 } 103 104 107 public synchronized void removeControllerFromSuspectList( 108 ControllerInfo controller) 109 { 110 super.removeControllerFromSuspectList(controller); 111 if (deadPreferredControllers.remove(controller)) 112 preferredControllers.add(controller); 113 } 114 115 118 public synchronized void suspectControllerOfFailure( 119 ControllerInfo controllerInfo) 120 { 121 super.suspectControllerOfFailure(controllerInfo); 122 if (preferredControllers.remove(controllerInfo)) 123 deadPreferredControllers.add(controllerInfo); 124 } 125 126 129 public synchronized ControllerInfo getController() 130 throws NoMoreControllerException 131 { 132 if (suspectedControllers.size() == controllerList.length) 133 throw new NoMoreControllerException(); 134 135 if (preferredControllers.isEmpty()) 136 { do 138 { 139 index = (index + 1) % controllerList.length; 140 } 141 while (suspectedControllers.contains(controllerList[index])); 142 if (debugLevel == CjdbcUrl.DEBUG_LEVEL_DEBUG) 143 System.out.println("Selected controller[" + index + "]:" 144 + controllerList[index]); 145 return controllerList[index]; 146 } 147 else 148 { 149 index = (index + 1) % preferredControllers.size(); 150 ControllerInfo controllerInfo = (ControllerInfo) preferredControllers 151 .get(index); 152 if (debugLevel == CjdbcUrl.DEBUG_LEVEL_DEBUG) 153 System.out.println("Selected controller[" + index + "]:" 154 + controllerInfo); 155 return controllerInfo; 156 } 157 } 158 159 } 160 | Popular Tags |