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 */
023public interface PerthreadManager
024{
025    /**
026     * Adds a listener to the hub. All listeners are discarded at the {@link #cleanup()}.
027     *
028     * @param listener
029     *         to add
030     * @deprecated Deprecated in 5.4, use {@link #addThreadCleanupCallback(Runnable)} instead.
031     */
032    void addThreadCleanupListener(ThreadCleanupListener listener);
033
034    /**
035     * Adds a callback to be invoked when {@link #cleanup()} is invoked; callbacks are then removed.
036     *
037     * @param callback
038     * @since 5.4
039     */
040    void addThreadCleanupCallback(Runnable callback);
041
042    /**
043     * Immediately performs a cleanup of the thread, invoking all callback, then discarding all per-thread data
044     * stored by the manager (including the list of callbacks).
045     */
046    void cleanup();
047
048    /**
049     * Creates a value using a unique internal key.
050     *
051     * @since 5.2.0
052     */
053    <T> PerThreadValue<T> createValue();
054
055    /**
056     * Return {@link ObjectCreator}, which for each thread,
057     * the first call will use the delegate {@link ObjectCreator} to create
058     * an instance, and later calls will reuse the same per-thread instance. The instance is stored in the
059     * {@link org.apache.tapestry5.ioc.services.PerthreadManager} and will be released at the end of the request.
060     *
061     * @since 5.4
062     */
063    <T> ObjectCreator<T> createValue(ObjectCreator<T> delegate);
064
065    /**
066     * Invokes {@link Runnable#run()}, providing a try...finally to {@linkplain #cleanup() cleanup} after.
067     *
068     * @since 5.2.0
069     */
070    void run(Runnable runnable);
071
072    /**
073     * Returns the result from the invocation, providing a try...finally to {@linkplain #cleanup() cleanup} after.
074     */
075    <T> T invoke(Invokable<T> invokable);
076}