setting application configuration and boolean remoting flag (bean) in hibernate proxy...
authorCherian Mathew <c.mathew@bgbm.org>
Thu, 13 Mar 2014 20:23:45 +0000 (20:23 +0000)
committerCherian Mathew <c.mathew@bgbm.org>
Thu, 13 Mar 2014 20:23:45 +0000 (20:23 +0000)
eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java
eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java
eu.etaxonomy.taxeditor.cdmlib/src/main/resources/eu/etaxonomy/cdm/localApplicationContext.xml
eu.etaxonomy.taxeditor.cdmlib/src/main/resources/eu/etaxonomy/cdm/remotingApplicationContext.xml
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/store/CdmStore.java

index 90451f8717b2a366dc29022367ff8eceb318c875..c67e5e2ad8db2a2842e917146e5c9d729086c952 100644 (file)
@@ -148,34 +148,41 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                                return true;
                        }
                        else {
                                return true;
                        }
                        else {
-                               boolean isExtraLazy = withTemporarySessionIfNeeded(
-                                               new LazyInitializationWork<Boolean>() {
-                                                       @Override
-                                                       public Boolean doWork() {
-                                                               CollectionEntry entry = session.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection.this );
-
-                                                               if ( entry != null ) {
-                                                                       CollectionPersister persister = entry.getLoadedPersister();
-                                                                       if ( persister.isExtraLazy() ) {
-                                                                               if ( hasQueuedOperations() ) {
-                                                                                       session.flush();
+                               // In remoting we are sure that session is null
+                               // both when using property paths and switching off conversations
+                               if(session == null && remoting) {                               
+                                       log.info("--> readSize, of " + getRole() + " with key " + getKey());
+                                       read();                                                         
+                               } else {
+                                       boolean isExtraLazy = withTemporarySessionIfNeeded(
+                                                       new LazyInitializationWork<Boolean>() {
+                                                               @Override
+                                                               public Boolean doWork() {
+                                                                       CollectionEntry entry = session.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection.this );
+
+                                                                       if ( entry != null ) {
+                                                                               CollectionPersister persister = entry.getLoadedPersister();
+                                                                               if ( persister.isExtraLazy() ) {
+                                                                                       if ( hasQueuedOperations() ) {
+                                                                                               session.flush();
+                                                                                       }
+                                                                                       cachedSize = persister.getSize( entry.getLoadedKey(), session );
+                                                                                       return true;
+                                                                               }
+                                                                               else {
+                                                                                       read();
                                                                                }
                                                                                }
-                                                                               cachedSize = persister.getSize( entry.getLoadedKey(), session );
-                                                                               return true;
                                                                        }
                                                                        }
-                                                                       else {
-                                                                               read();
+                                                                       else{
+                                                                               throwLazyInitializationExceptionIfNotConnected();
                                                                        }
                                                                        }
+                                                                       return false;
                                                                }
                                                                }
-                                                               else{
-                                                                       throwLazyInitializationExceptionIfNotConnected();
-                                                               }
-                                                               return false;
                                                        }
                                                        }
-                                               }
-                               );
-                               if ( isExtraLazy ) {
-                                       return true;
+                                                       );
+                                       if ( isExtraLazy ) {
+                                               return true;
+                                       }
                                }
                        }
                }
                                }
                        }
                }
