1 23 24 package org.objectweb.cjdbc.driver.connectpolicy; 25 26 import java.util.HashSet ; 27 28 import org.objectweb.cjdbc.common.exceptions.NoMoreControllerException; 29 import org.objectweb.cjdbc.driver.CjdbcUrl; 30 import org.objectweb.cjdbc.driver.ControllerInfo; 31 32 40 public abstract class AbstractControllerConnectPolicy 41 { 42 protected ControllerInfo[] controllerList; 43 protected HashSet suspectedControllers; 44 private long retryIntervalInMs; 45 private ControllerPingThread controllerPingThread = null; 46 protected int debugLevel = CjdbcUrl.DEBUG_LEVEL_OFF; 47 48 57 public AbstractControllerConnectPolicy(ControllerInfo[] controllerList, 58 long retryIntervalInMs, int debugLevel) 59 { 60 if (controllerList == null) 61 throw new NullPointerException ( 62 "Invalid null controller list in connect policy constructor"); 63 if (controllerList.length == 0) 64 throw new RuntimeException ( 65 "Invalid empty controller list in connect policy constructor"); 66 this.controllerList = controllerList; 67 this.suspectedControllers = new HashSet (controllerList.length); 68 this.retryIntervalInMs = retryIntervalInMs; 69 this.debugLevel = debugLevel; 70 } 71 72 78 protected void finalize() throws Throwable 79 { 80 super.finalize(); 81 suspectedControllers.clear(); 83 if (controllerPingThread != null) 84 synchronized (controllerPingThread) 85 { 86 controllerPingThread.notify(); 87 } 88 } 89 90 97 public abstract ControllerInfo getController() 98 throws NoMoreControllerException; 99 100 105 public ControllerInfo[] getControllerList() 106 { 107 return controllerList; 108 } 109 110 115 public HashSet getSuspectedControllers() 116 { 117 return suspectedControllers; 118 } 119 120 126 public boolean isSuspectedOfFailure(ControllerInfo controllerInfo) 127 { 128 return suspectedControllers.contains(controllerInfo); 129 } 130 131 136 public void setControllerList(ControllerInfo[] controllerList) 137 { 138 this.controllerList = controllerList; 139 } 140 141 146 public synchronized void suspectControllerOfFailure( 147 ControllerInfo controllerInfo) 148 { 149 for (int i = 0; i < controllerList.length; i++) 151 { 152 ControllerInfo controller = controllerList[i]; 153 if (controller.equals(controllerInfo)) 154 { 155 synchronized (suspectedControllers) 156 { 157 if (debugLevel >= CjdbcUrl.DEBUG_LEVEL_INFO) 158 System.out.println("Controller " + controllerInfo 159 + " is now suspected of failure"); 160 suspectedControllers.add(controllerInfo); 161 if ((controllerPingThread == null) 168 || (controllerPingThread.isTerminated())) 169 { 170 controllerPingThread = new ControllerPingThread(this, 171 retryIntervalInMs, debugLevel); 172 controllerPingThread.start(); 173 } 174 return; 175 } 176 } 177 } 178 } 179 180 185 public void removeControllerFromSuspectList(ControllerInfo controller) 186 { 187 synchronized (suspectedControllers) 188 { 189 if (debugLevel >= CjdbcUrl.DEBUG_LEVEL_INFO) 190 System.out.println("Controller " + controller 191 + " is removed from suspect list"); 192 suspectedControllers.remove(controller); 193 } 194 } 195 196 } 197 | Popular Tags |