📄 rdbmsdelegate.java
字号:
} while (more = rs.next());
RDBMSGroup result = realm.createGroup(name, members);
if (true)
{
Enumeration enum = members.elements();
while (enum.hasMoreElements())
{
Object obj = enum.nextElement();
if (obj instanceof Group)
{
Group g = (Group) obj;
if (g.isMember(result))
{
throw new RDBMSException("group membership circularity between \"" +
g.getName() + "\" and \"" + name + "\"");
}
}
}
}
if (more == false)
{
throw new Finished(result);
}
return result;
}
//增加组成员
public boolean addGroupMember(RDBMSGroup group, Principal member)
throws SQLException
{
addGroupMemberStmt.setString(1, group.getName());
addGroupMemberStmt.setString(2, member.getName());
int rows = addGroupMemberStmt.executeUpdate();
if (rows != 1)
{
throw new RDBMSException("insert updated " + rows + " rows (should be 1)");
}
return true;
}
//删除组成员
public boolean removeGroupMember(RDBMSGroup group, Principal member)
throws SQLException
{
removeGroupMemberStmt.setString(1, group.getName());
removeGroupMemberStmt.setString(2, member.getName());
int rows = removeGroupMemberStmt.executeUpdate();
if (rows != 1)
{
throw new RDBMSException("delete updated " + rows + " rows (should be 1)");
}
return true;
}
/**
*获取ACL
*/
public Acl getAcl(String name)
throws SQLException
{
if (realm.log != null)
realm.log.debug("getAcl(\"" + name + "\")");
getAclEntriesStmt.setString(1, name);
ResultSet rs = getAclEntriesStmt.executeQuery();
try
{
return rs.next() ? getAclInternal(name, rs) : null;
}
catch (Finished f)
{
return (Acl) f.getValue();
}
finally
{
rs.close();
}
}
/**
* 获取ACLs
*/
public Enumeration getAcls()
throws SQLException
{
if (realm.log != null)
realm.log.debug("getAcls()");
return new
RDBMSEnumeration(
getAclsStmt.executeQuery(),
new RDBMSNextHandler()
{
public Object handle(ResultSet resultSet) throws SQLException
{
try {
return getAclInternal(null, resultSet);
} catch (Finished finished) {
return finished.getValue();
}
}
}
);
}
/**
* 被getAcl和getAcls调用
*/
protected Acl getAclInternal(String name, ResultSet rs)
throws Finished, SQLException
{
// This method follows a similar pattern to getGroupInternal, but
// has the added twist that it expects rows for a given ACL to be
// grouped together by principal.
boolean more = true;
AclImpl result = new AclImpl(aclOwner, null);
AclEntryImpl entry = null;
String currentPrincipal = null;
try
{
do
{
String aclName = rs.getString(1);
String principal = rs.getString(2);
String permission = rs.getString(3);
if (name == null)
{
name = aclName;
}
else if (aclName.equals(name) == false)
{
break;
}
if (currentPrincipal == null || currentPrincipal.equals(principal) == false)
{
currentPrincipal = principal;
if (entry != null)
{
result.addEntry(aclOwner, entry);
}
Principal p = getPrincipal(principal);
if (p == null)
{
throw new RDBMSException("acl \"" + name + "\" contains nonexistent " +
"principal \"" + principal + "\"");
}
entry = new AclEntryImpl(p);
}
entry.addPermission(new PermissionImpl(permission));
} while (more = rs.next());
if (entry != null)
{
result.addEntry(aclOwner, entry);
}
result.setName(aclOwner, name);
}
catch (NotOwnerException e)
{
throw new RDBMSException("caught unexpected exception", e);
}
if (more == false)
{
throw new Finished(result);
}
return result;
}
/**
* 获取Principal
*/
public Principal getPrincipal(String name)
throws SQLException
{
Principal result = getUser(name);
if (result == null)
{
result = getGroup(name);
}
return result;
}
/**
* 获取permission
*/
public Permission getPermission(String name)
throws SQLException
{
if (realm.log != null)
realm.log.debug("getPermission(\"" + name + "\")");
getPermissionStmt.setString(1, name);
ResultSet rs = getPermissionStmt.executeQuery();
try
{
return rs.next() ? new PermissionImpl(rs.getString(1)) : null;
}
finally
{
rs.close();
}
}
/**
* 获取permissions
*/
public Enumeration getPermissions()
throws SQLException
{
if (realm.log != null)
realm.log.debug("getPermissions()");
return new
RDBMSEnumeration(
getPermissionsStmt.executeQuery(),
new RDBMSNextHandler()
{
public Object handle(ResultSet resultSet) throws SQLException
{
return new PermissionImpl(resultSet.getString(1));
}
}
);
}
/**
* 清除
*/
protected void finalize()
{
close();
}
/**
* 清除
*/
public void close()
{
try
{
try
{
if (conn != null)
conn.close();
}
catch (SQLException e)
{
// 忽略
}
try
{
if (conn2 != null)
conn2.close();
}
catch (SQLException e)
{
// 忽略
}
try
{
if (conn3 != null)
conn3.close();
}
catch (SQLException e)
{
// 忽略
}
try
{
if (conn4 != null)
conn4.close();
}
catch (SQLException e)
{
// 忽略
}
}
finally
{
realm = null;
conn = null;
conn2 = null;
conn3 = null;
conn4 = null;
getUserStmt = null;
getGroupMembersStmt = null;
getPermissionStmt = null;
getAclEntriesStmt = null;
getUsersStmt = null;
getGroupsStmt = null;
getAclsStmt = null;
getPermissionsStmt = null;
}
}
/**
* 创建代理实例的构造类
*/
static class DFactory implements Factory
{
/**
* 拥有代理的域
*/
private RDBMSRealm owner;
/**
* 构造方法
*/
DFactory(RDBMSRealm owner)
{
this.owner = owner;
}
/**
* 新的代理
*/
public Object newInstance()
throws InvocationTargetException
{
if (owner.log != null)
owner.log.debug("new instance");
return new RDBMSDelegate(owner);
}
/**
* 清除代理
*/
public void destroyInstance(Object obj)
{
if (owner.log != null)
owner.log.debug("destroy instance");
((RDBMSDelegate) obj).close();
}
}
/**
* 定义处理接口
*/
private interface RDBMSNextHandler
{
public Object handle(ResultSet resultSet) throws SQLException;
}
/**
* 定义结果集枚举的基类
*/
private class RDBMSEnumeration implements ClosableEnumeration
{
boolean closed = false;
ResultSet resultSet;
RDBMSNextHandler handler;
RDBMSEnumeration(ResultSet resultSet, RDBMSNextHandler handler)
{
this.resultSet = resultSet;
this.handler = handler;
increment();
}
private void increment()
{
try {
if (!resultSet.next()) {
close();
}
} catch (SQLException e) {
close();
}
}
public boolean hasMoreElements()
{
return (closed) ? false : true;
}
//下一个元素
public Object nextElement()
{
if (closed) {
throw new NoSuchElementException("RDBMEnumeration.nextElement");
}
try {
Object results = handler.handle(resultSet);
increment();
return results;
} catch (SQLException e) {
throw new RDBMSException("RDBMSEnumeration.nextElement failed", e);
}
}
//关闭
public void close()
{
if (closed) {
return;
}
try {
closed = true;
resultSet.close();
} catch (SQLException e) {
throw new RDBMSException("RDBMSEnumeration.close failed", e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -