View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.transport.mail.monitor;
18  
19  import javax.mail.Folder;
20  import javax.mail.Message;
21  import javax.mail.MessagingException;
22  import javax.mail.event.MessageCountAdapter;
23  import javax.mail.event.MessageCountEvent;
24  import javax.mail.event.MessageCountListener;
25  
26  import com.sun.mail.imap.IMAPFolder;
27  import org.springframework.util.Assert;
28  
29  /**
30   * Implementation of the {@link MonitoringStrategy} interface that uses the IMAP IDLE command for asynchronous message
31   * detection.
32   * <p/>
33   * <b>Note</b> that this implementation is only suitable for use with IMAP servers which support the IDLE command.
34   * Additionally, this strategy requires JavaMail version 1.4.1.
35   *
36   * @author Arjen Poutsma
37   * @since 1.5.0
38   */
39  public class ImapIdleMonitoringStrategy extends AbstractMonitoringStrategy {
40  
41      private MessageCountListener messageCountListener;
42  
43      protected void waitForNewMessages(Folder folder) throws MessagingException, InterruptedException {
44          Assert.isInstanceOf(IMAPFolder.class, folder);
45          IMAPFolder imapFolder = (IMAPFolder) folder;
46          // retrieve unseen messages before we enter the blocking idle call
47          if (searchForNewMessages(folder).length > 0) {
48              return;
49          }
50          if (messageCountListener == null) {
51              createMessageCountListener();
52          }
53          folder.addMessageCountListener(messageCountListener);
54          try {
55              imapFolder.idle();
56          }
57          finally {
58              folder.removeMessageCountListener(messageCountListener);
59          }
60      }
61  
62      private void createMessageCountListener() {
63          messageCountListener = new MessageCountAdapter() {
64              public void messagesAdded(MessageCountEvent e) {
65                  Message[] messages = e.getMessages();
66                  for (int i = 0; i < messages.length; i++) {
67                      try {
68                          // this will return the flow to the idle call, above
69                          messages[i].getLineCount();
70                      }
71                      catch (MessagingException ex) {
72                          // ignore
73                      }
74                  }
75              }
76          };
77      }
78  }