1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.pojo.util; |
8 |
| |
9 |
| import org.apache.commons.logging.Log; |
10 |
| import org.apache.commons.logging.LogFactory; |
11 |
| import org.jboss.aop.Advised; |
12 |
| import org.jboss.cache.CacheException; |
13 |
| import org.jboss.cache.Fqn; |
14 |
| import org.jboss.cache.pojo.PojoCacheException; |
15 |
| import org.jboss.cache.pojo.impl.CachedType; |
16 |
| import org.jboss.cache.pojo.impl.PojoCacheImpl; |
17 |
| import org.jboss.cache.pojo.memory.FieldPersistentReference; |
18 |
| |
19 |
| import java.lang.reflect.Field; |
20 |
| import java.util.Collection; |
21 |
| import java.util.HashSet; |
22 |
| import java.util.Iterator; |
23 |
| import java.util.Map; |
24 |
| import java.util.Set; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public final class ObjectUtil |
32 |
| { |
33 |
| private static final Log log = LogFactory.getLog(ObjectUtil.class.getName()); |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
3
| public static boolean isReachable(PojoCacheImpl cache, Object originalObject, Object thisObject)
|
45 |
| throws CacheException |
46 |
| { |
47 |
3
| HashSet objSet = new HashSet();
|
48 |
| |
49 |
3
| return isReachableInner(cache, originalObject, thisObject, objSet);
|
50 |
| } |
51 |
| |
52 |
16
| private static boolean isReachableInner(PojoCacheImpl cache, Object originalObject,
|
53 |
| Object thisObject, Set objSet) |
54 |
| throws CacheException |
55 |
| { |
56 |
| |
57 |
16
| if (!(originalObject instanceof Advised))
|
58 |
0
| throw new PojoCacheException("ObjectUtil.isReachable(): originalObject is not Advised.");
|
59 |
| |
60 |
16
| if (log.isTraceEnabled())
|
61 |
| { |
62 |
0
| log.trace("isReachable(): current object: " + originalObject + " this object: " + thisObject);
|
63 |
| } |
64 |
| |
65 |
16
| if (originalObject.equals(thisObject))
|
66 |
| { |
67 |
2
| if (log.isTraceEnabled())
|
68 |
| { |
69 |
0
| log.trace("isReachable(): object found reachable.");
|
70 |
| } |
71 |
| |
72 |
2
| return true;
|
73 |
| } |
74 |
| |
75 |
14
| if (!objSet.contains(originalObject))
|
76 |
| { |
77 |
10
| objSet.add(originalObject);
|
78 |
| } else |
79 |
| { |
80 |
4
| return false;
|
81 |
| } |
82 |
| |
83 |
10
| CachedType type = cache.getCachedType(originalObject.getClass());
|
84 |
10
| for (Iterator i = type.getFields().iterator(); i.hasNext();)
|
85 |
| { |
86 |
36
| Field field = (Field) (((FieldPersistentReference) i.next())).get();
|
87 |
36
| Object value = null;
|
88 |
36
| try
|
89 |
| { |
90 |
36
| value = field.get(originalObject);
|
91 |
| } |
92 |
| catch (IllegalAccessException e) |
93 |
| { |
94 |
0
| throw new CacheException("field access failed", e);
|
95 |
| } |
96 |
36
| CachedType fieldType = cache.getCachedType(field.getType());
|
97 |
36
| if (fieldType.isImmediate())
|
98 |
| { |
99 |
| } else |
100 |
| { |
101 |
16
| if (value instanceof Map)
|
102 |
| { |
103 |
0
| Set set = ((Map) value).keySet();
|
104 |
0
| for (Iterator it = set.iterator(); it.hasNext();)
|
105 |
| { |
106 |
0
| if (isReachableInner(cache, it.next(), thisObject, objSet))
|
107 |
0
| return true;
|
108 |
| } |
109 |
| |
110 |
0
| continue;
|
111 |
16
| } else if (value instanceof Collection)
|
112 |
| { |
113 |
10
| for (Iterator it = ((Collection) value).iterator(); it.hasNext();)
|
114 |
| { |
115 |
8
| if (isReachableInner(cache, it.next(), thisObject, objSet))
|
116 |
4
| return true;
|
117 |
| } |
118 |
| |
119 |
6
| continue;
|
120 |
| } |
121 |
| |
122 |
6
| if (!(value instanceof Advised))
|
123 |
1
| continue;
|
124 |
| |
125 |
5
| if (isReachableInner(cache, value, thisObject, objSet))
|
126 |
1
| return true;
|
127 |
| } |
128 |
| } |
129 |
| |
130 |
5
| return false;
|
131 |
| } |
132 |
| |
133 |
0
| public static String getIndirectFqn(Fqn fqn)
|
134 |
| { |
135 |
| |
136 |
| |
137 |
0
| return getIndirectFqn(fqn.toString());
|
138 |
| } |
139 |
| |
140 |
68
| public static String getIndirectFqn(String fqn)
|
141 |
| { |
142 |
| |
143 |
| |
144 |
68
| return fqn.replace('/', '_');
|
145 |
| } |
146 |
| |
147 |
4
| public static String identityString(Object object)
|
148 |
| { |
149 |
4
| return object.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(object));
|
150 |
| } |
151 |
| } |