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