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