📄 rdbmsdelegate.java
字号:
//声明本接口所在的包
package examples.security.rdbmsrealm;
//声明本类引入的其他类
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.Principal;
import java.security.acl.Acl;
import java.security.acl.Group;
import java.security.acl.NotOwnerException;
import java.security.acl.Permission;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Vector;
import weblogic.management.Admin;
import weblogic.management.configuration.RDBMSRealmMBean;
import weblogic.security.acl.AclEntryImpl;
import weblogic.security.acl.AclImpl;
import weblogic.security.acl.ClosableEnumeration;
import weblogic.security.acl.PermissionImpl;
import weblogic.security.acl.User;
import weblogic.security.acl.UserInfo;
import weblogic.security.utils.Factory;
/**
* 这个类的实例与数据库连接通信。并提供一个连接池。
*/
public class RDBMSDelegate
{
/**
* 这个代理关联的域
*/
protected RDBMSRealm realm;
/**
* 数据库连接
*/
protected Connection conn;
// 使用多个连接
protected Connection conn2;
protected Connection conn3;
protected Connection conn4;
private Properties schemaProperties;
// 声明SQL声明
private PreparedStatement getUserStmt;
private PreparedStatement getGroupMembersStmt;
private PreparedStatement getPermissionStmt;
private PreparedStatement getAclEntriesStmt;
private PreparedStatement getUsersStmt;
private PreparedStatement getGroupsStmt;
private PreparedStatement getAclsStmt;
private PreparedStatement getPermissionsStmt;
private PreparedStatement newUserStmt;
private PreparedStatement addGroupMemberStmt;
private PreparedStatement removeGroupMemberStmt;
private PreparedStatement deleteUserStmt1;
private PreparedStatement deleteUserStmt2;
private PreparedStatement deleteUserStmt3;
private PreparedStatement deleteGroupStmt1;
private PreparedStatement deleteGroupStmt2;
/**
* 是否创建新的声明
*/
private boolean getGroupNewStatement;
/**
* 用户
*/
protected Principal aclOwner = new User("unperson");
/**
* 准备
*
* @参数 name the name of the statement to prepare
*/
protected PreparedStatement prepare(String propKey)
throws SQLException, RDBMSException
{
String sqlStr = (String)schemaProperties.get(propKey);
if (sqlStr == null)
{
throw new RDBMSException("realm initialization failed, could not find property '" +
propKey + "' in the RDBMSRealmMBean's SchemaProperties.");
}
return conn.prepareStatement(sqlStr);
}
/**
* 创建新的代理
*
* @异常 RDBMSException an error occurred in fetching
* properties or communicating with the database
*/
protected RDBMSDelegate(RDBMSRealm realm)
{
this.realm = realm;
if (realm.log != null)
realm.log.debug("loading realm properties from the RDBMSRealmMBean.");
RDBMSRealmMBean mbean =
(RDBMSRealmMBean)(Admin.getActiveDomain().getSecurity().getRealm().getCachingRealm().getBasicRealm());
schemaProperties = mbean.getSchemaProperties();
String action = null; // used to help diagnose an exception
try
{
action = "schemaProperties.get, boolean";
getGroupNewStatement =
Boolean.valueOf(
(String)schemaProperties.get("getGroupNewStatement")
).booleanValue();
action = "mbean.getDatabaseDriver";
String driver = mbean.getDatabaseDriver();
if (realm.log != null)
realm.log.debug("driver is " + driver);
action = "driver.newInstance()";
Class.forName(driver).newInstance();
action = "mbean.getDatabaseURL";
String url = mbean.getDatabaseURL();
action = "mbean.getDatabaseUserName";
String user = mbean.getDatabaseUserName();
action = "mbean.getDatabasePassword";
String passwd = mbean.getDatabasePassword();
if (realm.log != null)
realm.log.debug("connecting to " + url);
action = "DriverManager.getConnection";
conn = DriverManager.getConnection(url, user, passwd);
}
catch (Exception e)
{
throw new RDBMSException("realm initialization failed, action '" +
action + "', ", e);
}
// pkey 用来帮助分析
String pkey = null;
try
{
if (realm.log != null)
realm.log.debug("preparing statements from the RDBMSRealmMBean's SchemaProperties");
getUserStmt = prepare(pkey = "getUser");
getAclEntriesStmt = prepare(pkey = "getAclEntries");
getUsersStmt = prepare(pkey = "getUsers");
getGroupsStmt = prepare(pkey = "getGroups");
getAclsStmt = prepare(pkey = "getAcls");
getPermissionStmt = prepare(pkey = "getPermission");
getPermissionsStmt = prepare(pkey = "getPermissions");
if (getGroupNewStatement == false)
{
getGroupMembersStmt = prepare(pkey = "getGroupMembers");
}
newUserStmt = prepare(pkey = "newUser");
addGroupMemberStmt = prepare(pkey = "addGroupMember");
removeGroupMemberStmt = prepare(pkey = "removeGroupMember");
deleteUserStmt1 = prepare(pkey = "deleteUser1");
deleteUserStmt2 = prepare(pkey = "deleteUser2");
deleteUserStmt3 = prepare(pkey = "deleteUser3");
deleteGroupStmt1 = prepare(pkey = "deleteGroup1");
deleteGroupStmt2 = prepare(pkey = "deleteGroup2");
}
catch (SQLException se)
{
String sqlStr = (String)schemaProperties.get(pkey); // this call will succeed, it has already worked in prepare()
throw new RDBMSException("realm initialization failed, Connection.prepareStatement() failed on statement \"" +
sqlStr + "\", ", se);
}
}
/**
* 内部类,指出调用方法已得到结果集
*/
protected static class Finished
extends Throwable
{
/**
* 对象值
*/
private Object value;
/**
* 用给定的值创建对象
*/
Finished(Object value)
{
this.value = value;
}
/**
* 返回和这个对象相关的值
*/
Object getValue()
{
return value;
}
}
/**
* 从数据库获取用户,如果用户不存在,返回null
*/
public User getUser(String name)
throws SQLException
{
if (realm.log != null)
realm.log.debug("getUser(\"" + name + "\")");
getUserStmt.setString(1, name);
ResultSet rs = getUserStmt.executeQuery();
try
{
return rs.next()
? realm.createUser(rs.getString(1), rs.getString(2)) : null;
}
finally
{
rs.close();
}
}
/**
* 获取所有用户
*/
public Enumeration getUsers()
throws SQLException
{
if (realm.log != null)
realm.log.debug("getUsers()");
return new
RDBMSEnumeration(
getUsersStmt.executeQuery(),
new RDBMSNextHandler()
{
public Object handle(ResultSet resultSet) throws SQLException
{
return realm.createUser(resultSet.getString(1), resultSet.getString(2));
}
}
);
}
/**
* 获取用户组
*/
public Group getGroup(String name)
throws SQLException
{
if (realm.log != null)
realm.log.debug("getGroup(\"" + name + "\")");
PreparedStatement stmt = getGroupNewStatement
? prepare("getGroupMembers")
: getGroupMembersStmt;
stmt.setString(1, name);
ResultSet rs = stmt.executeQuery();
try
{
return rs.next() ? getGroupInternal(name, rs) : null;
}
catch (Finished f)
{
return (RDBMSGroup) f.getValue();
}
finally
{
rs.close();
if (getGroupNewStatement)
{
stmt.close();
}
}
}
/**
* 获取所有用户组
*/
public Enumeration getGroups()
throws SQLException
{
if (realm.log != null)
realm.log.debug("getGroups()");
ResultSet rs = getGroupsStmt.executeQuery();
return new
RDBMSEnumeration(
getGroupsStmt.executeQuery(),
new RDBMSNextHandler()
{
public Object handle(ResultSet resultSet) throws SQLException
{
try {
return getGroupInternal(null, resultSet);
} catch (Finished finished) {
// Add the last element to the set of groups.
return finished.getValue();
}
}
}
);
}
//新建用户
public User newUser(String name, String passwd)
throws SQLException, SecurityException
{
if (realm.log != null)
realm.log.debug("newUser(\"" + name + "\", \"" + passwd + "\")");
if (getUser(name) != null)
{
throw new SecurityException("user \"" + name + "\" already exists");
}
newUserStmt.setString(1, name);
newUserStmt.setString(2, passwd);
int rows = newUserStmt.executeUpdate();
if (rows != 1)
{
throw new RDBMSException("insert updated " + rows + " rows (should be 1)");
}
return realm.createUser(name, passwd);
}
//删除用户
public void deleteUser(User user)
throws SQLException
{
String name = user.getName();
deleteUserStmt1.setString(1, name);
deleteUserStmt2.setString(1, name);
deleteUserStmt3.setString(1, name);
deleteUserStmt1.executeUpdate();
deleteUserStmt2.executeUpdate();
deleteUserStmt3.executeUpdate();
}
//删除组
public void deleteGroup(Group group)
throws SQLException
{
String name = group.getName();
deleteGroupStmt1.setString(1, name);
deleteGroupStmt2.setString(1, name);
deleteGroupStmt1.executeUpdate();
deleteGroupStmt2.executeUpdate();
}
/**
* 获取组
*/
protected Group getGroupInternal(String name, ResultSet rs)
throws Finished, SQLException
{
// All of the other methods in this class with similar names are
// patterned after this one.
// We expect the ResultSet that we are reading to cluster all the
// members of a given group together in contiguous rows. If this
// is not the case, this code will fail miserably.
Hashtable members = new Hashtable();
boolean more = true;
// We expect our ResultSet to already point at the first member of
// a group, hence this being a "do ... while" loop.
do
{
String groupName = rs.getString(1);
String memberName = rs.getString(2);
if (name == null)
{
name = groupName;
}
else if (groupName.equals(name) == false)
{
break;
}
Principal p = getPrincipal(memberName);
if (p == null)
{
throw new RDBMSException("group \"" + name + "\" contains nonexistent " +
"principal \"" + memberName + "\"");
}
members.put(memberName, p);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -