1 21 package oracle.toplink.essentials.internal.identitymaps; 23 24 import java.util.*; 25 import java.io.*; 26 import oracle.toplink.essentials.internal.helper.*; 27 28 38 public abstract class IdentityMap implements Serializable, Cloneable { 39 40 41 protected int maxSize; 42 43 44 protected CacheKey searchKey; 45 46 51 public IdentityMap(int size) { 52 maxSize = size; 53 searchKey = new CacheKey(new Vector(1), null, null); 54 } 55 56 59 public CacheKey acquireDeferredLock(Vector primaryKey) { 60 CacheKey key = null; 62 63 synchronized (this) { 65 key = getCacheKey(primaryKey); 67 if (key == null) { 68 CacheKey cacheKey = createCacheKey(primaryKey, null, null); 70 cacheKey.acquireDeferredLock(); 71 put(cacheKey); 72 return cacheKey; 73 } 74 } 75 76 key.acquireDeferredLock(); 78 79 return key; 80 } 81 82 87 public CacheKey acquireLock(Vector primaryKey, boolean forMerge) { 88 CacheKey key = null; 90 91 synchronized (this) { 93 key = getCacheKey(primaryKey); 95 if (key == null) { 96 CacheKey cacheKey = createCacheKey(primaryKey, null, null); 98 cacheKey.acquire(forMerge); 99 put(cacheKey); 100 return cacheKey; 101 } 102 } 103 104 key.acquire(); 106 107 return key; 108 } 109 110 115 public abstract void collectLocks(HashMap threadList); 116 117 122 public CacheKey acquireLockNoWait(Vector primaryKey, boolean forMerge) { 123 CacheKey key = null; 125 126 synchronized (this) { 128 key = getCacheKey(primaryKey); 129 if (key == null) { 130 CacheKey cacheKey = createCacheKey(primaryKey, null, null); 132 cacheKey.acquire(forMerge); 133 put(cacheKey); 134 return cacheKey; 135 } 136 } 137 138 if (key != null) { 140 if (!key.acquireNoWait(forMerge)) { 142 key = null; 143 } 144 } 145 146 return key; 147 } 148 149 155 public CacheKey acquireReadLockOnCacheKey(Vector primaryKey) { 156 CacheKey key = null; 158 159 synchronized (this) { 161 key = getCacheKey(primaryKey); 162 if (key == null) { 163 CacheKey cacheKey = createCacheKey(primaryKey, null, null); 165 166 cacheKey.acquireReadLock(); 169 return cacheKey; 170 } 171 } 172 173 key.acquireReadLock(); 174 175 return key; 176 } 177 178 185 public CacheKey acquireReadLockOnCacheKeyNoWait(Vector primaryKey) { 186 CacheKey key = null; 188 189 synchronized (this) { 191 key = getCacheKey(primaryKey); 192 if (key == null) { 193 CacheKey cacheKey = createCacheKey(primaryKey, null, null); 195 cacheKey.acquireReadLock(); 196 197 return cacheKey; 200 } 201 } 202 203 if (key != null) { 205 if (!key.acquireReadLockNoWait()) { 207 key = null; 208 } 209 } 210 211 return key; 212 } 213 214 218 public Object clone() { 219 Object object = null; 220 221 try { 222 object = super.clone(); 223 } catch (Exception e) { 224 ; 225 } 226 227 return object; 228 } 229 230 234 public boolean containsKey(Vector primaryKey) { 235 CacheKey wrapper = getCacheKeyWithReadLock(primaryKey); 236 237 if (wrapper == null) { 238 return false; 239 } else { 240 return true; 241 } 242 } 243 244 public CacheKey createCacheKey(Vector primaryKey, Object object, Object writeLockValue) { 245 return createCacheKey(primaryKey, object, writeLockValue, 0); 246 } 247 248 public CacheKey createCacheKey(Vector primaryKey, Object object, Object writeLockValue, long readTime) { 249 return new CacheKey(primaryKey, object, writeLockValue, readTime); 250 } 251 252 255 public abstract Enumeration elements(); 256 257 260 public Object get(Vector primaryKey) { 261 CacheKey cacheKey = getCacheKeyWithReadLock(primaryKey); 262 263 if (cacheKey == null) { 264 return null; 265 } 266 return cacheKey.getObject(); 267 } 268 269 273 protected CacheKey getCacheKey(Vector primaryKey) { 274 CacheKey key = null; 275 276 synchronized (this) { 277 getSearchKey().setKey(primaryKey); 278 key = getCacheKey(getSearchKey()); 279 } 280 281 return key; 282 } 283 284 287 protected abstract CacheKey getCacheKey(CacheKey cacheKey); 288 289 292 protected CacheKey getCacheKeyWithReadLock(Vector primaryKey) { 293 CacheKey key = getCacheKey(primaryKey); 294 295 if (key != null) { 296 key.acquireReadLock(); 297 key.releaseReadLock(); 298 } 299 300 return key; 301 } 302 303 310 public static Class getDefaultIdentityMapClass() { 311 return ClassConstants.SoftCacheWeakIdentityMap_Class; 312 } 313 314 317 public int getMaxSize() { 318 if (maxSize == -1) { 319 maxSize = 100; 320 } 321 return maxSize; 322 } 323 324 protected CacheKey getSearchKey() { 325 return searchKey; 326 } 327 328 331 public abstract int getSize(); 332 333 337 public abstract int getSize(Class myClass, boolean recurse); 338 339 343 public Object getWrapper(Vector primaryKey) { 344 CacheKey cacheKey = getCacheKeyWithReadLock(primaryKey); 345 346 if (cacheKey == null) { 347 return null; 348 } else { 349 return cacheKey.getWrapper(); 350 } 351 } 352 353 356 public Object getWriteLockValue(Vector primaryKey) { 357 CacheKey cacheKey = getCacheKeyWithReadLock(primaryKey); 358 359 if (cacheKey == null) { 360 return null; 361 } else { 362 return cacheKey.getWriteLockValue(); 363 } 364 } 365 366 371 public void initialize(int size) { 372 setMaxSize(size); 373 } 374 375 378 public abstract Enumeration keys(); 379 380 387 public abstract CacheKey put(Vector primaryKey, Object object, Object writeLockValue, long readTime); 388 389 393 protected abstract void put(CacheKey cacheKey); 394 395 398 public Object remove(Vector primaryKey) { 399 CacheKey key = getCacheKey(primaryKey); 400 return remove(key); 401 } 402 403 406 public abstract Object remove(CacheKey cacheKey); 407 408 412 protected void setMaxSize(int size) { 413 maxSize = size; 414 } 415 416 422 public synchronized void updateMaxSize(int maxSize) { 423 setMaxSize(maxSize); 424 } 425 426 protected void setSearchKey(CacheKey searchKey) { 427 this.searchKey = searchKey; 428 } 429 430 434 public void setWrapper(Vector primaryKey, Object wrapper) { 435 CacheKey cacheKey = getCacheKey(primaryKey); 436 437 if (cacheKey != null) { 438 cacheKey.setWrapper(wrapper); 439 } 440 } 441 442 445 public void setWriteLockValue(Vector primaryKey, Object writeLockValue) { 446 CacheKey cacheKey = getCacheKey(primaryKey); 447 448 if (cacheKey != null) { 449 cacheKey.acquire(); 451 cacheKey.setWriteLockValue(writeLockValue); 452 cacheKey.release(); 453 } 454 } 455 456 public String toString() { 457 return oracle.toplink.essentials.internal.helper.Helper.getShortClassName(getClass()) + "[" + getSize() + "]"; 458 } 459 460 463 public void updateCacheKey(CacheKey cacheKey) { 464 return; 465 } 466 } 467 | Popular Tags |