From c8d37de76178d525e0b1251b79483e826d56bb78 Mon Sep 17 00:00:00 2001 From: Cherian Mathew Date: Thu, 25 Jun 2015 17:21:07 +0200 Subject: [PATCH] Fix remoting code compilation errors --- .../cdm/api/lazyloading/CdmLazyLoader.java | 79 ++--- .../AbstractPersistentCollection.java | 303 ++++++++++-------- 2 files changed, 209 insertions(+), 173 deletions(-) diff --git a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/api/lazyloading/CdmLazyLoader.java b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/api/lazyloading/CdmLazyLoader.java index e98d40df7..db64c568c 100644 --- a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/api/lazyloading/CdmLazyLoader.java +++ b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/api/lazyloading/CdmLazyLoader.java @@ -1,6 +1,5 @@ package eu.etaxonomy.cdm.api.lazyloading; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -37,24 +36,24 @@ import eu.etaxonomy.cdm.model.common.PersistentMultiLanguageText; public class CdmLazyLoader { - private Set classes = new HashSet(); - + private final Set classes = new HashSet(); + public static boolean enableWeaving = true; private static Set classesToIgnore = new HashSet(); - - + + @Autowired private ICommonService commonService; public CdmLazyLoader() { //classesToIgnore.add("eu.etaxonomy.cdm.model.common.TermVocabulary"); //classesToIgnore.add("eu.etaxonomy.cdm.model.common.OrderedTermVocabulary"); - + } - + /** * Point cut for the 'initialize' method of the AbstractLazyInitializer. - * + * */ @Pointcut("execution(* org.hibernate.proxy.AbstractLazyInitializer.initialize())") public void possibleEntityLazyInitializationException() { @@ -63,43 +62,43 @@ public class CdmLazyLoader { /** * 'Around' advice for the initialization of CDM Entity Objects - * + * */ @Around(value = "possibleEntityLazyInitializationException()") - public Object preloadEntityOnDemand(ProceedingJoinPoint pjp) throws Throwable { + public Object preloadEntityOnDemand(ProceedingJoinPoint pjp) throws Throwable { if(enableWeaving) { - LazyInitializer ll = (LazyInitializer)pjp.getTarget(); - if(ll.isUninitialized()) { + LazyInitializer ll = (LazyInitializer)pjp.getTarget(); + if(ll.isUninitialized()) { int classid = ((Integer)ll.getIdentifier()).intValue(); System.out.print("--> AspectJ Compile-Time Weaving " + ll.getEntityName() + " with id " + classid); - Class clazz = (Class) Class.forName(ll.getEntityName()); + Class clazz = Class.forName(ll.getEntityName()); CdmBase cdmBase = CdmBase.deproxy(commonService.find(clazz,classid),clazz); ll.setImplementation(cdmBase); System.out.println("....Done"); } } - return pjp.proceed(); + return pjp.proceed(); } - + /** * Point cut for the 'initialize' method of the AbstractPersistentCollection. - * + * */ @Pointcut("execution(protected final void org.hibernate.collection.internal.AbstractPersistentCollection.initialize(..))") public void possibleCollectionLazyInitializationException() { } - + /** * 'Around' advice for the initialization of Collection objects - * + * */ - @Around(value = "possibleCollectionLazyInitializationException()") - public Object preloadCollectionOnDemand(ProceedingJoinPoint pjp) throws Throwable { + @Around(value = "possibleCollectionLazyInitializationException()") + public Object preloadCollectionOnDemand(ProceedingJoinPoint pjp) throws Throwable { if(enableWeaving) { PersistentCollection ps = (PersistentCollection) pjp.getTarget(); if (ps.getOwner() != null && !classesToIgnore.contains(ps.getOwner().getClass().getName()) && !ps.wasInitialized() && !classes.contains(ps.getKey())) { - System.out.print("--> AspectJCompile-Time Weaving " + ps.getRole()); + System.out.print("--> AspectJCompile-Time Weaving " + ps.getRole()); classes.add(ps.getKey()); try { String role = ps.getRole(); @@ -107,29 +106,31 @@ public class CdmLazyLoader { System.out.print(", field : " + fieldName); Object owner = ps.getOwner(); - PersistentCollection col = commonService.initializeCollection(ps); + Object col = commonService.initializeCollection(((CdmBase)owner).getUuid(), fieldName); ps.afterInitialize(); - Class clazz = ps.getClass(); - if (clazz != null) { - CollectionField cf = getCollectionField(col); - Field field = clazz.getDeclaredField(cf.getFieldName()); - field.setAccessible(true); - field.set(ps, cf.getCollection()); - } +// Class clazz = ps.getClass(); +// if (clazz != null) { +// CollectionField cf = getCollectionField(col); +// Field field = clazz.getDeclaredField(cf.getFieldName()); +// field.setAccessible(true); +// field.set(ps, cf.getCollection()); +// } + } catch (Exception ex) { ex.printStackTrace(); System.out.println("Error in ReattachSessionAspect : " + ex.getMessage()); } finally { classes.remove(ps.getKey()); System.out.println("....Done"); - } + throw new Exception("This code is invalid"); + } } - } + } return pjp.proceed(); } - + private CollectionField getCollectionField(PersistentCollection pc) { if(pc != null) { if(pc instanceof PersistentSet) { @@ -150,12 +151,12 @@ public class CdmLazyLoader { } return null; } - + private String getCollectionFieldName(PersistentCollection pc) { if(pc != null) { if(pc instanceof PersistentSet || pc instanceof PersistentSortedSet) { return "set"; - } + } if(pc instanceof PersistentList) { return "list"; } @@ -165,19 +166,19 @@ public class CdmLazyLoader { } return null; } - + private class CollectionField { - private Object col; - private String fieldName; + private final Object col; + private final String fieldName; public CollectionField(Object col, String fieldName) { this.col = col; this.fieldName = fieldName; } - + public Object getCollection() { return this.col; } - + public String getFieldName() { return this.fieldName; } 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 e6841035b..1bc42c44f 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 @@ -26,7 +26,6 @@ package org.hibernate.collection.internal; import java.io.Serializable; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -78,7 +77,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers private static final Logger log = Logger.getLogger( AbstractPersistentCollection.class ); private static final long serialVersionUID = -7238232378593030571L; - + private transient SessionImplementor session; private boolean initialized; private transient List operationQueue; @@ -97,31 +96,38 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers private String sessionFactoryUuid; private boolean specjLazyLoad = false; - public final String getRole() { + @Override + public final String getRole() { return role; } - public final Serializable getKey() { + @Override + public final Serializable getKey() { return key; } - public final boolean isUnreferenced() { + @Override + public final boolean isUnreferenced() { return role == null; } - public final boolean isDirty() { + @Override + public final boolean isDirty() { return dirty; } - public final void clearDirty() { + @Override + public final void clearDirty() { dirty = false; } - public final void dirty() { + @Override + public final void dirty() { dirty = true; } - public final Serializable getStoredSnapshot() { + @Override + public final Serializable getStoredSnapshot() { return storedSnapshot; } @@ -130,7 +136,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers /** * Is the initialized collection empty? */ - public abstract boolean empty(); + @Override + public abstract boolean empty(); /** * Called by any read-only method of the collection interface @@ -151,9 +158,9 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers else { // In remoting we are sure that session is null // both when using property paths and switching off conversations - if(session == null && remoting) { + if(session == null && remoting) { log.info("--> readSize, of " + getRole() + " with key " + getKey()); - read(); + read(); } else { boolean isExtraLazy = withTemporarySessionIfNeeded( new LazyInitializationWork() { @@ -236,7 +243,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers .getTransactionContext().getTransactionEnvironment() .getTransactionFactory() .compatibleWithJtaSynchronization(); - + if ( !isJTA ) { // Explicitly handle the transactions only if we're not in // a JTA environment. A lazy loading temporary session can @@ -245,7 +252,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers // multiple transactions. ( ( Session) session ).beginTransaction(); } - + session.getPersistenceContext().addUninitializedDetachedCollection( session.getFactory().getCollectionPersister( getRole() ), this @@ -286,9 +293,9 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers if ( !initialized ) { // In remoting we are sure that session is null // both when using property paths and switching off conversations - if(session == null && remoting) { + if(session == null && remoting) { log.info("--> readIndexExistence, of " + getRole() + " with key " + getKey()); - read(); + read(); } else { Boolean extraLazyExistenceCheck = withTemporarySessionIfNeeded( new LazyInitializationWork() { @@ -323,8 +330,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers // both when using property paths and switching off conversations if(session == null && remoting) { log.info("--> readElementExistence, of " + getRole() + " with key " + getKey()); - read(); - + read(); + } else { Boolean extraLazyExistenceCheck = withTemporarySessionIfNeeded( new LazyInitializationWork() { @@ -359,10 +366,10 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers if ( !initialized ) { // In remoting we are sure that session is null // both when using property paths and switching off conversations - if(session == null && remoting) { + if(session == null && remoting) { log.info("--> readElementByIndex, of " + getRole() + " with key " + getKey()); - read(); - + read(); + } else { class ExtraLazyElementByIndexReader implements LazyInitializationWork { private boolean isExtraLazy; @@ -510,7 +517,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers /** * After flushing, re-init snapshot state. */ - public void setSnapshot(Serializable key, String role, Serializable snapshot) { + @Override + public void setSnapshot(Serializable key, String role, Serializable snapshot) { this.key = key; this.role = role; this.storedSnapshot = snapshot; @@ -520,7 +528,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers * After flushing, clear any "queued" additions, since the * database state is now synchronized with the memory state. */ - public void postAction() { + @Override + public void postAction() { operationQueue = null; cachedSize = -1; clearDirty(); @@ -540,14 +549,16 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers /** * return the user-visible collection (or array) instance */ - public Object getValue() { + @Override + public Object getValue() { return this; } /** * Called just before reading any rows from the JDBC result set */ - public void beginRead() { + @Override + public void beginRead() { // override on some subclasses initializing = true; } @@ -555,12 +566,14 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers /** * Called after reading all rows from the JDBC result set */ - public boolean endRead() { + @Override + public boolean endRead() { //override on some subclasses return afterInitialize(); } - public boolean afterInitialize() { + @Override + public boolean afterInitialize() { setInitialized(); //do this bit after setting initialized to true or it will recurse if ( operationQueue != null ) { @@ -586,14 +599,14 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers // In remoting we are sure that session is null // both when using property paths and switching off conversations if(session == null && remoting) { - remoteInitialize(); + remoteInitialize(); } - + if ( initialized ) { return; } - + withTemporarySessionIfNeeded( new LazyInitializationWork() { @Override @@ -635,7 +648,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers * Could the application possibly have a direct reference to * the underlying collection implementation? */ - public boolean isDirectlyAccessible() { + @Override + public boolean isDirectlyAccessible() { return directlyAccessible; } @@ -644,7 +658,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers * * @return true if this was currently associated with the given session */ - public final boolean unsetSession(SessionImplementor currentSession) { + @Override + public final boolean unsetSession(SessionImplementor currentSession) { prepareForPossibleSpecialSpecjInitialization(); if ( currentSession == this.session ) { this.session = null; @@ -679,7 +694,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers * @throws HibernateException if the collection was already associated * with another open session */ - public final boolean setCurrentSession(SessionImplementor session) throws HibernateException { + @Override + public final boolean setCurrentSession(SessionImplementor session) throws HibernateException { if ( session == this.session ) { return false; } @@ -711,7 +727,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers /** * Do we need to completely recreate this collection when it changes? */ - public boolean needsRecreate(CollectionPersister persister) { + @Override + public boolean needsRecreate(CollectionPersister persister) { return false; } @@ -719,7 +736,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers * To be called internally by the session, forcing * immediate initialization. */ - public final void forceInitialization() throws HibernateException { + @Override + public final void forceInitialization() throws HibernateException { if ( !initialized ) { if ( initializing ) { throw new AssertionFailure( "force initialize loading collection" ); @@ -746,38 +764,45 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers /** * Is this instance initialized? */ - public final boolean wasInitialized() { + @Override + public final boolean wasInitialized() { return initialized; } - public boolean isRowUpdatePossible() { + @Override + public boolean isRowUpdatePossible() { return true; } /** * Does this instance have any "queued" additions? */ - public final boolean hasQueuedOperations() { + @Override + public final boolean hasQueuedOperations() { return operationQueue != null; } /** * Iterate the "queued" additions */ - public final Iterator queuedAdditionIterator() { + @Override + public final Iterator queuedAdditionIterator() { if ( hasQueuedOperations() ) { return new Iterator() { int i = 0; - public Object next() { + @Override + public Object next() { return operationQueue.get( i++ ).getAddedInstance(); } - public boolean hasNext() { + @Override + public boolean hasNext() { return i < operationQueue.size(); } - public void remove() { + @Override + public void remove() { throw new UnsupportedOperationException(); } }; @@ -790,7 +815,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers /** * Iterate the "queued" additions */ - @SuppressWarnings({"unchecked"}) + @Override + @SuppressWarnings({"unchecked"}) public final Collection getQueuedOrphans(String entityName) { if ( hasQueuedOperations() ) { Collection additions = new ArrayList( operationQueue.size() ); @@ -810,19 +836,22 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers * Called before inserting rows, to ensure that any surrogate keys * are fully generated */ - public void preInsert(CollectionPersister persister) throws HibernateException { + @Override + public void preInsert(CollectionPersister persister) throws HibernateException { } /** * Called after inserting a row, to fetch the natively generated id */ - public void afterRowInsert(CollectionPersister persister, Object entry, int i) throws HibernateException { + @Override + public void afterRowInsert(CollectionPersister persister, Object entry, int i) throws HibernateException { } /** * get all "orphaned" elements */ - public abstract Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException; + @Override + public abstract Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException; /** * Get the current session @@ -839,15 +868,18 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers this.itr = itr; } - public boolean hasNext() { + @Override + public boolean hasNext() { return itr.hasNext(); } - public Object next() { + @Override + public Object next() { return itr.next(); } - public void remove() { + @Override + public void remove() { write(); itr.remove(); } @@ -861,42 +893,51 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers this.itr = itr; } - @SuppressWarnings({"unchecked"}) + @Override + @SuppressWarnings({"unchecked"}) public void add(Object o) { write(); itr.add( o ); } - public boolean hasNext() { + @Override + public boolean hasNext() { return itr.hasNext(); } - public boolean hasPrevious() { + @Override + public boolean hasPrevious() { return itr.hasPrevious(); } - public Object next() { + @Override + public Object next() { return itr.next(); } - public int nextIndex() { + @Override + public int nextIndex() { return itr.nextIndex(); } - public Object previous() { + @Override + public Object previous() { return itr.previous(); } - public int previousIndex() { + @Override + public int previousIndex() { return itr.previousIndex(); } - public void remove() { + @Override + public void remove() { write(); itr.remove(); } - @SuppressWarnings({"unchecked"}) + @Override + @SuppressWarnings({"unchecked"}) public void set(Object o) { write(); itr.set( o ); @@ -911,63 +952,76 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers this.set = set; } - @SuppressWarnings({"unchecked"}) + @Override + @SuppressWarnings({"unchecked"}) public boolean add(Object o) { write(); return set.add( o ); } - @SuppressWarnings({"unchecked"}) + @Override + @SuppressWarnings({"unchecked"}) public boolean addAll(Collection c) { write(); return set.addAll( c ); } - public void clear() { + @Override + public void clear() { write(); set.clear(); } - public boolean contains(Object o) { + @Override + public boolean contains(Object o) { return set.contains( o ); } - public boolean containsAll(Collection c) { + @Override + public boolean containsAll(Collection c) { return set.containsAll( c ); } - public boolean isEmpty() { + @Override + public boolean isEmpty() { return set.isEmpty(); } - public Iterator iterator() { + @Override + public Iterator iterator() { return new IteratorProxy( set.iterator() ); } - public boolean remove(Object o) { + @Override + public boolean remove(Object o) { write(); return set.remove( o ); } - public boolean removeAll(Collection c) { + @Override + public boolean removeAll(Collection c) { write(); return set.removeAll( c ); } - public boolean retainAll(Collection c) { + @Override + public boolean retainAll(Collection c) { write(); return set.retainAll( c ); } - public int size() { + @Override + public int size() { return set.size(); } - public Object[] toArray() { + @Override + public Object[] toArray() { return set.toArray(); } - @SuppressWarnings({"unchecked"}) + @Override + @SuppressWarnings({"unchecked"}) public Object[] toArray(Object[] array) { return set.toArray( array ); } @@ -1207,50 +1261,53 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers } } - public Object getIdentifier(Object entry, int i) { + @Override + public Object getIdentifier(Object entry, int i) { throw new UnsupportedOperationException(); } - public Object getOwner() { + @Override + public Object getOwner() { return owner; } - public void setOwner(Object owner) { + @Override + public void setOwner(Object owner) { this.owner = owner; } /** ------ Below is section of code which makes remote service calls ----- */ - // The affected methods are : + // The affected methods are : // initialize(final boolean writing) // readSize() // readIndexExistence(final Object index) // readElementExistence(final Object element) - // readElementByIndex(final Object index) - + // readElementByIndex(final Object index) + private static ICdmApplicationConfiguration configuration; private static boolean remoting = false; - + public static void setConfiguration(ICdmApplicationConfiguration conf) { configuration = conf; - + if(conf instanceof CdmApplicationRemoteController) { remoting = true; } else { remoting = false; } } - - + + private void remoteInitialize() { - - if (getOwner() != null && !initialized) { - + + if (getOwner() != null && !initialized) { + try { String role = getRole(); String fieldName = role.substring(role.lastIndexOf(".") + 1); log.info("--> Remote Lazy Initializing " + getRole() + " , key : " + getKey() + " , field : " + fieldName); Object owner = getOwner(); - + if(configuration == null) { throw new HibernateException("CdmApplicationRemoteConfiguration not initialized (null)"); } @@ -1258,24 +1315,25 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers if(commonService == null) { throw new HibernateException("commonService not initialized (null)"); } - - PersistentCollection col = commonService.initializeCollection(this); - afterInitialize(); - - Class clazz = getClass(); - if (clazz != null) { - CollectionField cf = getCollectionField(col); - Field field = clazz.getDeclaredField(cf.getFieldName()); - field.setAccessible(true); - field.set(this, cf.getCollection()); - } + +// PersistentCollection col = commonService.initializeCollection(this); +// afterInitialize(); +// +// Class clazz = getClass(); +// if (clazz != null) { +// CollectionField cf = getCollectionField(col); +// Field field = clazz.getDeclaredField(cf.getFieldName()); +// field.setAccessible(true); +// field.set(this, cf.getCollection()); +// } } catch (Exception ex) { log.warn(ex.getMessage()); - } + } + log.warn("This code is invalid"); } } - - + + private CollectionField getCollectionField(PersistentCollection pc) { if(pc != null) { if(pc instanceof PersistentSet) { @@ -1296,12 +1354,12 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers } return null; } - + private String getCollectionFieldName(PersistentCollection pc) { if(pc != null) { if(pc instanceof PersistentSet || pc instanceof PersistentSortedSet) { return "set"; - } + } if(pc instanceof PersistentList) { return "list"; } @@ -1311,59 +1369,36 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers } return null; } - + private class CollectionField { - private Object col; - private String fieldName; + private final Object col; + private final String fieldName; public CollectionField(Object col, String fieldName) { this.col = col; this.fieldName = fieldName; } - + public Object getCollection() { return this.col; } - + public String getFieldName() { return this.fieldName; } } - + public static boolean isInitialized(List list) { return ((AbstractPersistentCollection)list).initialized; } - + public static boolean isInitialized(Map map) { return ((AbstractPersistentCollection)map).initialized; } - + public static boolean isInitialized(Set set) { return ((AbstractPersistentCollection)set).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; - } + + } -- 2.34.1