001    /**
002     * Copyright (c) 2000-present 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.messaging.proxy;
016    
017    import com.liferay.portal.kernel.messaging.Message;
018    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
019    
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    /**
024     * @author Tina Tian
025     */
026    public class MessageValuesThreadLocal {
027    
028            public static Object getValue(String key) {
029                    Map<String, Object> messageValues = _messageValuesThreadLocal.get();
030    
031                    if (messageValues == null) {
032                            return null;
033                    }
034    
035                    return messageValues.get(_THREAD_LOCAL_KEY_PREFIX.concat(key));
036            }
037    
038            public static void populateMessageFromThreadLocals(Message message) {
039                    Map<String, Object> messageValues = _messageValuesThreadLocal.get();
040    
041                    if (messageValues == null) {
042                            return;
043                    }
044    
045                    for (Map.Entry<String, Object> entry : messageValues.entrySet()) {
046                            message.put(entry.getKey(), entry.getValue());
047                    }
048            }
049    
050            public static void populateThreadLocalsFromMessage(Message message) {
051                    Map<String, Object> values = message.getValues();
052    
053                    if (values == null) {
054                            return;
055                    }
056    
057                    for (Map.Entry<String, Object> entry : values.entrySet()) {
058                            String key = entry.getKey();
059    
060                            if (key.startsWith(_THREAD_LOCAL_KEY_PREFIX)) {
061                                    doSetValue(key, entry.getValue());
062                            }
063                    }
064            }
065    
066            public static void setValue(String key, Object value) {
067                    doSetValue(_THREAD_LOCAL_KEY_PREFIX.concat(key), value);
068            }
069    
070            protected static void doSetValue(String key, Object value) {
071                    Map<String, Object> messageValues = _messageValuesThreadLocal.get();
072    
073                    if (messageValues == null) {
074                            messageValues = new HashMap<>();
075    
076                            _messageValuesThreadLocal.set(messageValues);
077                    }
078    
079                    messageValues.put(key, value);
080            }
081    
082            private static final String _THREAD_LOCAL_KEY_PREFIX =
083                    "THREAD_LOCAL_KEY_PREFIX#";
084    
085            private static final ThreadLocal<Map<String, Object>>
086                    _messageValuesThreadLocal = new AutoResetThreadLocal<>(
087                            MessageValuesThreadLocal.class.getName());
088    
089    }