|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
PageSource.java | 100% | 95.8% | 88.9% | 94.3% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.tapestry.pageload;
|
|
16 |
|
|
17 |
import org.apache.hivemind.ClassResolver;
|
|
18 |
import org.apache.tapestry.IEngine;
|
|
19 |
import org.apache.tapestry.IPage;
|
|
20 |
import org.apache.tapestry.IRequestCycle;
|
|
21 |
import org.apache.tapestry.Tapestry;
|
|
22 |
import org.apache.tapestry.engine.IMonitor;
|
|
23 |
import org.apache.tapestry.engine.IPageLoader;
|
|
24 |
import org.apache.tapestry.engine.IPageSource;
|
|
25 |
import org.apache.tapestry.resolver.PageSpecificationResolver;
|
|
26 |
import org.apache.tapestry.services.ObjectPool;
|
|
27 |
import org.apache.tapestry.util.MultiKey;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* A source for pages for a particular application. Each application should have its own
|
|
31 |
* <code>PageSource</code>, storing it into the {@link javax.servlet.ServletContext}using a
|
|
32 |
* unique key (usually built from the application name).
|
|
33 |
* <p>
|
|
34 |
* The <code>PageSource</code> acts as a pool for {@link IPage}instances. Pages are retrieved
|
|
35 |
* from the pool using {@link #getPage(IRequestCycle, String, IMonitor)}and are later returned to
|
|
36 |
* the pool using {@link #releasePage(IPage)}.
|
|
37 |
* <p>
|
|
38 |
* TBD: Pooled pages stay forever. Need a strategy for cleaning up the pool, tracking which pages
|
|
39 |
* have been in the pool the longest, etc. A mechanism for reporting pool statistics would be
|
|
40 |
* useful.
|
|
41 |
*
|
|
42 |
* @author Howard Lewis Ship
|
|
43 |
*/
|
|
44 |
|
|
45 |
public class PageSource implements IPageSource |
|
46 |
{ |
|
47 |
/** set by container */
|
|
48 |
private ClassResolver _classResolver;
|
|
49 |
|
|
50 |
/** @since 4.0 */
|
|
51 |
private PageSpecificationResolver _pageSpecificationResolver;
|
|
52 |
|
|
53 |
/** @since 4.0 */
|
|
54 |
|
|
55 |
private IPageLoader _loader;
|
|
56 |
|
|
57 |
/**
|
|
58 |
* The pool of {@link IPage}s. The key is a {@link MultiKey}, built from the page name and the
|
|
59 |
* page locale. This is a reference to a shared pool.
|
|
60 |
*/
|
|
61 |
|
|
62 |
private ObjectPool _pool;
|
|
63 |
|
|
64 | 0 |
public ClassResolver getClassResolver()
|
65 |
{ |
|
66 | 0 |
return _classResolver;
|
67 |
} |
|
68 |
|
|
69 |
/**
|
|
70 |
* Builds a key for a named page in the application's current locale.
|
|
71 |
*/
|
|
72 |
|
|
73 | 222 |
protected MultiKey buildKey(IEngine engine, String pageName)
|
74 |
{ |
|
75 | 222 |
Object[] keys; |
76 |
|
|
77 | 222 |
keys = new Object[]
|
78 |
{ pageName, engine.getLocale() }; |
|
79 |
|
|
80 |
// Don't make a copy, this array is just for the MultiKey.
|
|
81 |
|
|
82 | 222 |
return new MultiKey(keys, false); |
83 |
} |
|
84 |
|
|
85 |
/**
|
|
86 |
* Builds a key from an existing page, using the page's name and locale. This is used when
|
|
87 |
* storing a page into the pool.
|
|
88 |
*/
|
|
89 |
|
|
90 | 212 |
protected MultiKey buildKey(IPage page)
|
91 |
{ |
|
92 | 212 |
Object[] keys; |
93 |
|
|
94 | 212 |
keys = new Object[]
|
95 |
{ page.getPageName(), page.getLocale() }; |
|
96 |
|
|
97 |
// Don't make a copy, this array is just for the MultiKey.
|
|
98 |
|
|
99 | 212 |
return new MultiKey(keys, false); |
100 |
} |
|
101 |
|
|
102 |
/**
|
|
103 |
* Gets the page from a pool, or otherwise loads the page. This operation is threadsafe.
|
|
104 |
*/
|
|
105 |
|
|
106 | 222 |
public IPage getPage(IRequestCycle cycle, String pageName, IMonitor monitor)
|
107 |
{ |
|
108 | 222 |
IEngine engine = cycle.getEngine(); |
109 | 222 |
Object key = buildKey(engine, pageName); |
110 | 222 |
IPage result = (IPage) _pool.get(key); |
111 |
|
|
112 | 222 |
if (result == null) |
113 |
{ |
|
114 | 141 |
monitor.pageCreateBegin(pageName); |
115 |
|
|
116 | 141 |
_pageSpecificationResolver.resolve(cycle, pageName); |
117 |
|
|
118 | 140 |
result = _loader.loadPage( |
119 |
_pageSpecificationResolver.getSimplePageName(), |
|
120 |
_pageSpecificationResolver.getNamespace(), |
|
121 |
cycle, |
|
122 |
_pageSpecificationResolver.getSpecification()); |
|
123 |
|
|
124 | 131 |
monitor.pageCreateEnd(pageName); |
125 |
} |
|
126 |
|
|
127 | 212 |
return result;
|
128 |
} |
|
129 |
|
|
130 |
/**
|
|
131 |
* Returns the page to the appropriate pool. Invokes {@link IPage#detach()}.
|
|
132 |
*/
|
|
133 |
|
|
134 | 212 |
public void releasePage(IPage page) |
135 |
{ |
|
136 | 212 |
Tapestry.clearMethodInvocations(); |
137 |
|
|
138 | 212 |
page.detach(); |
139 |
|
|
140 | 212 |
Tapestry.checkMethodInvocation(Tapestry.ABSTRACTPAGE_DETACH_METHOD_ID, "detach()", page);
|
141 |
|
|
142 | 212 |
_pool.store(buildKey(page), page); |
143 |
} |
|
144 |
|
|
145 |
/** @since 4.0 */
|
|
146 |
|
|
147 | 45 |
public void setPool(ObjectPool pool) |
148 |
{ |
|
149 | 45 |
_pool = pool; |
150 |
} |
|
151 |
|
|
152 |
/** @since 4.0 */
|
|
153 |
|
|
154 | 45 |
public void setClassResolver(ClassResolver resolver) |
155 |
{ |
|
156 | 45 |
_classResolver = resolver; |
157 |
} |
|
158 |
|
|
159 |
/** @since 4.0 */
|
|
160 |
|
|
161 | 45 |
public void setPageSpecificationResolver(PageSpecificationResolver resolver) |
162 |
{ |
|
163 | 45 |
_pageSpecificationResolver = resolver; |
164 |
} |
|
165 |
|
|
166 |
/** @since 4.0 */
|
|
167 |
|
|
168 | 45 |
public void setLoader(IPageLoader loader) |
169 |
{ |
|
170 | 45 |
_loader = loader; |
171 |
} |
|
172 |
|
|
173 |
} |
|