View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2013  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  package davmail.exchange;
20  
21  import davmail.Settings;
22  import org.apache.log4j.Logger;
23  
24  import javax.mail.MessagingException;
25  import java.io.IOException;
26  import java.io.OutputStream;
27  import java.net.SocketException;
28  
29  /**
30   * Load folder messages in a separate thread.
31   */
32  public class FolderLoadThread extends Thread {
33      private static final Logger LOGGER = Logger.getLogger(FolderLoadThread.class);
34  
35      boolean isComplete = false;
36      ExchangeSession.Folder folder;
37      IOException exception;
38  
39      FolderLoadThread(String threadName, ExchangeSession.Folder folder) {
40          super(threadName + "-LoadFolder");
41          setDaemon(true);
42          this.folder = folder;
43      }
44  
45      public void run() {
46          try {
47              folder.loadMessages();
48          } catch (IOException e) {
49              exception = e;
50          } finally {
51              isComplete = true;
52          }
53      }
54  
55      /**
56       * Load folder in a separate thread.
57       *
58       * @param folder       current folder
59       * @param outputStream client connection
60       * @throws InterruptedException on error
61       * @throws IOException          on error
62       */
63      public static void loadFolder(ExchangeSession.Folder folder, OutputStream outputStream) throws InterruptedException, IOException {
64          FolderLoadThread folderLoadThread = new FolderLoadThread(currentThread().getName(), folder);
65          folderLoadThread.start();
66          while (!folderLoadThread.isComplete) {
67              folderLoadThread.join(20000);
68              LOGGER.debug("Still loading " + folder.folderPath + " (" + folder.count() + " messages)");
69              if (Settings.getBooleanProperty("davmail.enableKeepAlive", false)) {
70                  try {
71                      outputStream.write(' ');
72                      outputStream.flush();
73                  } catch (SocketException e) {
74                      folderLoadThread.interrupt();
75                      throw e;
76                  }
77              }
78          }
79          if (folderLoadThread.exception != null) {
80              throw folderLoadThread.exception;
81          }
82  
83      }
84  }