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