1 /*
2 * Copyright 2007 Jim Procter
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.castor.core.util;
17
18 import java.util.IdentityHashMap;
19 import java.util.concurrent.locks.ReentrantReadWriteLock;
20
21 /**
22 * <p>
23 * lightweight mechanism for thread-safe detection of cyclic calls to hashCode or equals in
24 * objects created by the XML CodeGenerator.
25 * </p>
26 * <p>Usage</p>
27 * <ol><li>
28 * startingToCycle is called on a particular object prior to recursing on it, and recursion
29 * should only occur if this call returns false.
30 * </li>
31 * <li>
32 * releaseCycleHandle is called after the recursive call returns in order to release the cycle
33 * lock on the object.
34 * </li>
35 * </ol>
36 * <p><strong>Note :</strong> Do not use this cycle breaking mechanism on object comparisons
37 * where two instances may share the same reference to some third object, such as a String constant.
38 * </p>
39 * @author <a href="mailto:jimp@compbio.dundee.ac.uk">Jim Procter</a>
40 */
41 public class CycleBreaker {
42
43 /**
44 * Hash of threads and objects that we are keeping track of.
45 */
46 private static final IdentityHashMap<Thread, IdentityHashMap<Object, Object>> _threadHash =
47 new IdentityHashMap<Thread, IdentityHashMap<Object, Object>>();
48
49 /**
50 * Lock used to isolate write accesses to <tt>_threadHash</tt>.
51 * @since 1.3.1
52 */
53 private static final ReentrantReadWriteLock _lock = new ReentrantReadWriteLock();
54
55 /**
56 * Test to see if we are about to begin cycling on a method call to beingHashed.
57 *
58 * @param beingHashed the object to check for a cycle.
59 * @return true if a cycle is about to occur on this non-null object.
60 */
61 public static boolean startingToCycle(final Object beingHashed) {
62 if (beingHashed == null) {
63 return false;
64 }
65
66 // before accessing _threadHash acquire read lock first
67 _lock.readLock().lock();
68 IdentityHashMap<Object, Object> hthr = _threadHash.get(Thread.currentThread());
69 _lock.readLock().unlock();
70 if (hthr == null) {
71 hthr = new IdentityHashMap<Object, Object>();
72 hthr.put(beingHashed, beingHashed);
73
74 // before accessing _threadHash acquire write lock first
75 _lock.writeLock().lock();
76 _threadHash.put(Thread.currentThread(), hthr);
77 _lock.writeLock().unlock();
78 return false; // first call. no cycle detected
79 }
80
81 // it's not necessary to lock here because every entry
82 // read from _threadHash is accessed by the same thread which
83 // added it to _threadHash (in _threadHash all values are keyed
84 // by the threads hash)
85
86 Object objhandle = hthr.get(beingHashed);
87 if (objhandle == null) {
88
89 // this is the default for a hash value currently being computed.
90 hthr.put(beingHashed, beingHashed);
91 return false; // first call. no cycle detected.
92 }
93 return true;
94 }
95
96 /**
97 * Called to release Cycling lock for this object at the end of a routine
98 * where cycles are to be detected.
99 *
100 * @param beingHashed the object for which the cycle-lock will be released.
101 */
102 public static void releaseCycleHandle(final Object beingHashed) {
103 if (beingHashed == null) {
104 return;
105 }
106
107 Thread currentThread = Thread.currentThread();
108
109 // before accessing _threadHash acquire read lock first
110 _lock.readLock().lock();
111 IdentityHashMap<Object, Object> hthr = _threadHash.get(currentThread);
112 _lock.readLock().unlock();
113 if (hthr != null) {
114
115 // it's not necessary to lock here because every entry
116 // read from _threadHash is accessed by the same thread which
117 // added it to _threadHash (in _threadHash all values are keyed
118 // by the threads hash)
119 if (hthr.containsKey(beingHashed)) {
120 hthr.remove(beingHashed);
121
122 // release any references if we have no more CycleHandles
123 if (hthr.size() == 0) {
124
125 // before removing the threads empty hash from _threadHash
126 // acquire write lock first
127 _lock.writeLock().lock();
128 _threadHash.remove(currentThread);
129 _lock.writeLock().unlock();
130 }
131 }
132 }
133 }
134 }