Clover coverage report - Code Coverage for tapestry release 4.0-rc-2
Coverage timestamp: Sat Dec 17 2005 09:39:46 PST
file stats: LOC: 180   Methods: 9
NCLOC: 81   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PageSource.java 100% 96% 88.9% 94.4%
coverage coverage
 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.
 40    *
 41    * @author Howard Lewis Ship
 42    */
 43   
 44    public class PageSource implements IPageSource
 45    {
 46    /** set by container */
 47    private ClassResolver _classResolver;
 48   
 49    /** @since 4.0 */
 50    private PageSpecificationResolver _pageSpecificationResolver;
 51   
 52    /** @since 4.0 */
 53   
 54    private IPageLoader _loader;
 55   
 56    /**
 57    * The pool of {@link IPage}s. The key is a {@link MultiKey}, built from the page name and the
 58    * page locale. This is a reference to a shared pool.
 59    */
 60   
 61    private ObjectPool _pool;
 62   
 63  0 public ClassResolver getClassResolver()
 64    {
 65  0 return _classResolver;
 66    }
 67   
 68    /**
 69    * Builds a key for a named page in the application's current locale.
 70    */
 71   
 72  185 protected MultiKey buildKey(IEngine engine, String pageName)
 73    {
 74  185 Object[] keys;
 75   
 76  185 keys = new Object[]
 77    { pageName, engine.getLocale() };
 78   
 79    // Don't make a copy, this array is just for the MultiKey.
 80   
 81  185 return new MultiKey(keys, false);
 82    }
 83   
 84    /**
 85    * Builds a key from an existing page, using the page's name and locale. This is used when
 86    * storing a page into the pool.
 87    */
 88   
 89  175 protected MultiKey buildKey(IPage page)
 90    {
 91  175 Object[] keys;
 92   
 93  175 keys = new Object[]
 94    { page.getPageName(), page.getLocale() };
 95   
 96    // Don't make a copy, this array is just for the MultiKey.
 97   
 98  175 return new MultiKey(keys, false);
 99    }
 100   
 101    /**
 102    * Gets the page from a pool, or otherwise loads the page. This operation is threadsafe.
 103    */
 104   
 105  185 public IPage getPage(IRequestCycle cycle, String pageName, IMonitor monitor)
 106    {
 107  185 IEngine engine = cycle.getEngine();
 108  185 Object key = buildKey(engine, pageName);
 109  185 IPage result = (IPage) _pool.get(key);
 110   
 111  185 if (result == null)
 112    {
 113  123 monitor.pageCreateBegin(pageName);
 114   
 115  123 _pageSpecificationResolver.resolve(cycle, pageName);
 116   
 117    // The loader is responsible for invoking attach()
 118   
 119  122 result = _loader.loadPage(
 120    _pageSpecificationResolver.getSimplePageName(),
 121    _pageSpecificationResolver.getNamespace(),
 122    cycle,
 123    _pageSpecificationResolver.getSpecification());
 124   
 125  113 monitor.pageCreateEnd(pageName);
 126    }
 127    else
 128    {
 129    // But for pooled pages, we are responsible
 130   
 131  62 result.attach(engine, cycle);
 132    }
 133   
 134  175 return result;
 135    }
 136   
 137    /**
 138    * Returns the page to the appropriate pool. Invokes {@link IPage#detach()}.
 139    */
 140   
 141  175 public void releasePage(IPage page)
 142    {
 143  175 Tapestry.clearMethodInvocations();
 144   
 145  175 page.detach();
 146   
 147  175 Tapestry.checkMethodInvocation(Tapestry.ABSTRACTPAGE_DETACH_METHOD_ID, "detach()", page);
 148   
 149  175 _pool.store(buildKey(page), page);
 150    }
 151   
 152    /** @since 4.0 */
 153   
 154  39 public void setPool(ObjectPool pool)
 155    {
 156  39 _pool = pool;
 157    }
 158   
 159    /** @since 4.0 */
 160   
 161  39 public void setClassResolver(ClassResolver resolver)
 162    {
 163  39 _classResolver = resolver;
 164    }
 165   
 166    /** @since 4.0 */
 167   
 168  39 public void setPageSpecificationResolver(PageSpecificationResolver resolver)
 169    {
 170  39 _pageSpecificationResolver = resolver;
 171    }
 172   
 173    /** @since 4.0 */
 174   
 175  39 public void setLoader(IPageLoader loader)
 176    {
 177  39 _loader = loader;
 178    }
 179   
 180    }