001    /**
002     * Copyright (c) 2000-2012 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.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    }