⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rdbmsrealm.java

📁 weblogic应用全实例
💻 JAVA
字号:
//声明本接口所在的包
package examples.security.rdbmsrealm;
//声明本类引入的其他类

import java.lang.reflect.InvocationTargetException;
import java.security.Principal;
import java.security.acl.Acl;
import java.security.acl.Group;
import java.security.acl.Permission;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import weblogic.logging.LogOutputStream;
import weblogic.security.acl.AbstractManageableRealm;
import weblogic.security.acl.AclImpl;
import weblogic.security.acl.BasicRealm;
import weblogic.security.acl.DebuggableRealm;
import weblogic.security.acl.User;
import weblogic.security.audit.Audit;
import weblogic.security.utils.Pool;


/**
 * 这个类代表空的组和ACLs。
 *
 */
public class RDBMSRealm
  extends AbstractManageableRealm
  implements DebuggableRealm
{
  /**
   * 数据库连接池数量
   */
  private static final int DEFAULT_POOL_SIZE = 6;
  
  private Pool delegatePool;
  
  /**
   * log.
   */
  LogOutputStream log;
  

  /**
   * 创建新的RDBMS域对象
   */
  public RDBMSRealm()
  {
    super("RDBMS Realm");
    delegatePool = createPool(DEFAULT_POOL_SIZE);
  }


  /**
   * 创建代理池
   *
   * @参数 size the number of pool instances to maintain
   */
  protected Pool createPool(int size)
  {
    return new Pool(new RDBMSDelegate.DFactory(this), size);
  }

  
  /**
   *返回代理
   *
   * @异常 RDBMSException 数据库访问错误
   */
  protected RDBMSDelegate getDelegate()
  {
    try
    {
      return (RDBMSDelegate) delegatePool.getInstance();
    }
    catch (InvocationTargetException e)
    {
      throw new RDBMSException("could not get delegate", e);
    }
  }


  /**
   * 返回代理
   *
   * @参看 #getDelegate
   */
  protected void returnDelegate(RDBMSDelegate delegate)
  {
    if (delegate != null)
    {
      delegatePool.returnInstance(delegate);
    }
  }

  
  /**
   * 返回用户
   *
   * @参数 name the name to obtain
   * @返回 the user, or null if none
   * @异常 RDBMSException 数据库访问错误
   */
  public User getUser(String name)
  {
    // Most of the other methods in this class are patterned after
    // this one.  The delegate does all of the actual work.  These
    // methods simply obtain a delegate from the pool, call through to
    // it, and handle any errors that arise.

    // Obtains a delegate to talk to.
    RDBMSDelegate delegate = getDelegate();

    try
    {
      return delegate.getUser(name);
    }
    catch (SQLException e)
    {
      // If the delegate throws an exception, the connection to the
      // database may be in an unsafe state.  Make sure we don't use
      // this delegate again.

      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      // This will do nothing if the reference to the delegate has
      // been set to null because of an error, so the delegate will
      // not be returned to the pool.
      
      returnDelegate(delegate);
    }
  }


  /**
   * 返回Principal
   *
   * @参数 name the name to obtain
   * @返回 the principal, or null if none
   * @异常 RDBMSException 数据库访问错误
   */
  protected Principal getPrincipal(String name)
  {
    RDBMSDelegate delegate = getDelegate();

    try
    {
      return delegate.getPrincipal(name);
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }

  
  protected Hashtable getGroupMembersInternal(String name)
  {
    // It's easiest to just call getGroup and use RDBMSGroup's
    // getMembers method to return the membership of this group.
    
    RDBMSGroup grp = (RDBMSGroup) getGroup(name);

    if (grp == null)
    {
      return null;
    } else {
      return grp.getMembers();
    }
  }

  
  /**
   * 返回一个组
   *
   * @参数 name the name to obtain
   * @返回 the group, or null if none
   * @异常 RDBMSException 数据库访问错误
   */
  public Group getGroup(String name)
  {
    RDBMSDelegate delegate = getDelegate();

    try
    {
      return delegate.getGroup(name);
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }


  /**
   * 返回ACL
   *
   * @参数 name the name to obtain
   * @返回 the ACL, or null if none
   * @异常 RDBMSException 数据库访问错误
   */
  public Acl getAcl(String name)
  {
    RDBMSDelegate delegate = getDelegate();

    try
    {
      return delegate.getAcl(name);
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }


  /**
   * 返回所有用户
   *
   * @返回 all users
   * @参看 weblogic.security.acl.User
   * @异常 RDBMSException 数据库访问错误
   */
  public Enumeration getUsers()
  {
    RDBMSDelegate delegate = getDelegate();

    try
    {
      return delegate.getUsers();
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }


  /**
   * 组中增加成员
   *
   * @参数 group the group to add to
   * @参数 principal the principal to add
   * @返回 true
   * @异常 RDBMSException 数据库访问错误
   */
  boolean addGroupMember(RDBMSGroup group, Principal member)
  {
    RDBMSDelegate delegate = getDelegate();

    try
    {
      return delegate.addGroupMember(group, member);
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }


  /**
   * 从组中删除用户
   *
   * @参数 group the group to remove from
   * @参数 principal the principal to remove
   * @返回 true
   * @异常 RDBMSException 数据库访问错误
   */
  boolean removeGroupMember(RDBMSGroup group, Principal member)
  {
    RDBMSDelegate delegate = getDelegate();

    try
    {
      return delegate.removeGroupMember(group, member);
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }


  /**
   * 返回所有组
   *
   * @返回 all groups
   * @参看 java.security.acl.Group
   * @异常 RDBMSException 数据库访问错误
   */
  public Enumeration getGroups()
  {
    RDBMSDelegate delegate = getDelegate();

    try
    {
      return delegate.getGroups();
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }


  /**
   * 返回ACLs
   *
   * @返回 all ACLs
   * @参看 java.security.acl.Acl
   * @异常 RDBMSException 数据库访问错误
   */
  public Enumeration getAcls()
  {
    RDBMSDelegate delegate = getDelegate();

    try
    {
      return delegate.getAcls();
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }


  /**
   * 返回所有的Permissions
   *
   * @返回 all permissions
   * @参看 java.security.acl.Permission
   * @异常 RDBMSException 数据库访问错误
   */
  public Enumeration getPermissions()
  {
    RDBMSDelegate delegate = getDelegate();

    try
    {
      return delegate.getPermissions();
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }


  /**
   * 鉴定用户
   *
   * @返回 the authenticated user, or null
   * @异常 RDBMSException 数据库访问错误
   */
  protected User authUserPassword(String name, String passwd)
  {
    RDBMSUser user = (RDBMSUser) getUser(name);

    if (user != null && user.authenticate(passwd) == false)
    {
      user = null;
    }
    
    return user;
  }
  

  /**
   * 获取Permission
   *
   * @参数 name the name of the permission
   * @返回 permission object, or nullp
   * @异常 RDBMSException 数据库访问错误
   */
  public Permission getPermission(String name)
  {
    RDBMSDelegate delegate = getDelegate();

    try
    {
      return delegate.getPermission(name);
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }


  /**
   * 创建新用户
   *
   * @参数 name the name of the new user
   * @参数 credential the credential for the user (must be a plaintext password)
   * @参数 constraints null, for this realm
   * @返回 the new User
   * @异常 SecurityException invalid credential or constraint
   */
  public User newUser(String name, Object credential, Object constraints)
    throws SecurityException
  {
    RDBMSDelegate delegate = getDelegate();

    if (constraints != null)
    {
      throw new SecurityException("newUser does not support constraints");
    }

    if (credential == null || !(credential instanceof String))
    {
      throw new SecurityException("invalid credential");
    }
    
    try
    {
      return delegate.newUser(name, (String) credential);
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }


  /**
   * 删除用户
   *
   * @参数 user the user to delete
   * @异常 SecurityException invalid user
   */
  public void deleteUser(User user)
    throws SecurityException
  {
    RDBMSDelegate delegate = getDelegate();

    if (!(user instanceof RDBMSUser))
    {
      throw new SecurityException("invalid user type: " + user.getClass().getName());
    }
    
    try
    {
      delegate.deleteUser(user);
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }


  /**
   * 删除组
   *
   * @参数 group the group to delete
   * @异常 SecurityException invalid group
   */
  public void deleteGroup(Group group)
    throws SecurityException
  {
    RDBMSDelegate delegate = getDelegate();

    if (!(group instanceof RDBMSGroup))
    {
      throw new SecurityException("invalid group type: " +
				  group.getClass().getName());
    }
    
    try
    {
      delegate.deleteGroup(group);
    }
    catch (SQLException e)
    {
      delegate.close();
      delegate = null;

      throw new RDBMSException("caught SQL exception", e);
    }
    finally
    {
      returnDelegate(delegate);
    }
  }


  /**
   * 是否debug
   */
  public void setDebug(boolean enable)
  {
    if (enable && log == null)
    {
      log = new LogOutputStream("RDBMSRealm");
    }
    if (!enable)
    {
      log = null;
    }
  }


  /**
   * debug纪录输出流
   */
  public LogOutputStream getDebugLog()
  {
    return log;
  }


  /**
   * 创建用户对象的构造方法。
   */
  User createUser(String name, String passwd)
  {
    return new RDBMSUser(name, passwd, this);
  }


  /**
   * 创建组对象的构造方法。
   */
  RDBMSGroup createGroup(String name, Hashtable members)
  {
    return new RDBMSGroup(name, this, members);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -