1 8 package org.apache.avalon.excalibur.util; 9 10 import org.apache.avalon.framework.activity.Disposable; 11 import org.apache.avalon.framework.activity.Initializable; 12 import org.apache.avalon.framework.activity.Startable; 13 import org.apache.avalon.framework.activity.Suspendable; 14 import org.apache.avalon.framework.component.Composable; 15 import org.apache.avalon.framework.configuration.Configurable; 16 import org.apache.avalon.framework.context.Contextualizable; 17 import org.apache.avalon.framework.logger.LogEnabled; 18 import org.apache.avalon.framework.logger.Loggable; 19 import org.apache.avalon.framework.parameters.Parameterizable; 20 21 29 public final class ComponentStateValidator 30 { 31 private static final String OOO_FAIL = "Initialization out of order"; 32 private static final String LOG_FAIL = OOO_FAIL + ": LogEnabled"; 33 private static final String CONTEXT_FAIL = OOO_FAIL + ": Contextualizable"; 34 private static final String PARAMETER_FAIL = OOO_FAIL + ": Parameterizable"; 35 private static final String CONFIGURE_FAIL = OOO_FAIL + ": Configurable"; 36 private static final String COMPOSE_FAIL = OOO_FAIL + ": Composable"; 37 private static final String INIT_FAIL = OOO_FAIL + ": Initializable"; 38 private static final String START_FAIL = OOO_FAIL + ": Startable.start()"; 39 private static final String STOP_FAIL = OOO_FAIL + ": Startable.stop()"; 40 private static final String SUSPEND_FAIL = OOO_FAIL + ": Suspendable.suspend()"; 41 private static final String RESUME_FAIL = OOO_FAIL + ": Suspendable.resume()"; 42 private static final String DISPOSE_FAIL = OOO_FAIL + ": Disposable"; 43 private static final String ACTIVE_FAIL = "Component is not Active"; 44 private static final String WRITE_FAIL = "Value is already bound"; 45 46 private static final long LOG_ENABLED = 0x00000001; 47 private static final long CONTEXTUALIZED = 0x00000002; 48 private static final long COMPOSED = 0x00000004; 49 private static final long CONFIGURED = 0x00000008; 50 private static final long PARAMETERIZED = 0x00000010; 51 private static final long INITIALIZED = 0x00000020; 52 private static final long STARTED = 0x00000040; 53 private static final long ACTIVE = 0x10000000; 54 private static final long SUSPENDED = 0x01000000; 55 private static final long STOPPED = 0x00000080; 56 private static final long DISPOSED = 0x00000100; 57 58 private static final long LOG_MASK = LOG_ENABLED; 59 private static final long CONTEXT_MASK = LOG_MASK | CONTEXTUALIZED; 60 private static final long COMPOSE_MASK = CONTEXT_MASK | COMPOSED; 61 private static final long CONFIGURE_MASK = COMPOSE_MASK | CONFIGURED; 62 private static final long PARAMETER_MASK = CONFIGURE_MASK | PARAMETERIZED; 63 private static final long INIT_MASK = PARAMETER_MASK | INITIALIZED; 64 private static final long START_MASK = INIT_MASK | STARTED; 65 private static final long STOP_MASK = START_MASK | STOPPED; 66 private static final long DISPOSE_MASK = STOP_MASK | DISPOSED; 67 68 private final long m_mask; 69 private long m_state; 70 71 75 public ComponentStateValidator( final Object object ) 76 { 77 int mask = 0; 78 79 if ( object instanceof LogEnabled || 80 object instanceof Loggable ) 81 { 82 mask |= LOG_ENABLED; 83 } 84 85 if ( object instanceof Contextualizable ) 86 { 87 mask |= CONTEXTUALIZED; 88 } 89 90 if ( object instanceof Parameterizable ) 91 { 92 mask |= PARAMETERIZED; 93 } 94 95 if ( object instanceof Configurable ) 96 { 97 mask |= CONFIGURED; 98 } 99 100 if ( object instanceof Composable ) 101 { 102 mask |= COMPOSED; 103 } 104 105 if ( object instanceof Initializable ) 106 { 107 mask |= INITIALIZED; 108 } 109 110 if ( object instanceof Disposable ) 111 { 112 mask |= DISPOSED; 113 } 114 115 if ( object instanceof Startable ) 116 { 117 mask |= STARTED | STOPPED; 118 } 119 120 if ( object instanceof Suspendable ) 121 { 122 mask |= SUSPENDED; 123 } 124 125 m_mask = mask & ~ACTIVE; 126 } 127 128 136 public void checkLogEnabled() 137 { 138 checkLogEnabled( LOG_FAIL ); 139 } 140 141 150 public void checkLogEnabled( final String message ) 151 { 152 boolean isValid = false; 153 isValid = (m_state & m_mask & LOG_ENABLED) == 0; 154 isValid = isValid && ((m_mask & LOG_MASK) | LOG_ENABLED) == 155 ((m_state & DISPOSE_MASK) | LOG_ENABLED); 156 157 if ( ! isValid ) 158 { 159 throw new IllegalStateException ( message ); 160 } 161 162 m_state |= LOG_ENABLED; 163 if ( (m_state & START_MASK) == (m_mask & START_MASK) ) 164 { 165 m_state |= ACTIVE; 166 } 167 } 168 169 176 public void checkContextualized() 177 { 178 checkContextualized(CONTEXT_FAIL); 179 } 180 181 189 public void checkContextualized( final String message ) 190 { 191 boolean isValid = false; 192 isValid = (m_state & m_mask & CONTEXTUALIZED) == 0; 193 isValid = isValid && ((m_mask & CONTEXT_MASK) | CONTEXTUALIZED) == 194 ((m_state & DISPOSE_MASK) | CONTEXTUALIZED); 195 196 if ( ! isValid ) 197 { 198 throw new IllegalStateException ( message ); 199 } 200 201 m_state |= CONTEXTUALIZED; 202 if ( (m_state & START_MASK) == (m_mask & START_MASK) ) 203 { 204 m_state |= ACTIVE; 205 } 206 } 207 208 215 public void checkParameterized() 216 { 217 checkParameterized( PARAMETER_FAIL ); 218 } 219 220 228 public void checkParameterized( final String message ) 229 { 230 boolean isValid = false; 231 isValid = (m_state & m_mask & PARAMETERIZED) == 0; 232 isValid = isValid && ((m_mask & PARAMETER_MASK) | PARAMETERIZED) == 233 ((m_state & DISPOSE_MASK) | PARAMETERIZED); 234 235 if ( ! isValid ) 236 { 237 throw new IllegalStateException ( message ); 238 } 239 240 m_state |= PARAMETERIZED; 241 if ( (m_state & START_MASK) == (m_mask & START_MASK) ) 242 { 243 m_state |= ACTIVE; 244 } 245 } 246 247 254 public void checkConfigured() 255 { 256 checkConfigured( CONFIGURE_FAIL ); 257 } 258 259 267 public void checkConfigured( final String message ) 268 { 269 boolean isValid = false; 270 isValid = (m_state & m_mask & CONFIGURED) == 0; 271 isValid = isValid && ((m_mask & CONFIGURE_MASK) | CONFIGURED) == 272 ((m_state & DISPOSE_MASK) | CONFIGURED); 273 274 if ( ! isValid ) 275 { 276 throw new IllegalStateException ( message ); 277 } 278 279 m_state |= CONFIGURED; 280 if ( (m_state & START_MASK) == (m_mask & START_MASK) ) 281 { 282 m_state |= ACTIVE; 283 } 284 } 285 286 293 public void checkComposed() 294 { 295 checkComposed( COMPOSE_FAIL ); 296 } 297 298 306 public void checkComposed( final String message ) 307 { 308 boolean isValid = false; 309 isValid = (m_state & m_mask & COMPOSED) == 0; 310 isValid = isValid && ((m_mask & COMPOSE_MASK) | COMPOSED) == 311 ((m_state & DISPOSE_MASK) | COMPOSED); 312 313 if ( ! isValid ) 314 { 315 throw new IllegalStateException ( message ); 316 } 317 318 m_state |= COMPOSED; 319 if ( (m_state & START_MASK) == (m_mask & START_MASK) ) 320 { 321 m_state |= ACTIVE; 322 } 323 } 324 325 332 public void checkInitialized() 333 { 334 checkInitialized( INIT_FAIL ); 335 } 336 337 345 public void checkInitialized( final String message ) 346 { 347 boolean isValid = false; 348 isValid = (m_state & m_mask & INITIALIZED) == 0; 349 isValid = isValid && ((m_mask & INIT_MASK) | INITIALIZED) == 350 ((m_state & DISPOSE_MASK) | INITIALIZED); 351 352 if ( ! isValid ) 353 { 354 throw new IllegalStateException ( message ); 355 } 356 357 m_state |= INITIALIZED; 358 if ( (m_state & START_MASK) == (m_mask & START_MASK) ) 359 { 360 m_state |= ACTIVE; 361 } 362 } 363 364 371 public void checkStarted() 372 { 373 checkStarted( START_FAIL ); 374 } 375 376 384 public void checkStarted( final String message ) 385 { 386 boolean isValid = false; 387 isValid = (m_state & m_mask & STARTED) == 0; 388 isValid = isValid && ((m_mask & START_MASK) | STARTED) == 389 ((m_state & DISPOSE_MASK) | STARTED); 390 391 if ( ! isValid ) 392 { 393 throw new IllegalStateException ( message ); 394 } 395 396 m_state |= STARTED; 397 if ( (m_state & START_MASK) == (m_mask & START_MASK) ) 398 { 399 m_state |= ACTIVE; 400 } 401 } 402 403 410 public void checkSuspended() 411 { 412 checkSuspended( SUSPEND_FAIL ); 413 } 414 415 423 public void checkSuspended( final String message ) 424 { 425 checkActive( message ); 426 boolean isValid = false; 427 isValid = (m_state & m_mask & SUSPENDED) == 0; 428 isValid = isValid && ((m_mask & START_MASK) & ~SUSPENDED) == 429 ((m_state & DISPOSE_MASK) & ~SUSPENDED); 430 431 if ( ! isValid ) 432 { 433 throw new IllegalStateException ( message ); 434 } 435 436 m_state |= SUSPENDED; 437 } 438 439 446 public void checkResumed() 447 { 448 checkResumed( RESUME_FAIL ); 449 } 450 451 459 public void checkResumed( final String message ) 460 { 461 checkActive( message ); 462 boolean isValid = false; 463 isValid = (m_state & m_mask & SUSPENDED) > 0; 464 isValid = isValid && ((m_mask & START_MASK) | SUSPENDED) == 465 ((m_state & DISPOSE_MASK) | SUSPENDED); 466 467 if ( ! isValid ) 468 { 469 throw new IllegalStateException ( message ); 470 } 471 472 m_state &= ~SUSPENDED; 473 } 474 475 482 public void checkStopped() 483 { 484 checkStopped( STOP_FAIL ); 485 } 486 487 495 public void checkStopped( final String message ) 496 { 497 boolean isValid = false; 498 isValid = (m_state & m_mask & STOPPED) == 0; 499 isValid = isValid && ((m_mask & STOP_MASK) | STOPPED) == 500 ((m_state & DISPOSE_MASK) | STOPPED); 501 502 if ( ! isValid ) 503 { 504 throw new IllegalStateException ( message ); 505 } 506 507 m_state &= ~ACTIVE; 508 m_state |= STOPPED; 509 } 510 511 518 public void checkDisposed() 519 { 520 checkDisposed( DISPOSE_FAIL ); 521 } 522 523 531 public void checkDisposed( final String message ) 532 { 533 boolean isValid = false; 534 isValid = (m_state & m_mask & DISPOSED) == 0; 535 isValid = isValid && ((m_mask & DISPOSE_MASK) | DISPOSED) == 536 ((m_state & DISPOSE_MASK) | DISPOSED); 537 538 if ( ! isValid ) 539 { 540 throw new IllegalStateException ( message ); 541 } 542 543 m_state &= ~ACTIVE; 544 m_state |= DISPOSED; 545 } 546 547 552 public void checkActive() 553 { 554 checkActive( ACTIVE_FAIL ); 555 } 556 557 563 public void checkActive( final String message ) 564 { 565 if ( isActive() ) 566 { 567 return; 568 } 569 570 throw new IllegalStateException ( message ); 571 } 572 573 578 public boolean isActive() 579 { 580 if ( (ACTIVE & m_state) > 0 ) 581 { 582 return true; 583 } 584 585 return false; 586 } 587 588 594 public void checkNotAssigned( final Object object ) 595 { 596 checkNotAssigned( object, WRITE_FAIL ); 597 } 598 599 606 public void checkNotAssigned( final Object object, final String message ) 607 { 608 if ( null != object ) 609 { 610 throw new IllegalStateException ( message ); 611 } 612 } 613 } 614 615 | Popular Tags |