1 package org.jacorb.poa; 2 3 22 23 import java.util.*; 24 25 import org.jacorb.poa.POA; 26 import org.jacorb.poa.except.POAInternalError; 27 import org.jacorb.poa.util.ByteArrayKey; 28 import org.jacorb.poa.util.POAUtil; 29 import org.jacorb.poa.util.StringPair; 30 31 import org.omg.PortableServer.POAPackage.ObjectAlreadyActive ; 32 import org.omg.PortableServer.POAPackage.ObjectNotActive ; 33 import org.omg.PortableServer.POAPackage.ServantAlreadyActive ; 34 import org.omg.PortableServer.Servant ; 35 import org.omg.PortableServer.ServantActivator ; 36 37 import org.apache.avalon.framework.logger.Logger; 38 39 47 48 public class AOM 49 { 50 private AOMListener aomListener; 51 52 private boolean unique; 53 private boolean singleThreaded; 54 private Logger logger; 55 56 private Hashtable objectMap = new Hashtable(); 60 private Hashtable servantMap; 64 private Vector etherealisationList = new Vector(); 66 private Vector incarnationList = new Vector(); 67 68 private Vector deactivationList = new Vector(); 69 71 private Object deactivationListLock = new Object (); 72 73 private AOM() 74 { 75 } 76 77 protected AOM( boolean _unique, 78 boolean single_threaded, 79 Logger _logger 80 ) 81 { 82 unique = _unique; 83 singleThreaded = single_threaded; 84 logger = _logger; 85 86 if (unique) 87 { 88 servantMap = new Hashtable(); 89 } 90 } 91 92 93 102 protected synchronized void add( byte[] oid, Servant servant ) 103 throws ObjectAlreadyActive , ServantAlreadyActive 104 { 105 ByteArrayKey oidbak = new ByteArrayKey (oid); 106 107 110 111 while (incarnationList.contains(oidbak) || 112 etherealisationList.contains(oidbak) || 113 deactivationList.contains( oidbak ) || 117 ( 120 servantMap != null && 121 servantMap.get( servant ) != null && 122 deactivationList.contains((ByteArrayKey)servantMap.get( servant )) 123 )) 124 { 125 try 126 { 127 wait(); 128 } 129 catch (InterruptedException e) 130 { 131 } 132 } 133 134 if (objectMap.containsKey(oidbak)) 135 throw new ObjectAlreadyActive (); 136 137 if (unique && servantMap.containsKey(servant)) 138 throw new ServantAlreadyActive (); 139 140 141 142 objectMap.put(oidbak, servant); 143 144 if ( unique ) 145 { 146 servantMap.put(servant, oidbak); 147 } 148 149 if (logger.isInfoEnabled()) 150 { 151 logger.info("oid: " + POAUtil.convert(oid) + 152 "object is activated"); 153 } 154 155 if (aomListener != null) 157 aomListener.objectActivated(oid, servant, objectMap.size()); 158 } 159 160 161 protected synchronized void addAOMListener(AOMListener listener) 162 { 163 aomListener = EventMulticaster.add(aomListener, listener); 164 } 165 166 167 boolean isDeactivating (ByteArrayKey oid) 168 { 169 return deactivationList.contains (oid); 170 } 171 172 173 protected boolean contains(Servant servant) 174 { 175 if (unique) 176 { 177 return servantMap.containsKey(servant); 178 } 179 else 180 { 181 return objectMap.contains(servant); 182 } 183 } 184 185 186 protected synchronized StringPair[] deliverContent() 187 { 188 StringPair[] result = new StringPair[objectMap.size()]; 189 ByteArrayKey oidbak; 190 Enumeration en = objectMap.keys(); 191 192 for ( int i = 0; i < result.length; i++ ) 193 { 194 oidbak = (ByteArrayKey) en.nextElement(); 195 result[i] = new StringPair 196 ( 197 oidbak.toString(), 198 objectMap.get(oidbak).getClass().getName() 199 ); 200 } 201 return result; 202 203 } 204 205 protected byte[] getObjectId(Servant servant) 206 { 207 if (!unique) 208 throw new POAInternalError("error: not UNIQUE_ID policy (getObjectId)"); 209 210 ByteArrayKey oidbak = (ByteArrayKey)servantMap.get(servant); 211 212 if (oidbak != null) 213 return oidbak.getBytes(); 214 215 return null; 216 } 217 218 219 220 protected Servant getServant(byte[] oid) 221 { 222 return (Servant ) objectMap.get( new ByteArrayKey( oid ) ); 223 } 224 225 226 227 228 protected synchronized Servant incarnate( byte[] oid, 229 ServantActivator servant_activator, 230 org.omg.PortableServer.POA poa ) 231 throws org.omg.PortableServer.ForwardRequest 232 { 233 ByteArrayKey oidbak = new ByteArrayKey( oid ); 234 Servant servant = null; 235 236 if (logger.isInfoEnabled()) 237 { 238 logger.info( "oid: " + POAUtil.convert(oid) + 239 "incarnate"); 240 } 241 242 243 244 245 246 while (!incarnationList.isEmpty() || !etherealisationList.isEmpty()) 247 { 248 try 249 { 250 wait(); 251 } 252 catch (InterruptedException e) { 253 } 254 } 255 256 257 if (objectMap.containsKey(oidbak)) 258 { 259 return (Servant ) objectMap.get(oidbak); 260 } 261 262 263 264 incarnationList.addElement(oidbak); 265 try 266 { 267 servant = servant_activator.incarnate(oid, poa); 268 } 269 finally 270 { 271 incarnationList.removeElement(oidbak); 272 notifyAll(); 273 } 274 275 if (servant == null) 276 { 277 if (logger.isInfoEnabled()) 278 { 279 logger.info("oid: " + POAUtil.convert(oid) + 280 "servant is not incarnated (incarnate returns null)"); 281 } 282 return null; 283 } 284 285 if (unique && servantMap.containsKey(servant)) 286 { 287 if (logger.isInfoEnabled()) 288 { 289 logger.info("oid: " + POAUtil.convert(oid) + 290 "servant is not incarnated (unique_id policy is violated)"); 291 } 292 return null; 293 } 294 295 if (logger.isDebugEnabled()) 296 { 297 logger.debug("oid: " + POAUtil.convert(oid) + 298 "servant is incarnated"); 299 } 300 301 if (aomListener != null) 303 aomListener.servantIncarnated(oid, servant); 304 305 306 307 try 308 { 309 add(oid, servant); 310 } 311 catch (ObjectAlreadyActive e) 312 { 313 throw new POAInternalError("error: object already active (AOM.incarnate)"); 314 } 315 catch (ServantAlreadyActive e) 316 { 317 throw new POAInternalError("error: servant already active (AOM.incarnate)"); 318 } 319 return servant; 320 } 321 322 protected void remove( byte[] oid, 323 RequestController requestController, 324 ServantActivator servantActivator, 325 POA poa, 326 boolean cleanupInProgress) 327 throws ObjectNotActive 328 { 329 ByteArrayKey oidbak = new ByteArrayKey( oid ); 330 331 synchronized( deactivationListLock ) 335 { 336 if ( !objectMap.containsKey( oidbak ) || 337 deactivationList.contains( oidbak ) ) 338 { 339 throw new ObjectNotActive (); 340 } 341 342 deactivationList.addElement(oidbak); 343 } 344 345 final byte[] oid_ = oid; 346 final RequestController requestController_ = requestController; 347 final ServantActivator servantActivator_ = servantActivator; 348 final POA poa_ = poa; 349 final boolean cleanupInProgress_ = cleanupInProgress; 350 351 Thread thread = new Thread ("AOM_RemovalThread") 352 { 353 public void run() 354 { 355 _remove( 356 oid_, 357 requestController_, 358 servantActivator_, 359 poa_, 360 cleanupInProgress_ 361 ); 362 } 363 }; 364 365 thread.start(); 366 } 367 368 369 379 private void _remove( byte[] oid, 380 RequestController requestController, 381 ServantActivator servantActivator, 382 POA poa, 383 boolean cleanupInProgress) 384 { 385 ByteArrayKey oidbak = new ByteArrayKey( oid ); 386 Servant servant = null; 387 388 if (!objectMap.containsKey(oidbak)) 389 { 390 deactivationList.removeElement(oidbak); 392 return; 393 } 394 395 if ( requestController != null) 397 requestController.waitForObjectCompletion(oid); 398 399 synchronized (this) 400 { 401 if ((servant = (Servant )objectMap.get(oidbak)) == null) { 402 return; 403 } 404 405 406 407 objectMap.remove(oidbak); 408 409 if (unique) 410 { 411 servantMap.remove(servant); 412 } 413 414 deactivationList.removeElement(oidbak); 418 419 if (logger.isInfoEnabled()) 420 { 421 logger.info("oid: " + POAUtil.convert(oid) + 422 "object is deactivated"); 423 } 424 425 if (aomListener != null) 427 aomListener.objectDeactivated(oid, servant, objectMap.size()); 428 429 if (servantActivator == null) 430 { 431 requestController.freeObject(oid); 432 notifyAll(); 434 return; 435 } 436 437 438 439 443 444 while (!incarnationList.isEmpty() || !etherealisationList.isEmpty()) 445 { 446 try 447 { 448 wait(); 449 } 450 catch (InterruptedException e) 451 { 452 } 453 } 454 etherealisationList.addElement(oidbak); 455 456 try 457 { 458 servantActivator.etherealize 459 ( 460 oid, 461 poa, 462 servant, 463 cleanupInProgress, 464 contains(servant) 465 ); 466 467 if (logger.isInfoEnabled()) 468 { 469 logger.info("oid: " + POAUtil.convert(oid) + 470 "servant is etherealized"); 471 } 472 473 475 if (aomListener != null) 476 aomListener.servantEtherialized(oid, servant); 477 478 } 479 catch (org.omg.CORBA.SystemException e) 480 { 481 if (logger.isWarnEnabled()) 482 { 483 logger.info("oid: " + POAUtil.convert(oid) + 484 "exception occurred during servant etherialisation: " + e.getMessage() 485 ); 486 } 487 } 488 finally 489 { 490 etherealisationList.removeElement(oidbak); 491 notifyAll(); 492 } 493 494 if (requestController != null) 496 requestController.freeObject(oid); 497 } 498 } 499 500 protected void removeAll( ServantActivator servant_activator, 501 POA poa, 502 boolean cleanup_in_progress ) 503 { 504 byte[] oid; 505 Enumeration en = objectMap.keys(); 506 while (en.hasMoreElements()) 507 { 508 oid = ((ByteArrayKey) en.nextElement()).getBytes(); 509 _remove(oid, null, servant_activator, poa, cleanup_in_progress); 510 } 511 } 512 513 protected synchronized void removeAOMListener(AOMListener listener) 514 { 515 aomListener = EventMulticaster.remove(aomListener, listener); 516 } 517 518 protected int size() 519 { 520 return objectMap.size(); 521 } 522 } 523 | Popular Tags |