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

📄 poolelement.java

📁 使用工具jublider开发的一个聊天室实现基本功能,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            PreparedStatement ps = getSelect();
            ps.setString(1, u.getName().toLowerCase().trim());
            Server.log(Thread.currentThread(), this.toString() + "LOGIN user uname=" + u.getName().toLowerCase() + "\r\n" + selStrg, Server.MSG_AUTH, Server.LVL_VERY_VERBOSE);
            ResultSet rs = ps.executeQuery();
            sCnt++;
            dbp.cacheMetaData(rs);
            if (!rs.next()) {
                Server.log(Thread.currentThread(), this.toString()+ "LOGIN no user mathing username " + u.getName(), Server.MSG_AUTH, Server.LVL_MINOR);
                rs.close();
                return u; // return unchanged user object
            } else if (!rs.isLast()) {
                Server.log(Thread.currentThread(), this.toString() + "LOGIN multible records returned for user " + u.getName(), Server.MSG_AUTH, Server.LVL_MAJOR);
                rs.close();
                return u; // return unchanged user object
            }
            
            // always check Pwd if the userobject is marked as unregistered
            // if there is an existing user having the same name but a differen password,
            // we must return login-failed (done by returning null instead of an user-object)
            if (u.isUnregistered == true) {
                String dbpwd = rs.getString(dbp.columns.length+1);
                if (dbpwd==null || !dbpwd.equals(password)) {
                    return null;
                }
                u.isUnregistered = false;
            }
            checkThread();
            
            // read all the other properties
            readColumns(u, rs);
            
            checkWarnings(ps, "loginUser (getData)");
            checkThread();
            // if a lastlogin-property exists, we have to update the data in the db
            if (!dbp.readOnly) {
            	doLoginUpdates(u, rs);
            }
            checkWarnings(ps, "loginUser (update Data)");
            rs.close();
            Server.log (Thread.currentThread(), this.toString() + "LOGIN returning " + u, Server.MSG_AUTH, Server.LVL_MAJOR);
            return u;
        } catch (Exception e) {
            Server.log(this, selStrg, Server.MSG_AUTH, Server.LVL_MAJOR);
            isValid=false;
            release();
            throw e;
        }
    }
    
    
    
    private void readColumns(User u, ResultSet rs) throws SQLException {
        for (int i = 0; i<dbp.columns.length; i++) {
            String cname = dbp.names[i];
            int idx = i+1;
            if ("userright".equals(cname)) {
                String val = rs.getString(idx);
                if (val == null || val.length()==0 || "null".equalsIgnoreCase(val)) {
                    u.setPermission(IUserStates.ROLE_USER);
                } else if ("true".equalsIgnoreCase (val)
                    || "yes".equalsIgnoreCase(val)
                    || "vip".equalsIgnoreCase(val)) {
                    u.setPermission(IUserStates.ROLE_VIP);
                } else if ("admin".equalsIgnoreCase(val)) {
                    u.setPermission(IUserStates.ROLE_GOD);
                } else if ("moderator".equalsIgnoreCase(val)) {
                    u.setPermission(IUserStates.ROLE_VIP | IUserStates.IS_MODERATOR);
                } else if ("guest".equalsIgnoreCase(val)) {
                    u.setPermission(IUserStates.IS_GUEST);
                } else if ("asshole".equalsIgnoreCase(val)) {
                    u.setPermission(IUserStates.ROLE_ASSHOLE);
                } else {
                    try {
                        u.setPermission(Integer.parseInt(val));
                    } catch (NumberFormatException nfe) {
                        Server.log(Thread.currentThread(), this.toString() + "LOGIN userrights-column contains unknown value, corrected to ROLE_USER\r\n(must be null/true,yes,vip or VIP/admin/moderator/guest/user/assohle) ", Server.MSG_AUTH, Server.LVL_MAJOR);
                        u.setPermission(IUserStates.ROLE_USER);
                    }
                }
            } else if ("id".equals(cname)) {
                u.setID(rs.getString(idx));
            } else if ("color".equals(cname)) {
                u.setColCode(rs.getString(idx));
            } else if ("chattime".equals(cname)) {
                u.setProperty("chattime", new Long(rs.getLong(idx)));
            } else if ("lastlogin".equals(cname)) {
                switch (dbp.types[i]) {
                    case Types.BIGINT:
                    case Types.INTEGER:
                    case Types.NUMERIC:
                    case Types.SMALLINT:
                        u.setProperty("lastlogin", new Timestamp (rs.getLong(idx)));
                        break;
                    case Types.DATE:
                    case Types.TIMESTAMP:
                        Timestamp ts = rs.getTimestamp(idx);
                        u.setProperty("lastlogin", ts);
                        break;
                    default:
                        String s = rs.getString(idx);
                        if (rs.wasNull()) {
                            u.setProperty("lastlogin", new Timestamp (System.currentTimeMillis()));
                            break;
                        }
                        try {
                            long l = Long.parseLong (s);
                            u.setProperty("lastlogin", new Timestamp(l));
                        } catch (NumberFormatException nfe) {
                            try {
                                u.setProperty("lastlogin", Timestamp.valueOf(s));
                            } catch (IllegalArgumentException iae) {
                                Server.log (this, "LOGIN Unable to retrieve lastlogin-value! " + s, Server.MSG_AUTH, Server.LVL_MAJOR);
                            }
                        }
                }
            } else if ("friends".equals(cname)) {
            	List users = pool.authenticator.parseUserList(rs.getString(idx));
                for (Iterator it = users.iterator(); it.hasNext(); ) {
                    u.addFriend((String) it.next());
                }
            } else if ("ignorelist".equals(cname)) {
                List ignores = pool.authenticator.parseUserList(rs.getString(idx));
                for (Iterator it = ignores.iterator(); it.hasNext(); ) {
                    u.ignoreUser((String) it.next());
                }
            } else if ("notifyfriends".equals(cname)) {
                switch (dbp.types[i]) {
                    case Types.BIGINT:
                    case Types.INTEGER:
                    case Types.NUMERIC:
                    case Types.SMALLINT:
                        u.setFriendsNotification(rs.getShort(idx));
                        break;
                    default:
                        u.setFriendsNotification(pool.authenticator.parseBoolean(rs.getString(idx)) ? Server.srv.FN_DEFAULT_MODE_TRUE : Server.srv.FN_DEFAULT_MODE_FALSE);
                }
                // u.setFriendsNotification(pool.authenticator.parseBoolean(rs.getString(idx)));
            } else if ("customtitle".equals(cname)) {
                u.setCustomTitle(rs.getString(idx));
            } else if ("blocked".equals(cname)) {
            	u.blocked = pool.authenticator.parseBoolean(rs.getString(idx));
            } else if ("activated".equals(cname)) {
            	u.activated = pool.authenticator.parseBoolean(rs.getString(idx));
            } else {
                String strg = getEncodedString (rs, idx);
                u.setProperty(cname, strg);
            }
        }
    }

    private void doLoginUpdates(User nu, ResultSet rs) throws Exception {
        boolean updated = false, error = false;
        long ts = System.currentTimeMillis();
        int idx = dbp.nameV.indexOf("lastlogin");
        if (idx > -1) {
            try {
                switch (dbp.types[idx]) {
                    case Types.INTEGER:
                    case Types.SMALLINT:
                        rs.updateInt(idx+1, (int) (ts/1000));
                        break;
                    case Types.BIGINT:
                    case Types.NUMERIC:
                    case Types.DECIMAL:
                        rs.updateLong(idx+1, ts/1000);
                        break;
                    case Types.DATE:
                    case Types.TIMESTAMP:
                        rs.updateTimestamp(idx+1, new Timestamp(ts));
                        break;
                    default:
                        rs.updateString(idx+1, String.valueOf(ts/1000));
                }
                updated=true;
            } catch (SQLException se) {
                Server.debug (Thread.currentThread(), this.toString() + "LOGIN unable to update lastlogin", se, Server.MSG_AUTH, Server.LVL_MAJOR);
                error=true;
            }
        }
        // update the cookie too (if set in the db properties)
        idx = dbp.nameV.indexOf("cookie");
        if (idx > -1) try {
            rs.updateString(idx+1, HashUtils.encodeMD5(nu.getCookie()));
        } catch (SQLException se) {
            Server.debug (Thread.currentThread(), this.toString() + "LOGIN unable to update cookie", se, Server.MSG_AUTH, Server.LVL_MAJOR);                    
        }
        try {
            if (updated) {
                rs.updateRow();
                con.commit();
            } else if (error) {
                rs.cancelRowUpdates();
            }
        } catch (SQLException se) {
            Server.debug (Thread.currentThread(), this.toString() + "LOGIN exception during updateRow/cancelRowUpdates", se, Server.MSG_AUTH, Server.LVL_MAJOR);
        }
    }
    

    
    public void logoutUser (User u) throws Exception {
        try {
            if (dbp.readOnly || dbp.updCols == null || dbp.updCols.length < 1)
                return;
            PreparedStatement ps = getUpdate();
            for (int i = 0; i < dbp.updCols.length; i++) {
                String cname = dbp.updNames[i];
                if ("chattime".equalsIgnoreCase(cname)) {
                    ps.setLong(i+1, u.getChattime());
                } else if ("userrights".equalsIgnoreCase(cname)) {
                    ps.setInt(i+1, u.getPermissionMap());
                } else if ("color".equalsIgnoreCase(cname)) {
                    ps.setString(i+1, u.getColCode());
                } else if ("friends".equalsIgnoreCase(cname)) {
                    StringBuffer sb = new StringBuffer();
                    for (Enumeration e = u.friends(); e.hasMoreElements(); ) {
                        String s = (String) e.nextElement();
                        sb.append (s);
                        if (e.hasMoreElements())
                            sb.append (", ");
                    }
                    ps.setString(i+1, sb.toString());
                } else if ("notifyfriends".equalsIgnoreCase(cname)) {
                    int idx = dbp.nameV.indexOf("notifyfriends");
                    switch (dbp.types[idx]) {
                        case Types.BIGINT:
                        case Types.BIT:
                        case Types.DECIMAL:
                        case Types.INTEGER:
                        case Types.SMALLINT:
                            ps.setInt(i+1, u.notifyFriends());
                            break;
                        case Types.BOOLEAN:
                            ps.setBoolean(i+1, u.notifyFriends()==User.FN_ALL ? true : false);
                            break;
                        default:
                            ps.setString(i+1, u.notifyFriends()==User.FN_ALL ? "true" : "false");
                    }
                } else if ("extratitle".equalsIgnoreCase(cname)) {
                    ps.setString(i+1, u.getCustomTitle());
                } else if ("cookie".equalsIgnoreCase(cname)) {
                	// and overwrite it with "not_logged_in" when the user loggs out
                    ps.setString(i+1, "not_logged_in");
                } else if ("blocked".equalsIgnoreCase(cname)) {
                    int idx = dbp.nameV.indexOf("blocked");
                    switch (dbp.types[idx]) {
                        case Types.BIGINT:
                        case Types.BIT:
                        case Types.DECIMAL:
                        case Types.INTEGER:
                        case Types.SMALLINT:
                            ps.setInt(i+1, u.blocked ? 1 : 0);
                            break;
                        case Types.BOOLEAN:
                            ps.setBoolean(i+1, u.blocked);
                            break;
                        default:
                            ps.setString(i+1, u.blocked ? "1" : "0");
                    }
                } else if ("activated".equalsIgnoreCase(cname)) {
                    int idx = dbp.nameV.indexOf("activated");
                    switch (dbp.types[idx]) {
                        case Types.BIGINT:
                        case Types.BIT:
                        case Types.DECIMAL:
                        case Types.INTEGER:
                        case Types.SMALLINT:
                            ps.setInt(i+1, u.activated ? 1 : 0);
                            break;
                        case Types.BOOLEAN:
                            ps.setBoolean(i+1, u.activated);
                            break;
                        default:
                            ps.setString(i+1, u.activated ? "1" : "0");
                    }
                }
            }
            if (dbp.idField != null) {
                if (u.getID()==null) {
                    Server.log(u, "Unable to store logout-data for " + u.getName() + " because of missing id-value", Server.MSG_AUTH, Server.LVL_MAJOR);
                    return;
                }
                ps.setString(dbp.updCols.length+1, u.getID());
            } else {
                ps.setString(dbp.updCols.length+1, u.getName().toLowerCase());
            }
            int rows = ps.executeUpdate();
            sCnt++;
            if (rows==1) {
                con.commit();
            } else if (rows < 1) {
                Server.log(Thread.currentThread(), this.toString() + "LOGOUT unable to update userdata! No record for: " + dbp.idField != null ? dbp.idField + " = " + u.getID() : "username = " + u.getName().toLowerCase(), Server.MSG_AUTH, Server.LVL_MAJOR);
                return;
            } else if (rows > 1) {
                Server.log(Thread.currentThread(), this.toString() + "LOGOUT unable to update userdata! More than one value would be updated: (" + dbp.idField != null ? dbp.idField + " = " + u.getID() : "username = " + u.getName().toLowerCase() + ")", Server.MSG_AUTH, Server.LVL_MAJOR);
                try {
                    con.rollback();
                    Server.log (Thread.currentThread(), this.toString() + "LOGOUT rollback successfully", Server.MSG_AUTH, Server.LVL_VERBOSE);
                } catch (SQLException se) {
                    Server.log (Thread.currentThread(), this.toString() + "LOGOUT rollback failed!!!", Server.MSG_AUTH, Server.LVL_MAJOR);
                }
            }
            checkWarnings(ps, "logoutUser");
        } catch (Exception e) {
            isValid=false;
            release();
            throw e;
        }
    }

    /**
     * replaces every < and every > with an HTML-entity and returns the value
     * @param rs
     * @param c
     * @return String The string having the replaces < and >-characters
     */
    private static String getEncodedString (ResultSet rs, int idx) {
        if (rs == null) 
            return null;
        try {
            String result = rs.getString (idx);
            if (result==null) 
                return null;
            result = result.replaceAll ("[<]", "&lt;");
            result = result.replaceAll ("[>]", "&gt;");
            return result;
        } catch (Exception e) {
            Server.debug ("static PoolElement", "getEncodedString: error geting encoded string", e, Server.MSG_ERROR, Server.LVL_MAJOR);
        }
        return null;
    }
    
    private void checkThread() throws CanceledRequestException {
        if (Thread.currentThread().isInterrupted())
            throw new CanceledRequestException ("ConnectionBuffer has been invalidated");
    }

    public void finalize() {
        if (Server.TRACE_CREATE_AND_FINALIZE)
            Server.log(this, "----------------------------------------FINALIZED", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
    }
}

⌨️ 快捷键说明

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