com.liferay.portal.kernel.util.CentralizedThreadLocal Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of portal-service Show documentation
Show all versions of portal-service Show documentation
Contains interfaces for the portal services. Interfaces are only loaded by the global class loader and are shared by all plugins.
/**
* Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.kernel.util;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author Shuyang Zhou
*/
public class CentralizedThreadLocal extends ThreadLocal {
public static void clearLongLivedThreadLocals() {
_longLivedThreadLocals.remove();
}
public static void clearShortLivedThreadLocals() {
_shortLivedThreadLocals.remove();
}
public static Map, Object>
getLongLivedThreadLocals() {
return _toMap(_longLivedThreadLocals.get());
}
public static Map, Object>
getShortLivedThreadLocals() {
return _toMap(_shortLivedThreadLocals.get());
}
public static void setThreadLocals(
Map, Object> longLivedThreadLocals,
Map, Object> shortLivedThreadLocals) {
ThreadLocalMap threadLocalMap = _longLivedThreadLocals.get();
for (Map.Entry, Object> entry :
longLivedThreadLocals.entrySet()) {
threadLocalMap.putEntry(entry.getKey(), entry.getValue());
}
threadLocalMap = _shortLivedThreadLocals.get();
for (Map.Entry, Object> entry :
shortLivedThreadLocals.entrySet()) {
threadLocalMap.putEntry(entry.getKey(), entry.getValue());
}
}
public CentralizedThreadLocal(boolean shortLived) {
_shortLived = shortLived;
if (shortLived) {
_hashCode = _shortLivedNextHasCode.getAndAdd(_HASH_INCREMENT);
}
else {
_hashCode = _longLivedNextHasCode.getAndAdd(_HASH_INCREMENT);
}
}
@Override
public T get() {
ThreadLocalMap threadLocalMap = _getThreadLocalMap();
Entry entry = threadLocalMap.getEntry(this);
if (entry == null) {
T value = initialValue();
threadLocalMap.putEntry(this, value);
return value;
}
else {
return (T)entry._value;
}
}
@Override
public int hashCode() {
return _hashCode;
}
@Override
public void remove() {
ThreadLocalMap threadLocalMap = _getThreadLocalMap();
threadLocalMap.removeEntry(this);
}
@Override
public void set(T value) {
ThreadLocalMap threadLocalMap = _getThreadLocalMap();
threadLocalMap.putEntry(this, value);
}
protected T copy(T value) {
if (value != null) {
Class> clazz = value.getClass();
if (_immutableTypes.contains(clazz)) {
return value;
}
}
return null;
}
private static Map, Object> _toMap(
ThreadLocalMap threadLocalMap) {
Map, Object> map =
new HashMap, Object>(
threadLocalMap._table.length);
for (Entry entry : threadLocalMap._table) {
if (entry != null) {
CentralizedThreadLocal