package org.hibernate.engine.internal;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.ManagedEntity;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.IdentityHashMap;
import java.util.Map;
public class EntityEntryContext {
private static final CoreMessageLogger log = CoreLogging.messageLogger( EntityEntryContext.class );
private transient PersistenceContext persistenceContext;
private transient IdentityHashMap<ManagedEntity,ImmutableManagedEntityHolder> immutableManagedEntityXref;
private transient ManagedEntity head;
private transient ManagedEntity tail;
private transient int count;
private transient IdentityHashMap<Object,ManagedEntity> nonEnhancedEntityXref;
@SuppressWarnings( {"unchecked"})
private transient Map.Entry<Object,EntityEntry>[] reentrantSafeEntries = new Map.Entry[0];
private transient boolean dirty;
public EntityEntryContext(PersistenceContext persistenceContext) {
this.persistenceContext = persistenceContext;
}
public void addEntityEntry(Object entity, EntityEntry entityEntry) {
dirty = true;
assert AbstractEntityEntry.class.isInstance( entityEntry );
if ( entityEntry.getPersister().isMutable() ) {
assert AbstractEntityEntry.class.cast( entityEntry ).getPersistenceContext() == persistenceContext;
}
ManagedEntity managedEntity = getAssociatedManagedEntity( entity );
final boolean alreadyAssociated = managedEntity != null;
if ( !alreadyAssociated ) {
if ( ManagedEntity.class.isInstance( entity ) ) {
if ( entityEntry.getPersister().isMutable() ) {
managedEntity = (ManagedEntity) entity;
checkNotAssociatedWithOtherPersistenceContextIfMutable( managedEntity );
}
else {
managedEntity = new ImmutableManagedEntityHolder( (ManagedEntity) entity );
if ( immutableManagedEntityXref == null ) {
immutableManagedEntityXref = new IdentityHashMap<ManagedEntity, ImmutableManagedEntityHolder>();
}
immutableManagedEntityXref.put(
(ManagedEntity) entity,
(ImmutableManagedEntityHolder) managedEntity
);
}
}
else {
if ( nonEnhancedEntityXref == null ) {
nonEnhancedEntityXref = new IdentityHashMap<Object, ManagedEntity>();
}
managedEntity = new ManagedEntityImpl( entity );
nonEnhancedEntityXref.put( entity, managedEntity );
}
}
managedEntity.$$_hibernate_setEntityEntry( entityEntry );
if ( alreadyAssociated ) {
return;
}
if ( tail == null ) {
assert head == null;
managedEntity.$$_hibernate_setPreviousManagedEntity( null );
managedEntity.$$_hibernate_setNextManagedEntity( null );
head = managedEntity;
tail = head;
count = 1;
}
else {
tail.$$_hibernate_setNextManagedEntity( managedEntity );
managedEntity.$$_hibernate_setPreviousManagedEntity( tail );
managedEntity.$$_hibernate_setNextManagedEntity( null );
tail = managedEntity;
count++;
}
}
private ManagedEntity getAssociatedManagedEntity(Object entity) {
if ( ManagedEntity.class.isInstance( entity ) ) {
final ManagedEntity managedEntity = (ManagedEntity) entity;
if ( managedEntity.$$_hibernate_getEntityEntry() == null ) {
return null;
}
final AbstractEntityEntry entityEntry = (AbstractEntityEntry) managedEntity.$$_hibernate_getEntityEntry();
if ( entityEntry.getPersister().isMutable() ) {
return entityEntry.getPersistenceContext() == persistenceContext
? managedEntity
: null;
}
else {
return immutableManagedEntityXref != null
? immutableManagedEntityXref.get( managedEntity )
: null;
}
}
else {
return nonEnhancedEntityXref != null
? nonEnhancedEntityXref.get( entity )
: null;
}
}
private void checkNotAssociatedWithOtherPersistenceContextIfMutable(ManagedEntity managedEntity) {
final AbstractEntityEntry entityEntry = (AbstractEntityEntry) managedEntity.$$_hibernate_getEntityEntry();
if ( entityEntry == null ||
!entityEntry.getPersister().isMutable() ||
entityEntry.getPersistenceContext() == null ||
entityEntry.getPersistenceContext() == persistenceContext ) {
return;
}
if ( entityEntry.getPersistenceContext().getSession().isOpen() ) {
throw new HibernateException(
"Illegal attempt to associate a ManagedEntity with two open persistence contexts. " + entityEntry
);
}
else {
log.stalePersistenceContextInEntityEntry( entityEntry.toString() );
}
}
public boolean hasEntityEntry(Object entity) {
return getEntityEntry( entity ) != null;
}
public EntityEntry getEntityEntry(Object entity) {
final ManagedEntity managedEntity = getAssociatedManagedEntity( entity );
return managedEntity == null
? null
: managedEntity.$$_hibernate_getEntityEntry();
}
public EntityEntry removeEntityEntry(Object entity) {
final ManagedEntity managedEntity = getAssociatedManagedEntity( entity );
if ( managedEntity == null ) {
return null;
}
dirty = true;
if ( ImmutableManagedEntityHolder.class.isInstance( managedEntity ) ) {
assert entity == ( (ImmutableManagedEntityHolder) managedEntity ).managedEntity;
immutableManagedEntityXref.remove( (ManagedEntity) entity );
}
else if ( !ManagedEntity.class.isInstance( entity ) ) {
nonEnhancedEntityXref.remove( entity );
}
final ManagedEntity previous = managedEntity.$$_hibernate_getPreviousManagedEntity();
final ManagedEntity next = managedEntity.$$_hibernate_getNextManagedEntity();
managedEntity.$$_hibernate_setPreviousManagedEntity( null );
managedEntity.$$_hibernate_setNextManagedEntity( null );
count--;
if ( count == 0 ) {
head = null;
tail = null;
assert previous == null;
assert next == null;
}
else {
if ( previous == null ) {
assert managedEntity == head;
head = next;
}
else {
previous.$$_hibernate_setNextManagedEntity( next );
}
if ( next == null ) {
assert managedEntity == tail;
tail = previous;
}
else {
next.$$_hibernate_setPreviousManagedEntity( previous );
}
}
final EntityEntry theEntityEntry = managedEntity.$$_hibernate_getEntityEntry();
managedEntity.$$_hibernate_setEntityEntry( null );
return theEntityEntry;
}
public Map.Entry<Object, EntityEntry>[] reentrantSafeEntityEntries() {
if ( dirty ) {
reentrantSafeEntries = new EntityEntryCrossRefImpl[count];
int i = 0;
ManagedEntity managedEntity = head;
while ( managedEntity != null ) {
reentrantSafeEntries[i++] = new EntityEntryCrossRefImpl(
managedEntity.$$_hibernate_getEntityInstance(),
managedEntity.$$_hibernate_getEntityEntry()
);
managedEntity = managedEntity.$$_hibernate_getNextManagedEntity();
}
dirty = false;
}
return reentrantSafeEntries;
}
public void clear() {
dirty = true;
ManagedEntity node = head;
while ( node != null ) {
final ManagedEntity nextNode = node.$$_hibernate_getNextManagedEntity();
node.$$_hibernate_setEntityEntry( null );
node.$$_hibernate_setPreviousManagedEntity( null );
node.$$_hibernate_setNextManagedEntity( null );
node = nextNode;
}
if ( immutableManagedEntityXref != null ) {
immutableManagedEntityXref.clear();
}
if ( nonEnhancedEntityXref != null ) {
nonEnhancedEntityXref.clear();
}
head = null;
tail = null;
count = 0;
reentrantSafeEntries = null;
}
public void downgradeLocks() {
if ( head == null ) {
return;
}
ManagedEntity node = head;
while ( node != null ) {
node.$$_hibernate_getEntityEntry().setLockMode( LockMode.NONE );
node = node.$$_hibernate_getNextManagedEntity();
}
}
public void serialize(ObjectOutputStream oos) throws IOException {
log.tracef( "Starting serialization of [%s] EntityEntry entries", count );
oos.writeInt( count );
if ( count == 0 ) {
return;
}
ManagedEntity managedEntity = head;
while ( managedEntity != null ) {
oos.writeBoolean( managedEntity == managedEntity.$$_hibernate_getEntityInstance() );
oos.writeObject( managedEntity.$$_hibernate_getEntityInstance() );
oos.writeInt( managedEntity.$$_hibernate_getEntityEntry().getClass().getName().length() );
oos.writeChars( managedEntity.$$_hibernate_getEntityEntry().getClass().getName() );
managedEntity.$$_hibernate_getEntityEntry().serialize( oos );
managedEntity = managedEntity.$$_hibernate_getNextManagedEntity();
}
}
public static EntityEntryContext deserialize(ObjectInputStream ois, StatefulPersistenceContext rtn)
throws IOException, ClassNotFoundException {
final int count = ois.readInt();
log.tracef( "Starting deserialization of [%s] EntityEntry entries", count );
final EntityEntryContext context = new EntityEntryContext( rtn );
context.count = count;
context.dirty = true;
if ( count == 0 ) {
return context;
}
ManagedEntity previous = null;
for ( int i = 0; i < count; i++ ) {
final boolean isEnhanced = ois.readBoolean();
final Object entity = ois.readObject();
final int numChars = ois.readInt();
final char[] entityEntryClassNameArr = new char[numChars];
for ( int j = 0; j < numChars; j++ ) {
entityEntryClassNameArr[j] = ois.readChar();
}
final EntityEntry entry = deserializeEntityEntry( entityEntryClassNameArr, ois, rtn );
final ManagedEntity managedEntity;
if ( isEnhanced ) {
if ( entry.getPersister().isMutable() ) {
managedEntity = (ManagedEntity) entity;
}
else {
managedEntity = new ImmutableManagedEntityHolder( (ManagedEntity) entity );
if ( context.immutableManagedEntityXref == null ) {
context.immutableManagedEntityXref =
new IdentityHashMap<ManagedEntity, ImmutableManagedEntityHolder>();
}
context.immutableManagedEntityXref.put(
(ManagedEntity) entity,
(ImmutableManagedEntityHolder) managedEntity
);
}
}
else {
managedEntity = new ManagedEntityImpl( entity );
if ( context.nonEnhancedEntityXref == null ) {
context.nonEnhancedEntityXref = new IdentityHashMap<Object, ManagedEntity>();
}
context.nonEnhancedEntityXref.put( entity, managedEntity );
}
managedEntity.$$_hibernate_setEntityEntry( entry );
if ( previous == null ) {
context.head = managedEntity;
}
else {
previous.$$_hibernate_setNextManagedEntity( managedEntity );
managedEntity.$$_hibernate_setPreviousManagedEntity( previous );
}
previous = managedEntity;
}
context.tail = previous;
return context;
}
private static EntityEntry deserializeEntityEntry(char[] entityEntryClassNameArr, ObjectInputStream ois, StatefulPersistenceContext rtn){
EntityEntry entry = null;
final String entityEntryClassName = new String( entityEntryClassNameArr );
final Class entityEntryClass = rtn.getSession().getFactory().getServiceRegistry().getService( ClassLoaderService.class ).classForName( entityEntryClassName );
try {
final Method deserializeMethod = entityEntryClass.getDeclaredMethod( "deserialize", ObjectInputStream.class, PersistenceContext.class );
entry = (EntityEntry) deserializeMethod.invoke( null, ois, rtn );
}
catch (NoSuchMethodException e) {
log.errorf( "Enable to deserialize [%s]", entityEntryClassName );
}
catch (InvocationTargetException e) {
log.errorf( "Enable to deserialize [%s]", entityEntryClassName );
}
catch (IllegalAccessException e) {
log.errorf( "Enable to deserialize [%s]", entityEntryClassName );
}
return entry;
}
public int getNumberOfManagedEntities() {
return count;
}
private static class ManagedEntityImpl implements ManagedEntity {
private final Object entityInstance;
private EntityEntry entityEntry;
private ManagedEntity previous;
private ManagedEntity next;
public ManagedEntityImpl(Object entityInstance) {
this.entityInstance = entityInstance;
}
@Override
public Object $$_hibernate_getEntityInstance() {
return entityInstance;
}
@Override
public EntityEntry $$_hibernate_getEntityEntry() {
return entityEntry;
}
@Override
public void $$_hibernate_setEntityEntry(EntityEntry entityEntry) {
this.entityEntry = entityEntry;
}
@Override
public ManagedEntity $$_hibernate_getNextManagedEntity() {
return next;
}
@Override
public void $$_hibernate_setNextManagedEntity(ManagedEntity next) {
this.next = next;
}
@Override
public ManagedEntity $$_hibernate_getPreviousManagedEntity() {
return previous;
}
@Override
public void $$_hibernate_setPreviousManagedEntity(ManagedEntity previous) {
this.previous = previous;
}
}
private static class ImmutableManagedEntityHolder implements ManagedEntity {
private ManagedEntity managedEntity;
private ManagedEntity previous;
private ManagedEntity next;
public ImmutableManagedEntityHolder(ManagedEntity immutableManagedEntity) {
this.managedEntity = immutableManagedEntity;
}
@Override
public Object $$_hibernate_getEntityInstance() {
return managedEntity.$$_hibernate_getEntityInstance();
}
@Override
public EntityEntry $$_hibernate_getEntityEntry() {
return managedEntity.$$_hibernate_getEntityEntry();
}
@Override
public void $$_hibernate_setEntityEntry(EntityEntry entityEntry) {
if ( entityEntry == null ) {
if ( canClearEntityEntryReference() ) {
managedEntity.$$_hibernate_setEntityEntry( null );
}
}
else {
managedEntity.$$_hibernate_setEntityEntry( entityEntry );
}
}
@Override
public ManagedEntity $$_hibernate_getPreviousManagedEntity() {
return previous;
}
@Override
public void $$_hibernate_setPreviousManagedEntity(ManagedEntity previous) {
this.previous = previous;
}
@Override
public ManagedEntity $$_hibernate_getNextManagedEntity() {
return next;
}
@Override
public void $$_hibernate_setNextManagedEntity(ManagedEntity next) {
this.next = next;
}
private boolean canClearEntityEntryReference(){
if( managedEntity.$$_hibernate_getEntityEntry() == null ) {
return true;
}
if( !(managedEntity.$$_hibernate_getEntityEntry() instanceof ImmutableEntityEntry) ) {
return true;
}
else if( managedEntity.$$_hibernate_getEntityEntry().getPersister().canUseReferenceCacheEntries() ) {
return false;
}
return true;
}
}
public static interface EntityEntryCrossRef extends Map.Entry<Object,EntityEntry> {
public Object getEntity();
public EntityEntry getEntityEntry();
}
private static class EntityEntryCrossRefImpl implements EntityEntryCrossRef {
private final Object entity;
private EntityEntry entityEntry;
private EntityEntryCrossRefImpl(Object entity, EntityEntry entityEntry) {
this.entity = entity;
this.entityEntry = entityEntry;
}
@Override
public Object getEntity() {
return entity;
}
@Override
public EntityEntry getEntityEntry() {
return entityEntry;
}
@Override
public Object getKey() {
return getEntity();
}
@Override
public EntityEntry getValue() {
return getEntityEntry();
}
@Override
public EntityEntry setValue(EntityEntry entityEntry) {
final EntityEntry old = this.entityEntry;
this.entityEntry = entityEntry;
return old;
}
}
}