1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.mercury.maildir.uid;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.io.RandomAccessFile;
18
19 import javax.mail.MessagingException;
20 import javax.mail.internet.MimeMessage;
21
22 import org.abstracthorizon.mercury.maildir.MaildirFolderData;
23 import org.abstracthorizon.mercury.maildir.MaildirMessage;
24 import org.abstracthorizon.mercury.maildir.MaildirStore;
25
26
27
28
29
30
31 public class UIDMaildirFolderData extends MaildirFolderData
32
33
34
35 public static final int MAX_RETRIES = 30;
36
37
38
39 protected long maxUid = -1;
40
41
42 protected long uidValidity = -1;
43
44
45
46
47
48
49 protected UIDMaildirFolderData(MaildirStore store, File file) {
50 super(store, file);
51 }
52
53
54
55
56
57 public long getMaxUID() {
58 return maxUid;
59 }
60
61
62
63
64
65
66
67
68
69
70
71 protected synchronized MaildirMessage createNewMaildirMessage(MimeMessage message, int num) throws IOException, MessagingException {
72 UIDMaildirMessage msg = new UIDMaildirMessage(this, message, num);
73 return msg;
74 }
75
76
77
78
79
80
81
82
83
84 protected MaildirMessage createExistingMaildirMessage(File file, int num) throws IOException, MessagingException {
85 UIDMaildirMessage msg = new UIDMaildirMessage(this, file, num);
86 return msg;
87 }
88
89
90
91
92
93
94
95 protected synchronized UID getNextUID() throws MessagingException {
96 RandomAccessFile uidFile = null;
97 File file = new File(getFolderFile(), UIDMaildirStore.NEXT_UID_FILE);
98 int retry = 0;
99 while ((uidFile == null) && (retry < MAX_RETRIES)) {
100 try {
101 uidFile = new RandomAccessFile(file, "rw");
102 } catch (IOException ignore) {
103 }
104 if (uidFile == null) {
105 try {
106 Thread.sleep(100);
107 } catch (InterruptedException ignore) {
108 }
109 retry = retry + 1;
110 }
111 }
112 try {
113 String old = uidFile.readLine();
114 long uid = 0;
115 try {
116 uid = Long.parseLong(old);
117 } catch (NumberFormatException ignore) {
118 }
119 uid = uid + 1;
120 uidFile.seek(0);
121 uidFile.writeBytes(Long.toString(uid));
122 uidFile.close();
123 maxUid = uid;
124 return new UID(uid);
125 } catch (IOException e) {
126 throw new MessagingException("Cannot obtain next uid", e);
127 } finally {
128 try {
129 uidFile.close();
130 } catch (IOException ignore) {
131 }
132 }
133 }
134
135
136
137
138
139 public long getUIDValidity() {
140 if (uidValidity != -1) {
141 return uidValidity;
142 }
143 File file = new File(getFolderFile(), UIDMaildirStore.UID_VALIDITY_FILE);
144 if (file.exists()) {
145 try {
146 RandomAccessFile uidFile = new RandomAccessFile(file, "r");
147 try {
148 String line = uidFile.readLine();
149 try {
150 uidValidity = Long.parseLong(line);
151 } catch (NumberFormatException ignore) {
152 }
153 } finally {
154 uidFile.close();
155 }
156 } catch (IOException ignore) {
157 }
158 }
159 if (uidValidity == -1) {
160 uidValidity = System.currentTimeMillis();
161 try {
162 RandomAccessFile uidFile = new RandomAccessFile(file, "rw");
163 try {
164 uidFile.writeBytes(Long.toString(uidValidity));
165 } finally {
166 uidFile.close();
167 }
168 } catch (IOException ignore) {
169 }
170 }
171 return uidValidity;
172 }
173
174
175
176
177
178 public int hashCode() {
179 return (int)getUIDValidity();
180 }
181 }