@@ -276,27 +283,34 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
 
        protected Boolean readIndexExistence(final Object index) {
                if ( !initialized ) {
 
        protected Boolean readIndexExistence(final Object index) {
                if ( !initialized ) {
-                       Boolean extraLazyExistenceCheck = withTemporarySessionIfNeeded(
-                                       new LazyInitializationWork<Boolean>() {
-                                               @Override
-                                               public Boolean doWork() {
-                                                       CollectionEntry entry = session.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection.this );
-                                                       CollectionPersister persister = entry.getLoadedPersister();
-                                                       if ( persister.isExtraLazy() ) {
-                                                               if ( hasQueuedOperations() ) {
-                                                                       session.flush();
+                       // In remoting we are sure that session is null
+                       // both when using property paths and switching off conversations
+                       if(session == null && remoting) {                       
+                               log.info("--> readIndexExistence, of " + getRole() + " with key " + getKey());
+                               read();                                                                         
+                       } else {
+                               Boolean extraLazyExistenceCheck = withTemporarySessionIfNeeded(
+                                               new LazyInitializationWork<Boolean>() {
+                                                       @Override
+                                                       public Boolean doWork() {
+                                                               CollectionEntry entry = session.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection.this );
+                                                               CollectionPersister persister = entry.getLoadedPersister();
+                                                               if ( persister.isExtraLazy() ) {
+                                                                       if ( hasQueuedOperations() ) {
+                                                                               session.flush();
+                                                                       }
+                                                                       return persister.indexExists( entry.getLoadedKey(), index, session );
                                                                }
                                                                }
-                                                               return persister.indexExists( entry.getLoadedKey(), index, session );
-                                                       }
-                                                       else {
-                                                               read();
+                                                               else {
+                                                                       read();
+                                                               }
+                                                               return null;
                                                        }
                                                        }
-                                                       return null;
                                                }
                                                }
-                                       }
-                       );
-                       if ( extraLazyExistenceCheck != null ) {
-                               return extraLazyExistenceCheck;
+                                               );
+                               if ( extraLazyExistenceCheck != null ) {
+                                       return extraLazyExistenceCheck;
+                               }
                        }
                }
                return null;
                        }
                }
                return null;
@@ -304,27 +318,35 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
 
        protected Boolean readElementExistence(final Object element) {
                if ( !initialized ) {
 
        protected Boolean readElementExistence(final Object element) {
                if ( !initialized ) {
-                       Boolean extraLazyExistenceCheck = withTemporarySessionIfNeeded(
-                                       new LazyInitializationWork<Boolean>() {
-                                               @Override
-                                               public Boolean doWork() {
-                                                       CollectionEntry entry = session.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection.this );
-                                                       CollectionPersister persister = entry.getLoadedPersister();
-                                                       if ( persister.isExtraLazy() ) {
-                                                               if ( hasQueuedOperations() ) {
-                                                                       session.flush();
+                       // In remoting we are sure that session is null
+                       // both when using property paths and switching off conversations
+                       if(session == null && remoting) {
+                               log.info("--> readElementExistence, of " + getRole() + " with key " + getKey());
+                               read();                         
+                               
+                       } else {
+                               Boolean extraLazyExistenceCheck = withTemporarySessionIfNeeded(
+                                               new LazyInitializationWork<Boolean>() {
+                                                       @Override
+                                                       public Boolean doWork() {
+                                                               CollectionEntry entry = session.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection.this );
+                                                               CollectionPersister persister = entry.getLoadedPersister();
+                                                               if ( persister.isExtraLazy() ) {
+                                                                       if ( hasQueuedOperations() ) {
+                                                                               session.flush();
+                                                                       }
+                                                                       return persister.elementExists( entry.getLoadedKey(), element, session );
                                                                }
                                                                }
-                                                               return persister.elementExists( entry.getLoadedKey(), element, session );
-                                                       }
-                                                       else {
-                                                               read();
+                                                               else {
+                                                                       read();
+                                                               }
+                                                               return null;
                                                        }
                                                        }
-                                                       return null;
                                                }
                                                }
-                                       }
-                       );
-                       if ( extraLazyExistenceCheck != null ) {
-                               return extraLazyExistenceCheck;
+                                               );
+                               if ( extraLazyExistenceCheck != null ) {
+                                       return extraLazyExistenceCheck;
+                               }
                        }
                }
                return null;
                        }
                }
                return null;
