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.portlet.exportimport.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 final ThreadLocal<Boolean> _inProgress =
060                    new AutoResetThreadLocal<>(
061                            MergeLayoutPrototypesThreadLocal.class + "._inProgress", false);
062            private static final 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 boolean equals(Object obj) {
076                            MethodKey methodKey = (MethodKey)obj;
077    
078                            if (Validator.equals(_method, methodKey._method) &&
079                                    Arrays.equals(_arguments, methodKey._arguments)) {
080    
081                                    return true;
082                            }
083    
084                            return false;
085                    }
086    
087                    @Override
088                    public int hashCode() {
089                            int hashCode = _method.hashCode();
090    
091                            if (_arguments != null) {
092                                    for (Object obj : _arguments) {
093                                            if (obj == null) {
094                                                    hashCode = HashUtil.hash(hashCode, 0);
095                                            }
096                                            else {
097                                                    hashCode = HashUtil.hash(hashCode, obj.hashCode());
098                                            }
099                                    }
100                            }
101    
102                            return hashCode;
103                    }
104    
105                    private final Object[] _arguments;
106                    private final Method _method;
107    
108            }
109    
110    }