IMAP: make keepalive spaces optional with new davmail.imapEnableKeepalive setting

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2143 3d1905a2-6b24-0410-a738-b14d5a86fcbd
master
mguessan 2013-06-13 08:53:44 +00:00
parent badbdc5bc6
commit 53819c01e8
3 changed files with 97 additions and 46 deletions

View File

@ -0,0 +1,84 @@
/*
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
* Copyright (C) 2013 Mickael Guessant
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package davmail.exchange;
import davmail.Settings;
import org.apache.log4j.Logger;
import javax.mail.MessagingException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;
/**
* Load folder messages in a separate thread.
*/
public class FolderLoadThread extends Thread {
private static final Logger LOGGER = Logger.getLogger(FolderLoadThread.class);
boolean isComplete = false;
ExchangeSession.Folder folder;
IOException exception;
FolderLoadThread(String threadName, ExchangeSession.Folder folder) {
super(threadName + "-LoadFolder");
setDaemon(true);
this.folder = folder;
}
public void run() {
try {
folder.loadMessages();
} catch (IOException e) {
exception = e;
} finally {
isComplete = true;
}
}
/**
* Load folder in a separate thread.
*
* @param folder current folder
* @param outputStream client connection
* @throws InterruptedException on error
* @throws IOException on error
*/
public static void loadFolder(ExchangeSession.Folder folder, OutputStream outputStream) throws InterruptedException, IOException {
FolderLoadThread folderLoadThread = new FolderLoadThread(currentThread().getName(), folder);
folderLoadThread.start();
while (!folderLoadThread.isComplete) {
folderLoadThread.join(20000);
LOGGER.debug("Still loading " + folder.folderPath + " (" + folder.count() + " messages)");
if (Settings.getBooleanProperty("davmail.imapEnableKeepalive", false)) {
try {
outputStream.write(' ');
outputStream.flush();
} catch (SocketException e) {
folderLoadThread.interrupt();
throw e;
}
}
}
if (folderLoadThread.exception != null) {
throw folderLoadThread.exception;
}
}
}

View File

@ -1,5 +1,6 @@
package davmail.exchange;
import davmail.Settings;
import org.apache.log4j.Logger;
import javax.mail.MessagingException;
@ -57,14 +58,16 @@ public class MessageLoadThread extends Thread {
while (!messageLoadThread.isComplete) {
messageLoadThread.join(10000);
LOGGER.debug("Still loading uid " + message.getUid() + " imapUid " + message.getImapUid());
try {
outputStream.write(' ');
outputStream.flush();
} catch (SocketException e) {
// client closed connection, stop thread
message.dropMimeMessage();
messageLoadThread.interrupt();
throw e;
if (Settings.getBooleanProperty("davmail.imapEnableKeepalive", false)) {
try {
outputStream.write(' ');
outputStream.flush();
} catch (SocketException e) {
// client closed connection, stop thread
message.dropMimeMessage();
messageLoadThread.interrupt();
throw e;
}
}
}
if (messageLoadThread.ioException != null) {

View File

@ -30,6 +30,7 @@ import davmail.exception.HttpNotFoundException;
import davmail.exception.InsufficientStorageException;
import davmail.exchange.ExchangeSession;
import davmail.exchange.ExchangeSessionFactory;
import davmail.exchange.FolderLoadThread;
import davmail.exchange.MessageLoadThread;
import davmail.ui.tray.DavGatewayTray;
import davmail.util.IOUtil;
@ -57,28 +58,6 @@ public class ImapConnection extends AbstractConnection {
protected String baseMailboxPath;
ExchangeSession.Folder currentFolder;
class FolderLoadThread extends Thread {
boolean isComplete = false;
ExchangeSession.Folder folder;
IOException exception;
FolderLoadThread(String threadName, ExchangeSession.Folder folder) {
super(threadName + "-LoadFolder");
setDaemon(true);
this.folder = folder;
}
public void run() {
try {
folder.loadMessages();
} catch (IOException e) {
exception = e;
} finally {
isComplete = true;
}
}
}
/**
* Initialize the streams and start the thread.
*
@ -251,25 +230,10 @@ public class ImapConnection extends AbstractConnection {
sendClient("* " + currentFolder.count() + " EXISTS");
} else {
// load folder in a separate thread
FolderLoadThread folderLoadThread = new FolderLoadThread(currentThread().getName(), currentFolder);
folderLoadThread.start();
LOGGER.debug("*");
os.write('*');
while (!folderLoadThread.isComplete) {
folderLoadThread.join(20000);
LOGGER.debug("Still loading " + currentFolder.folderPath+" ("+currentFolder.count()+" messages)");
try {
os.write(' ');
os.flush();
} catch (SocketException e) {
folderLoadThread.interrupt();
throw e;
}
}
FolderLoadThread.loadFolder(currentFolder, os);
sendClient(" " + currentFolder.count() + " EXISTS");
if (folderLoadThread.exception != null) {
throw folderLoadThread.exception;
}
}
sendClient("* " + currentFolder.recent + " RECENT");