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

📄 mboxmailrepository.java

📁 java 开发的邮件服务器平台。支持以下协议。 协议可以修改为自己的专门标识
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            if (foundMessage == null) {                getLogger().error("found message is null!");                return null;            }            res = new MailImpl(foundMessage);            res.setName(key);            if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {                StringBuffer logBuffer =                        new StringBuffer(128)                        .append(this.getClass().getName())                        .append(" Retrieving entry for key ")                        .append(key);                getLogger().debug(logBuffer.toString());            }        } catch (MessagingException e) {            getLogger().error("Unable to parse mime message for " + mboxFile + "\n" + e.getMessage(), e);        }        return res;    }    /**     * Remove an existing message     * @param mail     */    public void remove(MailImpl mail) {        // Convert the message into a key        Vector delVec = new Vector();        delVec.addElement(mail);        remove(delVec);    }    /**     * Attempt to get a lock on the mbox by creating     * the file mboxname.lock     * @throws Exception     */    private void lockMBox() throws Exception {        // Create the lock file (if possible)        String lockFileName = mboxFile + LOCKEXT;        int sleepCount = 0;        File mBoxLock = new File(lockFileName);        if (!mBoxLock.createNewFile()) {            // This is not good, somebody got the lock before me            // So wait for a file            while (!mBoxLock.createNewFile() && sleepCount < MAXSLEEPTIMES) {                try {                    if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {                        StringBuffer logBuffer =                                new StringBuffer(128)                                .append(this.getClass().getName())                                .append(" Waiting for lock on file ")                                .append(mboxFile);                        getLogger().debug(logBuffer.toString());                    }                    Thread.sleep(LOCKSLEEPDELAY);                    sleepCount++;                } catch (InterruptedException e) {                    getLogger().error("File lock wait for " + mboxFile + " interrupted!",e);                }            }            if (sleepCount >= MAXSLEEPTIMES) {                throw new Exception("Unable to get lock on file " + mboxFile);            }        }    }    /**     * Unlock a previously locked mbox file     */    private void unlockMBox() {        // Just delete the MBOX file        String lockFileName = mboxFile + LOCKEXT;        File mBoxLock = new File(lockFileName);        if (!mBoxLock.delete()) {            StringBuffer logBuffer =                    new StringBuffer(128)                    .append(this.getClass().getName())                    .append(" Failed to delete lock file ")                    .append(lockFileName);            getLogger().error(logBuffer.toString());        }    }    /**     * Remove a list of messages from disk     * The collection is simply a list of mails to delete     * @param mails     */    public void remove(final Collection mails)    {        if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {            StringBuffer logBuffer =                    new StringBuffer(128)                    .append(this.getClass().getName())                    .append(" Removing entry for key ")                    .append(mails);            getLogger().debug(logBuffer.toString());        }        // The plan is as follows:        // Attempt to locate the message in the file        // by reading through the        // once we've done that then seek to the file        try {            RandomAccessFile ins = new RandomAccessFile(mboxFile, "r"); // The source            final RandomAccessFile outputFile = new RandomAccessFile(mboxFile + WORKEXT, "rw"); // The destination            parseMboxFile(ins, new MessageAction() {                public boolean isComplete() { return false; }                public MimeMessage messageAction(String messageSeparator, String bodyText, long messageStart) {                    // Write out the messages as we go, until we reach the key we want                    try {                        String currentKey=generateKeyValue(bodyText);                        boolean foundKey=false;                        Iterator mailList = mails.iterator();                        String key;                        while (mailList.hasNext()) {                            // Attempt to find the current key in the array                            key = ((MailImpl)mailList.next()).getName();                            if (key.equals(currentKey)) {                                // Don't write the message to disk                                foundKey = true;                                break;                            }                        }                        if (foundKey == false)                        {                            // We didn't find the key in the array so we will keep it                            outputFile.writeBytes(messageSeparator + "\n");                            outputFile.writeBytes(bodyText);                        }                    } catch (NoSuchAlgorithmException e) {                        getLogger().error("MD5 not supported! ",e);                    } catch (IOException e) {                        getLogger().error("Unable to write file (General I/O problem) " + mboxFile, e);                    }                    return null;                }            });            ins.close();            outputFile.close();            // Delete the old mbox file            File mbox = new File(mboxFile);            mbox.delete();            // And rename the lock file to be the new mbox            mbox = new File(mboxFile + WORKEXT);            if (!mbox.renameTo(new File(mboxFile)))            {                 System.out.println("Failed to rename file!");            }            // Now delete the keys in mails from the main hash            Iterator mailList = mails.iterator();            String key;            while (mailList.hasNext()) {                // Attempt to find the current key in the array                key = ((MailImpl)mailList.next()).getName();                mList.remove(key);            }        } catch (FileNotFoundException e) {            getLogger().error("Unable to save(open) file (File not found) " + mboxFile, e);        } catch (IOException e) {            getLogger().error("Unable to write file (General I/O problem) " + mboxFile, e);        }    }    /**     * Remove a mail from the mbox file     * @param key The key of the mail to delete     */    public void remove(String key) {        loadKeys();        try {            lockMBox();        } catch (Exception e) {            getLogger().error("Lock failed!",e);            return; // No lock, so exit        }        ArrayList keys = new ArrayList();        keys.add(key);        this.remove(keys);        unlockMBox();    }    /**     * Not implemented     * @param key     * @return     */    public boolean lock(String key) {        return false;    }    /**     * Not implemented     * @param key     * @return     */    public boolean unlock(String key) {        return false;    }    public void compose(ComponentManager componentManager) throws ComponentException {    }    /**     * Configure the component     * @param conf     * @throws ConfigurationException     */    public void configure(Configuration conf) throws ConfigurationException {        String destination;        this.mList = null;        BUFFERING = conf.getAttributeAsBoolean("BUFFERING", true);        destination = conf.getAttribute("destinationURL");        if (destination.charAt(destination.length() - 1) == '/') {            // Remove the trailing / as well as the protocol marker            mboxFile = destination.substring("mbox://".length(), destination.lastIndexOf("/"));        } else {            mboxFile = destination.substring("mbox://".length());        }        if (getLogger().isDebugEnabled()) {            getLogger().debug("MBoxMailRepository.destinationURL: " + destination);        }        String checkType = conf.getAttribute("type");        if (!(checkType.equals("MAIL") || checkType.equals("SPOOL"))) {            String exceptionString = "Attempt to configure MboxMailRepository as " + checkType;            if (getLogger().isWarnEnabled()) {                getLogger().warn(exceptionString);            }            throw new ConfigurationException(exceptionString);        }    }    /**     * Initialise the component     * @throws Exception     */    public void initialize() throws Exception {    }    public static void main(String[] args) {        // Test invocation        MBoxMailRepository mbx = new MBoxMailRepository();        mbx.mboxFile = "C:\\java\\test\\1998-05.txt";        Iterator mList = mbx.list();        while (mList.hasNext()) {            String key = (String) mList.next();            //System.out.println("key=" + key);            /*MailImpl mi =  mbx.retrieve(key);            try            {                System.out.println("Subject : " +  (mi.getMessage()).getSubject());            }            catch (MessagingException e)            {                e.printStackTrace();  //To change body of catch statement use Options | File Templates.            } */        }/*        MailImpl mi = mbx.retrieve("ffffffb4ffffffe2f59fffffff291dffffffde4366243ffffff971d1f24");        try {            System.out.println("Subject : " + (mi.getMessage()).getSubject());        } catch (MessagingException e) {            e.printStackTrace();  //To change body of catch statement use Options | File Templates.        }        mbx.remove("ffffffb4ffffffe2f59fffffff291dffffffde4366243ffffff971d1f24");*/    }}

⌨️ 快捷键说明

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