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.HashUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.lang.reflect.Method;
022    
023    import java.util.Arrays;
024    import java.util.HashSet;
025    import java.util.Set;
026    
027    /**
028     * @author Raymond Aug??
029     * @author Shuyang Zhou
030     */
031    public class MergeLayoutPrototypesThreadLocal {
032    
033            public static void clearMergeComplete() {
034                    _mergeComplete.remove();
035            }
036    
037            public static boolean isInProgress() {
038                    return _inProgress.get();
039            }
040    
041            public static boolean isMergeComplete(Method method, Object[] arguments) {
042                    Set<MethodKey> methodKeys = _mergeComplete.get();
043    
044                    return methodKeys.contains(new MethodKey(method, arguments));
045            }
046    
047            public static void setInProgress(boolean inProgress) {
048                    _inProgress.set(inProgress);
049            }
050    
051            public static void setMergeComplete(Method method, Object[] arguments) {
052                    Set<MethodKey> methodKeys = _mergeComplete.get();
053    
054                    methodKeys.add(new MethodKey(method, arguments));
055    
056                    setInProgress(false);
057            }
058    
059            private static ThreadLocal<Boolean> _inProgress =
060                    new AutoResetThreadLocal<Boolean>(
061                            MergeLayoutPrototypesThreadLocal.class + "._inProgress", false);
062            private static ThreadLocal<Set<MethodKey>> _mergeComplete =
063                    new AutoResetThreadLocal<Set<MethodKey>>(
064                            MergeLayoutPrototypesThreadLocal.class + "._mergeComplete",
065                            new HashSet<MethodKey>());
066    
067            private static class MethodKey {
068    
069                    public MethodKey(Method method, Object[] arguments) {
070                            _method = method;
071                            _arguments = arguments;
072                    }
073    
074                    @Override
075                    public int hashCode() {
076                            int hashCode = _method.hashCode();
077    
078                            if (_arguments != null) {
079                                    for (Object obj : _arguments) {
080                                            if (obj == null) {
081                                                    hashCode = HashUtil.hash(hashCode, 0);
082                                            }
083                                            else {
084                                                    hashCode = HashUtil.hash(hashCode, obj.hashCode());
085                                            }
086                                    }
087                            }
088    
089                            return hashCode;
090                    }
091    
092                    @Override
093                    public boolean equals(Object obj) {
094                            MethodKey methodKey = (MethodKey)obj;
095    
096                            if (Validator.equals(_method, methodKey._method) &&
097                                    Arrays.equals(_arguments, methodKey._arguments)) {
098    
099                                    return true;
100                            }
101    
102                            return false;
103                    }
104    
105                    private final Object[] _arguments;
106                    private final Method _method;
107    
108            }
109    
110    }