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    
019    import java.util.Collections;
020    import java.util.Map;
021    import java.util.concurrent.Callable;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public abstract class CopyThreadLocalCallable<T> implements Callable<T> {
027    
028            public CopyThreadLocalCallable(boolean readOnly, boolean clearOnExit) {
029                    if (readOnly) {
030                            _longLivedThreadLocals = Collections.unmodifiableMap(
031                                    CentralizedThreadLocal.getLongLivedThreadLocals());
032                            _shortLivedlThreadLocals = Collections.unmodifiableMap(
033                                    CentralizedThreadLocal.getShortLivedThreadLocals());
034                    }
035                    else {
036                            _longLivedThreadLocals =
037                                    CentralizedThreadLocal.getLongLivedThreadLocals();
038                            _shortLivedlThreadLocals =
039                                    CentralizedThreadLocal.getShortLivedThreadLocals();
040                    }
041    
042                    _clearOnExit = clearOnExit;
043            }
044    
045            @Override
046            public final T call() throws Exception {
047                    CentralizedThreadLocal.setThreadLocals(
048                            _longLivedThreadLocals, _shortLivedlThreadLocals);
049    
050                    try {
051                            return doCall();
052                    }
053                    finally {
054                            if (_clearOnExit) {
055                                    CentralizedThreadLocal.clearLongLivedThreadLocals();
056                                    CentralizedThreadLocal.clearShortLivedThreadLocals();
057                            }
058                    }
059            }
060    
061            public abstract T doCall() throws Exception;
062    
063            private final boolean _clearOnExit;
064            private final Map<CentralizedThreadLocal<?>, Object> _longLivedThreadLocals;
065            private final Map<CentralizedThreadLocal<?>, Object>
066                    _shortLivedlThreadLocals;
067    
068    }