diff options
Diffstat (limited to 'plugins/org.eclipse.emf.cdo.server.hibernate/src/org/eclipse/emf/cdo/server/internal/hibernate/tuplizer/PersistableListHolder.java')
-rw-r--r-- | plugins/org.eclipse.emf.cdo.server.hibernate/src/org/eclipse/emf/cdo/server/internal/hibernate/tuplizer/PersistableListHolder.java | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/plugins/org.eclipse.emf.cdo.server.hibernate/src/org/eclipse/emf/cdo/server/internal/hibernate/tuplizer/PersistableListHolder.java b/plugins/org.eclipse.emf.cdo.server.hibernate/src/org/eclipse/emf/cdo/server/internal/hibernate/tuplizer/PersistableListHolder.java new file mode 100644 index 0000000000..91962e873a --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.server.hibernate/src/org/eclipse/emf/cdo/server/internal/hibernate/tuplizer/PersistableListHolder.java @@ -0,0 +1,103 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Martin Taal and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Martin Taal - copied from CDORevisionPropertyHandler and adapted + **************************************************************************/ +package org.eclipse.emf.cdo.server.internal.hibernate.tuplizer; + +import org.eclipse.emf.cdo.protocol.model.CDOFeature; +import org.eclipse.emf.cdo.protocol.revision.CDORevision; +import org.eclipse.emf.cdo.server.internal.hibernate.bundle.OM; + +import org.eclipse.net4j.internal.util.om.trace.ContextTracer; + +import org.hibernate.collection.PersistentCollection; + +import java.util.HashMap; +import java.util.Map; + +/** + * Keeps mappings from object/feature combinations to a hibernate persistable list. This works because the write action + * is done in one thread. + * + * @author Martin Taal + */ + +public class PersistableListHolder +{ + private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, PersistableListHolder.class); + + private static PersistableListHolder instance = new PersistableListHolder(); + + public static PersistableListHolder getInstance() + { + return instance; + } + + public static void setInstance(PersistableListHolder instance) + { + PersistableListHolder.instance = instance; + } + + private ThreadLocal<Map<Key, PersistentCollection>> listMapping = new ThreadLocal<Map<Key, PersistentCollection>>(); + + public void putListMapping(Object target, CDOFeature cdoFeature, PersistentCollection collection) + { + final Key key = new Key(target, cdoFeature); + getListMapping().put(key, collection); + TRACER.trace("Stored hb list in threadlocal: " + ((CDORevision)target).getCDOClass().getName() + "." + + cdoFeature.getName()); + } + + public PersistentCollection getListMapping(Object target, CDOFeature cdoFeature) + { + final Key key = new Key(target, cdoFeature); + return getListMapping().get(key); + } + + private Map<Key, PersistentCollection> getListMapping() + { + if (listMapping.get() == null) + { + listMapping.set(new HashMap<Key, PersistentCollection>()); + } + return listMapping.get(); + } + + private class Key + { + private Object owner; + + private CDOFeature cdoFeature; + + Key(Object owner, CDOFeature cdoFeature) + { + this.owner = owner; + this.cdoFeature = cdoFeature; + } + + @Override + public boolean equals(Object obj) + { + if (!(obj instanceof Key)) + { + return false; + } + final Key otherKey = (Key)obj; + // the owner is uniquely present in mem, the same applies for the cdoFeature + // therefore == is allowed + return owner == otherKey.owner && cdoFeature == otherKey.cdoFeature; + } + + @Override + public int hashCode() + { + return owner.hashCode() + cdoFeature.hashCode(); + } + } +} |