1 7 8 package com.sun.corba.se.impl.oa.poa; 9 10 import java.util.Iterator ; 11 import java.util.Collections ; 12 import java.util.Set ; 13 import java.util.HashSet ; 14 15 import org.omg.CORBA.LocalObject ; 16 import org.omg.CORBA.CompletionStatus ; 17 18 import org.omg.PortableServer.POAManager ; 19 import org.omg.PortableServer.POAManagerPackage.State ; 20 import org.omg.PortableServer.POA ; 21 22 import org.omg.PortableInterceptor.DISCARDING ; 23 import org.omg.PortableInterceptor.ACTIVE ; 24 import org.omg.PortableInterceptor.HOLDING ; 25 import org.omg.PortableInterceptor.INACTIVE ; 26 import org.omg.PortableInterceptor.NON_EXISTENT ; 27 28 import com.sun.corba.se.spi.protocol.PIHandler ; 29 30 import com.sun.corba.se.impl.logging.POASystemException ; 31 32 import com.sun.corba.se.impl.orbutil.ORBUtility ; 33 34 38 39 public class POAManagerImpl extends org.omg.CORBA.LocalObject implements 40 POAManager 41 { 42 private final POAFactory factory ; private PIHandler pihandler ; private State state; private Set poas = new HashSet (4) ; private int nInvocations=0; private int nWaiters=0; private int myId = 0 ; private boolean debug ; 52 private boolean explicitStateChange ; 56 private String stateToString( State state ) 57 { 58 switch (state.value()) { 59 case State._HOLDING : return "State[HOLDING]" ; 60 case State._ACTIVE : return "State[ACTIVE]" ; 61 case State._DISCARDING : return "State[DISCARDING]" ; 62 case State._INACTIVE : return "State[INACTIVE]" ; 63 } 64 65 return "State[UNKNOWN]" ; 66 } 67 68 public String toString() 69 { 70 return "POAManagerImpl[myId=" + myId + 71 " state=" + stateToString(state) + 72 " nInvocations=" + nInvocations + 73 " nWaiters=" + nWaiters + "]" ; 74 } 75 76 POAFactory getFactory() 77 { 78 return factory ; 79 } 80 81 PIHandler getPIHandler() 82 { 83 return pihandler ; 84 } 85 86 private void countedWait() 87 { 88 try { 89 if (debug) { 90 ORBUtility.dprint( this, "Calling countedWait on POAManager " + 91 this + " nWaiters=" + nWaiters ) ; 92 } 93 94 nWaiters++ ; 95 wait(); 96 } catch ( java.lang.InterruptedException ex ) { 97 } finally { 99 nWaiters-- ; 100 101 if (debug) { 102 ORBUtility.dprint( this, "Exiting countedWait on POAManager " + 103 this + " nWaiters=" + nWaiters ) ; 104 } 105 } 106 } 107 108 private void notifyWaiters() 109 { 110 if (debug) { 111 ORBUtility.dprint( this, "Calling notifyWaiters on POAManager " + 112 this + " nWaiters=" + nWaiters ) ; 113 } 114 115 if (nWaiters >0) 116 notifyAll() ; 117 } 118 119 public int getManagerId() 120 { 121 return myId ; 122 } 123 124 POAManagerImpl( POAFactory factory, PIHandler pihandler ) 125 { 126 this.factory = factory ; 127 factory.addPoaManager(this); 128 this.pihandler = pihandler ; 129 myId = factory.newPOAManagerId() ; 130 state = State.HOLDING; 131 debug = factory.getORB().poaDebugFlag ; 132 explicitStateChange = false ; 133 134 if (debug) { 135 ORBUtility.dprint( this, "Creating POAManagerImpl " + this ) ; 136 } 137 } 138 139 synchronized void addPOA(POA poa) 140 { 141 if (state.value() == State._INACTIVE) { 143 POASystemException wrapper = factory.getWrapper(); 144 throw wrapper.addPoaInactive( CompletionStatus.COMPLETED_NO ) ; 145 } 146 147 poas.add(poa); 148 } 149 150 synchronized void removePOA(POA poa) 151 { 152 poas.remove(poa); 153 if ( poas.isEmpty() ) { 154 factory.removePoaManager(this); 155 } 156 } 157 158 public short getORTState() 159 { 160 switch (state.value()) { 161 case State._HOLDING : return HOLDING.value ; 162 case State._ACTIVE : return ACTIVE.value ; 163 case State._INACTIVE : return INACTIVE.value ; 164 case State._DISCARDING : return DISCARDING.value ; 165 default : return NON_EXISTENT.value ; 166 } 167 } 168 169 186 187 191 public synchronized void activate() 192 throws org.omg.PortableServer.POAManagerPackage.AdapterInactive 193 { 194 explicitStateChange = true ; 195 196 if (debug) { 197 ORBUtility.dprint( this, 198 "Calling activate on POAManager " + this ) ; 199 } 200 201 try { 202 if ( state.value() == State._INACTIVE ) 203 throw new org.omg.PortableServer.POAManagerPackage.AdapterInactive (); 204 205 state = State.ACTIVE; 207 208 pihandler.adapterManagerStateChanged( myId, getORTState() ) ; 209 210 notifyWaiters(); 214 } finally { 215 if (debug) { 216 ORBUtility.dprint( this, 217 "Exiting activate on POAManager " + this ) ; 218 } 219 } 220 } 221 222 226 public synchronized void hold_requests(boolean wait_for_completion) 227 throws org.omg.PortableServer.POAManagerPackage.AdapterInactive 228 { 229 explicitStateChange = true ; 230 231 if (debug) { 232 ORBUtility.dprint( this, 233 "Calling hold_requests on POAManager " + this ) ; 234 } 235 236 try { 237 if ( state.value() == State._INACTIVE ) 238 throw new org.omg.PortableServer.POAManagerPackage.AdapterInactive (); 239 state = State.HOLDING; 241 242 pihandler.adapterManagerStateChanged( myId, getORTState() ) ; 243 244 notifyWaiters(); 248 249 if ( wait_for_completion ) { 250 while ( state.value() == State._HOLDING && nInvocations > 0 ) { 251 countedWait() ; 252 } 253 } 254 } finally { 255 if (debug) { 256 ORBUtility.dprint( this, 257 "Exiting hold_requests on POAManager " + this ) ; 258 } 259 } 260 } 261 262 266 public synchronized void discard_requests(boolean wait_for_completion) 267 throws org.omg.PortableServer.POAManagerPackage.AdapterInactive 268 { 269 explicitStateChange = true ; 270 271 if (debug) { 272 ORBUtility.dprint( this, 273 "Calling hold_requests on POAManager " + this ) ; 274 } 275 276 try { 277 if ( state.value() == State._INACTIVE ) 278 throw new org.omg.PortableServer.POAManagerPackage.AdapterInactive (); 279 280 state = State.DISCARDING; 282 283 pihandler.adapterManagerStateChanged( myId, getORTState() ) ; 284 285 notifyWaiters(); 290 291 if ( wait_for_completion ) { 292 while ( state.value() == State._DISCARDING && nInvocations > 0 ) { 293 countedWait() ; 294 } 295 } 296 } finally { 297 if (debug) { 298 ORBUtility.dprint( this, 299 "Exiting hold_requests on POAManager " + this ) ; 300 } 301 } 302 } 303 304 309 310 public void deactivate(boolean etherealize_objects, boolean wait_for_completion) 311 throws org.omg.PortableServer.POAManagerPackage.AdapterInactive 312 { 313 explicitStateChange = true ; 314 315 try { 316 synchronized( this ) { 317 if (debug) { 318 ORBUtility.dprint( this, 319 "Calling deactivate on POAManager " + this ) ; 320 } 321 322 if ( state.value() == State._INACTIVE ) 323 throw new org.omg.PortableServer.POAManagerPackage.AdapterInactive (); 324 325 state = State.INACTIVE; 326 327 pihandler.adapterManagerStateChanged( myId, getORTState() ) ; 328 329 notifyWaiters(); 334 } 335 336 POAManagerDeactivator deactivator = new POAManagerDeactivator( this, 337 etherealize_objects, debug ) ; 338 339 if (wait_for_completion) 340 deactivator.run() ; 341 else { 342 Thread thr = new Thread (deactivator) ; 343 thr.start() ; 344 } 345 } finally { 346 synchronized(this) { 347 if (debug) { 348 ORBUtility.dprint( this, 349 "Exiting deactivate on POAManager " + this ) ; 350 } 351 } 352 } 353 } 354 355 private class POAManagerDeactivator implements Runnable 356 { 357 private boolean etherealize_objects ; 358 private POAManagerImpl pmi ; 359 private boolean debug ; 360 361 POAManagerDeactivator( POAManagerImpl pmi, boolean etherealize_objects, 362 boolean debug ) 363 { 364 this.etherealize_objects = etherealize_objects ; 365 this.pmi = pmi ; 366 this.debug = debug ; 367 } 368 369 public void run() 370 { 371 try { 372 synchronized (pmi) { 373 if (debug) { 374 ORBUtility.dprint( this, 375 "Calling run with etherealize_objects=" + 376 etherealize_objects + " pmi=" + pmi ) ; 377 } 378 379 while ( pmi.nInvocations > 0 ) { 380 countedWait() ; 381 } 382 } 383 384 if (etherealize_objects) { 385 Iterator iterator = null ; 386 387 synchronized (pmi) { 389 if (debug) { 390 ORBUtility.dprint( this, 391 "run: Preparing to etherealize with pmi=" + 392 pmi ) ; 393 } 394 395 iterator = (new HashSet (pmi.poas)).iterator(); 396 } 397 398 while (iterator.hasNext()) { 399 ((POAImpl)iterator.next()).etherealizeAll(); 402 } 403 404 synchronized (pmi) { 405 if (debug) { 406 ORBUtility.dprint( this, 407 "run: removing POAManager and clearing poas " + 408 "with pmi=" + pmi ) ; 409 } 410 411 factory.removePoaManager(pmi); 412 poas.clear(); 413 } 414 } 415 } finally { 416 if (debug) { 417 synchronized (pmi) { 418 ORBUtility.dprint( this, "Exiting run" ) ; 419 } 420 } 421 } 422 } 423 } 424 425 429 430 public org.omg.PortableServer.POAManagerPackage.State get_state () { 431 return state; 432 } 433 434 437 438 synchronized void checkIfActive() 441 { 442 try { 443 if (debug) { 444 ORBUtility.dprint( this, 445 "Calling checkIfActive for POAManagerImpl " + this ) ; 446 } 447 448 checkState(); 449 } finally { 450 if (debug) { 451 ORBUtility.dprint( this, 452 "Exiting checkIfActive for POAManagerImpl " + this ) ; 453 } 454 } 455 } 456 457 private void checkState() 458 { 459 while ( state.value() != State._ACTIVE ) { 460 switch ( state.value() ) { 461 case State._HOLDING: 462 while ( state.value() == State._HOLDING ) { 463 countedWait() ; 464 } 465 break; 466 467 case State._DISCARDING: 468 throw factory.getWrapper().poaDiscarding() ; 469 470 case State._INACTIVE: 471 throw factory.getWrapper().poaInactive() ; 472 } 473 } 474 } 475 476 synchronized void enter() 477 { 478 try { 479 if (debug) { 480 ORBUtility.dprint( this, 481 "Calling enter for POAManagerImpl " + this ) ; 482 } 483 484 checkState(); 485 nInvocations++; 486 } finally { 487 if (debug) { 488 ORBUtility.dprint( this, 489 "Exiting enter for POAManagerImpl " + this ) ; 490 } 491 } 492 } 493 494 synchronized void exit() 495 { 496 try { 497 if (debug) { 498 ORBUtility.dprint( this, 499 "Calling exit for POAManagerImpl " + this ) ; 500 } 501 502 nInvocations--; 503 504 if ( nInvocations == 0 ) { 505 notifyWaiters(); 508 } 509 } finally { 510 if (debug) { 511 ORBUtility.dprint( this, 512 "Exiting exit for POAManagerImpl " + this ) ; 513 } 514 } 515 } 516 517 520 public synchronized void implicitActivation() 521 { 522 if (!explicitStateChange) 523 try { 524 activate() ; 525 } catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive ai) { 526 } 528 } 529 } 530 | Popular Tags |