001 // Copyright 2010, 2011 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry5.internal.services; 016 017 import org.apache.tapestry5.internal.structure.Page; 018 import org.apache.tapestry5.ioc.ScopeConstants; 019 import org.apache.tapestry5.ioc.annotations.PostInjection; 020 import org.apache.tapestry5.ioc.annotations.Scope; 021 import org.apache.tapestry5.ioc.internal.util.CollectionFactory; 022 import org.apache.tapestry5.ioc.internal.util.InternalUtils; 023 import org.apache.tapestry5.ioc.services.PerthreadManager; 024 import org.apache.tapestry5.ioc.services.ThreadCleanupListener; 025 import org.apache.tapestry5.services.ComponentClassResolver; 026 import org.slf4j.Logger; 027 028 import java.util.Map; 029 030 /** 031 * In Tapestry 5.1, the implementation of this worked with the page pool (a pool of page instances, reserved 032 * to individual requests/threads). Page pooling was deprecated in 5.2 and removed in 5.3. 033 * 034 * @since 5.2 035 */ 036 @Scope(ScopeConstants.PERTHREAD) 037 public class NonPoolingRequestPageCacheImpl implements RequestPageCache, ThreadCleanupListener 038 { 039 private final Logger logger; 040 041 private final ComponentClassResolver resolver; 042 043 private final PageSource pageSource; 044 045 private final Map<String, Page> cache = CollectionFactory.newMap(); 046 047 public NonPoolingRequestPageCacheImpl(Logger logger, ComponentClassResolver resolver, PageSource pageSource) 048 { 049 this.logger = logger; 050 this.resolver = resolver; 051 this.pageSource = pageSource; 052 } 053 054 @PostInjection 055 public void listenForThreadCleanup(PerthreadManager perthreadManager) 056 { 057 perthreadManager.addThreadCleanupListener(this); 058 } 059 060 public void threadDidCleanup() 061 { 062 for (Page page : cache.values()) 063 { 064 try 065 { 066 page.detached(); 067 } catch (Throwable t) 068 { 069 logger.error(String.format("Error detaching page %s: %s", page, InternalUtils.toMessage(t)), t); 070 } 071 } 072 } 073 074 public Page get(String pageName) 075 { 076 String canonical = resolver.canonicalizePageName(pageName); 077 078 Page page = cache.get(canonical); 079 080 if (page == null) 081 { 082 page = pageSource.getPage(canonical); 083 084 try 085 { 086 page.attached(); 087 } catch (Throwable t) 088 { 089 throw new RuntimeException(String.format("Unable to attach page %s: %s", canonical, 090 InternalUtils.toMessage(t)), t); 091 } 092 093 cache.put(canonical, page); 094 } 095 096 return page; 097 } 098 }