@@ -334,33 +356,41 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
 
        protected Object readElementByIndex(final Object index) {
                if ( !initialized ) {
 
        protected Object readElementByIndex(final Object index) {
                if ( !initialized ) {
-                       class ExtraLazyElementByIndexReader implements LazyInitializationWork {
-                               private boolean isExtraLazy;
-                               private Object element;
-
-                               @Override
-                               public Object doWork() {
-                                       CollectionEntry entry = session.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection.this );
-                                       CollectionPersister persister = entry.getLoadedPersister();
-                                       isExtraLazy = persister.isExtraLazy();
-                                       if ( isExtraLazy ) {
-                                               if ( hasQueuedOperations() ) {
-                                                       session.flush();
+                       // In remoting we are sure that session is null
+                       // both when using property paths and switching off conversations
+                       if(session == null && remoting) {                               
+                               log.info("--> readElementByIndex, of " + getRole() + " with key " + getKey());
+                               read();                         
+                               
+                       } else {
+                               class ExtraLazyElementByIndexReader implements LazyInitializationWork {
+                                       private boolean isExtraLazy;
+                                       private Object element;
+
+                                       @Override
+                                       public Object doWork() {
+                                               CollectionEntry entry = session.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection.this );
+                                               CollectionPersister persister = entry.getLoadedPersister();
+                                               isExtraLazy = persister.isExtraLazy();
+                                               if ( isExtraLazy ) {
+                                                       if ( hasQueuedOperations() ) {
+                                                               session.flush();
+                                                       }
+                                                       element = persister.getElementByIndex( entry.getLoadedKey(), index, session, owner );
                                                }
                                                }
-                                               element = persister.getElementByIndex( entry.getLoadedKey(), index, session, owner );
-                                       }
-                                       else {
-                                               read();
+                                               else {
+                                                       read();
+                                               }
+                                               return null;
                                        }
                                        }
-                                       return null;
                                }
                                }
-                       }
 
 
-                       ExtraLazyElementByIndexReader reader = new ExtraLazyElementByIndexReader();
-                       //noinspection unchecked
-                       withTemporarySessionIfNeeded( reader );
-                       if ( reader.isExtraLazy ) {
-                               return reader.element;
+                               ExtraLazyElementByIndexReader reader = new ExtraLazyElementByIndexReader();
+                               //noinspection unchecked
+                               withTemporarySessionIfNeeded( reader );
+                               if ( reader.isExtraLazy ) {
+                                       return reader.element;
+                               }
                        }
                }
                return UNKNOWN;
                        }
                }
                return UNKNOWN;
