1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.jboss.cache.pojo.impl; |
9 |
| |
10 |
| import org.apache.commons.logging.Log; |
11 |
| import org.apache.commons.logging.LogFactory; |
12 |
| import org.jboss.aop.Advised; |
13 |
| import org.jboss.aop.InstanceAdvisor; |
14 |
| import org.jboss.aop.advice.Interceptor; |
15 |
| import org.jboss.aop.proxy.ClassProxy; |
16 |
| import org.jboss.cache.Cache; |
17 |
| import org.jboss.cache.CacheException; |
18 |
| import org.jboss.cache.CacheSPI; |
19 |
| import org.jboss.cache.Fqn; |
20 |
| import org.jboss.cache.pojo.PojoCacheException; |
21 |
| import org.jboss.cache.pojo.collection.CollectionInterceptorUtil; |
22 |
| import org.jboss.cache.pojo.interceptors.dynamic.BaseInterceptor; |
23 |
| import org.jboss.cache.pojo.util.AopUtil; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| class ObjectGraphHandler |
33 |
| { |
34 |
| private PojoCacheImpl cache; |
35 |
| private InternalHelper internal_; |
36 |
| private final static Log log = LogFactory.getLog(ObjectGraphHandler.class); |
37 |
| |
38 |
499
| public ObjectGraphHandler(PojoCacheImpl cache, InternalHelper internal)
|
39 |
| { |
40 |
499
| this.cache = cache;
|
41 |
499
| internal_ = internal;
|
42 |
| } |
43 |
| |
44 |
0
| Object get(Fqn fqn, Class clazz, PojoInstance pojoInstance) throws CacheException
|
45 |
| { |
46 |
| |
47 |
0
| Object obj;
|
48 |
| |
49 |
0
| obj = cache.getObject(fqn);
|
50 |
0
| if (obj == null)
|
51 |
0
| throw new PojoCacheException("ObjectGraphHandler.get(): null object from internal ref node." +
|
52 |
| " Internal ref node: " + fqn); |
53 |
| |
54 |
0
| return obj;
|
55 |
| } |
56 |
| |
57 |
192
| void put(Fqn fqn, Object obj, String field) throws CacheException
|
58 |
| { |
59 |
192
| CachedType type = cache.getCachedType(obj.getClass());
|
60 |
| |
61 |
192
| InstanceAdvisor advisor = null;
|
62 |
192
| Interceptor interceptor = null;
|
63 |
| |
64 |
192
| if (obj instanceof Advised)
|
65 |
| { |
66 |
161
| advisor = ((Advised) obj)._getInstanceAdvisor();
|
67 |
161
| if (advisor == null)
|
68 |
0
| throw new PojoCacheException("put(): InstanceAdvisor is null for: " + obj);
|
69 |
| |
70 |
161
| interceptor = AopUtil.findCacheInterceptor(advisor);
|
71 |
| } |
72 |
| else |
73 |
| { |
74 |
31
| advisor = ((ClassProxy) obj)._getInstanceAdvisor();
|
75 |
31
| if (advisor == null)
|
76 |
0
| throw new PojoCacheException("put(): InstanceAdvisor is null for: " + obj);
|
77 |
31
| interceptor = CollectionInterceptorUtil.getInterceptor((ClassProxy) obj);
|
78 |
| } |
79 |
| |
80 |
192
| Fqn originalFqn = null;
|
81 |
| |
82 |
| |
83 |
192
| originalFqn = ((BaseInterceptor) interceptor).getFqn();
|
84 |
| |
85 |
| |
86 |
192
| setupRefCounting(fqn, originalFqn);
|
87 |
| |
88 |
192
| PojoReference pojoReference = new PojoReference();
|
89 |
192
| pojoReference.setFqn(originalFqn);
|
90 |
192
| pojoReference.setPojoClass(type.getType());
|
91 |
192
| internal_.putPojoReference(fqn, pojoReference, field);
|
92 |
| } |
93 |
| |
94 |
6316
| boolean isMultipleReferenced(Fqn internalFqn)
|
95 |
| { |
96 |
| |
97 |
6316
| PojoInstance pojoInstance = null;
|
98 |
6316
| try
|
99 |
| { |
100 |
6316
| pojoInstance = internal_.getPojoInstance(internalFqn);
|
101 |
| } |
102 |
| catch (CacheException e) |
103 |
| { |
104 |
0
| throw new PojoCacheException("Exception in isMultipleReferenced", e);
|
105 |
| } |
106 |
| |
107 |
6316
| return InternalHelper.isMultipleReferenced(pojoInstance);
|
108 |
| |
109 |
| } |
110 |
| |
111 |
38
| void remove(Fqn referencingFqn, Fqn internalFqn, Object pojo)
|
112 |
| throws CacheException |
113 |
| { |
114 |
38
| if (log.isDebugEnabled())
|
115 |
| { |
116 |
0
| log.debug("remove(): removing object fqn: " + referencingFqn
|
117 |
| + " Will just de-reference it."); |
118 |
| } |
119 |
38
| removeFromReference(referencingFqn, internalFqn);
|
120 |
| } |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
38
| private void removeFromReference(Fqn referencingFqn, Fqn originalFqn) throws CacheException
|
126 |
| { |
127 |
38
| synchronized (referencingFqn)
|
128 |
| { |
129 |
| |
130 |
38
| if (decrementRefCount(referencingFqn, originalFqn) == PojoInstance.INITIAL_COUNTER_VALUE)
|
131 |
| { |
132 |
| |
133 |
| |
134 |
0
| cache.detach(referencingFqn);
|
135 |
| } |
136 |
| } |
137 |
| } |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
192
| private void setupRefCounting(Fqn fqn, Fqn refFqn) throws CacheException
|
147 |
| { |
148 |
192
| synchronized (refFqn)
|
149 |
| { |
150 |
| |
151 |
192
| incrementRefCount(refFqn, fqn);
|
152 |
| |
153 |
192
| if (log.isTraceEnabled())
|
154 |
| { |
155 |
0
| log.trace("setupRefCounting(): current fqn: " + fqn + " set to point to: " + refFqn);
|
156 |
| } |
157 |
| } |
158 |
| } |
159 |
| |
160 |
192
| private int incrementRefCount(Fqn originalFqn, Fqn referencingFqn) throws CacheException
|
161 |
| { |
162 |
192
| return internal_.incrementRefCount(originalFqn, referencingFqn);
|
163 |
| } |
164 |
| |
165 |
38
| private int decrementRefCount(Fqn referencingFqn, Fqn originalFqn) throws CacheException
|
166 |
| { |
167 |
38
| int count = 0;
|
168 |
?
| if ((count = internal_.decrementRefCount(originalFqn, referencingFqn)) == (PojoInstance.INITIAL_COUNTER_VALUE + 1))
|
169 |
| { |
170 |
34
| internal_.removeIndirectFqn(originalFqn.toString());
|
171 |
| } |
172 |
| |
173 |
38
| return count;
|
174 |
| } |
175 |
| } |