1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.services.impl; |
16 |
|
|
17 |
|
import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock; |
18 |
|
import ognl.ClassCacheInspector; |
19 |
|
import ognl.Node; |
20 |
|
import ognl.Ognl; |
21 |
|
import ognl.OgnlRuntime; |
22 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
23 |
|
import org.apache.tapestry.AbstractComponent; |
24 |
|
import org.apache.tapestry.event.ReportStatusEvent; |
25 |
|
import org.apache.tapestry.event.ReportStatusListener; |
26 |
|
import org.apache.tapestry.event.ResetEventListener; |
27 |
|
import org.apache.tapestry.services.ExpressionCache; |
28 |
|
import org.apache.tapestry.services.ExpressionEvaluator; |
29 |
|
|
30 |
|
import java.beans.Introspector; |
31 |
|
import java.util.HashMap; |
32 |
|
import java.util.Map; |
33 |
|
import java.util.WeakHashMap; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
24 |
public class ExpressionCacheImpl implements ExpressionCache, ResetEventListener, ReportStatusListener, ClassCacheInspector { |
40 |
|
|
41 |
24 |
private final ReentrantLock _lock = new ReentrantLock(); |
42 |
|
|
43 |
|
private String _serviceId; |
44 |
|
|
45 |
24 |
private Map _cache = new WeakHashMap(); |
46 |
|
|
47 |
24 |
private Map _objectCache = new WeakHashMap(); |
48 |
|
|
49 |
|
private ExpressionEvaluator _evaluator; |
50 |
|
|
51 |
24 |
private final boolean _cachingDisabled = Boolean.getBoolean("org.apache.tapestry.disable-caching"); |
52 |
|
|
53 |
|
public void initializeService() |
54 |
|
{ |
55 |
0 |
if (_cachingDisabled) |
56 |
|
{ |
57 |
0 |
OgnlRuntime.setClassCacheInspector(this); |
58 |
|
} |
59 |
0 |
} |
60 |
|
|
61 |
|
public void resetEventDidOccur() |
62 |
|
{ |
63 |
|
try { |
64 |
|
|
65 |
0 |
_lock.lock(); |
66 |
|
|
67 |
0 |
_cache.clear(); |
68 |
0 |
_objectCache.clear(); |
69 |
|
|
70 |
0 |
Introspector.flushCaches(); |
71 |
|
|
72 |
|
} finally { |
73 |
|
|
74 |
0 |
_lock.unlock(); |
75 |
0 |
} |
76 |
0 |
} |
77 |
|
|
78 |
|
public boolean shouldCache(Class type) |
79 |
|
{ |
80 |
0 |
if (!_cachingDisabled || type == null |
81 |
0 |
|| AbstractComponent.class.isAssignableFrom(type)) |
82 |
0 |
return false; |
83 |
|
|
84 |
0 |
return true; |
85 |
|
} |
86 |
|
|
87 |
|
public void reportStatus(ReportStatusEvent event) |
88 |
|
{ |
89 |
0 |
event.title(_serviceId); |
90 |
|
|
91 |
0 |
event.property("cached expression count", _cache.size()); |
92 |
0 |
event.collection("cached expressions", _cache.keySet()); |
93 |
|
|
94 |
0 |
event.property("cached object expression count", _objectCache.size()); |
95 |
0 |
} |
96 |
|
|
97 |
|
public Object getCompiledExpression(Object target, String expression) |
98 |
|
{ |
99 |
|
try { |
100 |
|
|
101 |
11 |
_lock.lock(); |
102 |
|
|
103 |
11 |
Map cached = (Map)_objectCache.get(target.getClass()); |
104 |
|
|
105 |
11 |
if (cached == null) |
106 |
|
{ |
107 |
9 |
cached = new HashMap(); |
108 |
9 |
_objectCache.put(target.getClass(), cached); |
109 |
|
} |
110 |
|
|
111 |
11 |
Node result = (Node)cached.get(expression); |
112 |
|
|
113 |
11 |
if (result == null || result.getAccessor() == null) |
114 |
|
{ |
115 |
11 |
result = parse(target, expression); |
116 |
10 |
cached.put(expression, result); |
117 |
|
} |
118 |
|
|
119 |
10 |
return result; |
120 |
|
|
121 |
|
} finally { |
122 |
|
|
123 |
10 |
_lock.unlock(); |
124 |
|
} |
125 |
|
} |
126 |
|
|
127 |
|
public Object getCompiledExpression(String expression) |
128 |
|
{ |
129 |
|
try { |
130 |
|
|
131 |
6 |
_lock.lock(); |
132 |
|
|
133 |
6 |
Object result = _cache.get(expression); |
134 |
|
|
135 |
6 |
if (result == null) |
136 |
|
{ |
137 |
6 |
result = parse(expression); |
138 |
6 |
_cache.put(expression, result); |
139 |
|
} |
140 |
|
|
141 |
6 |
return result; |
142 |
|
} finally { |
143 |
|
|
144 |
6 |
_lock.unlock(); |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
private Node parse(Object target, String expression) |
149 |
|
{ |
150 |
|
try |
151 |
|
{ |
152 |
11 |
return Ognl.compileExpression(_evaluator.createContext(target), target, expression); |
153 |
|
} |
154 |
1 |
catch (Exception ex) |
155 |
|
{ |
156 |
1 |
throw new ApplicationRuntimeException(ImplMessages.unableToParseExpression(expression,ex), ex); |
157 |
|
} |
158 |
|
} |
159 |
|
|
160 |
|
private Object parse(String expression) |
161 |
|
{ |
162 |
|
try |
163 |
|
{ |
164 |
6 |
return Ognl.parseExpression(expression); |
165 |
|
} |
166 |
0 |
catch (Exception ex) |
167 |
|
{ |
168 |
0 |
throw new ApplicationRuntimeException(ImplMessages.unableToParseExpression(expression, ex), ex); |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
|
public void setServiceId(String serviceId) |
173 |
|
{ |
174 |
0 |
_serviceId = serviceId; |
175 |
0 |
} |
176 |
|
|
177 |
|
public void setEvaluator(ExpressionEvaluator evaluator) |
178 |
|
{ |
179 |
24 |
_evaluator = evaluator; |
180 |
24 |
} |
181 |
|
|
182 |
|
} |