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.kernel.staging;
016    
017    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
018    import com.liferay.portal.kernel.util.StringBundler;
019    
020    import java.util.HashSet;
021    import java.util.Set;
022    
023    /**
024     * @author Raymond Aug??
025     */
026    public class MergeLayoutPrototypesThreadLocal {
027    
028            public static void clearMergeComplete() {
029                    _mergeComplete.remove();
030            }
031    
032            public static boolean isInProgress() {
033                    return _inProgress.get();
034            }
035    
036            public static boolean isMergeComplete(
037                    String methodName, Object[] arguments, Class<?>[] parameterTypes) {
038    
039                    Set<String> methodKeys = _mergeComplete.get();
040    
041                    String methodKey = _buildMethodKey(
042                            methodName, arguments, parameterTypes);
043    
044                    return methodKeys.contains(methodKey);
045            }
046    
047            public static void setInProgress(boolean inProgress) {
048                    _inProgress.set(inProgress);
049            }
050    
051            public static void setMergeComplete(
052                    String methodName, Object[] arguments, Class<?>[] parameterTypes) {
053    
054                    Set<String> methodKeys = _mergeComplete.get();
055    
056                    String methodKey = _buildMethodKey(
057                            methodName, arguments, parameterTypes);
058    
059                    methodKeys.add(methodKey);
060    
061                    setInProgress(false);
062            }
063    
064            private static String _buildMethodKey(
065                    String methodName, Object[] arguments, Class<?>[] parameterTypes) {
066    
067                    if ((arguments == null) || (arguments.length == 0)) {
068                            return methodName;
069                    }
070    
071                    StringBundler sb = new StringBundler(arguments.length * 2 + 1);
072    
073                    sb.append(methodName);
074    
075                    for (int i = 0; i < arguments.length; i++) {
076                            sb.append(parameterTypes[i].getName());
077    
078                            sb.append(arguments[i]);
079                    }
080    
081                    return sb.toString();
082            }
083    
084            private static ThreadLocal<Boolean> _inProgress =
085                    new AutoResetThreadLocal<Boolean>(
086                            MergeLayoutPrototypesThreadLocal.class + "._inProgress", false);
087            private static ThreadLocal<Set<String>> _mergeComplete =
088                    new AutoResetThreadLocal<Set<String>>(
089                            MergeLayoutPrototypesThreadLocal.class + "._mergeComplete",
090                            new HashSet<String>());
091    
092    }