com.clustercontrol.accesscontrol.util.UserRoleCache.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.accesscontrol.util.UserRoleCache.java

Source

/*
    
Copyright (C) 2006 NTT DATA Corporation
    
This program is free software; you can redistribute it and/or
Modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
    
This program is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU General Public License for more details.
    
 */

package com.clustercontrol.accesscontrol.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.clustercontrol.accesscontrol.model.RoleInfo;
import com.clustercontrol.accesscontrol.model.SystemPrivilegeInfo;
import com.clustercontrol.accesscontrol.model.UserInfo;
import com.clustercontrol.commons.util.AbstractCacheManager;
import com.clustercontrol.commons.util.CacheManagerFactory;
import com.clustercontrol.commons.util.ICacheManager;
import com.clustercontrol.commons.util.ILock;
import com.clustercontrol.commons.util.ILockManager;
import com.clustercontrol.commons.util.JpaTransactionManager;
import com.clustercontrol.commons.util.LockManagerFactory;
import com.clustercontrol.fault.HinemosUnknown;

/**
 * ID?ID?????
 */
public class UserRoleCache {
    private static Log m_log = LogFactory.getLog(UserRoleCache.class);

    private static final ILock _lock;

    static {
        ILockManager lockManager = LockManagerFactory.instance().create();
        _lock = lockManager.create(UserRoleCache.class.getName());

        try {
            _lock.writeLock();

            HashMap<String, ArrayList<String>> roleUserCache = getRoleUserCache();
            HashMap<String, ArrayList<String>> userRoleCache = getUserRoleCache();
            HashMap<String, ArrayList<SystemPrivilegeInfo>> roleSystemPrivilegeCache = getRoleSystemPrivilegeCache();

            if (roleUserCache == null || userRoleCache == null || roleSystemPrivilegeCache == null) { // not null when clustered
                refresh();
            }
        } finally {
            _lock.writeUnlock();
        }
    }

    // ??????
    @SuppressWarnings("unchecked")
    private static HashMap<String, ArrayList<String>> getRoleUserCache() {
        ICacheManager cm = CacheManagerFactory.instance().create();
        Serializable cache = cm.get(AbstractCacheManager.KEY_ACCESS_ROLE_USER);
        if (m_log.isDebugEnabled())
            m_log.debug("get cache " + AbstractCacheManager.KEY_ACCESS_ROLE_USER + " : " + cache);
        return cache == null ? null : (HashMap<String, ArrayList<String>>) cache;
    }

    private static void storeRoleUserCache(HashMap<String, ArrayList<String>> newCache) {
        ICacheManager cm = CacheManagerFactory.instance().create();
        if (m_log.isDebugEnabled())
            m_log.debug("store cache " + AbstractCacheManager.KEY_ACCESS_ROLE_USER + " : " + newCache);
        cm.store(AbstractCacheManager.KEY_ACCESS_ROLE_USER, newCache);
    }

    // ????
    @SuppressWarnings("unchecked")
    private static HashMap<String, ArrayList<String>> getUserRoleCache() {
        ICacheManager cm = CacheManagerFactory.instance().create();
        Serializable cache = cm.get(AbstractCacheManager.KEY_ACCESS_USER_ROLE);
        return cache == null ? null : (HashMap<String, ArrayList<String>>) cache;
    }

    private static void storeUserRoleCache(HashMap<String, ArrayList<String>> newCache) {
        ICacheManager cm = CacheManagerFactory.instance().create();
        cm.store(AbstractCacheManager.KEY_ACCESS_USER_ROLE, newCache);
    }

    // ????????
    @SuppressWarnings("unchecked")
    private static HashMap<String, ArrayList<SystemPrivilegeInfo>> getRoleSystemPrivilegeCache() {
        ICacheManager cm = CacheManagerFactory.instance().create();
        Serializable cache = cm.get(AbstractCacheManager.KEY_ACCESS_ROLE_SYSTEMPRIVILEGE);
        return cache == null ? null : (HashMap<String, ArrayList<SystemPrivilegeInfo>>) cache;
    }

    private static void storeRoleSystemPrivilegeCache(HashMap<String, ArrayList<SystemPrivilegeInfo>> newCache) {
        ICacheManager cm = CacheManagerFactory.instance().create();
        cm.store(AbstractCacheManager.KEY_ACCESS_ROLE_SYSTEMPRIVILEGE, newCache);
    }

    /**
     * ID?
     * refresh???????????????????
     * 
     * @return ID????
     */
    public static List<String> getAllRoleIdList() {
        HashMap<String, ArrayList<String>> cache = getRoleUserCache();
        return new ArrayList<String>(cache.keySet());
    }

    /**
     * ID?
     * @return ID????
     */
    public static List<String> getAllUserIdList() {
        // ?????????????????????????
        // (?????????????????????????)
        HashMap<String, ArrayList<String>> cache = getUserRoleCache();
        return new ArrayList<String>(cache.keySet());
    }

