001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.util.InitialThreadLocal;
018    import com.liferay.portal.model.ResourcePermission;
019    
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    /**
025     * @author Shuyang Zhou
026     * @author Brian Wing Shun Chan
027     */
028    public class ResourcePermissionsThreadLocal {
029    
030            public static Map<Long, ResourcePermission> getResourcePermissions() {
031                    return _resourcePermissions.get();
032            }
033    
034            public static void setResourcePermissions(
035                    List<ResourcePermission> resourcePermissions) {
036    
037                    if (resourcePermissions != null) {
038                            Map<Long, ResourcePermission> resourcePermissionMap =
039                                    new HashMap<Long, ResourcePermission>();
040    
041                            for (ResourcePermission resourcePermission : resourcePermissions) {
042                                    resourcePermissionMap.put(
043                                            resourcePermission.getRoleId(), resourcePermission);
044                            }
045                    }
046                    else {
047                            _resourcePermissions.remove();
048                    }
049            }
050    
051            private static ThreadLocal<Map<Long, ResourcePermission>>
052                    _resourcePermissions = new InitialThreadLocal
053                            <Map<Long, ResourcePermission>>(
054                                    ResourcePermissionsThreadLocal.class + "._resourcePermissions",
055                                    null);
056    
057    }