68
|
1 /*
|
|
2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com>
|
|
3
|
|
4 Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 of this software and associated documentation files (the "Software"), to deal
|
|
6 in the Software without restriction, including without limitation the rights
|
|
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 copies of the Software, and to permit persons to whom the Software is
|
|
9 furnished to do so, subject to the following conditions:
|
|
10
|
|
11 The above copyright notice and this permission notice shall be included in
|
|
12 all copies or substantial portions of the Software.
|
|
13
|
|
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20 THE SOFTWARE.
|
|
21 */
|
|
22
|
|
23 package fschmidt.util.mail.javamail;
|
|
24
|
|
25 import java.io.File;
|
|
26 import java.io.IOException;
|
|
27 import java.util.NoSuchElementException;
|
|
28 import java.util.Properties;
|
|
29 import javax.mail.MessagingException;
|
|
30 import javax.mail.Session;
|
|
31 import javax.mail.Transport;
|
|
32 import javax.mail.PasswordAuthentication;
|
|
33 import javax.mail.Authenticator;
|
|
34 import javax.mail.Message;
|
|
35 import javax.mail.Folder;
|
|
36 import javax.mail.Store;
|
|
37 import javax.mail.URLName;
|
|
38 import javax.mail.internet.MimeMessage;
|
|
39 import fschmidt.util.mail.Mail;
|
|
40 import fschmidt.util.mail.MailException;
|
|
41 import fschmidt.util.mail.MailIterator;
|
|
42 import fschmidt.util.mail.InServer;
|
|
43
|
|
44
|
|
45 public final class MstorInServer implements InServer {
|
|
46 private final Properties props = new Properties();
|
|
47 private Session session = null;
|
|
48 private final File file;
|
|
49 private Store store = null;
|
|
50
|
|
51 public MstorInServer(File file) {
|
|
52 this.file = file;
|
|
53 }
|
|
54
|
|
55 public void setMetaEnabled(boolean b) {
|
|
56 props.setProperty( "mstor.meta.enabled", Boolean.toString(b) );
|
|
57 }
|
|
58
|
|
59 private synchronized Session getSession() {
|
|
60 if( session==null ) {
|
|
61 session = Session.getInstance(props);
|
|
62 }
|
|
63 return session;
|
|
64 }
|
|
65
|
|
66 public synchronized MailIterator getMail() throws MailException {
|
|
67 if( store != null ) {
|
|
68 store = null;
|
|
69 throw new MailException("previous MailIterator not closed");
|
|
70 }
|
|
71 try {
|
|
72 store = getSession().getStore(new URLName("mstor:"+file.getParentFile().getCanonicalPath()));
|
|
73 store.connect();
|
|
74 final Folder folder = store.getFolder(file.getName());
|
|
75 folder.open(Folder.READ_ONLY);
|
|
76 final int nMsgs = folder.getMessageCount();
|
|
77 //System.out.println("nMsgs="+nMsgs);
|
|
78 return new MailIterator() {
|
|
79 int i = 1;
|
|
80
|
|
81 public boolean hasNext() {
|
|
82 return i <= nMsgs;
|
|
83 }
|
|
84
|
|
85 public Mail next() throws MailException, NoSuchElementException {
|
|
86 if( i > nMsgs )
|
|
87 throw new NoSuchElementException();
|
|
88 //System.out.println("i="+i);
|
|
89 try {
|
|
90 MimeMessage msg = (MimeMessage)folder.getMessage(i++);
|
|
91 return new MailImpl(msg);
|
|
92 } catch(MessagingException e) {
|
|
93 throw MailImpl.e(toString(),e);
|
|
94 }
|
|
95 }
|
|
96
|
|
97 public void close() throws MailException {
|
|
98 try {
|
|
99 folder.close(false);
|
|
100 store.close();
|
|
101 } catch(MessagingException e) {
|
|
102 throw MailImpl.e(toString(),e);
|
|
103 } finally {
|
|
104 store = null;
|
|
105 }
|
|
106 }
|
|
107 };
|
|
108 } catch(IOException e) {
|
|
109 cleanup();
|
|
110 throw new MailException(toString(),e);
|
|
111 } catch(MessagingException e) {
|
|
112 cleanup();
|
|
113 throw MailImpl.e(toString(),e);
|
|
114 }
|
|
115 }
|
|
116
|
|
117 private void cleanup() {
|
|
118 if( store != null ) {
|
|
119 try {
|
|
120 store.close();
|
|
121 } catch(MessagingException e2) {}
|
|
122 store = null;
|
|
123 }
|
|
124 }
|
|
125
|
|
126 public String toString() {
|
|
127 return "MstorInServer("+file+")";
|
|
128 }
|
|
129
|
|
130 }
|