1 24 25 package org.objectweb.cjdbc.scenario.tools.components; 26 27 import java.io.IOException ; 28 import java.net.InetAddress ; 29 import java.net.Socket ; 30 import java.util.Hashtable ; 31 import java.util.Iterator ; 32 33 39 public abstract class ComponentManager implements ComponentManagerInterface 40 { 41 protected Hashtable processes = new Hashtable (); 42 43 44 52 public ComponentInterface instanciateProcess(String port) throws Exception 53 { 54 return instanciateProcess(port,getDefaultConfigurationFile()); 55 } 56 57 65 public abstract ComponentInterface instanciateProcess(String port,String configurationFile) throws Exception ; 66 67 73 public abstract String getDefaultConfigurationFile(); 74 75 76 77 85 public ComponentInterface startComponent(String port, String database) throws Exception 86 { 87 return start(port, database); 88 } 89 90 97 public ComponentInterface startComponent(String port) throws Exception 98 { 99 return start(port); 100 } 101 102 108 public void waitForStarted(String port) throws Exception 109 { 110 int retry = 10; 111 while (!isStarted(port)) 112 { 113 retry--; 114 if (retry == 0) 115 throw new IOException ("I think the component is not started"); 116 synchronized (this) 117 { 118 this.wait(1000); 119 } 120 } 121 } 122 123 129 public void waitForStopped(String port) throws Exception 130 { 131 int retry = 5; 132 while (isStarted(port)) 133 { 134 retry--; 135 if (retry == 0) 136 throw new IOException ("I think the component is still started"); 137 synchronized (this) 138 { 139 this.wait(2000); 140 } 141 } 142 } 143 144 150 public boolean isStarted(String port) 151 { 152 try 153 { 154 Socket socket = new Socket (InetAddress.getLocalHost().getHostAddress(), 155 Integer.parseInt(port)); 156 socket.close(); 157 return true; 158 } 159 catch (Exception e) 160 { 161 return false; 162 } 163 } 164 165 171 public void loaddatabase(String port) throws Exception 172 { 173 ((ComponentInterface) processes.get(port)).loadDatabase(); 174 } 175 176 183 public void loaddatabase(String port, String templateName) throws Exception 184 { 185 ((ComponentInterface) processes.get(port)).loadDatabase(templateName); 186 } 187 188 196 public void loaddatabase(String port, String templateName,String target) throws Exception 197 { 198 ((ComponentInterface) processes.get(port)).loadDatabase(templateName,target); 199 } 200 201 206 public void stop(ComponentInterface process) 207 { 208 if (process != null) 210 { 211 processes.remove(process.getPort()); 212 process.release(); 213 } 214 try 215 { 216 waitForStopped(process.getPort()); 217 } 218 catch (Exception e) 219 { 220 e.printStackTrace(); 221 } 222 } 223 224 229 public void stop(String componentOnPort) 230 { 231 stop((ComponentInterface)processes.get(componentOnPort)); 232 } 233 234 239 public void stop(int port) 240 { 241 stop(""+port); 242 } 243 244 247 public void stopAll() 248 { 249 this.release(); 250 } 251 252 260 public ComponentInterface start(String port, String database) throws Exception 261 { 262 ComponentInterface hs = instanciateProcess(port, database); 263 waitForStarted(port); 264 processes.put(port, hs); 265 return hs; 266 } 267 268 276 public ComponentInterface start(String port) throws Exception 277 { 278 return start(port,getDefaultConfigurationFile()); 279 } 280 281 282 285 public void release() 286 { 287 Iterator iter = processes.keySet().iterator(); 288 ComponentInterface component = null; 289 while (iter.hasNext()) 290 { 291 try 292 { 293 component = ((ComponentInterface) processes.get(iter.next())); 294 component.release(); 295 } 296 catch(Exception e) 297 { 298 e.printStackTrace(); 299 } 300 } 301 } 302 303 306 public void simulateFailure(final int port, final long wait, boolean rand) 307 { 308 simulateFailure(""+port,wait,rand); 309 } 310 311 314 public void simulateFailure(final String port, final long wait, final boolean rand) 315 { 316 Thread t = new Thread () 317 { 318 public void run() 319 { 320 synchronized(this) 321 { 322 try 323 { 324 wait(wait); 325 } 326 catch (InterruptedException e) 327 { 328 e.printStackTrace(); 329 } 330 ((ComponentInterface)processes.get(port)).release(); 331 } 332 } 333 }; 334 t.start(); 335 } 336 } | Popular Tags |