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.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.lang.reflect.Method;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     * @author Shuyang Zhou
025     */
026    public class InitialThreadLocal<T> extends CentralizedThreadLocal<T> {
027    
028            public InitialThreadLocal(String name, T initialValue) {
029                    this(name, initialValue, false);
030            }
031    
032            public InitialThreadLocal(String name, T initialValue, boolean shortLived) {
033                    super(shortLived);
034    
035                    _name = name;
036                    _initialValue = initialValue;
037    
038                    Method cloneMethod = null;
039    
040                    if (_initialValue instanceof Cloneable) {
041                            try {
042                                    Class<?> clazz = _initialValue.getClass();
043    
044                                    cloneMethod = clazz.getMethod(_METHOD_CLONE);
045                            }
046                            catch (Exception e) {
047                                    _log.error(e, e);
048                            }
049                    }
050    
051                    _cloneMethod = cloneMethod;
052            }
053    
054            @Override
055            public String toString() {
056                    if (_name != null) {
057                            return _name;
058                    }
059                    else {
060                            return super.toString();
061                    }
062            }
063    
064            @Override
065            protected T initialValue() {
066                    if (_cloneMethod != null) {
067                            try {
068                                    return (T)_cloneMethod.invoke(_initialValue);
069                            }
070                            catch (Exception e) {
071                                    _log.error(e, e);
072                            }
073                    }
074    
075                    return _initialValue;
076            }
077    
078            private static final String _METHOD_CLONE = "clone";
079    
080            private static final Log _log = LogFactoryUtil.getLog(
081                    InitialThreadLocal.class);
082    
083            private final Method _cloneMethod;
084            private final T _initialValue;
085            private final String _name;
086    
087    }