1 23 24 28 29 package com.sun.jts.utils.RecoveryHooks; 30 31 import java.text.MessageFormat ; 32 import java.util.Hashtable ; 33 import java.util.ResourceBundle ; 34 import com.sun.jts.jtsxa.Utility; 35 import com.sun.jts.otsidl.JCoordinator; 36 import com.sun.jts.otsidl.JCoordinatorHelper; 37 import com.sun.jts.CosTransactions.GlobalTID; 38 import org.omg.CosTransactions.Coordinator; 39 40 import java.util.logging.Logger ; 41 import java.util.logging.Level ; 42 import com.sun.logging.LogDomains; 43 import com.sun.jts.utils.LogFormatter; 44 79 public class FailureInducer { 80 81 public static final Integer ACTIVE = new Integer (0); 83 public static final Integer PREPARING = new Integer (1); 84 public static final Integer PREPARED = new Integer (2); 85 public static final Integer COMPLETING = new Integer (3); 86 public static final Integer COMPLETED = new Integer (4); 87 88 90 private static boolean failureInducerIsActive = false; 91 private static boolean crash = false; 92 private static int waitPeriod = 0; 93 private static int waitCount = 0; 94 private static Hashtable crashList = new Hashtable (); 95 private static Hashtable waitList = new Hashtable (); 96 private static Hashtable waitTime = new Hashtable (); 97 private static ResourceBundle messages = ResourceBundle. 98 getBundle("com.sun.jts.utils.RecoveryHooks.Messages"); 99 102 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 103 105 static { 106 107 108 } 109 110 112 116 public static void activateFailureInducer() { 117 failureInducerIsActive = true; 118 } 119 120 125 public static void deactivateFailureInducer() { 126 failureInducerIsActive = false; 127 } 128 129 132 public static boolean isFailureInducerActive() { 133 return failureInducerIsActive; 134 } 135 136 143 public static void setCrashPoint(Integer crashPoint) { 144 if (crashPoint == null) { 146 _logger.log(Level.SEVERE,"jts.invalid_crash_point"); 147 return; 148 } 149 150 Coordinator coord = Utility.getCoordinator(Utility.getControl()); 151 JCoordinator jcoord = JCoordinatorHelper.narrow(coord); 152 if (jcoord != null) { 153 GlobalTID gtid = new GlobalTID(jcoord.getGlobalTID()); 154 crashList.put(gtid, crashPoint); 155 } 156 } 157 158 166 public static void setWaitPoint(Integer waitPoint, int waitDuration) { 167 if (waitPoint == null) { 169 _logger.log(Level.SEVERE,"jts.invalid_wait_point"); 170 return; 171 } 172 173 Coordinator coord = Utility.getCoordinator(Utility.getControl()); 174 JCoordinator jcoord = JCoordinatorHelper.narrow(coord); 175 if (jcoord != null) { 176 GlobalTID gtid = new GlobalTID(jcoord.getGlobalTID()); 177 waitList.put(gtid, waitPoint); 178 waitTime.put(gtid, new Integer (waitDuration)); 179 } 180 } 181 182 185 public static void crash() { 186 crash = true; 187 } 188 189 192 private static void incrementWaitCount() { 193 waitCount++; 194 } 195 196 199 public static int getWaitCount() { 200 return waitCount; 201 } 202 203 214 public static void waitForFailure(GlobalTID gtid, Integer failPoint) { 215 216 if (gtid == null) 218 return; 219 220 Integer crashPoint = (Integer ) crashList.get(gtid); 221 Integer waitPoint = (Integer ) waitList.get(gtid); 222 223 if (crashPoint == null && waitPoint == null) { 225 return; 226 } 227 228 _logger.log(Level.WARNING,"jts.failpoint",failPoint); 229 if (crashPoint != null && crashPoint.equals(failPoint)) { 231 incrementWaitCount(); 232 while (crash == false) { 233 try { 234 Thread.sleep(3000); 235 } catch (Exception e) {} 236 } 237 System.exit(0); 238 } 239 240 if (waitPoint != null && waitPoint.equals(failPoint)) { 242 Integer waitDuration = (Integer ) waitTime.get(gtid); 243 244 if (waitDuration == null || waitDuration.intValue() < 0) 246 return; 247 248 try { 250 Thread.sleep(waitDuration.intValue() * 1000); 251 } catch (Exception e) {} 252 } 253 } 254 255 257 private static String getMessage(String key) { 258 return getMessage(key, null); 259 } 260 261 private static String getMessage(String key, Object [] inserts) { 262 if (messages == null || key == null) { 263 return null; 264 } 265 266 String msg = messages.getString(key); 267 if (msg == null) { 268 return null; 269 } 270 271 if (inserts == null) { 272 return msg; 273 } else { 274 return MessageFormat.format(msg, inserts); 275 } 276 } 277 } 278 | Popular Tags |