1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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();
}
}
}
|