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

📄 dbuserhandler.java

📁 Java Mail Server JAVA编写的邮件服务器
💻 JAVA
字号:
/**************************************************************** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.     * *                                                              * * Copyright 2008 Jun Li(SiChuan University, the School of      * * Software Engineering). All rights reserved.                  * *                                                              * * Licensed to the JMS under one  or more contributor license   * * agreements.  See the LICENCE file  distributed with this     * * work for additional information regarding copyright          * * ownership.  The JMS licenses this file  you may not use this * * file except in compliance  with the License.                 * *                                                              * * Unless required by applicable law or agreed to in writing,   * * software distributed under the License is distributed on an  * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       * * KIND, either express or implied.  See the License for the    * * specific language governing permissions and limitations      * * under the License.                                           * ****************************************************************/package org.jpxx.mail.User;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import org.apache.log4j.Logger;import org.jpxx.mail.Factory;import org.jpxx.mail.Service.JMSBasicDataSource;import org.jpxx.mail.Domain.DomainHandler;import org.jpxx.mail.Util.EmailAddress;import org.jpxx.mail.Util.Security.MD5;/** * The user who was stored in database *  * @author Jun Li * @version 0.0.3, Date: 2008/05/14 *  */class DBUserHandler extends AbstractMailUserHandler {    /**     * Creates an instance of Logger and initializes it.      * It is to write log for <code>DBUserHandler</code>.     */    private Logger log = Factory.getSingletonInstance().getLogger(this);    private Connection conn = null;    private Statement stmt = null;    private ResultSet rs = null;    private DomainHandler dc = null;    public DBUserHandler() {        dc = new DomainHandler();    }    /**     * @see MailUserHandler#check(org.jpxx.mail.Util.EmailAddress,      * java.lang.String, java.lang.String)      */    public boolean check(EmailAddress emailAddress, String password, String id) {        try {            conn = JMSBasicDataSource.getSingletonInstance().                    getDataSource().getConnection();            stmt = conn.createStatement();            rs = stmt.executeQuery("SELECT * FROM user WHERE username='" +                    emailAddress.getUserName() + "' AND domain='" +                    emailAddress.getDomain() + "' AND password='" +                    password + "'");            String pwd = null;            while (rs.next()) {                pwd = rs.getString("password");                break;            }            if (id != null) {                if (pwd != null) {                    close();                    String encryptedPwd = MD5.encode(id + pwd);                    return encryptedPwd.equals(password);                } else {                    close();                    return false;                }            } else {                close();                return pwd.equals(password);            }        } catch (SQLException e) {            close();            return false;        }    }    /**     * @see MailUserHandler#checkUser(java.lang.String, java.lang.String)      */    public boolean checkUser(String user, String domain) {        try {            conn = JMSBasicDataSource.getSingletonInstance().                    getDataSource().getConnection();            stmt = conn.createStatement();            rs = stmt.executeQuery("SELECT * FROM user WHERE username='" +                    user + "' AND domain='" + domain + "'");            while (rs.next()) {                close();                return true;            }            close();            return false;        } catch (SQLException e) {            close();            return false;        }    }    /**     * @see MailUserHandler#checkUser(org.jpxx.mail.Util.EmailAddress)      */    public boolean checkUser(EmailAddress emailAddress) {        return this.checkUser(emailAddress.getUserName(),                emailAddress.getDomain());    }    public boolean addUser(String userName, String password, long size) {        String domain = dc.getDefaultDomain();        if (domain == null) {            log.error("Please add a domain to server!");            return false;        }        return this.addUser(userName, password, domain, size);    }    public boolean addUser(            String userName,            String password,            String domain,            long size) {        if (userName == null ||                userName.trim().equals("") ||                password == null ||                password.trim().equals("") ||                domain == null ||                domain.trim().equals("") || size < 1) {            return false;        }        try {            conn = JMSBasicDataSource.getSingletonInstance().                    getDataSource().getConnection();            stmt = conn.createStatement();            stmt.execute(                    "INSERT INTO user (username,password,domain,size) VALUES " +                    "('" + userName + "','" + password + "','" +                    domain + "'," + size + ")");            close();            return checkUser(userName, domain);        } catch (SQLException e) {            close();            log.error("\r\n" + userName + " maybe exist. \r\n" + e);            return false;        }    }    /**     * @see MailUserHandler#modifyUser(java.lang.String, java.lang.String, long)      */    public boolean modifyUser(String userName, String password, long size) {        String domain = dc.getDefaultDomain();        if (domain == null) {            log.error("Please add a domain to server!");            return false;        }        return this.modifyUser(userName, password, domain, size);    }    public boolean modifyUser(            String userName,            String password,            String domain,            long size) {        if (userName == null ||                userName.trim().equals("") ||                password == null ||                password.trim().equals("") ||                domain == null ||                domain.trim().equals("") || size < 1) {            return false;        }        try {            conn = JMSBasicDataSource.getSingletonInstance().                    getDataSource().getConnection();            stmt = conn.createStatement();            stmt.executeUpdate(                    "Update user SET password='" + password + "',domain='" +                    domain + "',size=" + size + " WHERE username='" +                    userName + "'");            return true;        } catch (SQLException e) {            return false;        } finally {            close();        }    }    /**     * @see MailUserHandler#deleteUser(java.lang.String, java.lang.String)      */    public boolean deleteUser(String userName, String domain) {        try {            conn = JMSBasicDataSource.getSingletonInstance().                    getDataSource().getConnection();            stmt = conn.createStatement();            stmt.executeUpdate(                    "DELETE * FROM user WHERE username='" +                    userName + "' AND domain='" + domain + "'");            return !checkUser(userName, domain);        } catch (SQLException e) {            return false;        } finally {            close();        }    }    /**     * List all users in a domain.     *      * @param domain mail server domain     * @return users list.     *      * @since JMS 0.0.3     */    public List<String> list(String domain) {        try {            conn = JMSBasicDataSource.getSingletonInstance().                    getDataSource().getConnection();            stmt = conn.createStatement();            rs = stmt.executeQuery(                    "SELECT username FROM user WHERE domain='" + domain + "'");            List<String> users = new ArrayList<String>();            while (rs.next()) {                users.add(rs.getString(0));            }            return users;        } catch (SQLException e) {        } finally {            close();        }        return null;    }        /**     * Get a user's information     * @param userName User's login name     * @param domain mail server domain     * @return User object     *      * @since JMS 0.0.3     */    public User getUser(String userName, String domain){        try {            conn = JMSBasicDataSource.getSingletonInstance().                    getDataSource().getConnection();            stmt = conn.createStatement();            rs = stmt.executeQuery(                    "SELECT * FROM user WHERE username='" +                    userName + "' AND domain='" + domain + "'");            User user = null;            while (rs.next()) {                user = new User(                        rs.getString("username"),                        rs.getString("password"),                        rs.getString("domain"),                        rs.getLong("size"));            }            return user;        } catch (SQLException e) {        } finally {            close();        }        return null;    }    private void close() {        try {            rs.close();        } catch (Exception e) {        }        try {            stmt.close();        } catch (Exception e) {        }        try {            conn.close();        } catch (Exception e) {        }    }}

⌨️ 快捷键说明

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