Search in sources :

Example 6 with AppUser

use of com.yahoo.dba.perf.myperf.common.AppUser in project mysql_perf_analyzer by yahoo.

the class WebAppUtil method getDBConnection.

public static DBConnectionWrapper getDBConnection(HttpServletRequest req, MyPerfContext ctx, DBInstanceInfo dbinfo) {
    try {
        //get session
        HttpSession sess = req.getSession();
        //get connection manager
        UserDBConnections conns = UserDBConnections.class.cast(sess.getAttribute("UserDBConnections"));
        //if none, we need add one
        if (conns == null) {
            conns = new UserDBConnections();
            conns.setFrameworkContext(ctx);
            conns.setConnectionIdleTimeout(ctx.getConnectionIdleTime());
            //we don't have validated user, too
            conns.setAppUser(WebAppUtil.findUserFromRequest(req));
            sess.setAttribute("UserDBConnections", conns);
        }
        conns.closeExpired(ctx.getConnectionIdleTime());
        //first try to get connection without cred, meaning from saved
        DBConnectionWrapper connWrapper = conns.checkoutConnection(dbinfo, null);
        if (connWrapper != null) {
            //we will waste one query to check connectivity to avoid annoying error message
            if (DBUtils.checkConnection(connWrapper.getConnection()))
                return connWrapper;
            closeDBConnection(req, connWrapper, true, false);
            connWrapper = null;
        }
        //now get credential
        AppUser appUser = null;
        DBCredential cred = null;
        appUser = AppUser.class.cast(req.getSession().getAttribute(AppUser.SESSION_ATTRIBUTE));
        if (appUser == null)
            throw new RuntimeException("No user found. Session might not be valid.");
        cred = WebAppUtil.findDBCredential(ctx, dbinfo.getDbGroupName(), appUser);
        if (cred == null || cred.getPassword() == null)
            throw new RuntimeException("No valid credential provided for DB " + dbinfo.getDbGroupName());
        if (dbinfo.isConnectionVerified() || !dbinfo.supportClusterQuery()) {
            connWrapper = conns.checkoutConnection(dbinfo, cred);
            if (!DBUtils.checkConnection(connWrapper.getConnection())) {
                closeDBConnection(req, connWrapper, true, false);
                connWrapper = conns.checkoutConnection(dbinfo, cred);
            }
        }
        if (connWrapper == null && dbinfo.supportClusterQuery())
            connWrapper = conns.checkoutConnection(ctx.getDbInfoManager().findGroup(dbinfo.getDbGroupName()), cred);
        if (connWrapper == null)
            throw new RuntimeException("failed to connect to target db (" + dbinfo + ")");
        return connWrapper;
    } catch (Throwable th) {
        if (th instanceof RuntimeException)
            throw RuntimeException.class.cast(th);
        throw new RuntimeException(th);
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) AppUser(com.yahoo.dba.perf.myperf.common.AppUser) DBConnectionWrapper(com.yahoo.dba.perf.myperf.db.DBConnectionWrapper) UserDBConnections(com.yahoo.dba.perf.myperf.db.UserDBConnections) DBCredential(com.yahoo.dba.perf.myperf.common.DBCredential)

Example 7 with AppUser

use of com.yahoo.dba.perf.myperf.common.AppUser in project mysql_perf_analyzer by yahoo.

the class MetaDB method changePrivilege.

/**
   * Update user privilege
   * @param user
   * @param newPrivilege
   * @return
   */
public boolean changePrivilege(String user, int newPrivilege) {
    if (user == null)
        return false;
    String sql2 = "update " + APPUSER_TABLENAME + " set USER_PRIVILEGE=? where USERNAME=?";
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        //first, check if we have record
        AppUser appUser = this.retrieveUserInfo(conn, user);
        if (appUser == null)
            return false;
        appUser.setUserprivilege(newPrivilege);
        pstmt = conn.prepareStatement(sql2);
        pstmt.setInt(1, appUser.getUserprivilege());
        pstmt.setString(2, appUser.getName().toLowerCase());
        pstmt.execute();
        conn.commit();
        return true;
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Exception", ex);
        if (conn != null)
            try {
                conn.rollback();
            } catch (Exception iex) {
            }
        throw new RuntimeException(ex);
    } finally {
        DBUtils.close(pstmt);
        DBUtils.close(conn);
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) AppUser(com.yahoo.dba.perf.myperf.common.AppUser) SQLException(java.sql.SQLException)

Example 8 with AppUser

use of com.yahoo.dba.perf.myperf.common.AppUser in project mysql_perf_analyzer by yahoo.

the class MetaDB method changeEmail.

/**
   * Update user email address
   * @param user
   * @param newEmail
   * @return
   */
public boolean changeEmail(String user, String newEmail) {
    if (user == null)
        return false;
    String sql2 = "update " + APPUSER_TABLENAME + " set EMAIL=? where USERNAME=?";
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        //first, check if we have record
        AppUser appUser = this.retrieveUserInfo(conn, user);
        if (appUser == null)
            return false;
        pstmt = conn.prepareStatement(sql2);
        pstmt.setString(1, newEmail);
        pstmt.setString(2, appUser.getName().toLowerCase());
        pstmt.execute();
        conn.commit();
        return true;
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Exception", ex);
        if (conn != null)
            try {
                conn.rollback();
            } catch (Exception iex) {
            }
        throw new RuntimeException(ex);
    } finally {
        DBUtils.close(pstmt);
        DBUtils.close(conn);
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) AppUser(com.yahoo.dba.perf.myperf.common.AppUser) SQLException(java.sql.SQLException)

Example 9 with AppUser

use of com.yahoo.dba.perf.myperf.common.AppUser in project mysql_perf_analyzer by yahoo.

the class MetaDB method confirmUser.

public boolean confirmUser(String user, boolean confirmed) {
    if (user == null)
        return false;
    String sql2 = "update " + APPUSER_TABLENAME + " set VERIFIED=? where USERNAME=?";
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        //first, check if we have record
        AppUser appUser = this.retrieveUserInfo(conn, user);
        if (appUser == null)
            return false;
        pstmt = conn.prepareStatement(sql2);
        pstmt.setInt(1, confirmed ? 1 : 0);
        pstmt.setString(2, appUser.getName().toLowerCase());
        pstmt.execute();
        conn.commit();
        return true;
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Exception", ex);
        if (conn != null)
            try {
                conn.rollback();
            } catch (Exception iex) {
            }
        throw new RuntimeException(ex);
    } finally {
        DBUtils.close(pstmt);
        DBUtils.close(conn);
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) AppUser(com.yahoo.dba.perf.myperf.common.AppUser) SQLException(java.sql.SQLException)

Example 10 with AppUser

use of com.yahoo.dba.perf.myperf.common.AppUser in project mysql_perf_analyzer by yahoo.

the class MetaDB method retrieveUserInfo.

private AppUser retrieveUserInfo(Connection conn, String username) {
    String sql = "select * from " + APPUSER_TABLENAME + " where username=?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, username.toLowerCase());
        rs = pstmt.executeQuery();
        if (rs != null && rs.next()) {
            AppUser user = new AppUser();
            user.setName(rs.getString("USERNAME"));
            user.setMd5Hash(rs.getString("MD5HASH"));
            user.setUserprivilege(rs.getShort("USER_PRIVILEGE"));
            user.setEmail(rs.getString("EMAIL"));
            user.setVerified("1".equals(rs.getString("VERIFIED")));
            return user;
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Exception", ex);
    } finally {
        DBUtils.close(rs);
        DBUtils.close(pstmt);
    }
    return null;
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AppUser(com.yahoo.dba.perf.myperf.common.AppUser) SQLException(java.sql.SQLException)

Aggregations

AppUser (com.yahoo.dba.perf.myperf.common.AppUser)10 PreparedStatement (java.sql.PreparedStatement)6 SQLException (java.sql.SQLException)6 Connection (java.sql.Connection)5 ArrayList (java.util.ArrayList)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 ResultSet (java.sql.ResultSet)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 RedirectView (org.springframework.web.servlet.view.RedirectView)2 AlertDefinition (com.yahoo.dba.perf.myperf.common.AlertDefinition)1 ColumnDescriptor (com.yahoo.dba.perf.myperf.common.ColumnDescriptor)1 DBCredential (com.yahoo.dba.perf.myperf.common.DBCredential)1 DBGroupInfo (com.yahoo.dba.perf.myperf.common.DBGroupInfo)1 DBInstanceInfo (com.yahoo.dba.perf.myperf.common.DBInstanceInfo)1 InstanceStates (com.yahoo.dba.perf.myperf.common.InstanceStates)1 MetricsGroup (com.yahoo.dba.perf.myperf.common.MetricsGroup)1 ResultList (com.yahoo.dba.perf.myperf.common.ResultList)1 ResultRow (com.yahoo.dba.perf.myperf.common.ResultRow)1 StateSnapshot (com.yahoo.dba.perf.myperf.common.StateSnapshot)1