001: // Copyright 2006, 2007 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.tapestry.internal.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
018:
019: import java.util.List;
020: import java.util.Locale;
021: import java.util.Map;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.tapestry.internal.events.InvalidationListener;
025: import org.apache.tapestry.internal.structure.Page;
026: import org.apache.tapestry.ioc.internal.util.CollectionFactory;
027: import org.apache.tapestry.ioc.services.ThreadLocale;
028: import org.apache.tapestry.services.ComponentClassResolver;
029:
030: /**
031: * A very naive implementation just to get us past the start line.
032: * <p>
033: * Registered as an invalidation listener with the
034: * {@link org.apache.tapestry.internal.services.PageLoader}; thus the pool is cleared whenever
035: */
036: public class PagePoolImpl implements PagePool, InvalidationListener {
037: private final Log _log;
038:
039: private final PageLoader _pageLoader;
040:
041: private final ThreadLocale _threadLocale;
042:
043: private final ComponentClassResolver _resolver;
044:
045: private final Map<PageLocator, List<Page>> _pool = newMap();
046:
047: public PagePoolImpl(Log log, PageLoader pageLoader,
048: ThreadLocale threadLocale, ComponentClassResolver resolver) {
049: _log = log;
050: _pageLoader = pageLoader;
051: _threadLocale = threadLocale;
052: _resolver = resolver;
053: }
054:
055: public synchronized Page checkout(String logicalPageName) {
056: String canonicalPageName = _resolver
057: .canonicalizePageName(logicalPageName);
058:
059: Locale locale = _threadLocale.getLocale();
060: List<Page> pages = _pool.get(new PageLocator(canonicalPageName,
061: locale));
062:
063: // When we load a page, we load it in the active locale, whatever that is.
064: // Even if the locale later changes, we keep the version we originally got.
065: // This is not as bad in T5 as in T4, since a seperate request will
066: // render the response (and will have a chance to get the page in a different locale).
067:
068: if (pages == null || pages.isEmpty())
069: return _pageLoader.loadPage(canonicalPageName, locale);
070:
071: // Remove and return the last page in the pool.
072:
073: return pages.remove(pages.size() - 1);
074: }
075:
076: public synchronized void release(Page page) {
077: boolean dirty = page.detached();
078:
079: if (dirty) {
080: _log.error(ServicesMessages.pageIsDirty(page));
081: return;
082: }
083:
084: PageLocator locator = new PageLocator(page.getLogicalName(),
085: page.getLocale());
086: List<Page> pages = _pool.get(locator);
087:
088: if (pages == null) {
089: pages = CollectionFactory.newList();
090: _pool.put(locator, pages);
091: }
092:
093: pages.add(page);
094: }
095:
096: public synchronized void objectWasInvalidated() {
097: _pool.clear();
098: }
099:
100: }
|