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 boolean isInProgress() {
029                    return _inProgress.get().booleanValue();
030            }
031    
032            public static boolean isMergeComplete(
033                    String methodName, Object[] arguments, Class<?>[] parameterTypes) {
034    
035                    Set<String> methodKeys = _mergeComplete.get();
036    
037                    String methodKey = _buildMethodKey(
038                            methodName, arguments, parameterTypes);
039    
040                    return methodKeys.contains(methodKey);
041            }
042    
043            public static void setInProgress(boolean inProgress) {
044                    _inProgress.set(inProgress);
045            }
046    
047            public static void setMergeComplete(
048                    String methodName, Object[] arguments, Class<?>[] parameterTypes) {
049    
050                    Set<String> methodKeys = _mergeComplete.get();
051    
052                    String methodKey = _buildMethodKey(
053                            methodName, arguments, parameterTypes);
054    
055                    methodKeys.add(methodKey);
056    
057                    setInProgress(false);
058            }
059    
060            private static String _buildMethodKey(
061                    String methodName, Object[] arguments, Class<?>[] parameterTypes) {
062    
063                    if ((arguments == null) || (arguments.length == 0)) {
064                            return methodName;
065                    }
066    
067                    StringBundler sb = new StringBundler(arguments.length * 2 + 1);
068    
069                    sb.append(methodName);
070    
071                    for (int i = 0; i < arguments.length; i++) {
072                            sb.append(parameterTypes[0].getClass().getName());
073    
074                            sb.append(arguments.toString());
075                    }
076    
077                    return sb.toString();
078            }
079    
080            private static ThreadLocal<Boolean> _inProgress =
081                    new AutoResetThreadLocal<Boolean>(
082                            MergeLayoutPrototypesThreadLocal.class + "._inProgress", false);
083            private static ThreadLocal<Set<String>> _mergeComplete =
084                    new AutoResetThreadLocal<Set<String>>(
085                            MergeLayoutPrototypesThreadLocal.class + "._mergeComplete",
086                            new HashSet<String>());
087    
088    }