    001//  Copyright 2008-2013 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
    015package org.apache.tapestry5.ioc.internal;
    016
    017import org.apache.tapestry5.ioc.IOOperation;
    018import org.apache.tapestry5.ioc.Invokable;
    019import org.apache.tapestry5.ioc.OperationTracker;
    020import org.slf4j.Logger;
    021
    022import java.io.IOException;
    023
    024/**
    025 * Manages a per-thread OperationTracker using a ThreadLocal.
    026 */
    027public class PerThreadOperationTracker implements OperationTracker
    028{
    029    private final Logger logger;
    030
    031    private final ThreadLocal<OperationTrackerImpl> perThread = new ThreadLocal<OperationTrackerImpl>()
    032    {
    033        @Override
    034        protected OperationTrackerImpl initialValue()
    035        {
    036            return new OperationTrackerImpl(logger);
    037        }
    038    };
    039
    040    public PerThreadOperationTracker(Logger logger)
    041    {
    042        this.logger = logger;
    043    }
    044
    045    OperationTracker get()
    046    {
    047        return perThread.get();
    048    }
    049
    050    void cleanup()
    051    {
    052        if (perThread.get().isEmpty()) perThread.remove();
    053    }
    054
    055    @Override
    056    public void run(String description, Runnable operation)
    057    {
    058        try
    059        {
    060            get().run(description, operation);
    061        } finally
    062        {
    063            cleanup();
    064        }
    065    }
    066
    067    @Override
    068    public <T> T invoke(String description, Invokable<T> operation)
    069    {
    070        try
    071        {
    072            return get().invoke(description, operation);
    073        } finally
    074        {
    075            cleanup();
    076        }
    077    }
    078
    079    @Override
    080    public <T> T perform(String description, IOOperation<T> operation) throws IOException
    081    {
    082        try
    083        {
    084            return get().perform(description, operation);
    085        } finally
    086        {
    087            cleanup();
    088        }
    089    }
    090}