From: Cherian Mathew Date: Thu, 13 Mar 2014 20:23:45 +0000 (+0000) Subject: setting application configuration and boolean remoting flag (bean) in hibernate proxy... X-Git-Tag: 3.6.0~716 X-Git-Url: https://dev.e-taxonomy.eu/gitweb/taxeditor.git/commitdiff_plain/ddf2f5290113012fa58f8e8dbac6f882bba06e6c setting application configuration and boolean remoting flag (bean) in hibernate proxy classes to differentiate local and remoting --- diff --git a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java index 90451f871..c67e5e2ad 100644 --- a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java +++ b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java @@ -148,34 +148,41 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers return true; } else { - boolean isExtraLazy = withTemporarySessionIfNeeded( - new LazyInitializationWork() { - @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() { + @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 ) { - Boolean extraLazyExistenceCheck = withTemporarySessionIfNeeded( - new LazyInitializationWork() { - @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() { + @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; @@ -304,27 +318,35 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers protected Boolean readElementExistence(final Object element) { if ( !initialized ) { - Boolean extraLazyExistenceCheck = withTemporarySessionIfNeeded( - new LazyInitializationWork() { - @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() { + @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; @@ -334,33 +356,41 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers 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; @@ -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 - if(session == null) { - remoteInitialize(); + if(session == null && remoting) { + remoteInitialize(); } if ( initialized ) { @@ -1188,15 +1218,28 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers 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 boolean remoting = false; 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) { @@ -1204,7 +1247,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers 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) { @@ -1231,6 +1274,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers } } + 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; + } } diff --git a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java index d40b90e85..3aa9fccdf 100644 --- a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java +++ b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java @@ -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 - if(session == null) { + if(session == null && remoting) { 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; + private static boolean remoting = false; 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(); - System.out.print("--> Remote Lazy Initializing" + getEntityName() + " with id " + classid); + log.debug("--> Remote Lazy Initializing" + getEntityName() + " with id " + classid); Class clazz; try { clazz = (Class) Class.forName(getEntityName()); @@ -450,7 +457,7 @@ public abstract class AbstractLazyInitializer implements LazyInitializer { CdmBase cdmBase = CdmBase.deproxy(commonService.find(clazz,classid),clazz); setImplementation(cdmBase); - System.out.println("....Done"); + } } diff --git a/eu.etaxonomy.taxeditor.cdmlib/src/main/resources/eu/etaxonomy/cdm/localApplicationContext.xml b/eu.etaxonomy.taxeditor.cdmlib/src/main/resources/eu/etaxonomy/cdm/localApplicationContext.xml index f90ee4017..30fb5af4c 100644 --- a/eu.etaxonomy.taxeditor.cdmlib/src/main/resources/eu/etaxonomy/cdm/localApplicationContext.xml +++ b/eu.etaxonomy.taxeditor.cdmlib/src/main/resources/eu/etaxonomy/cdm/localApplicationContext.xml @@ -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"> - + + + + diff --git a/eu.etaxonomy.taxeditor.cdmlib/src/main/resources/eu/etaxonomy/cdm/remotingApplicationContext.xml b/eu.etaxonomy.taxeditor.cdmlib/src/main/resources/eu/etaxonomy/cdm/remotingApplicationContext.xml index a4819b226..b55fc4cae 100644 --- a/eu.etaxonomy.taxeditor.cdmlib/src/main/resources/eu/etaxonomy/cdm/remotingApplicationContext.xml +++ b/eu.etaxonomy.taxeditor.cdmlib/src/main/resources/eu/etaxonomy/cdm/remotingApplicationContext.xml @@ -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"> - + + + + diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/store/CdmStore.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/store/CdmStore.java index 06c791ca1..01ded5e28 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/store/CdmStore.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/store/CdmStore.java @@ -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.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; @@ -193,6 +195,8 @@ public class CdmStore { private CdmStore(ICdmApplicationConfiguration applicationController, ICdmDataSource dataSource) { this.applicationConfiguration = applicationController; + AbstractLazyInitializer.setConfiguration(applicationController); + AbstractPersistentCollection.setConfiguration(applicationController); this.cdmDatasource = dataSource; isConnected = true; }