1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.pageload; |
16 |
|
|
17 |
|
import org.apache.commons.pool.BaseKeyedPoolableObjectFactory; |
18 |
|
import org.apache.commons.pool.impl.GenericKeyedObjectPool; |
19 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
20 |
|
import org.apache.hivemind.ClassResolver; |
21 |
|
import org.apache.hivemind.events.RegistryShutdownListener; |
22 |
|
import org.apache.tapestry.IEngine; |
23 |
|
import org.apache.tapestry.IPage; |
24 |
|
import org.apache.tapestry.IRequestCycle; |
25 |
|
import org.apache.tapestry.Tapestry; |
26 |
|
import org.apache.tapestry.engine.IPageLoader; |
27 |
|
import org.apache.tapestry.engine.IPageSource; |
28 |
|
import org.apache.tapestry.engine.IPropertySource; |
29 |
|
import org.apache.tapestry.event.ReportStatusEvent; |
30 |
|
import org.apache.tapestry.event.ReportStatusListener; |
31 |
|
import org.apache.tapestry.event.ResetEventListener; |
32 |
|
import org.apache.tapestry.internal.pageload.PageKey; |
33 |
|
import org.apache.tapestry.resolver.PageSpecificationResolver; |
34 |
|
import org.apache.tapestry.util.MultiKey; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
2 |
public class PageSource extends BaseKeyedPoolableObjectFactory implements IPageSource, ResetEventListener, ReportStatusListener, RegistryShutdownListener { |
52 |
|
|
53 |
|
|
54 |
|
private ClassResolver _classResolver; |
55 |
|
|
56 |
|
|
57 |
|
private PageSpecificationResolver _pageSpecificationResolver; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
private IPageLoader _loader; |
62 |
|
|
63 |
|
private IPropertySource _propertySource; |
64 |
|
|
65 |
|
private String _serviceId; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
private IRequestCycle _cycle; |
71 |
|
|
72 |
|
static final long MINUTE = 1000 * 60; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
GenericKeyedObjectPool _pool; |
79 |
|
|
80 |
|
public void initializeService() |
81 |
|
{ |
82 |
0 |
_pool = new GenericKeyedObjectPool(this); |
83 |
|
|
84 |
0 |
_pool.setMaxActive(Integer.parseInt(_propertySource.getPropertyValue("org.apache.tapestry.page-pool-max-active"))); |
85 |
0 |
_pool.setMaxIdle(Integer.parseInt(_propertySource.getPropertyValue("org.apache.tapestry.page-pool-max-idle"))); |
86 |
|
|
87 |
0 |
_pool.setMinIdle(Integer.parseInt(_propertySource.getPropertyValue("org.apache.tapestry.page-pool-min-idle"))); |
88 |
|
|
89 |
0 |
_pool.setMinEvictableIdleTimeMillis(MINUTE * Long.parseLong(_propertySource.getPropertyValue("org.apache.tapestry.page-pool-evict-idle-page-minutes"))); |
90 |
0 |
_pool.setTimeBetweenEvictionRunsMillis(MINUTE * Long.parseLong(_propertySource.getPropertyValue("org.apache.tapestry.page-pool-evict-thread-sleep-minutes"))); |
91 |
|
|
92 |
0 |
_pool.setTestWhileIdle(false); |
93 |
0 |
_pool.setTestOnBorrow(false); |
94 |
0 |
_pool.setTestOnReturn(false); |
95 |
0 |
} |
96 |
|
|
97 |
|
public void registryDidShutdown() |
98 |
|
{ |
99 |
|
try |
100 |
|
{ |
101 |
0 |
_pool.close(); |
102 |
0 |
} catch (Exception e) { |
103 |
|
|
104 |
0 |
} |
105 |
0 |
} |
106 |
|
|
107 |
|
public ClassResolver getClassResolver() |
108 |
|
{ |
109 |
0 |
return _classResolver; |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
protected PageKey buildKey(IEngine engine, String pageName) |
124 |
|
{ |
125 |
0 |
return new PageKey(pageName, engine.getLocale()); |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
protected PageKey buildKey(IPage page) |
139 |
|
{ |
140 |
0 |
return new PageKey(page.getPageName(), page.getLocale()); |
141 |
|
} |
142 |
|
|
143 |
|
public Object makeObject(Object key) |
144 |
|
throws Exception |
145 |
|
{ |
146 |
0 |
PageKey pageKey = (PageKey) key; |
147 |
|
|
148 |
0 |
_pageSpecificationResolver.resolve(_cycle, pageKey.getPageName()); |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
0 |
return _loader.loadPage(_pageSpecificationResolver.getSimplePageName(), |
154 |
|
_pageSpecificationResolver.getNamespace(), |
155 |
|
_cycle, |
156 |
|
_pageSpecificationResolver.getSpecification()); |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
public IPage getPage(IRequestCycle cycle, String pageName) |
164 |
|
{ |
165 |
|
|
166 |
0 |
IEngine engine = cycle.getEngine(); |
167 |
0 |
Object key = buildKey(engine, pageName); |
168 |
|
|
169 |
|
IPage result; |
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
try |
176 |
|
{ |
177 |
0 |
result = (IPage) _pool.borrowObject(key); |
178 |
|
|
179 |
0 |
} catch (Exception ex) |
180 |
|
{ |
181 |
0 |
if (RuntimeException.class.isInstance(ex)) |
182 |
0 |
throw (RuntimeException)ex; |
183 |
|
else |
184 |
0 |
throw new ApplicationRuntimeException(PageloadMessages.errorPagePoolGet(key), ex); |
185 |
0 |
} |
186 |
|
|
187 |
|
|
188 |
0 |
if (result.getEngine() == null) |
189 |
|
{ |
190 |
|
|
191 |
|
|
192 |
0 |
result.attach(engine, cycle); |
193 |
|
} |
194 |
|
|
195 |
0 |
return result; |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
public void releasePage(IPage page) |
203 |
|
{ |
204 |
0 |
Tapestry.clearMethodInvocations(); |
205 |
|
|
206 |
0 |
page.detach(); |
207 |
|
|
208 |
0 |
Tapestry.checkMethodInvocation(Tapestry.ABSTRACTPAGE_DETACH_METHOD_ID, "detach()", page); |
209 |
|
|
210 |
0 |
PageKey key = buildKey(page); |
211 |
|
|
212 |
|
try |
213 |
|
{ |
214 |
0 |
_pool.returnObject(key, page); |
215 |
|
|
216 |
0 |
} catch (Exception ex) |
217 |
|
{ |
218 |
0 |
if (RuntimeException.class.isInstance(ex)) |
219 |
0 |
throw (RuntimeException)ex; |
220 |
|
else |
221 |
0 |
throw new ApplicationRuntimeException(PageloadMessages.errorPagePoolGet(key), ex); |
222 |
0 |
} |
223 |
0 |
} |
224 |
|
|
225 |
|
public void resetEventDidOccur() |
226 |
|
{ |
227 |
0 |
_pool.clear(); |
228 |
0 |
} |
229 |
|
|
230 |
|
public void reportStatus(ReportStatusEvent event) |
231 |
|
{ |
232 |
0 |
event.title(_serviceId); |
233 |
|
|
234 |
0 |
event.section("Page Pool"); |
235 |
|
|
236 |
0 |
event.property("active", _pool.getNumActive()); |
237 |
0 |
event.property("idle", _pool.getNumIdle()); |
238 |
0 |
} |
239 |
|
|
240 |
|
public void setServiceId(String serviceId) |
241 |
|
{ |
242 |
0 |
_serviceId = serviceId; |
243 |
0 |
} |
244 |
|
|
245 |
|
public void setRequestCycle(IRequestCycle cycle) |
246 |
|
{ |
247 |
0 |
_cycle = cycle; |
248 |
0 |
} |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
public void setClassResolver(ClassResolver resolver) |
253 |
|
{ |
254 |
0 |
_classResolver = resolver; |
255 |
0 |
} |
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
public void setPageSpecificationResolver(PageSpecificationResolver resolver) |
260 |
|
{ |
261 |
0 |
_pageSpecificationResolver = resolver; |
262 |
0 |
} |
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
public void setLoader(IPageLoader loader) |
267 |
|
{ |
268 |
0 |
_loader = loader; |
269 |
0 |
} |
270 |
|
|
271 |
|
public void setPropertySource(IPropertySource propertySource) |
272 |
|
{ |
273 |
0 |
_propertySource = propertySource; |
274 |
0 |
} |
275 |
|
} |