1 package org.apache.ojb.broker.core; 2 3 17 18 import java.lang.reflect.Constructor ; 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.Enumeration ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Set ; 26 27 import org.apache.commons.lang.ObjectUtils; 28 import org.apache.ojb.broker.Identity; 29 import org.apache.ojb.broker.IdentityFactory; 30 import org.apache.ojb.broker.ManageableCollection; 31 import org.apache.ojb.broker.MtoNImplementor; 32 import org.apache.ojb.broker.OptimisticLockException; 33 import org.apache.ojb.broker.PBKey; 34 import org.apache.ojb.broker.PBState; 35 import org.apache.ojb.broker.PersistenceBrokerException; 36 import org.apache.ojb.broker.TransactionAbortedException; 37 import org.apache.ojb.broker.TransactionInProgressException; 38 import org.apache.ojb.broker.TransactionNotInProgressException; 39 import org.apache.ojb.broker.accesslayer.ChainingIterator; 40 import org.apache.ojb.broker.accesslayer.ConnectionManagerFactory; 41 import org.apache.ojb.broker.accesslayer.ConnectionManagerIF; 42 import org.apache.ojb.broker.accesslayer.JdbcAccess; 43 import org.apache.ojb.broker.accesslayer.JdbcAccessFactory; 44 import org.apache.ojb.broker.accesslayer.OJBIterator; 45 import org.apache.ojb.broker.accesslayer.PagingIterator; 46 import org.apache.ojb.broker.accesslayer.PkEnumeration; 47 import org.apache.ojb.broker.accesslayer.RelationshipPrefetcherFactory; 48 import org.apache.ojb.broker.accesslayer.StatementManagerFactory; 49 import org.apache.ojb.broker.accesslayer.StatementManagerIF; 50 import org.apache.ojb.broker.accesslayer.sql.SqlGenerator; 51 import org.apache.ojb.broker.accesslayer.sql.SqlGeneratorFactory; 52 import org.apache.ojb.broker.cache.MaterializationCache; 53 import org.apache.ojb.broker.cache.ObjectCache; 54 import org.apache.ojb.broker.cache.ObjectCacheFactory; 55 import org.apache.ojb.broker.cache.ObjectCacheInternal; 56 import org.apache.ojb.broker.core.proxy.AbstractProxyFactory; 57 import org.apache.ojb.broker.core.proxy.CollectionProxy; 58 import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl; 59 import org.apache.ojb.broker.core.proxy.IndirectionHandler; 60 import org.apache.ojb.broker.core.proxy.ProxyFactory; 61 import org.apache.ojb.broker.core.proxy.VirtualProxy; 62 import org.apache.ojb.broker.metadata.ClassDescriptor; 63 import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException; 64 import org.apache.ojb.broker.metadata.CollectionDescriptor; 65 import org.apache.ojb.broker.metadata.DescriptorRepository; 66 import org.apache.ojb.broker.metadata.FieldDescriptor; 67 import org.apache.ojb.broker.metadata.MetadataManager; 68 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor; 69 import org.apache.ojb.broker.metadata.fieldaccess.PersistentField; 70 import org.apache.ojb.broker.query.Query; 71 import org.apache.ojb.broker.query.QueryByIdentity; 72 import org.apache.ojb.broker.query.QueryBySQL; 73 import org.apache.ojb.broker.util.BrokerHelper; 74 import org.apache.ojb.broker.util.IdentityArrayList; 75 import org.apache.ojb.broker.util.ObjectModification; 76 import org.apache.ojb.broker.util.logging.Logger; 77 import org.apache.ojb.broker.util.logging.LoggerFactory; 78 import org.apache.ojb.broker.util.sequence.SequenceManager; 79 import org.apache.ojb.broker.util.sequence.SequenceManagerFactory; 80 81 95 public class PersistenceBrokerImpl extends PersistenceBrokerAbstractImpl implements PBState 96 { 97 private Logger logger = LoggerFactory.getLogger(PersistenceBrokerImpl.class); 98 99 protected PersistenceBrokerFactoryIF pbf; 100 protected BrokerHelper brokerHelper; 101 protected MtoNBroker mtoNBroker; 102 protected QueryReferenceBroker referencesBroker; 103 104 107 private boolean isClosed; 108 111 private boolean inTransaction; 112 116 private boolean managed; 117 private MaterializationCache objectCache; 118 121 private JdbcAccess dbAccess; 122 125 private DescriptorRepository descriptorRepository = null; 126 private ConnectionManagerIF connectionManager = null; 127 private SequenceManager sequenceManager = null; 128 private StatementManagerIF statementManager = null; 129 private SqlGenerator sqlGenerator; 130 private IdentityFactory identityFactory; 131 private RelationshipPrefetcherFactory relationshipPrefetcherFactory; 132 private ProxyFactory proxyFactory; 133 private PBKey pbKey; 134 135 140 145 private List nowStoring = new IdentityArrayList(); 146 147 152 160 private List markedForDelete = new IdentityArrayList(); 161 162 165 174 private Set deletedDuringTransaction = new HashSet (); 175 176 179 public PersistenceBrokerImpl(PBKey key, PersistenceBrokerFactoryIF pbf) 180 { 181 refresh(); 182 if(key == null) throw new PersistenceBrokerException("Could not instantiate broker with PBKey 'null'"); 183 this.pbf = pbf; 184 this.pbKey = key; 185 188 brokerHelper = new BrokerHelper(this); 189 connectionManager = ConnectionManagerFactory.getInstance().createConnectionManager(this); 190 196 objectCache = ObjectCacheFactory.getInstance().createObjectCache(this); 197 sequenceManager = SequenceManagerFactory.getSequenceManager(this); 198 dbAccess = JdbcAccessFactory.getInstance().createJdbcAccess(this); 199 statementManager = StatementManagerFactory.getInstance().createStatementManager(this); 200 sqlGenerator = SqlGeneratorFactory.getInstance().createSqlGenerator( 201 connectionManager.getSupportedPlatform()); 202 mtoNBroker = new MtoNBroker(this); 203 referencesBroker = new QueryReferenceBroker(this); 204 identityFactory = new IdentityFactoryImpl(this); 205 relationshipPrefetcherFactory = new RelationshipPrefetcherFactory(this); 206 proxyFactory = AbstractProxyFactory.getProxyFactory(); 207 } 208 209 public MaterializationCache getInternalCache() 210 { 211 return objectCache; 212 } 213 214 public IdentityFactory serviceIdentity() 215 { 216 return this.identityFactory; 217 } 218 219 public SqlGenerator serviceSqlGenerator() 220 { 221 return this.sqlGenerator; 222 } 223 224 public StatementManagerIF serviceStatementManager() 225 { 226 return statementManager; 227 } 228 229 public JdbcAccess serviceJdbcAccess() 230 { 231 return dbAccess; 232 } 233 234 public ConnectionManagerIF serviceConnectionManager() 235 { 236 return connectionManager; 237 } 238 239 public SequenceManager serviceSequenceManager() 240 { 241 return this.sequenceManager; 242 } 243 244 public BrokerHelper serviceBrokerHelper() 245 { 246 return this.brokerHelper; 247 } 248 249 public ObjectCache serviceObjectCache() 250 { 251 return this.objectCache; 252 } 253 254 public QueryReferenceBroker getReferenceBroker() 255 { 256 return this.referencesBroker; 257 } 258 259 public RelationshipPrefetcherFactory getRelationshipPrefetcherFactory() 260 { 261 return relationshipPrefetcherFactory; 262 } 263 264 public boolean isClosed() 265 { 266 return this.isClosed; 267 } 268 269 public void setClosed(boolean closed) 270 { 271 if(!closed) 275 { 276 refresh(); 277 } 278 this.isClosed = closed; 279 } 280 281 285 public boolean isManaged() 286 { 287 return managed; 288 } 289 290 295 public void setManaged(boolean managed) 296 { 297 this.managed = managed; 298 } 299 300 305 public void refresh() 306 { 307 setInTransaction(false); 309 this.descriptorRepository = MetadataManager.getInstance().getRepository(); 310 } 311 312 317 public void destroy() 318 { 319 removeAllListeners(); 320 if (connectionManager != null) 321 { 322 if(connectionManager.isInLocalTransaction()) 323 { 324 connectionManager.localRollback(); 325 } 326 connectionManager.releaseConnection(); 327 } 328 this.setClosed(true); 329 330 this.descriptorRepository = null; 331 this.pbKey = null; 332 this.pbf = null; 333 this.connectionManager = null; 334 this.dbAccess = null; 335 this.objectCache = null; 336 this.sequenceManager = null; 337 this.sqlGenerator = null; 338 this.statementManager = null; 339 } 340 341 public PBKey getPBKey() 342 { 343 return pbKey; 344 } 345 346 public void setPBKey(PBKey key) 347 { 348 this.pbKey = key; 349 } 350 351 354 public boolean close() 355 { 356 360 if (isInTransaction()) 361 { 362 logger.error("Broker is still in PB-transaction, do automatic abort before close!"); 363 abortTransaction(); 364 } 365 if (logger.isDebugEnabled()) 366 { 367 logger.debug("PB.close was called: " + this); 368 } 369 try 370 { 371 fireBrokerEvent(BEFORE_CLOSE_EVENT); 372 clearRegistrationLists(); 373 referencesBroker.removePrefetchingListeners(); 374 if (connectionManager != null) 375 { 376 connectionManager.releaseConnection(); 377 387 connectionManager.setBatchMode(false); 388 } 389 } 390 finally 391 { 392 setManaged(false); 394 descriptorRepository = null; 396 removeAllListeners(); 397 this.setClosed(true); 398 } 399 return true; 400 } 401 402 408 public synchronized void abortTransaction() throws TransactionNotInProgressException 409 { 410 if(isInTransaction()) 411 { 412 fireBrokerEvent(BEFORE_ROLLBACK_EVENT); 413 setInTransaction(false); 414 clearRegistrationLists(); 415 referencesBroker.removePrefetchingListeners(); 416 422 if(connectionManager.isInLocalTransaction()) this.connectionManager.localRollback(); 423 fireBrokerEvent(AFTER_ROLLBACK_EVENT); 424 } 425 } 426 427 434 public synchronized void beginTransaction() throws TransactionInProgressException, TransactionAbortedException 435 { 436 if (isInTransaction()) 437 { 438 throw new TransactionInProgressException("PersistenceBroker is already in transaction"); 439 } 440 fireBrokerEvent(BEFORE_BEGIN_EVENT); 441 setInTransaction(true); 442 this.connectionManager.localBegin(); 443 fireBrokerEvent(AFTER_BEGIN_EVENT); 444 } 445 446 455 public synchronized void commitTransaction() throws TransactionNotInProgressException, TransactionAbortedException 456 { 457 if (!isInTransaction()) 458 { 459 throw new TransactionNotInProgressException("PersistenceBroker is NOT in transaction, can't commit"); 460 } 461 fireBrokerEvent(BEFORE_COMMIT_EVENT); 462 setInTransaction(false); 463 clearRegistrationLists(); 464 referencesBroker.removePrefetchingListeners(); 465 472 if(connectionManager.isInLocalTransaction()) 473 { 474 this.connectionManager.localCommit(); 475 } 476 fireBrokerEvent(AFTER_COMMIT_EVENT); 477 } 478 479 492 public void delete(Object obj, boolean ignoreReferences) throws PersistenceBrokerException 493 { 494 if(isTxCheck() && !isInTransaction()) 495 { 496 if(logger.isEnabledFor(Logger.ERROR)) 497 { 498 String msg = "No running PB-tx found. Please, only delete objects in context of a PB-transaction" + 499 " to avoid side-effects - e.g. when rollback of complex objects."; 500 try 501 { 502 throw new Exception ("** Delete object without active PersistenceBroker transaction **"); 503 } 504 catch(Exception e) 505 { 506 logger.error(msg, e); 507 } 508 } 509 } 510 try 511 { 512 doDelete(obj, ignoreReferences); 513 } 514 finally 515 { 516 markedForDelete.clear(); 517 } 518 } 519 520 523 public void delete(Object obj) throws PersistenceBrokerException 524 { 525 delete(obj, false); 526 } 527 528 532 private void doDelete(Object obj, boolean ignoreReferences) throws PersistenceBrokerException 533 { 534 if (obj != null) 537 { 538 obj = getProxyFactory().getRealObject(obj); 539 548 if (markedForDelete.contains(obj)) 549 { 550 return; 551 } 552 553 ClassDescriptor cld = getClassDescriptor(obj.getClass()); 554 if (!serviceBrokerHelper().assertValidPkForDelete(cld, obj)) 556 { 557 String msg = "Cannot delete object without valid PKs. " + obj; 558 logger.error(msg); 559 return; 560 } 561 562 566 markedForDelete.add(obj); 567 Identity oid = serviceIdentity().buildIdentity(cld, obj); 568 569 BEFORE_DELETE_EVENT.setTarget(obj); 571 fireBrokerEvent(BEFORE_DELETE_EVENT); 572 BEFORE_DELETE_EVENT.setTarget(null); 573 574 if (!ignoreReferences && cld.getCollectionDescriptors().size() > 0) 576 { 577 deleteCollections(obj, cld.getCollectionDescriptors()); 578 } 579 try 581 { 582 dbAccess.executeDelete(cld, obj); } 584 catch(OptimisticLockException e) 585 { 586 objectCache.remove(oid); 588 throw e; 589 } 590 591 deletedDuringTransaction.add(oid); 593 594 if (cld.getObjectReferenceDescriptors().size() > 0) 596 { 597 deleteReferences(obj, cld.getObjectReferenceDescriptors(), ignoreReferences); 598 } 599 objectCache.remove(oid); 601 602 AFTER_DELETE_EVENT.setTarget(obj); 604 fireBrokerEvent(AFTER_DELETE_EVENT); 605 AFTER_DELETE_EVENT.setTarget(null); 606 607 connectionManager.executeBatchIfNecessary(); 609 } 610 } 611 612 618 private void deleteByQuery(Query query, ClassDescriptor cld) throws PersistenceBrokerException 619 { 620 if (logger.isDebugEnabled()) 621 { 622 logger.debug("deleteByQuery " + cld.getClassNameOfObject() + ", " + query); 623 } 624 625 if (query instanceof QueryBySQL) 626 { 627 String sql = ((QueryBySQL) query).getSql(); 628 this.dbAccess.executeUpdateSQL(sql, cld); 629 } 630 else 631 { 632 if (query instanceof QueryByIdentity) 634 { 635 QueryByIdentity qbi = (QueryByIdentity) query; 636 Object oid = qbi.getExampleObject(); 637 if (!(oid instanceof Identity)) 639 { 640 oid = serviceIdentity().buildIdentity(oid); 641 } 642 query = referencesBroker.getPKQuery((Identity) oid); 643 } 644 645 if (!cld.isInterface()) 646 { 647 this.dbAccess.executeDelete(query, cld); 648 } 649 650 String lastUsedTable = cld.getFullTableName(); 652 if (cld.isExtent()) 653 { 654 Iterator extents = getDescriptorRepository().getAllConcreteSubclassDescriptors(cld).iterator(); 655 656 while (extents.hasNext()) 657 { 658 ClassDescriptor extCld = (ClassDescriptor) extents.next(); 659 660 if (!extCld.getFullTableName().equals(lastUsedTable)) 662 { 663 lastUsedTable = extCld.getFullTableName(); 664 this.dbAccess.executeDelete(query, extCld); 665 } 666 } 667 } 668 669 } 670 } 671 672 675 public void deleteByQuery(Query query) throws PersistenceBrokerException 676 { 677 ClassDescriptor cld = getClassDescriptor(query.getSearchClass()); 678 deleteByQuery(query, cld); 679 } 680 681 695 private void deleteReferences(Object obj, List listRds, boolean ignoreReferences) throws PersistenceBrokerException 696 { 697 Iterator i = listRds.iterator(); 699 while (i.hasNext()) 700 { 701 ObjectReferenceDescriptor rds = (ObjectReferenceDescriptor) i.next(); 702 if ((!ignoreReferences && rds.getCascadingDelete() == ObjectReferenceDescriptor.CASCADE_OBJECT) 703 || rds.isSuperReferenceDescriptor()) 704 { 705 Object referencedObject = rds.getPersistentField().get(obj); 706 if (referencedObject != null) 707 { 708 doDelete(referencedObject, ignoreReferences); 709 } 710 } 711 } 712 } 713 714 724 private void deleteCollections(Object obj, List listCds) throws PersistenceBrokerException 725 { 726 Iterator i = listCds.iterator(); 728 729 while (i.hasNext()) 730 { 731 CollectionDescriptor cds = (CollectionDescriptor) i.next(); 732 if(cds.getCascadingDelete() != ObjectReferenceDescriptor.CASCADE_NONE) 733 { 734 if(cds.isMtoNRelation()) 735 { 736 mtoNBroker.deleteMtoNImplementor(cds, obj); 738 } 739 744 if (cds.getCascadingDelete() == ObjectReferenceDescriptor.CASCADE_OBJECT) 745 { 746 Object col = cds.getPersistentField().get(obj); 747 if (col != null) 748 { 749 Iterator colIterator = BrokerHelper.getCollectionIterator(col); 750 while (colIterator.hasNext()) 751 { 752 doDelete(colIterator.next(), false); 753 } 754 } 755 } 756 } 757 } 758 } 759 760 764 public void store(Object obj) throws PersistenceBrokerException 765 { 766 obj = extractObjectToStore(obj); 767 if(obj == null) return; 769 770 ClassDescriptor cld = getClassDescriptor(obj.getClass()); 771 775 boolean insert = serviceBrokerHelper().hasNullPKField(cld, obj); 776 Identity oid = serviceIdentity().buildIdentity(cld, obj); 777 781 if (!insert) 782 { 783 insert = objectCache.lookup(oid) == null 784 && !serviceBrokerHelper().doesExist(cld, oid, obj); 785 } 786 store(obj, oid, cld, insert); 787 } 788 789 794 private Object extractObjectToStore(Object obj) 795 { 796 Object result = obj; 797 if(result != null) 799 { 800 result = getProxyFactory().getRealObjectIfMaterialized(obj); 803 if (result == null) 805 { 806 if(logger.isDebugEnabled()) 807 logger.debug("No materialized object could be found -> nothing to store," + 808 " object was " + ObjectUtils.identityToString(obj)); 809 } 810 } 811 return result; 812 } 813 814 828 public void store(Object obj, Identity oid, ClassDescriptor cld, boolean insert, boolean ignoreReferences) 829 { 830 if(obj == null || nowStoring.contains(obj)) 831 { 832 return; 833 } 834 835 839 if (!insert) 841 { 842 insert = deletedDuringTransaction.contains(oid); 843 } 844 845 if(isTxCheck() && !isInTransaction()) 848 { 849 if(logger.isEnabledFor(Logger.ERROR)) 850 { 851 try 852 { 853 throw new Exception ("** Try to store object without active PersistenceBroker transaction **"); 854 } 855 catch(Exception e) 856 { 857 logger.error("No running tx found, please only store in context of an PB-transaction" + 858 ", to avoid side-effects - e.g. when rollback of complex objects", e); 859 } 860 } 861 } 862 if (insert) 864 { 865 BEFORE_STORE_EVENT.setTarget(obj); 866 fireBrokerEvent(BEFORE_STORE_EVENT); 867 BEFORE_STORE_EVENT.setTarget(null); 868 } 869 else 870 { 871 BEFORE_UPDATE_EVENT.setTarget(obj); 872 fireBrokerEvent(BEFORE_UPDATE_EVENT); 873 BEFORE_UPDATE_EVENT.setTarget(null); 874 } 875 876 try 877 { 878 nowStoring.add(obj); 879 storeToDb(obj, cld, oid, insert, ignoreReferences); 880 } 881 finally 882 { 883 nowStoring.remove(obj); 885 } 886 887 888 if (insert) 890 { 891 AFTER_STORE_EVENT.setTarget(obj); 892 fireBrokerEvent(AFTER_STORE_EVENT); 893 AFTER_STORE_EVENT.setTarget(null); 894 } 895 else 896 { 897 AFTER_UPDATE_EVENT.setTarget(obj); 898 fireBrokerEvent(AFTER_UPDATE_EVENT); 899 AFTER_UPDATE_EVENT.setTarget(null); 900 } 901 904 if(deletedDuringTransaction.size() > 0) deletedDuringTransaction.remove(oid); 906 907 connectionManager.executeBatchIfNecessary(); 909 } 910 911 914 protected void store(Object obj, Identity oid, ClassDescriptor cld, boolean insert) 915 { 916 store(obj, oid, cld, insert, false); 917 } 918 919 931 private void storeReferences(Object obj, ClassDescriptor cld, boolean insert, boolean ignoreReferences) 932 { 933 Collection listRds = cld.getObjectReferenceDescriptors(); 935 if(listRds == null || listRds.size() == 0) 937 { 938 return; 939 } 940 Iterator i = listRds.iterator(); 941 while (i.hasNext()) 942 { 943 ObjectReferenceDescriptor rds = (ObjectReferenceDescriptor) i.next(); 944 949 if((!ignoreReferences && rds.getCascadingStore() != ObjectReferenceDescriptor.CASCADE_NONE) 950 || rds.isSuperReferenceDescriptor()) 951 { 952 storeAndLinkOneToOne(false, obj, cld, rds, insert); 953 } 954 } 955 } 956 957 964 private void storeAndLinkOneToOne(boolean onlyLink, Object obj, ClassDescriptor cld, 965 ObjectReferenceDescriptor rds, boolean insert) 966 { 967 Object ref = rds.getPersistentField().get(obj); 968 if (!onlyLink && rds.getCascadingStore() == ObjectReferenceDescriptor.CASCADE_OBJECT) 969 { 970 if(rds.isSuperReferenceDescriptor()) 971 { 972 ClassDescriptor superCld = rds.getClassDescriptor().getSuperClassDescriptor(); 973 Identity oid = serviceIdentity().buildIdentity(superCld, ref); 974 storeToDb(ref, superCld, oid, insert); 975 } 976 else store(ref); 977 } 978 link(obj, cld, rds, ref, insert); 979 } 980 981 988 private void storeCollections(Object obj, ClassDescriptor cld, boolean insert) throws PersistenceBrokerException 989 { 990 Collection listCods = cld.getCollectionDescriptors(); 992 if (listCods.size() == 0) 994 { 995 return; 996 } 997 Iterator i = listCods.iterator(); 998 while (i.hasNext()) 999 { 1000 CollectionDescriptor cod = (CollectionDescriptor) i.next(); 1001 1002 if (cod.getCascadingStore() != ObjectReferenceDescriptor.CASCADE_NONE) 1004 { 1005 Object referencedObjects = cod.getPersistentField().get(obj); 1006 if (cod.isMtoNRelation()) 1007 { 1008 storeAndLinkMtoN(false, obj, cod, referencedObjects, insert); 1009 } 1010 else 1011 { 1012 storeAndLinkOneToMany(false, obj, cod, referencedObjects, insert); 1013 } 1014 1015 if ((cod.getCascadingStore() == ObjectReferenceDescriptor.CASCADE_OBJECT) 1018 && (referencedObjects instanceof ManageableCollection)) 1019 { 1020 ((ManageableCollection) referencedObjects).afterStore(this); 1021 } 1022 } 1023 } 1024 } 1025 1026 1034 private void storeAndLinkMtoN(boolean onlyLink, Object obj, CollectionDescriptor cod, 1035 Object referencedObjects, boolean insert) 1036 { 1037 1043 if(insert || !(referencedObjects instanceof CollectionProxy 1044 && !((CollectionProxy) referencedObjects).isLoaded())) 1045 { 1046 if(referencedObjects == null) 1048 { 1049 referencedObjects = Collections.EMPTY_LIST; 1050 } 1051 1059 Iterator referencedObjectsIterator; 1060 1061 if(!onlyLink && cod.getCascadingStore() == ObjectReferenceDescriptor.CASCADE_OBJECT) 1062 { 1063 referencedObjectsIterator = BrokerHelper.getCollectionIterator(referencedObjects); 1064 while (referencedObjectsIterator.hasNext()) 1065 { 1066 store(referencedObjectsIterator.next()); 1067 } 1068 } 1069 1070 Collection existingMtoNKeys; 1071 if(!insert) 1072 { 1073 existingMtoNKeys = mtoNBroker.getMtoNImplementor(cod, obj); 1074 referencedObjectsIterator = BrokerHelper.getCollectionIterator(referencedObjects); 1076 mtoNBroker.deleteMtoNImplementor(cod, obj, referencedObjectsIterator, existingMtoNKeys); 1078 } 1079 else 1080 { 1081 existingMtoNKeys = Collections.EMPTY_LIST; 1082 } 1083 referencedObjectsIterator = BrokerHelper.getCollectionIterator(referencedObjects); 1085 while (referencedObjectsIterator.hasNext()) 1086 { 1087 Object refObj = referencedObjectsIterator.next(); 1088 mtoNBroker.storeMtoNImplementor(cod, obj, refObj, existingMtoNKeys); 1092 } 1093 } 1094 } 1095 1096 1106 private void storeAndLinkOneToMany(boolean linkOnly, Object obj, CollectionDescriptor cod, 1107 Object referencedObjects, boolean insert) 1108 { 1109 if(referencedObjects == null) 1110 { 1111 return; 1112 } 1113 1123 if(insert || !(referencedObjects instanceof CollectionProxyDefaultImpl 1124 && !((CollectionProxyDefaultImpl) referencedObjects).isLoaded())) 1125 { 1126 Iterator it = BrokerHelper.getCollectionIterator(referencedObjects); 1127 Object refObj; 1128 while(it.hasNext()) 1129 { 1130 refObj = it.next(); 1131 1141 if(insert || getProxyFactory().isMaterialized(refObj)) 1142 { 1143 ClassDescriptor refCld = getClassDescriptor(getProxyFactory().getRealClass(refObj)); 1144 refObj = getProxyFactory().getRealObject(refObj); 1146 link(refObj, refCld, cod, obj, insert); 1147 if(!linkOnly && cod.getCascadingStore() == ObjectReferenceDescriptor.CASCADE_OBJECT) 1149 { 1150 store(refObj); 1151 } 1152 } 1153 } 1154 } 1155 } 1156 1157 1167 public void link(Object targetObject, ClassDescriptor cld, ObjectReferenceDescriptor rds, Object referencedObject, boolean insert) 1168 { 1169 if (referencedObject == null) 1175 { 1176 1182 if(!insert) 1183 { 1184 unlinkFK(targetObject, cld, rds); 1185 } 1186 } 1187 else 1188 { 1189 setFKField(targetObject, cld, rds, referencedObject); 1190 } 1191 } 1192 1193 1201 public void unlinkFK(Object targetObject, ClassDescriptor cld, ObjectReferenceDescriptor rds) 1202 { 1203 setFKField(targetObject, cld, rds, null); 1204 } 1205 1206 1215 private void setFKField(Object targetObject, ClassDescriptor cld, ObjectReferenceDescriptor rds, Object referencedObject) 1216 { 1217 ValueContainer[] refPkValues; 1218 FieldDescriptor fld; 1219 FieldDescriptor[] objFkFields = rds.getForeignKeyFieldDescriptors(cld); 1220 if (objFkFields == null) 1221 { 1222 throw new PersistenceBrokerException("No foreign key fields defined for class '"+cld.getClassNameOfObject()+"'"); 1223 } 1224 if(referencedObject == null) 1225 { 1226 refPkValues = null; 1227 } 1228 else 1229 { 1230 Class refClass = proxyFactory.getRealClass(referencedObject); 1231 ClassDescriptor refCld = getClassDescriptor(refClass); 1232 refPkValues = brokerHelper.getKeyValues(refCld, referencedObject, false); 1233 } 1234 for (int i = 0; i < objFkFields.length; i++) 1235 { 1236 fld = objFkFields[i]; 1237 1244 if(refPkValues != null || !fld.isPrimaryKey()) 1245 { 1246 fld.getPersistentField().set(targetObject, refPkValues != null ? refPkValues[i].getValue(): null); 1247 } 1248 } 1249 } 1250 1251 1259 public void linkOneToOne(Object obj, ClassDescriptor cld, ObjectReferenceDescriptor rds, boolean insert) 1260 { 1261 storeAndLinkOneToOne(true, obj, cld, rds, true); 1262 } 1263 1264 1271 public void linkOneToMany(Object obj, CollectionDescriptor cod, boolean insert) 1272 { 1273 Object referencedObjects = cod.getPersistentField().get(obj); 1274 storeAndLinkOneToMany(true, obj, cod,referencedObjects, insert); 1275 } 1276 1277 1285 public void linkMtoN(Object obj, CollectionDescriptor cod, boolean insert) 1286 { 1287 Object referencedObjects = cod.getPersistentField().get(obj); 1288 storeAndLinkMtoN(true, obj, cod, referencedObjects, insert); 1289 } 1290 1291 public void unlinkXtoN(Object obj, CollectionDescriptor col) 1292 { 1293 if(col.isMtoNRelation()) 1294 { 1295 mtoNBroker.deleteMtoNImplementor(col, obj); 1297 } 1298 else 1299 { 1300 Object collectionObject = col.getPersistentField().get(obj); 1301 if (collectionObject != null) 1302 { 1303 Iterator colIterator = BrokerHelper.getCollectionIterator(collectionObject); 1304 ClassDescriptor cld = null; 1305 while (colIterator.hasNext()) 1306 { 1307 Object target = colIterator.next(); 1308 if(cld == null) cld = getClassDescriptor(getProxyFactory().getRealClass(target)); 1309 unlinkFK(target, cld, col); 1310 } 1311 } 1312 } 1313 } 1314 1315 1320 public void retrieveAllReferences(Object pInstance) throws PersistenceBrokerException 1321 { 1322 if (logger.isDebugEnabled()) 1323 { 1324 logger.debug("Manually retrieving all references for object " + serviceIdentity().buildIdentity(pInstance)); 1325 } 1326 ClassDescriptor cld = getClassDescriptor(pInstance.getClass()); 1327 getInternalCache().enableMaterializationCache(); 1328 Identity oid = serviceIdentity().buildIdentity(pInstance); 1330 if(getInternalCache().doLocalLookup(oid) == null) 1332 { 1333 getInternalCache().doInternalCache(oid, pInstance, MaterializationCache.TYPE_TEMP); 1334 } 1336 try 1337 { 1338 referencesBroker.retrieveReferences(pInstance, cld, true); 1339 referencesBroker.retrieveCollections(pInstance, cld, true); 1340 getInternalCache().disableMaterializationCache(); 1345 } 1346 catch(RuntimeException e) 1347 { 1348 getInternalCache().doLocalClear(); 1349 throw e; 1350 } 1351 } 1352 1353 1359 public void retrieveReference(Object pInstance, String pAttributeName) throws PersistenceBrokerException 1360 { 1361 if (logger.isDebugEnabled()) 1362 { 1363 logger.debug("Retrieving reference named ["+pAttributeName+"] on object of type ["+ 1364 pInstance.getClass().getName()+"]"); 1365 } 1366 ClassDescriptor cld = getClassDescriptor(pInstance.getClass()); 1367 CollectionDescriptor cod = cld.getCollectionDescriptorByName(pAttributeName); 1368 getInternalCache().enableMaterializationCache(); 1369 Identity oid = serviceIdentity().buildIdentity(pInstance); 1371 boolean needLocalRemove = false; 1372 if(getInternalCache().doLocalLookup(oid) == null) 1373 { 1374 getInternalCache().doInternalCache(oid, pInstance, MaterializationCache.TYPE_TEMP); 1375 needLocalRemove = true; 1376 } 1377 try 1378 { 1379 if (cod != null) 1380 { 1381 referencesBroker.retrieveCollection(pInstance, cld, cod, true); 1382 } 1383 else 1384 { 1385 ObjectReferenceDescriptor ord = cld.getObjectReferenceDescriptorByName(pAttributeName); 1386 if (ord != null) 1387 { 1388 referencesBroker.retrieveReference(pInstance, cld, ord, true); 1389 } 1390 else 1391 { 1392 throw new PersistenceBrokerException("did not find attribute " + pAttributeName + 1393 " for class " + pInstance.getClass().getName()); 1394 } 1395 } 1396 if(needLocalRemove) getInternalCache().doLocalRemove(oid); 1399 getInternalCache().disableMaterializationCache(); 1400 } 1401 catch(RuntimeException e) 1402 { 1403 getInternalCache().doLocalClear(); 1404 throw e; 1405 } 1406 } 1407 1408 1417 public void checkRefreshRelationships(Object obj, Identity oid, ClassDescriptor cld) 1418 { 1419 Iterator iter; 1420 CollectionDescriptor cds; 1421 ObjectReferenceDescriptor rds; 1422 Object tmp = getInternalCache().doLocalLookup(oid); 1424 if(tmp != null && getInternalCache().isEnabledMaterialisationCache()) 1425 { 1426 1431 return; 1432 } 1433 try 1434 { 1435 getInternalCache().enableMaterializationCache(); 1436 if(tmp == null) 1437 { 1438 getInternalCache().doInternalCache(oid, obj, MaterializationCache.TYPE_TEMP); 1439 } 1440 if(logger.isDebugEnabled()) logger.debug("Refresh relationships for " + oid); 1441 iter = cld.getCollectionDescriptors().iterator(); 1442 while (iter.hasNext()) 1443 { 1444 cds = (CollectionDescriptor) iter.next(); 1445 if (cds.isRefresh()) 1446 { 1447 referencesBroker.retrieveCollection(obj, cld, cds, false); 1448 } 1449 } 1450 iter = cld.getObjectReferenceDescriptors().iterator(); 1451 while (iter.hasNext()) 1452 { 1453 rds = (ObjectReferenceDescriptor) iter.next(); 1454 if (rds.isRefresh()) 1455 { 1456 referencesBroker.retrieveReference(obj, cld, rds, false); 1457 } 1458 } 1459 getInternalCache().disableMaterializationCache(); 1460 } 1461 catch(RuntimeException e) 1462 { 1463 getInternalCache().doLocalClear(); 1464 throw e; 1465 } 1466 } 1467 1468 1473 public ManageableCollection getCollectionByQuery(Class collectionClass, Query query) 1474 throws PersistenceBrokerException 1475 { 1476 return referencesBroker.getCollectionByQuery(collectionClass, query, false); 1477 } 1478 1479 1482 public Collection getCollectionByQuery(Query query) throws PersistenceBrokerException 1483 { 1484 return referencesBroker.getCollectionByQuery(query, false); 1485 } 1486 1487 1496 private Object getPlainDBObject(ClassDescriptor cld, Identity oid) throws ClassNotPersistenceCapableException 1497 { 1498 Object newObj = null; 1499 1500 if (!cld.isInterface()) 1502 { 1503 newObj = dbAccess.materializeObject(cld, oid); 1505 } 1506 1507 if (newObj == null && cld.isExtent()) 1510 { 1511 Iterator extents = getDescriptorRepository().getAllConcreteSubclassDescriptors(cld).iterator(); 1512 1513 while (extents.hasNext()) 1514 { 1515 ClassDescriptor extCld = (ClassDescriptor) extents.next(); 1516 1517 newObj = dbAccess.materializeObject(extCld, oid); 1518 if (newObj != null) 1519 { 1520 break; 1521 } 1522 } 1523 } 1524 return newObj; 1525 } 1526 1527 1528 1537 private Object getDBObject(Identity oid) throws ClassNotPersistenceCapableException 1538 { 1539 Class c = oid.getObjectsRealClass(); 1540 1541 if (c == null) 1542 { 1543 logger.info("Real class for used Identity object is 'null', use top-level class instead"); 1544 c = oid.getObjectsTopLevelClass(); 1545 } 1546 1547 ClassDescriptor cld = getClassDescriptor(c); 1548 Object newObj = getPlainDBObject(cld, oid); 1549 1550 if (newObj != null) 1552 { 1553 if (oid.getObjectsRealClass() == null) 1554 { 1555 oid.setObjectsRealClass(newObj.getClass()); 1556 } 1557 1558 1562 synchronized (newObj) 1563 { 1564 objectCache.enableMaterializationCache(); 1565 try 1566 { 1567 objectCache.doInternalCache(oid, newObj, ObjectCacheInternal.TYPE_NEW_MATERIALIZED); 1570 1571 1584 ClassDescriptor newObjCld = getClassDescriptor(newObj.getClass()); 1585 final boolean unforced = false; 1587 1588 referencesBroker.retrieveReferences(newObj, newObjCld, unforced); 1590 referencesBroker.retrieveCollections(newObj, newObjCld, unforced); 1592 objectCache.disableMaterializationCache(); 1593 } 1594 catch(RuntimeException e) 1595 { 1596 objectCache.doLocalClear(); 1597 throw e; 1598 } 1599 } 1600 } 1601 1602 return newObj; 1603 } 1604 1605 1611 public Iterator getIteratorByQuery(Query query) throws PersistenceBrokerException 1612 { 1613 Class itemClass = query.getSearchClass(); 1614 ClassDescriptor cld = getClassDescriptor(itemClass); 1615 return getIteratorFromQuery(query, cld); 1616 } 1617 1618 1625 protected OJBIterator getIteratorFromQuery(Query query, ClassDescriptor cld) throws PersistenceBrokerException 1626 { 1627 RsIteratorFactory factory = RsIteratorFactoryImpl.getInstance(); 1628 OJBIterator result = getRsIteratorFromQuery(query, cld, factory); 1629 1630 if (query.usePaging()) 1631 { 1632 result = new PagingIterator(result, query.getStartAtIndex(), query.getEndAtIndex()); 1633 } 1634 return result; 1635 } 1636 1637 public Object getObjectByIdentity(Identity id) throws PersistenceBrokerException 1638 { 1639 objectCache.enableMaterializationCache(); 1640 Object result = null; 1641 try 1642 { 1643 result = doGetObjectByIdentity(id); 1644 objectCache.disableMaterializationCache(); 1645 } 1646 catch(RuntimeException e) 1647 { 1648 objectCache.doLocalClear(); 1650 throw e; 1651 } 1652 return result; 1653 } 1654 1655 1662 public Object doGetObjectByIdentity(Identity id) throws PersistenceBrokerException 1663 { 1664 if (logger.isDebugEnabled()) logger.debug("getObjectByIdentity " + id); 1665 1666 Object obj = objectCache.lookup(id); 1668 if (obj == null) 1670 { 1671 obj = getDBObject(id); 1672 } 1673 else 1674 { 1675 ClassDescriptor cld = getClassDescriptor(obj.getClass()); 1676 if (cld.isAlwaysRefresh()) 1678 { 1679 refreshInstance(obj, id, cld); 1680 } 1681 checkRefreshRelationships(obj, id, cld); 1683 } 1684 1685 AFTER_LOOKUP_EVENT.setTarget(obj); 1687 fireBrokerEvent(AFTER_LOOKUP_EVENT); 1688 AFTER_LOOKUP_EVENT.setTarget(null); 1689 1690 return obj; 1692 } 1693 1694 1703 private void refreshInstance(Object cachedInstance, Identity oid, ClassDescriptor cld) 1704 { 1705 Object freshInstance = getPlainDBObject(cld, oid); 1707 1708 FieldDescriptor[] fields = cld.getFieldDescriptions(); 1710 FieldDescriptor fmd; 1711 PersistentField fld; 1712 for (int i = 0; i < fields.length; i++) 1713 { 1714 fmd = fields[i]; 1715 fld = fmd.getPersistentField(); 1716 fld.set(cachedInstance, fld.get(freshInstance)); 1717 } 1718 } 1719 1720 1724 public Object getObjectByQuery(Query query) throws PersistenceBrokerException 1725 { 1726 Object result = null; 1727 if (query instanceof QueryByIdentity) 1728 { 1729 Object obj = query.getExampleObject(); 1731 if (obj instanceof Identity) 1732 { 1733 Identity oid = (Identity) obj; 1734 result = getObjectByIdentity(oid); 1735 } 1736 else 1737 { 1738 if (!serviceBrokerHelper().hasNullPKField(getClassDescriptor(obj.getClass()), obj)) 1740 { 1741 Identity oid = serviceIdentity().buildIdentity(obj); 1742 result = getObjectByIdentity(oid); 1743 } 1744 } 1745 } 1746 else 1747 { 1748 Class itemClass = query.getSearchClass(); 1749 ClassDescriptor cld = getClassDescriptor(itemClass); 1750 1754 OJBIterator it = getIteratorFromQuery(query, cld); 1755 1766 try 1767 { 1768 while (result==null && it.hasNext()) 1769 { 1770 result = it.next(); 1771 } 1772 } finally 1774 { 1775 if(it != null) it.releaseDbResources(); 1776 } 1777 } 1778 return result; 1779 } 1780 1781 1790 public Enumeration getPKEnumerationByQuery(Class primaryKeyClass, Query query) throws PersistenceBrokerException 1791 { 1792 if (logger.isDebugEnabled()) logger.debug("getPKEnumerationByQuery " + query); 1793 1794 query.setFetchSize(1); 1795 ClassDescriptor cld = getClassDescriptor(query.getSearchClass()); 1796 return new PkEnumeration(query, cld, primaryKeyClass, this); 1797 } 1798 1799 1807 public void store(Object obj, ObjectModification mod) throws PersistenceBrokerException 1808 { 1809 obj = extractObjectToStore(obj); 1810 if (obj == null) 1812 { 1813 return; 1814 } 1815 1816 ClassDescriptor cld = getClassDescriptor(obj.getClass()); 1817 Identity oid = serviceIdentity().buildIdentity(cld, obj); 1819 if (mod.needsInsert()) 1821 { 1822 store(obj, oid, cld, true); 1823 } 1824 else if (mod.needsUpdate()) 1825 { 1826 store(obj, oid, cld, false); 1827 } 1828 1832 else 1833 { 1834 storeCollections(obj, cld, mod.needsInsert()); 1836 } 1837 } 1838 1839 1849 private void storeToDb(Object obj, ClassDescriptor cld, Identity oid, boolean insert, boolean ignoreReferences) 1850 { 1851 storeReferences(obj, cld, insert, ignoreReferences); 1853 1854 Object [] pkValues = oid.getPrimaryKeyValues(); 1855 if (!serviceBrokerHelper().assertValidPksForStore(cld.getPkFields(), pkValues)) 1856 { 1857 pkValues = serviceBrokerHelper().getKeyValues(cld, obj); 1860 if (!serviceBrokerHelper().assertValidPksForStore(cld.getPkFields(), pkValues)) 1861 { 1862 String append = insert ? " on insert" : " on update" ; 1863 throw new PersistenceBrokerException("assertValidPkFields failed for Object of type: " + cld.getClassNameOfObject() + append); 1864 } 1865 } 1866 1867 1878 if(cld.getSuperClass() != null) 1879 { 1880 1881 ClassDescriptor superCld = getDescriptorRepository().getDescriptorFor(cld.getSuperClass()); 1882 storeToDb(obj, superCld, oid, insert); 1883 } 1886 1887 if (insert) 1890 { 1891 dbAccess.executeInsert(cld, obj); 1892 if(oid.isTransient()) 1893 { 1894 oid = serviceIdentity().buildIdentity(cld, obj); 1896 } 1897 } 1898 else 1900 { 1901 try 1902 { 1903 dbAccess.executeUpdate(cld, obj); 1904 } 1905 catch(OptimisticLockException e) 1906 { 1907 objectCache.remove(oid); 1909 throw e; 1910 } 1911 } 1912 objectCache.doInternalCache(oid, obj, ObjectCacheInternal.TYPE_WRITE); 1915 if(!ignoreReferences) storeCollections(obj, cld, insert); 1917 } 1918 1919 1928 private void storeToDb(Object obj, ClassDescriptor cld, Identity oid, boolean insert) 1929 { 1930 storeToDb(obj, cld, oid, insert, false); 1931 } 1932 1933 1937 public boolean isInTransaction() 1938 { 1939 return inTransaction; 1941 } 1942 1943 public void setInTransaction(boolean inTransaction) 1944 { 1945 this.inTransaction = inTransaction; 1946 } 1947 1948 1951 public void removeFromCache(Object objectOrIdentity) throws PersistenceBrokerException 1952 { 1953 Identity identity; 1954 if (objectOrIdentity instanceof Identity) 1955 { 1956 identity = (Identity)objectOrIdentity; 1957 } 1958 else 1959 { 1960 identity = serviceIdentity().buildIdentity(objectOrIdentity); 1961 } 1962 objectCache.remove(identity); 1963 } 1964 1965 1970 public ClassDescriptor getClassDescriptor(Class clazz) throws PersistenceBrokerException 1971 { 1972 return descriptorRepository.getDescriptorFor(clazz); 1973 } 1974 1975 public boolean hasClassDescriptor(Class clazz) 1976 { 1977 return descriptorRepository.hasDescriptorFor(clazz); 1978 } 1979 1980 1987 public void clearCache() throws PersistenceBrokerException 1988 { 1989 objectCache.clear(); 1990 } 1991 1992 1995 public Class getTopLevelClass(Class clazz) throws PersistenceBrokerException 1996 { 1997 try 1998 { 1999 return descriptorRepository.getTopLevelClass(clazz); 2000 } 2001 catch (ClassNotPersistenceCapableException e) 2002 { 2003 throw new PersistenceBrokerException(e); 2004 } 2005 } 2006 2007 2010 public int getCount(Query query) throws PersistenceBrokerException 2011 { 2012 Query countQuery = serviceBrokerHelper().getCountQuery(query); 2013 Iterator iter; 2014 int result = 0; 2015 2016 if (logger.isDebugEnabled()) logger.debug("getCount " + countQuery.getSearchClass() + ", " + countQuery); 2017 2018 iter = getReportQueryIteratorByQuery(countQuery); 2019 try 2020 { 2021 while (iter.hasNext()) 2022 { 2023 Object [] row = (Object []) iter.next(); 2024 result += ((Number ) row[0]).intValue(); 2025 } 2026 } 2027 finally 2028 { 2029 if (iter instanceof OJBIterator) 2030 { 2031 ((OJBIterator) iter).releaseDbResources(); 2032 } 2033 } 2034 2035 return result; 2036 } 2037 2038 2044 public Iterator getReportQueryIteratorByQuery(Query query) throws PersistenceBrokerException 2045 { 2046 ClassDescriptor cld = getClassDescriptor(query.getSearchClass()); 2047 return getReportQueryIteratorFromQuery(query, cld); 2048 } 2049 2050 2058 private OJBIterator getRsIteratorFromQuery(Query query, ClassDescriptor cld, RsIteratorFactory factory) 2059 throws PersistenceBrokerException 2060 { 2061 query.setFetchSize(1); 2062 if (query instanceof QueryBySQL) 2063 { 2064 if(logger.isDebugEnabled()) logger.debug("Creating SQL-RsIterator for class ["+cld.getClassNameOfObject()+"]"); 2065 return factory.createRsIterator((QueryBySQL) query, cld, this); 2066 } 2067 2068 if (!cld.isExtent() || !query.getWithExtents()) 2069 { 2070 if(logger.isDebugEnabled()) logger.debug("Creating RsIterator for class ["+cld.getClassNameOfObject()+"]"); 2072 2073 return factory.createRsIterator(query, cld, this); 2074 } 2075 2076 if(logger.isDebugEnabled()) logger.debug("Creating ChainingIterator for class ["+cld.getClassNameOfObject()+"]"); 2077 2078 ChainingIterator chainingIter = new ChainingIterator(); 2079 2080 if (!cld.isInterface()) 2082 { 2083 if(logger.isDebugEnabled()) logger.debug("Adding RsIterator for class ["+cld.getClassNameOfObject()+"] to ChainingIterator"); 2084 2085 chainingIter.addIterator(factory.createRsIterator(query, cld, this)); 2086 } 2087 2088 Iterator extents = getDescriptorRepository().getAllConcreteSubclassDescriptors(cld).iterator(); 2089 while (extents.hasNext()) 2090 { 2091 ClassDescriptor extCld = (ClassDescriptor) extents.next(); 2092 2093 if (chainingIter.containsIteratorForTable(extCld.getFullTableName())) 2095 { 2096 if(logger.isDebugEnabled()) logger.debug("Skipping class ["+extCld.getClassNameOfObject()+"]"); 2097 } 2098 else 2099 { 2100 if(logger.isDebugEnabled()) logger.debug("Adding RsIterator of class ["+extCld.getClassNameOfObject()+"] to ChainingIterator"); 2101 2102 chainingIter.addIterator(factory.createRsIterator(query, extCld, this)); 2104 } 2105 } 2106 2107 return chainingIter; 2108 } 2109 2110 2117 private OJBIterator getReportQueryIteratorFromQuery(Query query, ClassDescriptor cld) throws PersistenceBrokerException 2118 { 2119 RsIteratorFactory factory = ReportRsIteratorFactoryImpl.getInstance(); 2120 OJBIterator result = getRsIteratorFromQuery(query, cld, factory); 2121 2122 if (query.usePaging()) 2123 { 2124 result = new PagingIterator(result, query.getStartAtIndex(), query.getEndAtIndex()); 2125 } 2126 2127 return result; 2128 } 2129 2130 2133 public org.odbms.Query query() 2134 { 2135 return new org.apache.ojb.soda.QueryImpl(this); 2136 } 2137 2138 2141 public DescriptorRepository getDescriptorRepository() 2142 { 2143 return descriptorRepository; 2144 } 2145 2146 protected void finalize() 2147 { 2148 if (!isClosed) 2149 { 2150 close(); 2151 } 2152 } 2153 2154 2157 private void clearRegistrationLists() 2158 { 2159 nowStoring.clear(); 2160 objectCache.doLocalClear(); 2161 deletedDuringTransaction.clear(); 2162 2167 mtoNBroker.reset(); 2168 } 2169 2170 2171 2172 2175 public void deleteMtoNImplementor(MtoNImplementor m2nImpl) throws PersistenceBrokerException 2176 { 2177 mtoNBroker.deleteMtoNImplementor(m2nImpl); 2178 } 2179 2180 2183 public void addMtoNImplementor(MtoNImplementor m2n) throws PersistenceBrokerException 2184 { 2185 mtoNBroker.storeMtoNImplementor(m2n); 2186 } 2187 2188 public ProxyFactory getProxyFactory() { 2189 return proxyFactory; 2190 } 2191 2192 2201 public Object createProxy(Class baseClassForProxy, Identity realSubjectsIdentity) 2202 { 2203 try 2204 { 2205 IndirectionHandler handler = getProxyFactory().createIndirectionHandler(pbKey, realSubjectsIdentity); 2207 2208 if (VirtualProxy.class.isAssignableFrom(baseClassForProxy)) 2210 { 2211 Constructor constructor = baseClassForProxy.getDeclaredConstructor(new Class []{ IndirectionHandler.class }); 2212 return constructor.newInstance(new Object []{ handler }); 2213 } 2214 else 2215 { 2216 return getProxyFactory().createProxy(baseClassForProxy,handler); 2217 } 2218 2219 2220 } 2221 catch (Exception ex) 2222 { 2223 throw new PersistenceBrokerException("Unable to create proxy using class:"+baseClassForProxy.getName(), ex); 2224 } 2225 } 2226 2227 2228} 2229 | Popular Tags |