1 package org.apache.ojb.broker.core.proxy; 2 3 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 22 import org.apache.ojb.broker.ManageableCollection; 23 import org.apache.ojb.broker.OJBRuntimeException; 24 import org.apache.ojb.broker.PBFactoryException; 25 import org.apache.ojb.broker.PBKey; 26 import org.apache.ojb.broker.PersistenceBroker; 27 import org.apache.ojb.broker.PersistenceBrokerException; 28 import org.apache.ojb.broker.PersistenceBrokerFactory; 29 import org.apache.ojb.broker.metadata.MetadataManager; 30 import org.apache.ojb.broker.metadata.MetadataException; 31 import org.apache.ojb.broker.core.PersistenceBrokerThreadMapping; 32 import org.apache.ojb.broker.query.Query; 33 import org.apache.ojb.broker.util.collections.IRemovalAwareCollection; 34 import org.apache.ojb.broker.util.collections.RemovalAwareCollection; 35 36 43 public class CollectionProxyDefaultImpl implements Collection , ManageableCollection, CollectionProxy 44 { 45 46 private PBKey _brokerKey; 47 48 private boolean _perThreadDescriptorsEnabled; 49 50 private Object _profileKey; 51 52 private Query _query; 53 54 private Collection _data; 55 56 private Class _collectionClass; 57 58 private int _size = -1; 59 65 private boolean _needsClose; 66 67 private transient ArrayList _listeners; 68 69 77 public CollectionProxyDefaultImpl(PBKey brokerKey, Query query) 78 { 79 this(brokerKey, RemovalAwareCollection.class, query); 80 } 81 82 89 public CollectionProxyDefaultImpl(PBKey brokerKey, Class collClass, Query query) 90 { 91 MetadataManager mm = MetadataManager.getInstance(); 92 _perThreadDescriptorsEnabled = mm.isEnablePerThreadChanges(); 93 if (_perThreadDescriptorsEnabled) 94 { 95 final Object key = mm.getCurrentProfileKey(); 98 if (key == null) 99 { 100 throw new MetadataException("Trying to create a Collection proxy with per-thread metadata changes enabled, but no profile key."); 102 } 103 setProfileKey(key); 104 } 105 setBrokerKey(brokerKey); 106 setCollectionClass(collClass); 107 setQuery(query); 108 } 109 110 116 protected void loadProfileIfNeeded() 117 { 118 final Object key = getProfileKey(); 119 if (key != null) 120 { 121 final MetadataManager mm = MetadataManager.getInstance(); 122 if (!key.equals(mm.getCurrentProfileKey())) 123 { 124 mm.loadProfile(key); 125 } 126 } 127 } 128 129 134 public boolean isLoaded() 135 { 136 return _data != null; 137 } 138 139 145 protected synchronized int loadSize() throws PersistenceBrokerException 146 { 147 PersistenceBroker broker = getBroker(); 148 try 149 { 150 return broker.getCount(getQuery()); 151 } 152 catch (Exception ex) 153 { 154 throw new PersistenceBrokerException(ex); 155 } 156 finally 157 { 158 releaseBroker(broker); 159 } 160 } 161 162 167 protected synchronized void setSize(int size) 168 { 169 _size = size; 170 } 171 172 178 protected Collection loadData() throws PersistenceBrokerException 179 { 180 PersistenceBroker broker = getBroker(); 181 try 182 { 183 Collection result; 184 185 if (_data != null) { 187 result = _data; 188 } 189 else if (_size != 0) 190 { 191 result = (Collection ) broker.getCollectionByQuery(getCollectionClass(), getQuery()); 194 } 195 else 196 { 197 result = (Collection )getCollectionClass().newInstance(); 198 } 199 return result; 200 } 201 catch (Exception ex) 202 { 203 throw new PersistenceBrokerException(ex); 204 } 205 finally 206 { 207 releaseBroker(broker); 208 } 209 } 210 211 214 protected void beforeLoading() 215 { 216 if (_listeners != null) 217 { 218 CollectionProxyListener listener; 219 220 if (_perThreadDescriptorsEnabled) { 221 loadProfileIfNeeded(); 222 } 223 for (int idx = _listeners.size() - 1; idx >= 0; idx--) 224 { 225 listener = (CollectionProxyListener)_listeners.get(idx); 226 listener.beforeLoading(this); 227 } 228 } 229 } 230 231 234 protected void afterLoading() 235 { 236 if (_listeners != null) 237 { 238 CollectionProxyListener listener; 239 240 if (_perThreadDescriptorsEnabled) { 241 loadProfileIfNeeded(); 242 } 243 for (int idx = _listeners.size() - 1; idx >= 0; idx--) 244 { 245 listener = (CollectionProxyListener)_listeners.get(idx); 246 listener.afterLoading(this); 247 } 248 } 249 } 250 251 254 public int size() 255 { 256 if (isLoaded()) 257 { 258 return getData().size(); 259 } 260 else 261 { 262 if (_size < 0) 263 { 264 _size = loadSize(); 265 } 266 return _size; 267 } 268 } 269 270 273 public boolean isEmpty() 274 { 275 return size() == 0; 276 } 277 278 281 public boolean contains(Object o) 282 { 283 return getData().contains(o); 284 } 285 286 289 public Iterator iterator() 290 { 291 return getData().iterator(); 292 } 293 294 297 public Object [] toArray() 298 { 299 return getData().toArray(); 300 } 301 302 305 public Object [] toArray(Object [] a) 306 { 307 return getData().toArray(a); 308 } 309 310 313 public boolean add(Object o) 314 { 315 return getData().add(o); 316 } 317 318 321 public boolean remove(Object o) 322 { 323 return getData().remove(o); 324 } 325 326 329 public boolean containsAll(Collection c) 330 { 331 return getData().containsAll(c); 332 } 333 334 337 public boolean addAll(Collection c) 338 { 339 return getData().addAll(c); 340 } 341 342 345 public boolean removeAll(Collection c) 346 { 347 return getData().removeAll(c); 348 } 349 350 353 public boolean retainAll(Collection c) 354 { 355 return getData().retainAll(c); 356 } 357 358 363 public void clear() 364 { 365 Class collClass = getCollectionClass(); 366 367 if (IRemovalAwareCollection.class.isAssignableFrom(collClass)) 370 { 371 getData().clear(); 372 } 373 else 374 { 375 Collection coll; 376 try 379 { 380 coll = (Collection ) collClass.newInstance(); 381 } 382 catch (Exception e) 383 { 384 coll = new ArrayList (); 385 } 386 387 setData(coll); 388 } 389 _size = 0; 390 } 391 392 397 public Query getQuery() 398 { 399 return _query; 400 } 401 402 407 protected void setQuery(Query query) 408 { 409 _query = query; 410 } 411 412 415 protected synchronized void releaseBroker(PersistenceBroker broker) 416 { 417 423 if (broker != null && _needsClose) 424 { 425 _needsClose = false; 426 broker.close(); 427 } 428 } 429 430 435 protected synchronized PersistenceBroker getBroker() throws PBFactoryException 436 { 437 474 if (_perThreadDescriptorsEnabled) 475 { 476 loadProfileIfNeeded(); 477 } 478 479 PersistenceBroker broker; 480 if (getBrokerKey() == null) 481 { 482 487 throw new OJBRuntimeException("Can't find associated PBKey. Need PBKey to obtain a valid" + 488 "PersistenceBroker instance from intern resources."); 489 } 490 broker = PersistenceBrokerThreadMapping.currentPersistenceBroker(getBrokerKey()); 492 if (broker == null || broker.isClosed()) 494 { 495 broker = PersistenceBrokerFactory.createPersistenceBroker(getBrokerKey()); 496 _needsClose = true; 499 } 500 return broker; 501 } 502 503 508 public synchronized Collection getData() 509 { 510 if (!isLoaded()) 511 { 512 beforeLoading(); 513 setData(loadData()); 514 afterLoading(); 515 } 516 return _data; 517 } 518 519 524 public void setData(Collection data) 525 { 526 _data = data; 527 } 528 529 534 public Class getCollectionClass() 535 { 536 return _collectionClass; 537 } 538 539 544 protected void setCollectionClass(Class collClass) 545 { 546 _collectionClass = collClass; 547 } 548 549 552 public void ojbAdd(Object anObject) 553 { 554 add(anObject); 555 } 556 557 560 public void ojbAddAll(ManageableCollection otherCollection) 561 { 562 addAll((CollectionProxyDefaultImpl)otherCollection); 563 } 564 565 568 public Iterator ojbIterator() 569 { 570 return iterator(); 571 } 572 573 576 public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException 577 { 578 Collection c = getData(); 581 582 if (c instanceof ManageableCollection) 583 { 584 ((ManageableCollection)c).afterStore(broker); 585 } 586 } 587 588 593 public PBKey getBrokerKey() 594 { 595 return _brokerKey; 596 } 597 598 603 protected void setBrokerKey(PBKey brokerKey) 604 { 605 _brokerKey = brokerKey; 606 } 607 608 613 protected Object getProfileKey() 614 { 615 return _profileKey; 616 } 617 618 623 public void setProfileKey(Object profileKey) 624 { 625 _profileKey = profileKey; 626 } 627 628 633 public synchronized void addListener(CollectionProxyListener listener) 634 { 635 if (_listeners == null) 636 { 637 _listeners = new ArrayList (); 638 } 639 if(!_listeners.contains(listener)) 641 { 642 _listeners.add(listener); 643 } 644 } 645 646 651 public synchronized void removeListener(CollectionProxyListener listener) 652 { 653 if (_listeners != null) 654 { 655 _listeners.remove(listener); 656 } 657 } 658 659 } 660 | Popular Tags |