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.security.permission;
016    
017    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.util.HashSet;
022    import java.util.Set;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     * @author Raymond Aug??
027     */
028    public class PermissionThreadLocal {
029    
030            public static PermissionChecker getPermissionChecker() {
031                    return _permissionChecker.get();
032            }
033    
034            public static boolean isAddResource() {
035                    return _addResource.get();
036            }
037    
038            /**
039             * @deprecated As of 7.0.0, replaced by {@link
040             *             #isFlushResourceBlockEnabled(long, long, String)} and
041             *             {@link #isFlushResourcePermissionEnabled(String, String)}
042             */
043            @Deprecated
044            public static boolean isFlushEnabled() {
045                    Set<String> flushBlockSet = _flushResourceBlockEnabled.get();
046    
047                    if (!flushBlockSet.isEmpty()) {
048                            return false;
049                    }
050    
051                    Set<String> flushPermissionSet = _flushResourcePermissionEnabled.get();
052    
053                    if (!flushPermissionSet.isEmpty()) {
054                            return false;
055                    }
056    
057                    return _flushEnabled.get();
058            }
059    
060            public static boolean isFlushResourceBlockEnabled(
061                    long companyId, long groupId, String name) {
062    
063                    Set<String> set = _flushResourceBlockEnabled.get();
064    
065                    StringBundler sb = new StringBundler(5);
066    
067                    sb.append(companyId);
068                    sb.append(StringPool.UNDERLINE);
069                    sb.append(groupId);
070                    sb.append(StringPool.UNDERLINE);
071                    sb.append(name);
072    
073                    return !set.contains(sb.toString());
074            }
075    
076            public static boolean isFlushResourcePermissionEnabled(
077                    String resourceName, String primKey) {
078    
079                    Set<String> set = _flushResourcePermissionEnabled.get();
080    
081                    return !set.contains(resourceName + StringPool.UNDERLINE + primKey);
082            }
083    
084            public static void setAddResource(boolean addResource) {
085                    _addResource.set(addResource);
086            }
087    
088            public static void setFlushResourceBlockEnabled(
089                    long companyId, long groupId, String name, boolean enabled) {
090    
091                    Set<String> set = _flushResourceBlockEnabled.get();
092    
093                    StringBundler sb = new StringBundler(5);
094    
095                    sb.append(companyId);
096                    sb.append(StringPool.UNDERLINE);
097                    sb.append(groupId);
098                    sb.append(StringPool.UNDERLINE);
099                    sb.append(name);
100    
101                    if (enabled) {
102                            set.remove(sb.toString());
103                    }
104                    else {
105                            set.add(sb.toString());
106                    }
107            }
108    
109            public static void setFlushResourcePermissionEnabled(
110                    String resourceName, String primKey, boolean enabled) {
111    
112                    Set<String> set = _flushResourcePermissionEnabled.get();
113    
114                    if (enabled) {
115                            set.remove(resourceName + StringPool.UNDERLINE + primKey);
116                    }
117                    else {
118                            set.add(resourceName + StringPool.UNDERLINE + primKey);
119                    }
120            }
121    
122            /**
123             * @deprecated As of 7.0.0, replaced by {@link
124             *             #setFlushResourceBlockEnabled(long, long, String, boolean)}
125             *             and {@link #setFlushResourcePermissionEnabled(
126             *             String, String, boolean)}
127             */
128            @Deprecated
129            public static void setIndexEnabled(boolean indexEnabled) {
130                    _flushEnabled.set(indexEnabled);
131            }
132    
133            public static void setPermissionChecker(
134                    PermissionChecker permissionChecker) {
135    
136                    _permissionChecker.set(permissionChecker);
137            }
138    
139            private static ThreadLocal<Boolean> _addResource =
140                    new AutoResetThreadLocal<Boolean>(
141                            PermissionThreadLocal.class + "._addResource", true);
142            private static final ThreadLocal<Set<String>> _flushResourceBlockEnabled =
143                    new AutoResetThreadLocal<Set<String>>(
144                            PermissionThreadLocal.class + "._flushResourceBlockEnabled",
145                            new HashSet<String>());
146            private static final ThreadLocal<Set<String>>
147                    _flushResourcePermissionEnabled = new AutoResetThreadLocal<Set<String>>(
148                            PermissionThreadLocal.class +
149                                    "._flushResourcePermissionEnabled",
150                            new HashSet<String>());
151            private static ThreadLocal<Boolean> _flushEnabled =
152                    new AutoResetThreadLocal<Boolean>(
153                            PermissionThreadLocal.class + "._flushEnabled", true);
154    
155            private static ThreadLocal<PermissionChecker> _permissionChecker =
156                    new AutoResetThreadLocal<PermissionChecker>(
157                            PermissionThreadLocal.class + "._permissionChecker") {
158    
159                                    @Override
160                                    protected PermissionChecker copy(
161                                            PermissionChecker permissionChecker) {
162    
163                                            return permissionChecker;
164                                    }
165    
166                            };
167    
168    }