Java tutorial
/** * PureInfo Quake * @(#)AuthMgrSRMImpl.java 1.0 Nov 30, 2005 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.srm.auth.domain.impl; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.dom4j.Element; import com.pureinfo.ark.auth.domain.impl.AuthMgrImpl; import com.pureinfo.ark.auth.model.IUser; import com.pureinfo.ark.content.model.ArkContent; import com.pureinfo.force.exception.PureException; import com.pureinfo.force.io.ClassResourceUtil; import com.pureinfo.force.lang.BitsValue; import com.pureinfo.force.xml.XMLUtil; import com.pureinfo.srm.SRMExceptionTypes; import com.pureinfo.srm.auth.SRMAuthHelper; import com.pureinfo.srm.auth.model.SRMUser; /** * <P> * Created on Nov 30, 2005 8:25:02 PM <BR> * Last modified on Nov 30, 2005 * </P> * AuthMgrSRMImpl: IAuthMgr implementation in SRM. * * @author Why * @version 1.0, Nov 30, 2005 * @since Quake 1.0 */ public class AuthMgrSRMImpl extends AuthMgrImpl { //logger private final static Logger logger = Logger.getLogger(AuthMgrSRMImpl.class.getName()); /** configuration resource file */ public final static String RESOURCE = "auth-rights.cfg.xml"; //user rights: <Content Type(Integer), User Rights Map(Map)> //User rights map: <User Level(Integer), User Rights(BitsValue)> private Map m_hRights = new HashMap(); private boolean m_bReady = false; /** * Constructor: default */ public AuthMgrSRMImpl() { super(); } /** * @see com.pureinfo.ark.auth.domain.IAuthMgr#getUserRight(com.pureinfo.ark.auth.model.IUser, * com.pureinfo.ark.content.model.ArkContent) */ public BitsValue getUserRight(IUser _user, ArkContent _content) throws PureException { return this.getUserRight(_user, _content.getArkType()); } /** * @see com.pureinfo.ark.auth.domain.IAuthMgr#getUserRight(com.pureinfo.ark.auth.model.IUser, * int) */ public BitsValue getUserRight(IUser _user, int _nContentType) throws PureException { this.ensureReady(); Map map = (Map) m_hRights.get(new Integer(_nContentType)); if (map == null) { return super.getUserRight(_user, _nContentType); } //else int nUserLevel = SRMAuthHelper.getLevel((SRMUser) _user); BitsValue rights = (BitsValue) map.get(new Integer(nUserLevel)); if (rights == null) { rights = new BitsValue(0); } return rights; } /** * Ensures the configuration has been loaded. * * @throws PureException * if failed to load configuration from resource. */ public void ensureReady() throws PureException { if (m_bReady) return; //else synchronized (this) { if (m_bReady) return; this.loadConfig(); m_bReady = true; } } /** * Loads user rights configuration. * * @throws PureException * if failed to load from resource file. */ private void loadConfig() throws PureException { logger.debug("to load user rights configuration from " + RESOURCE + "..."); try { int nContentType, nUserLevel, nRights; String sFile = ClassResourceUtil.mapFullPath(RESOURCE, true); Element xmlRoot = XMLUtil.fileToElement(sFile); Element element; List elements = xmlRoot.elements("content"); for (int i = 0; i < elements.size(); i++) { element = (Element) elements.get(i); nContentType = XMLUtil.getAttributeValueAsInt(element, "type"); List children = element.elements("user"); Map map = new HashMap(children.size()); for (int j = 0; j < children.size(); j++) { element = (Element) children.get(j); nUserLevel = XMLUtil.getAttributeValueAsInt(element, "level"); nRights = XMLUtil.getAttributeValueAsInt(element, "rights"); map.put(new Integer(nUserLevel), new BitsValue(nRights)); } m_hRights.put(new Integer(nContentType), map); children.clear(); } //endfor elements.clear(); logger.debug("user rights configuration loaded successfully"); } catch (Exception ex) { throw new PureException(SRMExceptionTypes.RIGHTS_CONFIG_LOAD, RESOURCE, ex); } } }