@@ -554,8 +584,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
        protected final void initialize(final boolean writing) {
                // In remoting we are sure that session is null
                // both when using property paths and switching off conversations
        protected final void initialize(final boolean writing) {
                // In remoting we are sure that session is null
                // both when using property paths and switching off conversations
-               if(session == null) {
-                       remoteInitialize();
+               if(session == null && remoting) {
+                       remoteInitialize();                     
                }
                
                if ( initialized ) {
                }
                
                if ( initialized ) {
@@ -1188,15 +1218,28 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                this.owner = owner;
        }
 
                this.owner = owner;
        }
 
-       /** Below is section of code which makes remote service calls */
+       /** ------ Below is section of code which makes remote service calls ----- */
+       // The affected methods are : 
+       // initialize(final boolean writing)
+       // readSize()
+       // readIndexExistence(final Object index)
+       // readElementExistence(final Object element)
+       // readElementByIndex(final Object index) 
        
        private static ICdmApplicationConfiguration configuration;
        
        private static ICdmApplicationConfiguration configuration;
-
+       private static boolean remoting = false;
        
        public static void setConfiguration(ICdmApplicationConfiguration conf) {
                configuration = conf;
        
        public static void setConfiguration(ICdmApplicationConfiguration conf) {
                configuration = conf;
+               Boolean isRemoting = (Boolean)configuration.getBean("isRemoting");              
+               if(isRemoting != null) {
+                       remoting = isRemoting.booleanValue();
+               } else {
+                       remoting = false;
+               }
        }
        
        }
        
+       
        private void remoteInitialize() {
                
                if (getOwner() != null && !initialized) {                       
        private void remoteInitialize() {
                
                if (getOwner() != null && !initialized) {                       
@@ -1204,7 +1247,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                        try {
                                String role = getRole();
                                String fieldName = role.substring(role.lastIndexOf(".") + 1);
                        try {
                                String role = getRole();
                                String fieldName = role.substring(role.lastIndexOf(".") + 1);
-                               log.info("--> Remote Lazy Initializing " + getRole() + " , field : " + fieldName);
+                               log.info("--> Remote Lazy Initializing " + getRole() + " , key : " + getKey() + " , field : " + fieldName);
                                Object owner = getOwner();
                                
                                if(configuration == null) {
                                Object owner = getOwner();
                                
                                if(configuration == null) {
@@ -1231,6 +1274,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                }
        }
        
                }
        }
        
+       
        private CollectionField getCollectionField(PersistentCollection pc) {
                if(pc != null) {
                        if(pc instanceof PersistentSet) {
        private CollectionField getCollectionField(PersistentCollection pc) {
                if(pc != null) {
                        if(pc instanceof PersistentSet) {
@@ -1284,5 +1328,37 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                }
        }
        
                }
        }
        
+       public static boolean isInitialized(List list) {
+               return ((AbstractPersistentCollection)list).initialized;
+       }
+       
+       public static boolean isInitialized(Map map) {
+               return ((AbstractPersistentCollection)map).initialized;
+       }
+       
+       //FIXME:Remoting These methods may no longer be required since we are 
+       //               initialising collections as default behaviour
+       private int remoteSize() {
+               int size = configuration.getCommonService().size(this);
+               log.debug("--> Remote Lazy Initializing size of " + getRole() + " to " + size);
+               if(size == -1) {
+                       throw new HibernateException("size of " + getClass() + " could not be retrieved from remote service");
+               }
+               return size;
+       }
+       
+       private Object remoteReadElementByIndex(int index) {
+               Object element = configuration.getCommonService().get(this,index);
+               log.debug("--> Remote Lazy Initializing element from " + getRole() + " at index " + index);
+               return element;
+       }
+       
+       private boolean remoteReadElementExistence(Object element) {
+               return configuration.getCommonService().contains(this,element);
+       }
+       
+       private boolean remoteReadIndexExistence(Object index) {
+               return false;
+       }
 }
 
 }
 
index d40b90e8561ce82788b43d125f93841dd04f7760..3aa9fccdf02197672f01439024302db06cbbc836 100644 (file)
@@ -167,7 +167,7 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
        public final void initialize() throws HibernateException {
                // In remoting we are sure that session is null
                // both when using property paths and switching off conversations
        public final void initialize() throws HibernateException {
                // In remoting we are sure that session is null
                // both when using property paths and switching off conversations
-               if(session == null) {
+               if(session == null && remoting) {
                        remoteInitialize();
                }
                if ( !initialized ) {
                        remoteInitialize();
                }
                if ( !initialized ) {
@@ -423,9 +423,16 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
        /** Below is section of code which makes remote service calls */
        
        private static ICdmApplicationConfiguration configuration;
        /** Below is section of code which makes remote service calls */
        
        private static ICdmApplicationConfiguration configuration;
+       private static boolean remoting = false;
        
        public static void setConfiguration(ICdmApplicationConfiguration conf) {
                configuration = conf;
        
        public static void setConfiguration(ICdmApplicationConfiguration conf) {
                configuration = conf;
+               Boolean isRemoting = (Boolean)configuration.getBean("isRemoting");
+               if(isRemoting != null) {
+                       remoting = isRemoting.booleanValue();
+               } else {
+                       remoting = false;
+               }
        }
        
        
        }
        
        
@@ -433,7 +440,7 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
                
                if(!initialized) {                              
                        int classid = ((Integer)getIdentifier()).intValue();
                
                if(!initialized) {                              
                        int classid = ((Integer)getIdentifier()).intValue();
-                       System.out.print("--> Remote Lazy Initializing" + getEntityName() + " with id " + classid);
+                       log.debug("--> Remote Lazy Initializing" + getEntityName() + " with id " + classid);
                        Class clazz;
                        try {
                                clazz = (Class<? extends CdmBase>) Class.forName(getEntityName());
                        Class clazz;
                        try {
                                clazz = (Class<? extends CdmBase>) Class.forName(getEntityName());
@@ -450,7 +457,7 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
                        
                        CdmBase cdmBase = CdmBase.deproxy(commonService.find(clazz,classid),clazz);
                        setImplementation(cdmBase);
                        
                        CdmBase cdmBase = CdmBase.deproxy(commonService.find(clazz,classid),clazz);
                        setImplementation(cdmBase);
-                       System.out.println("....Done");
+                       
                }
        }
        
                }
        }
        
index f90ee4017937cc8f679910832f9f32d669a40665..30fb5af4cf339e89936512140b5fb26bb016e2e3 100644 (file)
@@ -7,7 +7,10 @@
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
        
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
        
-
+       <bean id="isRemoting" class="java.lang.Boolean">
+       <constructor-arg value="false" />
+       </bean> 
+       
     <!-- Default application context and term initializer -->
   <import resource="classpath:/eu/etaxonomy/cdm/defaultApplicationContext.xml"/>
   <bean id="persistentTermInitializer" class="eu.etaxonomy.cdm.database.PersistentTermInitializer">
     <!-- Default application context and term initializer -->
   <import resource="classpath:/eu/etaxonomy/cdm/defaultApplicationContext.xml"/>
   <bean id="persistentTermInitializer" class="eu.etaxonomy.cdm.database.PersistentTermInitializer">
index a4819b226cf98266382f24a10902db4693015c84..b55fc4cae2a31de16de2118b6cf9ae8e1de62fc1 100644 (file)
@@ -7,7 +7,10 @@
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">\r
        \r
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">\r
        \r
-\r
+       <bean id="isRemoting" class="java.lang.Boolean">\r
+       <constructor-arg value="true" />\r
+       </bean> \r
+       \r
     <!--  <bean id="remoteTermInitializer" class="eu.etaxonomy.cdm.remote.service.RemoteTermInitializer"/>-->\r
         \r
        <context:annotation-config/>\r
     <!--  <bean id="remoteTermInitializer" class="eu.etaxonomy.cdm.remote.service.RemoteTermInitializer"/>-->\r
         \r
        <context:annotation-config/>\r
index 06c791ca1b1b898b92b013ef0ce9db56c57a5bcc..01ded5e282b0873b6f2c26d532dc7b30cc1c9094 100644 (file)
@@ -17,6 +17,8 @@ import java.util.EnumSet;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.swt.widgets.Display;
+import org.hibernate.collection.internal.AbstractPersistentCollection;
+import org.hibernate.proxy.AbstractLazyInitializer;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 import org.springframework.security.access.PermissionEvaluator;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 import org.springframework.security.access.PermissionEvaluator;
@@ -193,6 +195,8 @@ public class CdmStore {
        private CdmStore(ICdmApplicationConfiguration applicationController,
                        ICdmDataSource dataSource) {
                this.applicationConfiguration = applicationController;
        private CdmStore(ICdmApplicationConfiguration applicationController,
                        ICdmDataSource dataSource) {
                this.applicationConfiguration = applicationController;
+               AbstractLazyInitializer.setConfiguration(applicationController);
+               AbstractPersistentCollection.setConfiguration(applicationController);
                this.cdmDatasource = dataSource;
                isConnected = true;
        }
                this.cdmDatasource = dataSource;
                isConnected = true;
        }