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