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

📄 identitydefaultuserstorereader.java

📁 开源的OpenId的一个java实现
💻 JAVA
字号:
package org.wso2.solutions.identity.users;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.wso2.usermanager.UserManagerException;
import org.wso2.usermanager.readwrite.DefaultStrategy;
import org.wso2.usermanager.readwrite.DefaultUserStoreReader;

public class IdentityDefaultUserStoreReader extends DefaultUserStoreReader
        implements IdentityUserStoreReader {

    private static Log log = LogFactory
            .getLog(IdentityDefaultUserStoreReader.class);

    /**
     * Class constructor specifying the data-source
     * @param dataSource DataSource
     */
    public IdentityDefaultUserStoreReader(DataSource dataSource) {
        super(dataSource);

    }

    /**
     * Class constructor specifying the data-source and the default strategy
     * @param dataSource DataSource
     * @param store DefaultStrategy
     */
    public IdentityDefaultUserStoreReader(DataSource dataSource,
            DefaultStrategy store) {
        super(dataSource, store);
    }

    /**
     * {@inheritDoc}
     */
    public Map<String,String> getUserProperties(String userName) throws UserManagerException {

        Map<String,String>  props = null;
        Connection dbConnection = null;
        PreparedStatement sqlStatement = null;

        try {

            dbConnection = dataSource.getConnection();

            if (dbConnection == null) {
                throw new UserManagerException("null_connection");
            }
            dbConnection.setAutoCommit(false);

            props = new HashMap<String,String> ();
            String userid = data.getUserId(userName);

            sqlStatement = dbConnection
                    .prepareStatement("select attr_name,attr_value from user_profile_values where profile_id = (select profile_id from user_profile where user_id=? and is_default=?)");
            sqlStatement.setString(1, userid);
            sqlStatement.setBoolean(2, true);

            ResultSet rs = sqlStatement.executeQuery();

            while (rs.next()) {
                String name = rs.getString(1);
                String value = rs.getString(2);
                props.put(name, value);
            }

        } catch (SQLException e) {
            log.debug(e);
            throw new UserManagerException("errorReadingFromUserStore", e);
        } finally {
            try {
                if (sqlStatement != null) {
                    sqlStatement.close();
                }
                if (dbConnection != null) {
                    dbConnection.close();
                }
            } catch (SQLException e) {
                throw new UserManagerException("errorClosingConnection", e);
            }
        }
        return props;
    }

    /**
     * {@inheritDoc}
     */
    public String[] getUserPropertyNames() throws UserManagerException {

        String[] propNames = null;
        Connection dbConnection = null;
        PreparedStatement sqlStatement = null;

        try {

            dbConnection = dataSource.getConnection();

            if (dbConnection == null) {
                throw new UserManagerException("null_connection");
            }

            dbConnection.setAutoCommit(false);

            sqlStatement = dbConnection
                    .prepareStatement("select distinct attr_name from user_profile_values");
            ResultSet rs = sqlStatement.executeQuery();

            List<String> lst = new ArrayList<String>();

            while (rs.next()) {
                lst.add(rs.getString(1));
            }

            propNames = (String[]) lst.toArray(new String[lst.size()]);

            if (propNames == null)
                return new String[0];

        } catch (SQLException e) {
            log.debug(e);
            throw new UserManagerException("errorReadingFromUserStore", e);
        } finally {
            try {
                if (sqlStatement != null) {
                    sqlStatement.close();
                }
                if (dbConnection != null) {
                    dbConnection.close();
                }
            } catch (SQLException e) {
                throw new UserManagerException("errorClosingConnection", e);
            }
        }
        return propNames;
    }

    /**
     * {@inheritDoc}
     */
    public String getDefaultUserProfileName(String userName)
            throws UserManagerException {

        Connection dbConnection = null;
        PreparedStatement sqlStatement = null;
        String name = null;

        try {

            dbConnection = dataSource.getConnection();

            if (dbConnection == null) {
                throw new UserManagerException("null_connection");
            }
            dbConnection.setAutoCommit(false);

            String userid = data.getUserId(userName);

            sqlStatement = dbConnection
                    .prepareStatement("select profile_name from user_profile where user_id=? and is_default=?");
            sqlStatement.setString(1, userid);
            sqlStatement.setBoolean(2, true);

            ResultSet rs = sqlStatement.executeQuery();

            while (rs.next()) {
                name = rs.getString(1);
            }

        } catch (SQLException e) {
            log.debug(e);
            throw new UserManagerException("errorReadingFromUserStore", e);
        } finally {
            try {
                if (sqlStatement != null) {
                    sqlStatement.close();
                }
                if (dbConnection != null) {
                    dbConnection.close();
                }
            } catch (SQLException e) {
                throw new UserManagerException("errorClosingConnection", e);
            }
        }
        return name;
    }

    /**
     * {@inheritDoc}
     */
    public List<String> getUserProfileNames(String userName)
            throws UserManagerException {

        List<String>  profileNames = null;
        Connection dbConnection = null;
        PreparedStatement sqlStatement = null;
  
        try {

            dbConnection = dataSource.getConnection();

            if (dbConnection == null) {
                throw new UserManagerException("null_connection");
            }
            dbConnection.setAutoCommit(false);

            profileNames = new ArrayList<String> ();
            String userid = data.getUserId(userName);

            sqlStatement = dbConnection
                    .prepareStatement("select profile_name from user_profile where user_id=?");
            sqlStatement.setString(1, userid);

            ResultSet rs = sqlStatement.executeQuery();

            while (rs.next()) {
                profileNames.add(rs.getString(1));
            }

        } catch (SQLException e) {
            log.debug(e);
            throw new UserManagerException("errorReadingFromUserStore", e);
        } finally {
            try {
                if (sqlStatement != null) {
                    sqlStatement.close();
                }
                if (dbConnection != null) {
                    dbConnection.close();
                }
            } catch (SQLException e) {
                throw new UserManagerException("errorClosingConnection", e);
            }
        }
        return profileNames;
    }

    /**
     * {@inheritDoc}
     */
    public Map<String,String> getUserProperties(String userName, String profileName)
            throws UserManagerException {

        Map<String,String> props = null;
        Connection dbConnection = null;
        PreparedStatement sqlStatement = null;

        try {

            dbConnection = dataSource.getConnection();

            if (dbConnection == null) {
                throw new UserManagerException("null_connection");
            }
            dbConnection.setAutoCommit(false);

            props = new HashMap<String,String>();
            String userid = data.getUserId(userName);

            sqlStatement = dbConnection
                    .prepareStatement("select attr_name,attr_value from user_profile_values where profile_id = (select profile_id from user_profile where user_id=? and profile_name=?)");
            sqlStatement.setString(1, userid);
            sqlStatement.setString(2, profileName);

            ResultSet rs = sqlStatement.executeQuery();

            while (rs.next()) {
                String name = rs.getString(1);
                String value = rs.getString(2);
                props.put(name, value);
            }

        } catch (SQLException e) {
            log.debug(e);
            throw new UserManagerException("errorReadingFromUserStore", e);
        } finally {
            try {
                if (sqlStatement != null) {
                    sqlStatement.close();
                }
                if (dbConnection != null) {
                    dbConnection.close();
                }
            } catch (SQLException e) {
                throw new UserManagerException("errorClosingConnection", e);
            }
        }
        return props;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isExistingUserProfile(String userName, String profileName)
            throws UserManagerException {

        Map<String,String> properties = null;

        properties = getUserProperties(userName, profileName);

        if (properties != null && properties.size() > 0)
            return true;

        return false;
    }
}

⌨️ 快捷键说明

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