Coverage Report - org.apache.tapestry.pageload.PageSource
 
Classes in this File Line Coverage Branch Coverage Complexity
PageSource
0%
0/62
0%
0/10
1.625
 
 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.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  
  * A source for pages for a particular application. Each application should have its own
 37  
  * <code>PageSource</code>, storing it into the {@link javax.servlet.ServletContext}using a
 38  
  * unique key (usually built from the application name).
 39  
  * <p>
 40  
  * The <code>PageSource</code> acts as a pool for {@link IPage}instances. Pages are retrieved
 41  
  * from the pool using {@link #getPage(IRequestCycle, String)}and are later returned to
 42  
  * the pool using {@link #releasePage(IPage)}.
 43  
  * <p>
 44  
  * TBD: Pooled pages stay forever. Need a strategy for cleaning up the pool, tracking which pages
 45  
  * have been in the pool the longest, etc.
 46  
  *
 47  
  * @author Howard Lewis Ship
 48  
  */
 49  
 
 50  0
 public class PageSource extends BaseKeyedPoolableObjectFactory implements IPageSource, ResetEventListener, ReportStatusListener, RegistryShutdownListener {
 51  
     
 52  
     /** set by container. */
 53  
     private ClassResolver _classResolver;
 54  
 
 55  
     /** @since 4.0 */
 56  
     private PageSpecificationResolver _pageSpecificationResolver;
 57  
 
 58  
     /** @since 4.0 */
 59  
 
 60  
     private IPageLoader _loader;
 61  
 
 62  
     private IPropertySource _propertySource;
 63  
 
 64  
     private String _serviceId;
 65  
 
 66  
     /**
 67  
      * Thread safe reference to current request.
 68  
      */
 69  
     private IRequestCycle _cycle;
 70  
 
 71  
     static final long MINUTE = 1000 * 60;
 72  
     
 73  
     /**
 74  
      * The pool of {@link IPage}s. The key is a {@link org.apache.tapestry.util.MultiKey}, 
 75  
      * built from the page name and the page locale.
 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  
             // ignore
 103  0
         }
 104  0
     }
 105  
 
 106  
     public ClassResolver getClassResolver()
 107  
     {
 108  0
         return _classResolver;
 109  
     }
 110  
 
 111  
     /**
 112  
      * Builds a key for a named page in the application's current locale.
 113  
      *
 114  
      * @param engine
 115  
      *          The current engine servicing this request.
 116  
      * @param pageName
 117  
      *          The name of the page to build key for.
 118  
      *
 119  
      * @return The unique key for ths specified page and current {@link java.util.Locale}. 
 120  
      */
 121  
 
 122  
     protected PageKey buildKey(IEngine engine, String pageName)
 123  
     {
 124  0
         return new PageKey(pageName, engine.getLocale());
 125  
     }
 126  
 
 127  
     /**
 128  
      * Builds a key from an existing page, using the page's name and locale. This is used when
 129  
      * storing a page into the pool.
 130  
      *
 131  
      * @param page
 132  
      *          The page to build the key for.
 133  
      *
 134  
      * @return The unique key for the specified page instance.
 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  
         // The loader is responsible for invoking attach(),
 150  
         // and for firing events to PageAttachListeners
 151  
 
 152  0
         return _loader.loadPage(_pageSpecificationResolver.getSimplePageName(),
 153  
                                 _pageSpecificationResolver.getNamespace(),
 154  
                                 _cycle,
 155  
                                 _pageSpecificationResolver.getSpecification());
 156  
     }
 157  
 
 158  
     /**
 159  
      * Gets the page from a pool, or otherwise loads the page. This operation is threadsafe.
 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  
         // lock our page specific key lock first
 171  
         // This is only a temporary measure until a more robust
 172  
         // page pool implementation can be created.
 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  
             // This call will also fire events to any PageAttachListeners
 190  
             
 191  0
             result.attach(engine, cycle);
 192  
         }
 193  
 
 194  0
         return result;
 195  
     }
 196  
 
 197  
     /**
 198  
      * Returns the page to the appropriate pool. Invokes {@link IPage#detach()}.
 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  
     /** @since 4.0 */
 250  
 
 251  
     public void setClassResolver(ClassResolver resolver)
 252  
     {
 253  0
         _classResolver = resolver;
 254  0
     }
 255  
 
 256  
     /** @since 4.0 */
 257  
 
 258  
     public void setPageSpecificationResolver(PageSpecificationResolver resolver)
 259  
     {
 260  0
         _pageSpecificationResolver = resolver;
 261  0
     }
 262  
 
 263  
     /** @since 4.0 */
 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  
 }