1 9 package org.ozoneDB.core.storage.classicStore; 10 11 import java.io.*; 12 import java.util.*; 13 import java.lang.reflect.*; 14 import org.ozoneDB.*; 15 import org.ozoneDB.io.stream.ResolvingObjectInputStream; 16 import org.ozoneDB.tools.OPP.OPP; 17 import org.ozoneDB.DxLib.*; 18 import org.ozoneDB.core.*; 19 import org.ozoneDB.util.*; 20 21 22 25 public final class ClassicObjectContainer extends AbstractObjectContainer implements Externalizable { 26 27 28 protected final static long serialVersionUID = 2; 29 protected final static byte subSerialVersionUID = 1; 30 31 32 private static DxHashMap methodTable = new DxHashMap( 64 ); 33 34 35 protected static boolean keepObjectsAlive = true; 36 37 38 protected transient Env env; 39 protected transient ClusterSpace clusterSpace; 40 41 42 protected Permissions permissions; 43 44 45 protected OzoneCompatible target; 46 47 protected OzoneCompatible targetShadow; 48 49 protected byte[] targetBackup; 50 51 52 protected long touchTime = System.currentTimeMillis(); 53 protected short touchCount = 1; 54 protected boolean touched = false; 55 56 57 protected String name; 58 59 60 protected ObjectID objID; 61 62 protected ClusterID clusterID; 63 64 65 protected Lock lock = null; 66 67 68 protected boolean deleted = false; 69 70 protected boolean created = false; 71 72 73 76 public ClassicObjectContainer() { 77 created = false; 78 deleted = false; 79 } 80 81 82 85 public ClassicObjectContainer( OzoneCompatible _target, ObjectID _objID, Permissions _permissions ) { 86 env = Env.currentEnv(); 87 clusterSpace = ((ClassicStore)env.getStoreManager()).objectSpace.clusterSpace; 88 89 target = _target; 90 objID = _objID; 91 permissions = _permissions; 92 created = true; 95 deleted = false; 96 97 target.setContainer( this ); 98 } 99 100 102 107 public long modTime() { 108 return 0; 109 } 110 111 public Lock lock() { 112 return null; 113 } 114 115 public void setTarget(OzoneCompatible _target) { 116 } 117 118 public Class targetClass() { 119 return null; 120 } 121 122 127 public void pin() { 128 } 129 130 134 public void unpin() { 135 } 136 137 140 public boolean isPinned() { 141 return false; 142 } 143 144 157 public int ensureGarbageCollectionLevel(int newGarbageCollectionLevel) { 158 return 0; 159 } 160 161 164 public int getGarbageCollectionLevel() { 165 return 0; 166 } 167 168 169 172 public boolean equals( Object obj ) { 173 if (obj instanceof ClassicObjectContainer) { 174 ClassicObjectContainer rhs = (ClassicObjectContainer)obj; 175 if (objID.equals( rhs.objID )) { 176 return true; 177 } 178 } 179 180 return false; 181 } 182 183 184 185 public void finalizeTarget() throws Exception { 186 } 189 190 191 192 public ObjectID id() { 193 return objID; 194 } 195 196 197 198 public boolean isCreated() { 199 return created; 200 } 201 202 203 204 public void deleteTarget() { 205 deleted = true; 206 } 207 208 209 210 public boolean isDeleted() { 211 return deleted; 212 } 213 214 215 216 public Object invokeTarget( Env env, String methodName, String sig, Object [] args ) throws Exception { 217 clusterSpace.touchObject( objID ); 218 219 Method method = methodFor( methodName, sig, args ); 220 if (method == null) { 221 throw new MethodNotFoundException( methodName ); 222 } 223 224 return method.invoke( activatedObject(), args ); 225 } 226 227 228 229 public void touch() { 230 touched = true; 231 ++touchCount; 232 touchTime = System.currentTimeMillis(); 233 } 234 235 236 237 public String name() { 238 return name; 239 } 240 241 242 243 public void setName( String _name ) { 244 nameTarget( _name ); 245 } 246 247 248 249 public void nameTarget( String _name ) { 250 name = _name; 251 } 252 253 254 255 public Permissions permissions() { 256 return permissions; 257 } 258 259 260 264 public OzoneCompatible targetClone() throws Exception { 265 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 266 ObjectOutputStream out = new ObjectOutputStream( bout ); 267 out.writeObject( activatedObject() ); 268 out.close(); 269 ObjectInputStream in = new ResolvingObjectInputStream( new ByteArrayInputStream( bout.toByteArray() ) ); 270 OzoneCompatible result = (OzoneCompatible)in.readObject(); 271 in.close(); 272 273 return result; 274 } 275 276 277 279 public OzoneProxy ozoneProxy() { 280 try { 281 String name = objectClass().getName() + PROXYNAME_POSTFIX; 282 OzoneProxy rObj = (OzoneProxy)env.classManager.classForName( name ).newInstance(); 283 rObj.remoteID = id(); 284 rObj.link = env.database; 285 return rObj; 286 } catch (Exception e) { 287 env.logWriter.newEntry( this, "ozoneProxy(): unable to create proper proxy object.", e, LogWriter.WARN ); 288 return new OzoneProxy( id(), env.database ); 289 } 290 } 291 292 293 294 public int lockLevel( Transaction ta ) { 295 return lock.level( ta ); 296 } 297 298 299 303 public DxCollection allLockers() { 304 DxCollection lockerIDs = lock.lockerIDs(); 305 306 DxArrayBag result = new DxArrayBag( lockerIDs.count() ); 307 DxIterator it = lockerIDs.iterator(); 308 while (it.next() != null) { 309 result.add( env.transactionManager.taForID( (TransactionID)it.object() ) ); 310 } 311 312 return result; 313 } 314 315 316 317 public synchronized void notifyAllTAs( Transaction ta ) { 318 env.logWriter.newEntry( this, ta.toString() + " notify all TAs...", LogWriter.DEBUG3 ); 319 lock.notifyAll(); 320 } 321 322 323 325 326 328 public OzoneCompatible targetShadow() { 329 return targetShadow; 330 } 331 332 333 337 public OzoneCompatible target() { 338 return target; 339 } 340 341 342 349 protected OzoneCompatible setObject( OzoneCompatible obj ) { 350 OzoneCompatible old = target; 351 target = obj; 352 if (target != null) { 353 target.setContainer( this ); 354 if (targetShadow != null) { 355 env.logWriter.newEntry( this, "setObject(): targetShadow != null !", LogWriter.WARN ); 356 } 357 } 358 return old; 359 } 360 361 362 363 protected void setOwner( User newOwner ) { 364 permissions.setOwner( newOwner ); 365 } 366 367 368 369 protected int touchCount() { 370 return touchCount; 371 } 372 373 374 375 protected boolean touched() { 376 return touched; 377 } 378 379 380 381 protected long touchTime() { 382 return touchTime; 383 } 384 385 386 387 protected ClusterID clusterID() { 388 return clusterID; 389 } 390 391 392 393 protected void setClusterID( ClusterID _clusterID ) { 394 clusterID = _clusterID; 395 } 396 397 398 400 protected Class objectClass() throws Exception { 401 return activatedObject().getClass(); 402 } 403 404 405 411 protected OzoneCompatible activatedObject() throws Exception { 412 if (target != null) { 415 return target; 416 } else if (targetShadow != null) { 417 return targetShadow; 418 } else { 419 clusterSpace.activateObject( this ); 420 return target; 421 } 422 } 423 424 425 426 protected synchronized void commitTarget( Transaction ta ) { 427 if (targetShadow != null) { 428 target = targetShadow; 429 targetShadow = null; 430 targetBackup = null; 431 } 432 created = false; 433 lock.release( ta ); 434 } 435 436 437 438 protected synchronized void abortTarget( Transaction ta ) { 439 if (targetShadow != null) { 440 try { 441 ObjectInputStream in = new ResolvingObjectInputStream( new ByteArrayInputStream( targetBackup ) ); 442 target = (OzoneCompatible)in.readObject(); 443 in.close(); 444 } catch (Exception e) { 445 env.logWriter.newEntry( this, "abortObject(): ", e, LogWriter.WARN ); 447 target = null; 448 } 449 targetShadow = null; 450 targetBackup = null; 451 } 452 created = false; 453 deleted = false; 454 lock.release( ta ); 455 } 456 457 458 459 protected synchronized void upgradeLockLevel( Transaction ta, int lockLevel ) throws Exception { 460 activatedObject(); 461 lock.tryAcquire( ta, Lock.LEVEL_READ ); 462 if (lockLevel > Lock.LEVEL_READ) { 463 lock.tryAcquire( ta, lockLevel ); 464 if (!isCreated()) { 466 createShadow(); 467 } 468 } 469 } 470 471 472 473 protected synchronized void createShadow() throws Exception { 474 try { 475 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 476 ObjectOutputStream out = new ObjectOutputStream( bout ); 477 out.writeObject( activatedObject() ); 478 out.close(); 479 targetBackup = bout.toByteArray(); 480 targetShadow = target; 481 target = null; 482 } catch (Exception e) { 483 env.logWriter.newEntry( this, "lockWrite(): ", e, LogWriter.WARN ); 485 } 486 } 487 488 489 492 public void writeExternal( ObjectOutput out ) throws IOException { 493 500 out.writeByte( subSerialVersionUID ); 501 502 out.writeObject( permissions ); 503 out.writeObject( objID ); 504 out.writeObject( target ); 505 out.writeObject( targetShadow ); 506 out.writeObject( targetBackup ); 507 out.writeObject( name ); 508 out.writeObject( clusterID ); 509 out.writeLong( touchTime ); 510 out.writeShort( touchCount ); 511 out.writeBoolean( touched ); 512 out.writeBoolean( created ); 513 out.writeBoolean( deleted ); 514 out.writeObject( lock ); 515 } 516 517 518 522 public synchronized void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException { 523 env = Env.currentEnv(); 524 clusterSpace = ((ClassicStore)env.getStoreManager()).objectSpace.clusterSpace; 525 526 byte streamVersionUID = in.readByte(); 527 528 permissions = (Permissions)in.readObject(); 529 objID = (ObjectID)in.readObject(); 530 target = (OzoneCompatible)in.readObject(); 531 targetShadow = (OzoneCompatible)in.readObject(); 532 targetBackup = (byte[])in.readObject(); 533 if (target != null) { 534 target.setContainer( this ); 535 } 536 if (targetShadow != null) { 537 targetShadow.setContainer( this ); 538 } 539 540 name = (String )in.readObject(); 541 clusterID = (ClusterID)in.readObject(); 542 touchTime = in.readLong(); 543 touchCount = in.readShort(); 544 touched = in.readBoolean(); 545 created = in.readBoolean(); 546 deleted = in.readBoolean(); 547 lock = (Lock)in.readObject(); 548 549 } 556 557 558 561 protected void storeExternal( ObjectOutput out ) throws IOException { 562 out.writeLong( serialVersionUID ); 563 out.writeObject( permissions ); 564 out.writeObject( objID ); 565 out.writeObject( name ); 566 out.writeObject( clusterID ); 567 } 568 569 570 574 protected void loadExternal( ObjectInput in ) throws IOException, ClassNotFoundException { 575 env = Env.currentEnv(); 576 clusterSpace = ((ClassicStore)env.getStoreManager()).objectSpace.clusterSpace; 577 578 long streamSerialVersionUID = in.readLong(); 579 permissions = (Permissions)in.readObject(); 580 objID = (ObjectID)in.readObject(); 581 name = (String )in.readObject(); 582 clusterID = (ClusterID)in.readObject(); 583 584 created = false; 585 deleted = false; 586 touchTime = System.currentTimeMillis(); 587 touchCount = 1; 588 touched = false; 589 } 592 593 594 599 private Method methodFor( String methodName, String sig, Object [] args ) throws Exception { 600 String key = objectClass().getName() + methodName + sig; 601 602 Method method = (Method)methodTable.elementForKey( key ); 603 if (method == null) { 605 int argNum = args.length; 606 Class [] classes = new Class [argNum]; 607 if (sig == null) { 608 for (int i = 0; i < argNum; i++) { 611 classes[i] = args[i].getClass(); 612 } 613 } else { 614 StringTokenizer st = new StringTokenizer( sig, OPP.SIGNATURE_DELIMITER ); 616 int i = 0; 617 while (st.hasMoreTokens()) { 618 classes[i++] = env.classManager.classForName( st.nextToken() ); 619 } 620 } 622 method = objectClass().getMethod( methodName, classes ); 623 methodTable.addForKey( method, key ); 624 } 625 626 return method; 627 } 628 } 629 | Popular Tags |