001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.executor;
016    
017    import com.liferay.portal.kernel.util.CentralizedThreadLocal;
018    import com.liferay.portal.kernel.util.ThreadLocalBinder;
019    
020    import java.util.Collections;
021    import java.util.Map;
022    import java.util.concurrent.Callable;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public abstract class CopyThreadLocalCallable<T> implements Callable<T> {
028    
029            public CopyThreadLocalCallable(boolean readOnly, boolean clearOnExit) {
030                    this(null, readOnly, clearOnExit);
031            }
032    
033            public CopyThreadLocalCallable(
034                    ThreadLocalBinder threadLocalBinder, boolean readOnly,
035                    boolean clearOnExit) {
036    
037                    _threadLocalBinder = threadLocalBinder;
038    
039                    if (_threadLocalBinder != null) {
040                            _threadLocalBinder.record();
041                    }
042    
043                    if (readOnly) {
044                            _longLivedThreadLocals = Collections.unmodifiableMap(
045                                    CentralizedThreadLocal.getLongLivedThreadLocals());
046                            _shortLivedlThreadLocals = Collections.unmodifiableMap(
047                                    CentralizedThreadLocal.getShortLivedThreadLocals());
048                    }
049                    else {
050                            _longLivedThreadLocals =
051                                    CentralizedThreadLocal.getLongLivedThreadLocals();
052                            _shortLivedlThreadLocals =
053                                    CentralizedThreadLocal.getShortLivedThreadLocals();
054                    }
055    
056                    _clearOnExit = clearOnExit;
057            }
058    
059            public final T call() throws Exception {
060                    CentralizedThreadLocal.setThreadLocals(
061                            _longLivedThreadLocals, _shortLivedlThreadLocals);
062    
063                    if (_threadLocalBinder != null) {
064                            _threadLocalBinder.bind();
065                    }
066    
067                    try {
068                            return doCall();
069                    }
070                    finally {
071                            if (_clearOnExit) {
072                                    if (_threadLocalBinder != null) {
073                                            _threadLocalBinder.cleanUp();
074                                    }
075    
076                                    CentralizedThreadLocal.clearLongLivedThreadLocals();
077                                    CentralizedThreadLocal.clearShortLivedThreadLocals();
078                            }
079                    }
080            }
081    
082            public abstract T doCall() throws Exception;
083    
084            private final boolean _clearOnExit;
085            private final Map<CentralizedThreadLocal<?>, Object> _longLivedThreadLocals;
086            private final Map<CentralizedThreadLocal<?>, Object>
087                    _shortLivedlThreadLocals;
088            private final ThreadLocalBinder _threadLocalBinder;
089    
090    }