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

📄 jdbcmailrepository.java

📁 java 开发的邮件服务器平台。支持以下协议。 协议可以修改为自己的专门标识
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            PreparedStatement checkMessageExists = null;            ResultSet rsExists = null;            boolean exists = false;            try {                checkMessageExists =                     conn.prepareStatement(sqlQueries.getSqlString("checkMessageExistsSQL", true));                checkMessageExists.setString(1, mc.getName());                checkMessageExists.setString(2, repositoryName);                rsExists = checkMessageExists.executeQuery();                exists = rsExists.next() && rsExists.getInt(1) > 0;            } finally {                theJDBCUtil.closeJDBCResultSet(rsExists);                theJDBCUtil.closeJDBCStatement(checkMessageExists);            }            if (exists) {                //Update the existing record                PreparedStatement updateMessage = null;                try {                    updateMessage =                        conn.prepareStatement(sqlQueries.getSqlString("updateMessageSQL", true));                    updateMessage.setString(1, mc.getState());                    updateMessage.setString(2, mc.getErrorMessage());                    if (mc.getSender() == null) {                        updateMessage.setNull(3, java.sql.Types.VARCHAR);                    } else {                        updateMessage.setString(3, mc.getSender().toString());                    }                    StringBuffer recipients = new StringBuffer();                    for (Iterator i = mc.getRecipients().iterator(); i.hasNext(); ) {                        recipients.append(i.next().toString());                        if (i.hasNext()) {                            recipients.append("\r\n");                        }                    }                    updateMessage.setString(4, recipients.toString());                    updateMessage.setString(5, mc.getRemoteHost());                    updateMessage.setString(6, mc.getRemoteAddr());                    updateMessage.setTimestamp(7, new java.sql.Timestamp(mc.getLastUpdated().getTime()));                    updateMessage.setString(8, mc.getName());                    updateMessage.setString(9, repositoryName);                    updateMessage.execute();                } finally {                    Statement localUpdateMessage = updateMessage;                    // Clear reference to statement                    updateMessage = null;                    theJDBCUtil.closeJDBCStatement(localUpdateMessage);                }                //Determine whether attributes are used and available for storing                if (jdbcMailAttributesReady && mc.hasAttributes()) {                    String updateMessageAttrSql =                        sqlQueries.getSqlString("updateMessageAttributesSQL", false);                    PreparedStatement updateMessageAttr = null;                    try {                        updateMessageAttr =                            conn.prepareStatement(updateMessageAttrSql);                        ByteArrayOutputStream baos = new ByteArrayOutputStream();                        ObjectOutputStream oos = new ObjectOutputStream(baos);                        try {                            oos.writeObject(((MailImpl)mc).getAttributesRaw());                            oos.flush();                            ByteArrayInputStream attrInputStream =                                new ByteArrayInputStream(baos.toByteArray());                            updateMessageAttr.setBinaryStream(1, attrInputStream, baos.size());                        } finally {                            try {                                if (oos != null) {                                    oos.close();                                }                            } catch (IOException ioe) {                                getLogger().debug("JDBCMailRepository: Unexpected exception while closing output stream.");                            }                        }                        updateMessageAttr.setString(2, mc.getName());                        updateMessageAttr.setString(3, repositoryName);                        updateMessageAttr.execute();                    } catch (SQLException sqle) {                        getLogger().info("JDBCMailRepository: Trying to update mail attributes failed.",sqle);                                            } finally {                        theJDBCUtil.closeJDBCStatement(updateMessageAttr);                    }                }                //Determine whether the message body has changed, and possibly avoid                //  updating the database.                MimeMessage messageBody = mc.getMessage();                boolean saveBody = false;                if (messageBody instanceof MimeMessageWrapper) {                    MimeMessageWrapper message = (MimeMessageWrapper)messageBody;                    saveBody = message.isModified();                } else {                    saveBody = true;                }                if (saveBody) {                    try {                        updateMessage =                            conn.prepareStatement(sqlQueries.getSqlString("updateMessageBodySQL", true));                        ByteArrayOutputStream headerOut = new ByteArrayOutputStream();                        OutputStream bodyOut = null;                        try {                            if (sr == null) {                                //If there is no filestore, use the byte array to store headers                                //  and the body                                bodyOut = headerOut;                            } else {                                //Store the body in the stream repository                                bodyOut = sr.put(mc.getName());                            }                                    //Write the message to the headerOut and bodyOut.  bodyOut goes straight to the file                            MimeMessageWrapper.writeTo(messageBody, headerOut, bodyOut);                                    //Store the headers in the database                            ByteArrayInputStream headerInputStream =                                new ByteArrayInputStream(headerOut.toByteArray());                            updateMessage.setBinaryStream(1, headerInputStream, headerOut.size());                        } finally {                            closeOutputStreams(headerOut, bodyOut);                        }                        updateMessage.setString(2, mc.getName());                        updateMessage.setString(3, repositoryName);                        updateMessage.execute();                    } finally {                        theJDBCUtil.closeJDBCStatement(updateMessage);                    }                }            } else {                //Insert the record into the database                PreparedStatement insertMessage = null;                try {                    String insertMessageSQL = sqlQueries.getSqlString("insertMessageSQL", true);                    int number_of_parameters = getNumberOfParameters (insertMessageSQL);                    insertMessage =                        conn.prepareStatement(insertMessageSQL);                    insertMessage.setString(1, mc.getName());                    insertMessage.setString(2, repositoryName);                    insertMessage.setString(3, mc.getState());                    insertMessage.setString(4, mc.getErrorMessage());                    if (mc.getSender() == null) {                        insertMessage.setNull(5, java.sql.Types.VARCHAR);                    } else {                        insertMessage.setString(5, mc.getSender().toString());                    }                    StringBuffer recipients = new StringBuffer();                    for (Iterator i = mc.getRecipients().iterator(); i.hasNext(); ) {                        recipients.append(i.next().toString());                        if (i.hasNext()) {                            recipients.append("\r\n");                        }                    }                    insertMessage.setString(6, recipients.toString());                    insertMessage.setString(7, mc.getRemoteHost());                    insertMessage.setString(8, mc.getRemoteAddr());                    insertMessage.setTimestamp(9, new java.sql.Timestamp(mc.getLastUpdated().getTime()));                    MimeMessage messageBody = mc.getMessage();                        ByteArrayOutputStream headerOut = new ByteArrayOutputStream();                    OutputStream bodyOut = null;                    try {                        if (sr == null) {                            //If there is no sr, then use the same byte array to hold the headers                            //  and the body                            bodyOut = headerOut;                        } else {                            //Store the body in the file system.                            bodyOut = sr.put(mc.getName());                        }                                //Write the message to the headerOut and bodyOut.  bodyOut goes straight to the file                        MimeMessageWrapper.writeTo(messageBody, headerOut, bodyOut);                        ByteArrayInputStream headerInputStream =                            new ByteArrayInputStream(headerOut.toByteArray());                        insertMessage.setBinaryStream(10, headerInputStream, headerOut.size());                    } finally {                        closeOutputStreams(headerOut, bodyOut);                    }                    //Store the headers in the database                    //Store attributes                    if (number_of_parameters > 10) {                        ByteArrayOutputStream baos = new ByteArrayOutputStream();                        ObjectOutputStream oos = new ObjectOutputStream(baos);                        try {                            oos.writeObject(((MailImpl)mc).getAttributesRaw());                            oos.flush();                            ByteArrayInputStream attrInputStream =                                new ByteArrayInputStream(baos.toByteArray());                            insertMessage.setBinaryStream(11, attrInputStream, baos.size());                        } finally {                            try {                                if (oos != null) {                                    oos.close();                                }                            } catch (IOException ioe) {                                getLogger().debug("JDBCMailRepository: Unexpected exception while closing output stream.");                            }                        }                                            }                                        insertMessage.execute();                } finally {                    theJDBCUtil.closeJDBCStatement(insertMessage);                }            }            conn.commit();            conn.setAutoCommit(true);            synchronized (this) {//                notifyAll();                notify();            }        } catch (Exception e) {            throw new MessagingException("Exception caught while storing mail Container: " + e);        } finally {            theJDBCUtil.closeJDBCConnection(conn);        }    }    /**     * Retrieves a message given a key. At the moment, keys can be obtained     * from list()     *     * @param key the key of the message to retrieve     * @return the mail corresponding to this key, null if none exists     */    public MailImpl retrieve(String key) throws MessagingException {        if (DEEP_DEBUG) {            System.err.println("retrieving " + key);        }        Connection conn = null;        PreparedStatement retrieveMessage = null;        ResultSet rsMessage = null;        try {            conn = datasource.getConnection();            if (DEEP_DEBUG) {                System.err.println("got a conn " + key);            }            retrieveMessage =                conn.prepareStatement(sqlQueries.getSqlString("retrieveMessageSQL", true));            retrieveMessage.setString(1, key);            retrieveMessage.setString(2, repositoryName);            rsMessage = retrieveMessage.executeQuery();            if (DEEP_DEBUG) {                System.err.println("ran the query " + key);            }            if (!rsMessage.next()) {                if (getLogger().isDebugEnabled()) {                    StringBuffer debugBuffer =                        new StringBuffer(64)                                .append("Did not find a record ")                                .append(key)                                .append(" in ")                                .append(repositoryName);                    getLogger().debug(debugBuffer.toString());                }                return null;            }            //Determine whether attributes are used and retrieve them            PreparedStatement retrieveMessageAttr = null;            HashMap attributes = null;            if (jdbcMailAttributesReady) {                String retrieveMessageAttrSql =                    sqlQueries.getSqlString("retrieveMessageAttributesSQL", false);                ResultSet rsMessageAttr = null;

⌨️ 快捷键说明

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