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.cluster;
016    
017    import com.liferay.portal.kernel.util.InitialThreadLocal;
018    
019    import java.io.Serializable;
020    
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    /**
025     * @author Tina Tian
026     */
027    public class ClusterableContextThreadLocal {
028    
029            public static void putThreadLocalContext(String key, Serializable value) {
030                    Map<String, Serializable> context = _contextThreadLocal.get();
031    
032                    context.put(key, value);
033            }
034    
035            protected static Map<String, Serializable> collectThreadLocalContext() {
036                    Map<String, Serializable> context = _contextThreadLocal.get();
037    
038                    _contextThreadLocal.remove();
039    
040                    return context;
041            }
042    
043            private static ThreadLocal<HashMap<String, Serializable>>
044                    _contextThreadLocal =
045                            new InitialThreadLocal<HashMap<String, Serializable>>(
046                                    ClusterableContextThreadLocal.class.getName() +
047                                            "._contextThreadLocal",
048                                    new HashMap<String, Serializable>(), true);
049    
050    }