首先,我定义了一个群组接口,除了继承用户的方法以外还有两个方法,getUsers,getSubGroup.代码如后面所示.用户除了各种不同的属性以外还必须设置其角色以及所属的群组.
然后定义一个AbstractGroup,他继承了Group以及AbstractUser,并实现了Group接口定义的两个方法.用户只要根据实现情况继承AbstractGroup把自己要定义的属性进行处理即可.(因为每个系统的用户都会有不同的属性,所以留成抽象类供使用者自己扩展). 只要初始化变量description, name ,group ,id, prop,role,propMap,subGroup,user即可.
最后定义了一个类XMLGroup来实现具体的Group作随包的一个Group供一般使用者使用.不建议直接使用XMLGroup,因为以后的版本可能会重写该类.如果要使用的话可以把该类改名为自己的类即可.
这部分涉及到了一个ResourceLib类,该类中存是我自己定义的存储各种资源配置用的类,可以直接使用.
======================Group.java===============================
package org.fswan.permission;
public interface Group extends User
{
/**
* 获取组下所有的用户
* @return 用户数组
*/
public User[] getUsers();
/**
* 获取组下属的组
* @return 组的数组
*/
public Group[] getSubGroup();
}
=======================AbstractGroup===================================
package org.fswan.permission;
import org.fswan.Identity;
import org.fswan.workflow.exception.IdentityMappingError;
public class AbstractGroup extends AbstractUser implements Group
{
protected Identity[] subGroup;
protected Identity[] user;
/* (non-Javadoc)
* @see org.fswan.permission.Group#getSubGroup()
*/
public Group[] getSubGroup()
{
Group[] groups = new Group[subGroup.length];
for (int i = 0; i < groups.length; i++)
{
try
{
groups[i] = (Group)subGroup[i].newInstance();
} catch (IdentityMappingError e)
{
e.printStackTrace();
}
}
return groups;
}
/* (non-Javadoc)
* @see org.fswan.permission.Group#getUsers()
*/
public User[] getUsers()
{
User[] users = new User[user.length];
for (int i = 0; i < users.length; i++)
{
try
{
users[i] = (User)user[i].newInstance();
} catch (IdentityMappingError e)
{
e.printStackTrace();
}
}
return users;
}
public String toString()
{
String retStr = id.getIdName();
retStr +=":"+name;
retStr += " "+description;
return retStr;
}
}
==============================XMLGroup.java===========================
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 XMLGroup extends AbstractGroup implements ImplementIdentity
{
/**
* XML中定义群组的标签
*/
public static final String GROUP = "Group";
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(GROUP);
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.XMLGROUP,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();
//加载父群组
tempNl = tempEl.getElementsByTagName("ParentGroup");
Identity[] groups = new Identity[tempNl.getLength()];
for (int iGroup=0; iGroup < tempNl.getLength(); iGroup++)
{
tempEl = (Element)tempNl.item(iGroup);
groups[iGroup] = new IdentityImpl(Identity.GROUP,Identity.XMLGROUP,tempEl.getAttribute("id"));
}
this.subGroup = groups;
//加载角色
tempNl = tempEl.getElementsByTagName("Role");
Identity[] roles = new Identity[tempNl.getLength()];
for (int iRole=0; iRole < tempNl.getLength(); iRole++)
{
tempEl = (Element)tempNl.item(iRole);
roles[iRole] = new IdentityImpl(Identity.ROLE,Identity.XMLROLE,tempEl.getAttribute("id"));
}
this.role = roles;
//加载属性
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"));
}
//加载子群组
tempNl = tempEl.getElementsByTagName("SubGroup");
if (tempNl != null)
{
subGroup = new Identity[tempNl.getLength()];
for (int k = 0; k < subGroup.length; k++)
{
Element tempElement = (Element) tempNl.item(k);
subGroup[k] = new IdentityImpl(Identity.GROUP,Identity.XMLGROUP,tempElement.getAttribute("id"));
}
}
//加载用户
tempNl = tempEl.getElementsByTagName("User");
if (tempNl != null)
{
user = new Identity[tempNl.getLength()];
for (int k = 0; k < subGroup.length; k++)
{
Element tempElement = (Element) tempNl.item(k);
user[k] = new IdentityImpl(Identity.USER,Identity.XMLUSER,tempElement.getAttribute("id"));
}
}
return this;
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
return null;
}
}
========================XML格式=====================
<Group id="G01">
<Name>系统部</Name>
<Description>系统部</Description>
</Group>
========================XML描述文件==================
<xs:complexType name="Group">
<xs:annotation>
<xs:documentation>群组</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="User">
<xs:sequence>
<xs:element name="SubGroup" type="Identity" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="User" type="Identity" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>