1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.pojo.interceptors.dynamic; |
8 |
| |
9 |
| import org.jboss.aop.advice.Interceptor; |
10 |
| import org.jboss.cache.Fqn; |
11 |
| import org.jboss.cache.pojo.collection.CachedListImpl; |
12 |
| import org.jboss.cache.pojo.collection.CollectionInterceptorUtil; |
13 |
| import org.jboss.cache.pojo.impl.PojoCacheImpl; |
14 |
| |
15 |
| import java.util.ArrayList; |
16 |
| import java.util.List; |
17 |
| import java.util.Map; |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| @SuppressWarnings({"CanBeFinal"}) |
26 |
| public class CachedListInterceptor extends AbstractCollectionInterceptor |
27 |
| { |
28 |
| |
29 |
| |
30 |
| private static final Map managedMethods_ = |
31 |
| CollectionInterceptorUtil.getManagedMethods(List.class); |
32 |
| |
33 |
| private Map methodMap_; |
34 |
| |
35 |
| private List cacheImpl_; |
36 |
| |
37 |
| private List inMemImpl_; |
38 |
| |
39 |
| private List current_; |
40 |
| |
41 |
1572
| public CachedListInterceptor(PojoCacheImpl cache, Fqn fqn, Class clazz, List obj)
|
42 |
| { |
43 |
1572
| super(cache, fqn);
|
44 |
1572
| methodMap_ = CollectionInterceptorUtil.getMethodMap(clazz);
|
45 |
1572
| cacheImpl_ = new CachedListImpl(cache, this);
|
46 |
1572
| inMemImpl_ = obj;
|
47 |
1572
| current_ = cacheImpl_;
|
48 |
| } |
49 |
| |
50 |
0
| private CachedListInterceptor(PojoCacheImpl cache, Fqn fqn)
|
51 |
| { |
52 |
0
| super(cache, fqn);
|
53 |
| } |
54 |
| |
55 |
0
| public Object clone()
|
56 |
| { |
57 |
0
| CachedListInterceptor interceptor = new CachedListInterceptor(cache, fqn);
|
58 |
0
| interceptor.setFqn(getFqn());
|
59 |
0
| interceptor.setAopInstance(getAopInstance());
|
60 |
0
| interceptor.setCurrentCopy(getCurrentCopy());
|
61 |
0
| interceptor.setInMemoryCopy(getInMemoryCopy());
|
62 |
0
| interceptor.setCacheCopy(getCacheCopy());
|
63 |
0
| return interceptor;
|
64 |
| } |
65 |
| |
66 |
0
| public void setInterceptor(Interceptor intcptr)
|
67 |
| { |
68 |
0
| CachedListInterceptor interceptor = (CachedListInterceptor) intcptr;
|
69 |
0
| setFqn(interceptor.getFqn());
|
70 |
0
| setAopInstance(interceptor.getAopInstance());
|
71 |
0
| setCurrentCopy(interceptor.getCurrentCopy());
|
72 |
0
| setInMemoryCopy(interceptor.getInMemoryCopy());
|
73 |
0
| setCacheCopy(interceptor.getCacheCopy());
|
74 |
| } |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
0
| public void attach(Fqn fqn, boolean copyToCache)
|
81 |
| { |
82 |
0
| super.attach(fqn, copyToCache);
|
83 |
| |
84 |
0
| if (copyToCache)
|
85 |
0
| toCache();
|
86 |
0
| current_ = cacheImpl_;
|
87 |
| } |
88 |
| |
89 |
0
| private void toCache()
|
90 |
| { |
91 |
0
| if (inMemImpl_ == null)
|
92 |
0
| throw new IllegalStateException("CachedListInterceptor.toCache(). inMemImpl is null.");
|
93 |
| |
94 |
| |
95 |
0
| List tmpList = new ArrayList();
|
96 |
0
| for (int i = inMemImpl_.size(); i > 0; i--)
|
97 |
| { |
98 |
0
| Object obj = inMemImpl_.remove(i - 1);
|
99 |
0
| tmpList.add(obj);
|
100 |
| } |
101 |
| |
102 |
0
| int size = tmpList.size();
|
103 |
0
| for (int i = 0; i < tmpList.size(); i++)
|
104 |
| { |
105 |
0
| cacheImpl_.add(tmpList.get(size - i - 1));
|
106 |
| } |
107 |
| |
108 |
0
| inMemImpl_ = null;
|
109 |
| } |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
1303
| public void detach(boolean removeFromCache)
|
116 |
| { |
117 |
1303
| super.detach(removeFromCache);
|
118 |
1303
| toMemory(removeFromCache);
|
119 |
1303
| current_ = inMemImpl_;
|
120 |
| } |
121 |
| |
122 |
1303
| public Object getCurrentCopy()
|
123 |
| { |
124 |
1303
| return current_;
|
125 |
| } |
126 |
| |
127 |
0
| void setInMemoryCopy(Object obj)
|
128 |
| { |
129 |
0
| inMemImpl_ = (List) obj;
|
130 |
| } |
131 |
| |
132 |
0
| Object getInMemoryCopy()
|
133 |
| { |
134 |
0
| return inMemImpl_;
|
135 |
| } |
136 |
| |
137 |
0
| Object getCacheCopy()
|
138 |
| { |
139 |
0
| return cacheImpl_;
|
140 |
| } |
141 |
| |
142 |
0
| void setCacheCopy(Object obj)
|
143 |
| { |
144 |
0
| cacheImpl_ = (List) obj;
|
145 |
| } |
146 |
| |
147 |
0
| void setCurrentCopy(Object obj)
|
148 |
| { |
149 |
0
| current_ = (List) obj;
|
150 |
| } |
151 |
| |
152 |
1303
| private void toMemory(boolean removeFromCache)
|
153 |
| { |
154 |
1303
| if (inMemImpl_ == null)
|
155 |
| { |
156 |
0
| inMemImpl_ = new ArrayList();
|
157 |
| } |
158 |
| |
159 |
| |
160 |
1303
| List tmpList = new ArrayList();
|
161 |
1303
| for (int i = cacheImpl_.size(); i > 0; i--)
|
162 |
| { |
163 |
2872
| int j = i - 1;
|
164 |
2872
| Object obj = null;
|
165 |
2872
| if (removeFromCache)
|
166 |
| { |
167 |
2872
| obj = cacheImpl_.remove(j);
|
168 |
| } |
169 |
| else |
170 |
| { |
171 |
0
| obj = cacheImpl_.get(j);
|
172 |
| } |
173 |
| |
174 |
2872
| tmpList.add(obj);
|
175 |
| } |
176 |
| |
177 |
1303
| int size = tmpList.size();
|
178 |
1303
| inMemImpl_.clear();
|
179 |
1303
| for (int i = 0; i < tmpList.size(); i++)
|
180 |
| { |
181 |
2872
| inMemImpl_.add(tmpList.get(size - i - 1));
|
182 |
| } |
183 |
| } |
184 |
| |
185 |
0
| public String getName()
|
186 |
| { |
187 |
0
| return "CachedListInterceptor";
|
188 |
| } |
189 |
| |
190 |
4319
| public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable
|
191 |
| { |
192 |
4319
| if (current_ == null)
|
193 |
0
| throw new IllegalStateException("CachedListInterceptor.invoke(). current_ is null.");
|
194 |
| |
195 |
4319
| return CollectionInterceptorUtil.invoke(invocation,
|
196 |
| this, |
197 |
| current_, |
198 |
| methodMap_, managedMethods_); |
199 |
| } |
200 |
| } |