001    /**
002     * Copyright (c) 2000-2012 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            public final T call() throws Exception {
046                    CentralizedThreadLocal.setThreadLocals(
047                            _longLivedThreadLocals, _shortLivedlThreadLocals);
048    
049                    try {
050                            return doCall();
051                    }
052                    finally {
053                            if (_clearOnExit) {
054                                    CentralizedThreadLocal.clearLongLivedThreadLocals();
055                                    CentralizedThreadLocal.clearShortLivedThreadLocals();
056                            }
057                    }
058            }
059    
060            public abstract T doCall() throws Exception;
061    
062            private final boolean _clearOnExit;
063            private final Map<CentralizedThreadLocal<?>, Object> _longLivedThreadLocals;
064            private final Map<CentralizedThreadLocal<?>, Object>
065                    _shortLivedlThreadLocals;
066    
067    }