1 24 25 package org.objectweb.clif.server.lib; 26 27 import org.objectweb.fractal.api.control.BindingController; 28 import org.objectweb.fractal.api.control.LifeCycleController; 29 import org.objectweb.util.monolog.Monolog; 30 import org.objectweb.util.monolog.api.Logger; 31 import org.objectweb.util.monolog.api.BasicLevel; 32 import org.objectweb.clif.supervisor.api.BladeState; 33 import org.objectweb.clif.storage.api.StorageProxyAdmin; 34 import org.objectweb.clif.storage.api.LifeCycleEvent; 35 import org.objectweb.clif.storage.api.AlarmEvent; 36 import org.objectweb.clif.supervisor.api.SupervisorInfo; 37 import org.objectweb.clif.server.api.BladeInsertResponse; 38 import org.objectweb.clif.server.api.BladeControl; 39 import org.objectweb.clif.datacollector.api.DataCollectorWrite; 40 import java.io.Serializable ; 41 42 49 public class BladeInsertAdapterImpl 50 implements 51 BladeControl, 52 BladeInsertResponse, 53 BindingController, 54 LifeCycleController, 55 Runnable 56 { 57 private static Logger log = Monolog.getDefaultMonologFactory().getLogger(BladeInsertAdapterImpl.class.toString()); 58 static final String [] interfaceNames = new String [] { 59 SupervisorInfo.SUPERVISOR_INFO, 60 BladeControl.BLADE_INSERT_CONTROL, 61 StorageProxyAdmin.STORAGEPROXY_ADMIN, 62 DataCollectorWrite.DATA_COLLECTOR_WRITE }; 63 final int OP_IDLE = 0; 64 final int OP_INIT = 1; 65 final int OP_START = 2; 66 final int OP_STOP = 3; 67 final int OP_SUSPEND = 4; 68 final int OP_RESUME = 5; 69 final int OP_TERM = 6; 70 71 volatile int current_op = -1; 72 Serializable current_testId = null; 73 volatile Thread op_thread = null; 74 75 private SupervisorInfo sis; 76 private BladeControl bladeCtl; 77 private StorageProxyAdmin spa; 78 private DataCollectorWrite dcw; 79 private BladeState bladeState; 80 private String argument; 81 private String bladeId; 82 83 84 public BladeInsertAdapterImpl() 85 { 86 } 87 88 89 93 94 97 public void run() 98 { 99 synchronized (Thread.currentThread()) 100 { 101 while (op_thread != null) 102 { 103 switch (current_op) 104 { 105 case OP_IDLE: 106 try 107 { 108 op_thread.wait(); 109 } 110 catch (InterruptedException ex) 111 { 112 ex.printStackTrace(System.err); 113 } 114 break; 115 case OP_INIT: 116 if (bladeState == BladeState.DEPLOYED 117 || bladeState == BladeState.COMPLETED 118 || bladeState == BladeState.STOPPED 119 || bladeState == BladeState.ABORTED) 120 { 121 do_init(); 122 } 123 current_op = OP_IDLE; 124 op_thread.notify(); 125 break; 126 case OP_START: 127 if (bladeState == BladeState.INITIALIZED) 128 { 129 do_start(); 130 } 131 current_op = OP_IDLE; 132 op_thread.notify(); 133 break; 134 case OP_STOP: 135 if (bladeState == BladeState.INITIALIZED 136 || bladeState == BladeState.SUSPENDED 137 || bladeState == BladeState.RUNNING) 138 { 139 do_stop(); 140 } 141 current_op = OP_IDLE; 142 op_thread.notify(); 143 break; 144 case OP_SUSPEND: 145 if (bladeState == BladeState.RUNNING) 146 { 147 do_suspend(); 148 } 149 current_op = OP_IDLE; 150 op_thread.notify(); 151 break; 152 case OP_RESUME: 153 if (bladeState == BladeState.SUSPENDED) 154 { 155 do_resume(); 156 } 157 current_op = OP_IDLE; 158 op_thread.notify(); 159 break; 160 default: 161 throw new Error ("unexpected operation id: " + current_op); 162 } 163 } 164 current_op = OP_TERM; 165 Thread.currentThread().notify(); 166 } 167 } 168 169 170 174 175 public String getFcState() 176 { 177 return op_thread != null ? LifeCycleController.STARTED : LifeCycleController.STOPPED; 178 } 179 180 181 public synchronized void startFc() 182 { 183 current_op = OP_IDLE; 184 bladeState = BladeState.DEPLOYED; 185 op_thread = new Thread (this, "Blade adapter control"); 186 op_thread.start(); 187 } 188 189 190 public synchronized void stopFc() 191 { 192 if (op_thread != null) 193 { 194 Object lock = op_thread; 195 synchronized (lock) 196 { 197 op_thread = null; 198 lock.notify(); 199 if (current_op != OP_TERM) 200 { 201 try 202 { 203 lock.wait(); 204 } 205 catch (InterruptedException ex) 206 { 207 ex.printStackTrace(System.err); 208 } 209 } 210 } 211 } 212 } 213 214 215 219 220 public Object lookupFc(String clientItfName) 221 { 222 if (clientItfName.equals(SupervisorInfo.SUPERVISOR_INFO)) 223 { 224 return sis; 225 } 226 else if (clientItfName.equals(BladeControl.BLADE_INSERT_CONTROL)) 227 { 228 return bladeCtl; 229 } 230 else if (clientItfName.startsWith(StorageProxyAdmin.STORAGEPROXY_ADMIN)) 231 { 232 return spa; 233 } 234 else if (clientItfName.equals(DataCollectorWrite.DATA_COLLECTOR_WRITE)) 235 { 236 return dcw; 237 } 238 else 239 { 240 return null; 241 } 242 } 243 244 245 public void bindFc(String clientItfName, Object serverItf) 246 { 247 if (clientItfName.equals(SupervisorInfo.SUPERVISOR_INFO)) 248 { 249 sis = (SupervisorInfo) serverItf; 250 } 251 else if (clientItfName.equals(BladeControl.BLADE_INSERT_CONTROL)) 252 { 253 bladeCtl = (BladeControl) serverItf; 254 } 255 else if (clientItfName.startsWith(StorageProxyAdmin.STORAGEPROXY_ADMIN)) 256 { 257 spa = (StorageProxyAdmin) serverItf; 258 } 259 else if (clientItfName.equals(DataCollectorWrite.DATA_COLLECTOR_WRITE)) 260 { 261 dcw = (DataCollectorWrite) serverItf; 262 } 263 } 264 265 266 public void unbindFc(String clientItfName) 267 { 268 if (clientItfName.equals(SupervisorInfo.SUPERVISOR_INFO)) 269 { 270 sis = null; 271 } 272 else if (clientItfName.equals(BladeControl.BLADE_INSERT_CONTROL)) 273 { 274 bladeCtl = null; 275 } 276 else if (clientItfName.startsWith(StorageProxyAdmin.STORAGEPROXY_ADMIN)) 277 { 278 spa = null; 279 } 280 else if (clientItfName.equals(DataCollectorWrite.DATA_COLLECTOR_WRITE)) 281 { 282 dcw = null; 283 } 284 } 285 286 287 public String [] listFc() 288 { 289 return interfaceNames; 290 } 291 292 293 297 298 301 public void setArgument(String argument) 302 { 303 this.argument = argument; 304 } 305 306 307 310 public void setId(String id) 311 { 312 bladeId = id; 313 spa.init(id); 314 } 315 316 317 320 public String getId() 321 { 322 return bladeId; 323 } 324 325 326 330 public synchronized void init(Serializable testId) 331 { 332 if (op_thread != null) 333 { 334 synchronized(op_thread) 335 { 336 while (current_op != OP_IDLE) 337 { 338 try 339 { 340 op_thread.wait(); 341 } 342 catch (InterruptedException ex) 343 { 344 ex.printStackTrace(System.err); 345 } 346 } 347 current_op = OP_INIT; 348 current_testId = testId; 349 op_thread.notify(); 350 } 351 } 352 } 353 354 355 public void do_init() 356 { 357 log.log(BasicLevel.DEBUG, "Init blade"); 358 sis.setBladeState(bladeId, BladeState.INITIALIZING); 359 try 360 { 361 spa.newTest(current_testId); 362 dcw.init(current_testId, bladeId); 363 bladeCtl.setArgument(argument); 364 bladeCtl.setId(bladeId); 365 dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.INITIALIZING)); 366 bladeCtl.init(current_testId); 367 bladeState = BladeState.INITIALIZED; 368 dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.INITIALIZED)); 369 } 370 catch (Throwable ex) 371 { 372 sis.setBladeState(bladeId, BladeState.DEPLOYED); 373 sis.alarm(new AlarmEvent( 374 System.currentTimeMillis(), 375 bladeId, 376 AlarmEvent.ERROR, 377 ex)); 378 379 } 380 System.gc(); 381 log.log(BasicLevel.DEBUG, "Change blade state for: " + bladeState); 382 sis.setBladeState(bladeId, bladeState); 383 } 384 385 386 389 public synchronized void start() 390 { 391 if (op_thread != null) 392 { 393 synchronized(op_thread) 394 { 395 while (current_op != OP_IDLE) 396 { 397 try 398 { 399 op_thread.wait(); 400 } 401 catch (InterruptedException ex) 402 { 403 ex.printStackTrace(System.err); 404 } 405 } 406 current_op = OP_START; 407 op_thread.notify(); 408 } 409 } 410 } 411 412 413 public void do_start() 414 { 415 log.log(BasicLevel.DEBUG, "Change injector state for: " + BladeState.STARTING); 416 sis.setBladeState(bladeId, BladeState.STARTING); 417 try 418 { 419 dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.STARTING)); 420 bladeCtl.start(); 421 bladeState = BladeState.RUNNING; 422 dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.RUNNING)); 423 } 424 catch (Throwable e) 425 { 426 e.printStackTrace(System.err); 427 } 428 log.log(BasicLevel.DEBUG, "Change injector state for: " + bladeState); 429 sis.setBladeState(bladeId, bladeState); 430 } 431 432 433 436 public synchronized void join() 437 { 438 bladeCtl.join(); 440 } 441 442 443 446 public synchronized void stop() 447 { 448 if (op_thread != null) 449 { 450 synchronized(op_thread) 451 { 452 while (current_op != OP_IDLE) 453 { 454 try 455 { 456 op_thread.wait(); 457 } 458 catch (InterruptedException ex) 459 { 460 ex.printStackTrace(System.err); 461 } 462 } 463 current_op = OP_STOP; 464 op_thread.notify(); 465 } 466 } 467 } 468 469 470 protected void do_stop() 471 { 472 log.log(BasicLevel.DEBUG, "Change injector state for: " + BladeState.STOPPING); 473 sis.setBladeState(bladeId, BladeState.STOPPING); 474 try 475 { 476 dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.STOPPING)); 477 bladeCtl.stop(); 478 bladeState = BladeState.STOPPED; 479 dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.STOPPED)); 480 dcw.terminate(); 481 spa.closeTest(); 482 } 483 catch (Throwable e) 484 { 485 e.printStackTrace(System.err); 486 } 487 log.log(BasicLevel.DEBUG, "Change injector state for: " + bladeState); 488 sis.setBladeState(bladeId, bladeState); 489 } 490 491 492 495 public synchronized void suspend() 496 { 497 if (op_thread != null) 498 { 499 synchronized(op_thread) 500 { 501 while (current_op != OP_IDLE) 502 { 503 try 504 { 505 op_thread.wait(); 506 } 507 catch (InterruptedException ex) 508 { 509 ex.printStackTrace(System.err); 510 } 511 } 512 current_op = OP_SUSPEND; 513 op_thread.notify(); 514 } 515 } 516 } 517 518 519 protected void do_suspend() 520 { 521 log.log(BasicLevel.DEBUG, "Change blade " + bladeId + " state to " + BladeState.SUSPENDING); 522 sis.setBladeState(bladeId, BladeState.SUSPENDING); 523 try 524 { 525 dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.SUSPENDING)); 526 bladeCtl.suspend(); 527 bladeState = BladeState.SUSPENDED; 528 dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.SUSPENDED)); 529 } 530 catch (Throwable e) 531 { 532 e.printStackTrace(System.err); 533 } 534 log.log(BasicLevel.DEBUG, "Change injector state for: " + bladeState); 535 sis.setBladeState(bladeId, bladeState); 536 } 537 538 539 542 public synchronized void resume() 543 { 544 if (op_thread != null) 545 { 546 synchronized(op_thread) 547 { 548 while (current_op != OP_IDLE) 549 { 550 try 551 { 552 op_thread.wait(); 553 } 554 catch (InterruptedException ex) 555 { 556 ex.printStackTrace(System.err); 557 } 558 } 559 current_op = OP_RESUME; 560 op_thread.notify(); 561 } 562 } 563 } 564 565 566 protected void do_resume() 567 { 568 log.log(BasicLevel.DEBUG, "Change injector state for: " + BladeState.RESUMING); 569 sis.setBladeState(bladeId, BladeState.RESUMING); 570 try 571 { 572 dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.RESUMING)); 573 bladeCtl.resume(); 574 bladeState = BladeState.RUNNING; 575 dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.RUNNING)); 576 } 577 catch (Throwable e) 578 { 579 e.printStackTrace(System.err); 580 } 581 log.log(BasicLevel.DEBUG, "Change injector state for: " + bladeState); 582 sis.setBladeState(bladeId, bladeState); 583 } 584 585 586 590 591 public void aborted() 592 { 593 bladeState = BladeState.ABORTED; 594 log.log(BasicLevel.DEBUG, "Change blade state for: " + bladeState); 595 dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.ABORTED)); 596 dcw.terminate(); 597 spa.closeTest(); 598 sis.setBladeState(bladeId, bladeState); 599 } 600 601 602 public void completed() 603 { 604 bladeState = BladeState.COMPLETED; 605 log.log(BasicLevel.DEBUG, "Change blade state for: " + bladeState); 606 dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.COMPLETED)); 607 dcw.terminate(); 608 spa.closeTest(); 609 sis.setBladeState(bladeId, bladeState); 610 } 611 } 612 | Popular Tags |