首先,我定义了一个角色接口,可以从其中取出其的各种属性.代码如后面所示.
然后定义一个AbstractRole把Role共性的东西进行处理.所有的取属性的方法都已实现.用户只要根据实现情况继承AbstractRole把自己要定义的属性进行处理即可.(因为每个系统的用户都会有不同的属性,所以留成抽象类供使用者自己扩展). 只要初始化变量description, name ,id, prop,users(可能是群组), permissionMap即可.
最后定义了一个类XMLRole来实现具体的Role作随包的一个Role供一般使用者使用.不建议直接使用XMLRole,因为以后的版本可能会重写该类.如果要使用的话可以把该类改名为自己的类即可.
这部分涉及到了一个ResourceLib类,该类中存是我自己定义的存储各种资源配置用的类,可以直接使用.
======================Role.java===============================
package org.fswan.permission;
import org.fswan.Identity;
import java.util.Properties;
import org.fswan.workflow.exception.CantFoundIDException;
public interface Role
{
/**
* 获取角色ID
* @return
*/
public Identity getId();
/**
* 获取角色的名称
* @return 名称
*/
public String getName();
/**
* 获取角色的描述
* @return 描述
*/
public String getDescription();
/**
* 获取角色的所有的用户(组)
* @return 用户
*/
public User[] getUsers() throws CantFoundIDException;
/**
* 角色的属性
* @return 属性值
*/
public Properties getProperties();
/**
* 获取用户用作权限判断的属性
* @param prop 权限属性
* @return 属性值
*/
public Object getPermissionProp(String prop);
}
=======================AbstractRole===================================
package org.fswan.permission;
import java.util.HashMap;
import java.util.Properties;
import org.fswan.Identity;
import org.fswan.workflow.exception.IdentityMappingError;
public class AbstractRole implements Role
{
/**
* 描述
*/
protected String description ;
/**
* 名称
*/
protected String name;
/**
* 标识
*/
protected Identity id;
/**
* 属性
*/
protected Properties prop;
/**
* 该角色所有用户的标识
*/
protected Identity[] users;
/**
* 权限属性表
*/
protected HashMap permissionMap;
/* (non-Javadoc)
* @see org.fswan.permission.Role#getDescription()
*/
public String getDescription()
{
return description;
}
/* (non-Javadoc)
* @see org.fswan.permission.Role#getId()
*/
public Identity getId()
{
return id;
}
/* (non-Javadoc)
* @see org.fswan.permission.Role#getName()
*/
public String getName()
{
return name;
}
/* (non-Javadoc)
* @see org.fswan.permission.Role#getProperties()
*/
public Properties getProperties()
{
return prop;
}
/* (non-Javadoc)
* @see org.fswan.permission.Role#getUsers()
*/
public User[] getUsers()
{
User[] retUser = new User[users.length];
for (int i = 0; i < retUser.length; i++)
{
try
{
retUser[i] = (User)users[i].newInstance();
} catch (IdentityMappingError e)
{
e.printStackTrace();
}
}
return retUser;
}
public String toString()
{
String retStr = id.getIdName();
retStr +=":"+name;
retStr += " "+description;
return retStr;
}
/* (non-Javadoc)
* @see org.fswan.permission.Role#getPermissionProp(java.lang.String)
*/
public Object getPermissionProp(String prop)
{
return permissionMap.get(prop);
}
}
==============================XMLRole.java===========================
/*
* Created on 2004-4-21
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package org.fswan.permission;
import java.util.ArrayList;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilderFactory;
import org.fswan.Identity;
import org.fswan.IdentityImpl;
import org.fswan.ImplementIdentity;
import org.fswan.ResourceLib;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class XMLRole extends AbstractRole implements ImplementIdentity
{
/**
* XML标签用的字符串
*/
public static final String ROLE = "Role";
public Object newInstance(Identity id)
{
ArrayList sources = ResourceLib.getXmlResource();
for (int i = 0; i < sources.size(); i++)
{
try
{
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(sources.get(i).toString());
NodeList nl = doc.getElementsByTagName(ROLE);
for (int j = 0; j < nl.getLength(); j++)
{
Element tempEl = (Element)nl.item(j);
String idStr = tempEl.getAttribute("id");
if(idStr!=null && !idStr.equals(""))
{
if(idStr.equals(id.getIdName()))
{
this.id = new IdentityImpl(Identity.SWITCH,Identity.XMLROLE,idStr);
NodeList tempNl = tempEl.getElementsByTagName("Name");
name = tempNl.item(0).getChildNodes().item(0).getNodeValue();
tempNl = tempEl.getElementsByTagName("Description");
description = tempNl.item(0).getChildNodes().item(0).getNodeValue();
this.prop = new Properties();
tempNl = tempEl.getElementsByTagName("Property");
if(tempNl!=null)
for (int k = 0; k < tempNl.getLength(); k++)
{
Element tempElement = (Element)tempNl.item(k);
this.prop.setProperty(tempElement.getAttribute("name"),tempElement.getAttribute("value"));
}
return this;
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
return null;
}
}
========================XML格式=====================
<Role id="R01">
<Name>系统管理员</Name>
<Description>系统管理员</Description>
</Role>
========================XML描述文件==================
<xs:complexType name="Role">
<xs:annotation>
<xs:documentation>角色名</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="Name"/>
<xs:element name="Description"/>
<xs:element name="Property" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" use="required"/>
</xs:complexType>
本文地址:http://com.8s8s.com/it/it13900.htm