Coverage Report - org.apache.tapestry.pageload.PageSource
 
Classes in this File Line Coverage Branch Coverage Complexity
PageSource
2% 
0% 
1.375
 
 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.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  
  * A source for pages for a particular application. Each application should have its own
 38  
  * <code>PageSource</code>, storing it into the {@link javax.servlet.ServletContext}using a
 39  
  * unique key (usually built from the application name).
 40  
  * <p>
 41  
  * The <code>PageSource</code> acts as a pool for {@link IPage}instances. Pages are retrieved
 42  
  * from the pool using {@link #getPage(IRequestCycle, String)}and are later returned to
 43  
  * the pool using {@link #releasePage(IPage)}.
 44  
  * <p>
 45  
  * TBD: Pooled pages stay forever. Need a strategy for cleaning up the pool, tracking which pages
 46  
  * have been in the pool the longest, etc.
 47  
  *
 48  
  * @author Howard Lewis Ship
 49  
  */
 50  
 
 51  2
 public class PageSource extends BaseKeyedPoolableObjectFactory implements IPageSource, ResetEventListener, ReportStatusListener, RegistryShutdownListener {
 52  
     
 53  
     /** set by container. */
 54  
     private ClassResolver _classResolver;
 55  
 
 56  
     /** @since 4.0 */
 57  
     private PageSpecificationResolver _pageSpecificationResolver;
 58  
 
 59  
     /** @since 4.0 */
 60  
 
 61  
     private IPageLoader _loader;
 62  
 
 63  
     private IPropertySource _propertySource;
 64  
 
 65  
     private String _serviceId;
 66  
 
 67  
     /**
 68  
      * Thread safe reference to current request.
 69  
      */
 70  
     private IRequestCycle _cycle;
 71  
 
 72  
     static final long MINUTE = 1000 * 60;
 73  
     
 74  
     /**
 75  
      * The pool of {@link IPage}s. The key is a {@link MultiKey}, built from the page name and the
 76  
      * page locale.
 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  
             // ignore
 104  0
         }
 105  0
     }
 106  
 
 107  
     public ClassResolver getClassResolver()
 108  
     {
 109  0
         return _classResolver;
 110  
     }
 111  
 
 112  
     /**
 113  
      * Builds a key for a named page in the application's current locale.
 114  
      *
 115  
      * @param engine
 116  
      *          The current engine servicing this request.
 117  
      * @param pageName
 118  
      *          The name of the page to build key for.
 119  
      *
 120  
      * @return The unique key for ths specified page and current {@link java.util.Locale}. 
 121  
      */
 122  
 
 123  
     protected PageKey buildKey(IEngine engine, String pageName)
 124  
     {
 125  0
         return new PageKey(pageName, engine.getLocale());
 126  
     }
 127  
 
 128  
     /**
 129  
      * Builds a key from an existing page, using the page's name and locale. This is used when
 130  
      * storing a page into the pool.
 131  
      *
 132  
      * @param page
 133  
      *          The page to build the key for.
 134  
      *
 135  
      * @return The unique key for the specified page instance.
 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  
         // The loader is responsible for invoking attach(),
 151  
         // and for firing events to PageAttachListeners
 152  
 
 153  0
         return _loader.loadPage(_pageSpecificationResolver.getSimplePageName(),
 154  
                                 _pageSpecificationResolver.getNamespace(),
 155  
                                 _cycle,
 156  
                                 _pageSpecificationResolver.getSpecification());
 157  
     }
 158  
 
 159  
     /**
 160  
      * Gets the page from a pool, or otherwise loads the page. This operation is threadsafe.
 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  
         // lock our page specific key lock first
 172  
         // This is only a temporary measure until a more robust
 173  
         // page pool implementation can be created.
 174  
 
 175  
         try
 176  
         {
 177  0
             result = (IPage) _pool.borrowObject(key);
 178  
             
 179  0
         } catch (Exception ex)
 180  
         {
 181  0
             throw new ApplicationRuntimeException(PageloadMessages.errorPagePoolGet(key), ex);
 182  0
         }
 183  
 
 184  
 
 185  0
         if (result.getEngine() == null)
 186  
         {
 187  
             // This call will also fire events to any PageAttachListeners
 188  
             
 189  0
             result.attach(engine, cycle);
 190  
         }
 191  
 
 192  0
         return result;
 193  
     }
 194  
 
 195  
     /**
 196  
      * Returns the page to the appropriate pool. Invokes {@link IPage#detach()}.
 197  
      */
 198  
 
 199  
     public void releasePage(IPage page)
 200  
     {
 201  0
         Tapestry.clearMethodInvocations();
 202  
 
 203  0
         page.detach();
 204  
 
 205  0
         Tapestry.checkMethodInvocation(Tapestry.ABSTRACTPAGE_DETACH_METHOD_ID, "detach()", page);
 206  
 
 207  0
         PageKey key = buildKey(page);
 208  
 
 209  
         try
 210  
         {
 211  0
             _pool.returnObject(key, page);
 212  
 
 213  0
         } catch (Exception ex)
 214  
         {
 215  0
             throw new ApplicationRuntimeException(PageloadMessages.errorPagePoolGet(key), ex);
 216  0
         }        
 217  0
     }
 218  
 
 219  
     public void resetEventDidOccur()
 220  
     {
 221  0
         _pool.clear();
 222  0
     }
 223  
 
 224  
     public void reportStatus(ReportStatusEvent event)
 225  
     {
 226  0
         event.title(_serviceId);
 227  
 
 228  0
         event.section("Page Pool");
 229  
 
 230  0
         event.property("active", _pool.getNumActive());
 231  0
         event.property("idle", _pool.getNumIdle());
 232  0
     }
 233  
 
 234  
     public void setServiceId(String serviceId)
 235  
     {
 236  0
         _serviceId = serviceId;
 237  0
     }
 238  
 
 239  
     public void setRequestCycle(IRequestCycle cycle)
 240  
     {
 241  0
         _cycle = cycle;
 242  0
     }
 243  
 
 244  
     /** @since 4.0 */
 245  
 
 246  
     public void setClassResolver(ClassResolver resolver)
 247  
     {
 248  0
         _classResolver = resolver;
 249  0
     }
 250  
 
 251  
     /** @since 4.0 */
 252  
 
 253  
     public void setPageSpecificationResolver(PageSpecificationResolver resolver)
 254  
     {
 255  0
         _pageSpecificationResolver = resolver;
 256  0
     }
 257  
 
 258  
     /** @since 4.0 */
 259  
 
 260  
     public void setLoader(IPageLoader loader)
 261  
     {
 262  0
         _loader = loader;
 263  0
     }
 264  
 
 265  
     public void setPropertySource(IPropertySource propertySource)
 266  
     {
 267  0
         _propertySource = propertySource;
 268  0
     }
 269  
 }