    /**
     * ???ID??
     * 
     * @param roleId ID
     * @return ID????
     */
    public static List<String> getUserIdList(String roleId) {
        m_log.debug("getUserIdList() : roleId " + roleId);

        // ?????????????????????????
        // (?????????????????????????)
        HashMap<String, ArrayList<String>> cache = getRoleUserCache();
        return cache.get(roleId);
    }

    /**
     * ??ID??
     * 
     * @param userId ID
     * @return ID????
     */
    public static List<String> getRoleIdList(String userId) {
        m_log.debug("getRoleIdList() : userId " + userId);

        // ?????????????????????????
        // (?????????????????????????)
        HashMap<String, ArrayList<String>> cache = getUserRoleCache();
        return cache.get(userId);
    }

    /**
     * ???????
     * 
     * @param roleId ID
     * @return ???
     * @throws HinemosUnknown
     */
    public static List<SystemPrivilegeInfo> getSystemPrivilegeList(String roleId) throws HinemosUnknown {
        m_log.debug("getSystemPrivilegeList() : roleId " + roleId);

        // ?????????????????????????
        // (?????????????????????????)
        HashMap<String, ArrayList<SystemPrivilegeInfo>> cache = getRoleSystemPrivilegeCache();
        return cache.get(roleId);
    }

    /**
     * ????????????
     * 
     * @param userId ID
     * @param systemPrivilegeInfo ???
     * @return true:???false:???
     * @throws HinemosUnknown
     */
    public static boolean isSystemPrivilege(String userId, SystemPrivilegeInfo info) throws HinemosUnknown {
        m_log.debug("isSystemPrivilege() : userId " + userId);

        // ????
        List<String> roleIdList = getRoleIdList(userId);

        for (String roleId : roleIdList) {
            List<SystemPrivilegeInfo> systemPrivilegeList = getSystemPrivilegeList(roleId);
            for (SystemPrivilegeInfo cache : systemPrivilegeList) {
                if (cache.getSystemFunction().equals(info.getSystemFunction())
                        && cache.getSystemPrivilege().equals(info.getSystemPrivilege())) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * ?
     */
    public static void refresh() {
        m_log.info("refreshing cache : " + UserRoleCache.class.getSimpleName());

        try {
            _lock.writeLock();

            long startTime = System.currentTimeMillis();
            new JpaTransactionManager().getEntityManager().clear();

            // ???
            HashMap<String, ArrayList<String>> roleUserMap = new HashMap<String, ArrayList<String>>();
            List<RoleInfo> roleEntities = QueryUtil.getAllRole_NONE();

            for (RoleInfo roleEntity : roleEntities) {
                ArrayList<String> userIdList = new ArrayList<String>();
                if (roleEntity.getUserInfoList() != null) {
                    for (UserInfo userEntity : roleEntity.getUserInfoList()) {
                        userIdList.add(userEntity.getUserId());
                    }
                }
                roleUserMap.put(roleEntity.getRoleId(), userIdList);
            }

            // ?????
            HashMap<String, ArrayList<SystemPrivilegeInfo>> roleSystemPrivilegeMap = new HashMap<String, ArrayList<SystemPrivilegeInfo>>();

            for (RoleInfo roleEntity : roleEntities) {
                roleSystemPrivilegeMap.put(roleEntity.getRoleId(),
                        roleEntity.getSystemPrivilegeList() != null
                                ? new ArrayList<SystemPrivilegeInfo>(roleEntity.getSystemPrivilegeList())
                                : new ArrayList<SystemPrivilegeInfo>());
            }

            // ???
            HashMap<String, ArrayList<String>> userRoleMap = new HashMap<String, ArrayList<String>>();
            List<UserInfo> userEntities = QueryUtil.getAllUser_NONE();

            for (UserInfo userEntity : userEntities) {
                ArrayList<String> roleIdList = new ArrayList<String>();
                if (userEntity.getRoleList() != null) {
                    for (RoleInfo roleEntity : userEntity.getRoleList()) {
                        roleIdList.add(roleEntity.getRoleId());
                    }
                }
                userRoleMap.put(userEntity.getUserId(), roleIdList);
            }

            storeRoleUserCache(roleUserMap);
            storeRoleSystemPrivilegeCache(roleSystemPrivilegeMap);
            storeUserRoleCache(userRoleMap);

            m_log.info("refresh UserRoleCache " + (System.currentTimeMillis() - startTime) + "ms. roleUserMap size="
                    + roleUserMap.size() + " roleSystemPrivilegeMap size=" + roleSystemPrivilegeMap.size()
                    + " userRoleMap size=" + userRoleMap.size());
        } finally {
            _lock.writeUnlock();
        }
    }
}