001// Licensed under the Apache License, Version 2.0 (the "License"); 002// you may not use this file except in compliance with the License. 003// You may obtain a copy of the License at 004// 005// http://www.apache.org/licenses/LICENSE-2.0 006// 007// Unless required by applicable law or agreed to in writing, software 008// distributed under the License is distributed on an "AS IS" BASIS, 009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 010// See the License for the specific language governing permissions and 011// limitations under the License. 012 013package org.apache.tapestry5.ioc.services; 014 015import org.apache.tapestry5.ioc.Invokable; 016import org.apache.tapestry5.ioc.ObjectCreator; 017 018/** 019 * Manages per-thread data, and provides a way for listeners to know when such data should be cleaned up. Typically, 020 * data is cleaned up at the end of the request (in a web application). Tapestry IoC has any number of objects that need 021 * to know when this event occurs, so that they can clean up any per-thread/per-request state. 022 * <p/> 023 * Due to <a href="https://issues.apache.org/jira/browse/TAPESTRY-2141">TAPESTRY-2141<a> (and the underlying JDK 1.5 bug 024 * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5025230">5025230</a>), this service has expanded to 025 * manage per-thread data (not just end-of-request listeners). 026 */ 027public interface PerthreadManager 028{ 029 /** 030 * Adds a listener to the hub. All listeners are discarded at the {@link #cleanup()}. 031 * 032 * @param listener 033 * to add 034 * @deprecated Deprecated in 5.4, use {@link #addThreadCleanupCallback(Runnable)} instead. 035 */ 036 void addThreadCleanupListener(ThreadCleanupListener listener); 037 038 /** 039 * Adds a callback to be invoked when {@link #cleanup()} is invoked; callbacks are then removed. 040 * 041 * @param callback 042 * @since 5.4 043 */ 044 void addThreadCleanupCallback(Runnable callback); 045 046 /** 047 * Immediately performs a cleanup of the thread, invoking all callback, then discarding all per-thread data 048 * stored by the manager (including the list of callbacks). 049 */ 050 void cleanup(); 051 052 /** 053 * Creates a value using a unique internal key. 054 * 055 * @since 5.2.0 056 */ 057 <T> PerThreadValue<T> createValue(); 058 059 /** 060 * Return {@link org.apache.tapestry5.ioc.ObjectCreator}, which for each thread, 061 * the first call will use the delegate {@link org.apache.tapestry5.ioc.ObjectCreator} to create 062 * an instance, and later calls will reuse the same per-thread instance. The instance is stored in the 063 * {@link org.apache.tapestry5.ioc.services.PerthreadManager} and will be released at the end of the request. 064 * 065 * @since 5.4 066 */ 067 <T> ObjectCreator<T> createValue(ObjectCreator<T> delegate); 068 069 /** 070 * Invokes {@link Runnable#run()}, providing a try...finally to {@linkplain #cleanup() cleanup} after. 071 * 072 * @since 5.2.0 073 */ 074 void run(Runnable runnable); 075 076 /** 077 * Returns the result from the invocation, providing a try...finally to {@linkplain #cleanup() cleanup} after. 078 */ 079 <T> T invoke(Invokable<T> invokable); 080}