ref #9204 revert changes to AbstractPersistentCollection, only keep new serialVersion...
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / org / hibernate / collection / internal / AbstractPersistentCollection.java
index 7f2110d38fa0ab8cebc2ec6a6c1e27eefadb9751..18101250c9bf197a9495353f5fa9e3fe94be4945 100644 (file)
@@ -60,19 +60,43 @@ import org.hibernate.type.Type;
 import org.jboss.logging.Logger;
 
 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteConfiguration;
+import eu.etaxonomy.cdm.cache.ProxyUtils;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.taxeditor.remoting.CdmEagerLoadingException;
-import eu.etaxonomy.taxeditor.remoting.cache.ProxyUtils;
 import eu.etaxonomy.taxeditor.service.ICachedCommonService;
 
 /**
  * Base class implementing {@link org.hibernate.collection.spi.PersistentCollection}
  *
+ * This is an extended copy of the original class from hibernate. It has been extended to
+ * allow making remote service calls to spring httpinvoker services (see section at the bottom
+ * of this class).
+ *
  * @author Gavin King
+ * @author Cherian Mathew
  */
 public abstract class AbstractPersistentCollection implements Serializable, PersistentCollection {
        private static final Logger log = Logger.getLogger( AbstractPersistentCollection.class );
 
-       private static final long serialVersionUID = -7238232378593030571L;
+       /**
+        * <b>IMPORTANT:</b><br>
+        * This serialVersionUID must be kept in sync with the serialVersionUID which is generated
+        * on the fly for serialized AbstractPersistentCollection objects coming from the httpInvoker
+        * service.
+        * This is most probably necessary after updating hibernate to a newer version. In any case
+        * it the need for updating this <code>serialVersionUID</code> becomes obvious when the attempt
+        * to connect to the server side fails with an  <code>InvalidClassException</code>:
+        *
+        * <pre>
+        * java.io.InvalidClassException: org.hibernate.collection.internal.AbstractPersistentCollection;
+        * local class incompatible:
+        * stream classdesc serialVersionUID = 2742261122392386159,
+        * local class serialVersionUID = -7238232378593030571
+        * </pre>
+        * The correct <code>serialVersionUID</code> is the <code>stream classdesc serialVersionUID</code>
+        * from the error message.
+        */
+       private static final long serialVersionUID = 7094296207968006972L;
 
        private transient SessionImplementor session;
        private boolean initialized;
@@ -235,11 +259,11 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                if ( isTempSession ) {
                        // TODO: On the next major release, add an
                        // 'isJTA' or 'getTransactionFactory' method to Session.
-                       isJTA = session.getTransactionCoordinator()
+                       /*isJTA = session.getTransactionCoordinator()
                                        .getTransactionContext().getTransactionEnvironment()
                                        .getTransactionFactory()
-                                       .compatibleWithJtaSynchronization();
-
+                                       .compatibleWithJtaSynchronization();*/
+                       isJTA = session.getTransactionCoordinator().getTransactionCoordinatorBuilder().isJta();
                        if ( !isJTA ) {
                                // Explicitly handle the transactions only if we're not in
                                // a JTA environment.  A lazy loading temporary session can
@@ -1271,7 +1295,13 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                this.owner = owner;
        }
 
-       /** ------ Below is section of code which makes remote service calls ----- */
+/** #################################################################### 
+    
+    ADDED PART: Below is section of code which makes remote service calls 
+    
+    #################################################################### */
+    
+       
        // The affected methods are :
        // initialize(final boolean writing)
        // readSize()
@@ -1287,7 +1317,6 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                configuration = conf;
        }
 
-
        private void remoteInitialize() {
 
                if (getOwner() != null && !initialized) {
@@ -1295,9 +1324,14 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                        try {
                                String role = getRole();
                                String fieldName = role.substring(role.lastIndexOf(".") + 1);
-                               log.info("--> Remote Lazy Initializing Collection " + getRole() + " , key : " + getKey() + " , field : " + fieldName);
+                               log.info("--> Remote Lazy Initializing Collection " + getRole() + " , owner : " + getOwner().getClass() + "/" + getKey() + " , field : " + fieldName);
                                Object owner = getOwner();
-
+                               CdmBase cdmBase;
+                               if(owner instanceof CdmBase) {
+                                   cdmBase = (CdmBase)owner;
+                               } else {
+                                   throw new HibernateException("Owner of persistent collection is not a cdm entity");
+                               }
                                if(configuration == null) {
                                        throw new HibernateException("CdmApplicationRemoteConfiguration not initialized (null)");
                                }
@@ -1306,14 +1340,18 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                                        throw new HibernateException("commonService not initialized (null)");
                                }
 
-                               Object obj = ProxyUtils.deproxy(cachedCommonService.initializeCollection(this));
+                               //Object obj = ProxyUtils.deproxyIfInitialized(cachedCommonService.initializeCollection(this));
+                               Object obj = ProxyUtils.deproxyIfInitialized(cachedCommonService.initializeCollection(cdmBase.getUuid(), fieldName));
+                               if(ProxyUtils.isUninitializedProxy(obj)) {
+                                   throw new HibernateException("Persistent Collection initialized but is still a proxy");
+                               }
                                afterInitialize();
 
                                Class<?> clazz = getClass();
                                if (clazz != null) {
                                        //CollectionField cf = cachedCommonService.getCollectionField(col);
                                        //cachedCommonService.updatePersistentCollection(cf);
-                                   Object collectionType = ProxyUtils.getCollectionType(obj);
+                                   Object collectionType = ProxyUtils.getCollectionType(obj, clazz);
                                        Field field = clazz.getDeclaredField(collectionType.toString());
                                        field.setAccessible(true);
                                        field.set(this, obj);
@@ -1325,8 +1363,4 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                        }
                }
        }
-
-
-
-}
-
+}
\ No newline at end of file