1 | /** |
---|
2 | * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. |
---|
3 | * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior |
---|
4 | * |
---|
5 | * This program is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU Affero General Public License as |
---|
7 | * published by the Free Software Foundation, either version 3 of the |
---|
8 | * 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 Affero General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Affero General Public License |
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | */ |
---|
18 | |
---|
19 | /******************************************************************************\ |
---|
20 | * |
---|
21 | * This product was developed by |
---|
22 | * |
---|
23 | * SERVIÃO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), |
---|
24 | * |
---|
25 | * a government company established under Brazilian law (5.615/70), |
---|
26 | * at Department of Development of Porto Alegre. |
---|
27 | * |
---|
28 | \******************************************************************************/ |
---|
29 | |
---|
30 | package serpro.mailarchiver.session; |
---|
31 | |
---|
32 | import java.nio.file.Files; |
---|
33 | import java.nio.file.Path; |
---|
34 | import java.util.Map; |
---|
35 | import java.util.Map.Entry; |
---|
36 | import java.util.TreeMap; |
---|
37 | import java.util.UUID; |
---|
38 | import java.util.concurrent.BlockingQueue; |
---|
39 | import java.util.concurrent.BrokenBarrierException; |
---|
40 | import java.util.concurrent.CyclicBarrier; |
---|
41 | import java.util.concurrent.LinkedBlockingQueue; |
---|
42 | |
---|
43 | import org.apache.commons.lang3.mutable.MutableBoolean; |
---|
44 | |
---|
45 | import org.springframework.beans.factory.annotation.Autowired; |
---|
46 | import org.springframework.beans.factory.annotation.Configurable; |
---|
47 | |
---|
48 | import serpro.mailarchiver.service.Utils; |
---|
49 | import serpro.mailarchiver.service.dto.TFault; |
---|
50 | import serpro.mailarchiver.util.Logger; |
---|
51 | import serpro.mailarchiver.util.LuceneIndex; |
---|
52 | |
---|
53 | @Configurable(preConstruction=true) |
---|
54 | public class Session { |
---|
55 | |
---|
56 | private static final Logger log = Logger.getLocalLogger(); |
---|
57 | |
---|
58 | @Autowired |
---|
59 | private Utils utils; |
---|
60 | |
---|
61 | private static final InheritableThreadLocal<Session> threadSession = new InheritableThreadLocal<Session>(); |
---|
62 | |
---|
63 | public static void setThreadSession(Session session) { |
---|
64 | threadSession.set(session); |
---|
65 | } |
---|
66 | |
---|
67 | public static Session getThreadSession() { |
---|
68 | return threadSession.get(); |
---|
69 | } |
---|
70 | |
---|
71 | public static String internalizeFolderId(String id) { |
---|
72 | if((id == null) || id.isEmpty() || id.endsWith("home")) { |
---|
73 | return getHomeFolderId(); |
---|
74 | } |
---|
75 | else if(id.endsWith("inbox")) { |
---|
76 | return getInboxFolderId(); |
---|
77 | } |
---|
78 | else if(id.endsWith("outbox")) { |
---|
79 | return getOutboxFolderId(); |
---|
80 | } |
---|
81 | else if(id.endsWith("drafts")) { |
---|
82 | return getDraftsFolderId(); |
---|
83 | } |
---|
84 | else if(id.endsWith("sent")) { |
---|
85 | return getSentFolderId(); |
---|
86 | } |
---|
87 | else if(id.endsWith("spam")) { |
---|
88 | return getSpamFolderId(); |
---|
89 | } |
---|
90 | else if(id.endsWith("trash")) { |
---|
91 | return getTrashFolderId(); |
---|
92 | } |
---|
93 | else { |
---|
94 | return id; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | public static String getHomeFolderId() { |
---|
99 | return "_" + getThreadSession().userId.toLowerCase() + "_home"; |
---|
100 | } |
---|
101 | |
---|
102 | public static String getInboxFolderId() { |
---|
103 | return "_" + getThreadSession().userId.toLowerCase() + "_inbox"; |
---|
104 | } |
---|
105 | |
---|
106 | public static String getOutboxFolderId() { |
---|
107 | return "_" + getThreadSession().userId.toLowerCase() + "_outbox"; |
---|
108 | } |
---|
109 | |
---|
110 | public static String getDraftsFolderId() { |
---|
111 | return "_" + getThreadSession().userId.toLowerCase() + "_drafts"; |
---|
112 | } |
---|
113 | |
---|
114 | public static String getSentFolderId() { |
---|
115 | return "_" + getThreadSession().userId.toLowerCase() + "_sent"; |
---|
116 | } |
---|
117 | |
---|
118 | public static String getSpamFolderId() { |
---|
119 | return "_" + getThreadSession().userId.toLowerCase() + "_spam"; |
---|
120 | } |
---|
121 | |
---|
122 | public static String getTrashFolderId() { |
---|
123 | return "_" + getThreadSession().userId.toLowerCase() + "_trash"; |
---|
124 | } |
---|
125 | |
---|
126 | public static LuceneIndex getLuceneIndex() { |
---|
127 | return getThreadSession().index; |
---|
128 | } |
---|
129 | |
---|
130 | //-------------------------------------------------------------------------- |
---|
131 | private final String sessionId; |
---|
132 | |
---|
133 | private final String userId; |
---|
134 | |
---|
135 | private final LuceneIndex index; |
---|
136 | |
---|
137 | public Session(String userId) throws Exception { |
---|
138 | |
---|
139 | try { |
---|
140 | sessionId = UUID.randomUUID().toString(); |
---|
141 | |
---|
142 | this.userId = userId; |
---|
143 | |
---|
144 | Session.setThreadSession(this); |
---|
145 | SessionMap.put(this); |
---|
146 | |
---|
147 | utils.provideMailHome(userId); |
---|
148 | |
---|
149 | index = LuceneIndex.getInstance(userId); |
---|
150 | |
---|
151 | Path indexAbsolutePath = index.getAbsolutePath(); |
---|
152 | if(Files.notExists(indexAbsolutePath)) { |
---|
153 | Files.createDirectory(indexAbsolutePath); |
---|
154 | } |
---|
155 | |
---|
156 | startGateKeeper(); |
---|
157 | |
---|
158 | log.info("new session:%s user:%s", sessionId, userId); |
---|
159 | } |
---|
160 | catch(Exception ex) { |
---|
161 | SessionMap.remove(this); |
---|
162 | Session.setThreadSession(null); |
---|
163 | throw ex; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | public String getUserId() { |
---|
168 | return userId; |
---|
169 | } |
---|
170 | |
---|
171 | public String getSessionId() { |
---|
172 | return sessionId; |
---|
173 | } |
---|
174 | |
---|
175 | public LuceneIndex getIndex() { |
---|
176 | return index; |
---|
177 | } |
---|
178 | |
---|
179 | //-------------------------------------------------------------------------- |
---|
180 | private final BlockingQueue<MutableBoolean> queue = new LinkedBlockingQueue<MutableBoolean>(); |
---|
181 | private final CyclicBarrier barrier = new CyclicBarrier(2); |
---|
182 | |
---|
183 | final void startGateKeeper() { |
---|
184 | |
---|
185 | new Thread() { |
---|
186 | @Override |
---|
187 | public void run() { |
---|
188 | for(;;) { |
---|
189 | |
---|
190 | MutableBoolean waitSlot = null; |
---|
191 | try { |
---|
192 | waitSlot = queue.take(); |
---|
193 | } |
---|
194 | catch(InterruptedException ex) { |
---|
195 | log.warn(ex); |
---|
196 | } |
---|
197 | |
---|
198 | synchronized(waitSlot) { |
---|
199 | waitSlot.setValue(true); |
---|
200 | waitSlot.notify(); |
---|
201 | } |
---|
202 | |
---|
203 | try { |
---|
204 | barrier.await(); |
---|
205 | } |
---|
206 | catch(InterruptedException ex) { |
---|
207 | log.warn(ex); |
---|
208 | } |
---|
209 | catch(BrokenBarrierException ex) { |
---|
210 | log.warn(ex); |
---|
211 | } |
---|
212 | |
---|
213 | barrier.reset(); |
---|
214 | } |
---|
215 | } |
---|
216 | } |
---|
217 | .start(); |
---|
218 | } |
---|
219 | |
---|
220 | public void enroll() { |
---|
221 | final MutableBoolean waitSlot = new MutableBoolean(false); |
---|
222 | try { |
---|
223 | queue.put(waitSlot); |
---|
224 | } |
---|
225 | catch(InterruptedException ex) { |
---|
226 | log.warn(ex); |
---|
227 | } |
---|
228 | |
---|
229 | synchronized(waitSlot) { |
---|
230 | while(waitSlot.isFalse()) { |
---|
231 | try { |
---|
232 | waitSlot.wait(); |
---|
233 | } |
---|
234 | catch(InterruptedException ex) { |
---|
235 | log.warn(ex); |
---|
236 | } |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | public void depart() { |
---|
242 | try { |
---|
243 | barrier.await(); |
---|
244 | } |
---|
245 | catch(InterruptedException ex) { |
---|
246 | log.warn(ex); |
---|
247 | } |
---|
248 | catch(BrokenBarrierException ex) { |
---|
249 | log.warn(ex); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | //-------------------------------------------------------------------------- |
---|
254 | private TFault lastFault; |
---|
255 | |
---|
256 | public TFault getLastFault() { |
---|
257 | return lastFault; |
---|
258 | } |
---|
259 | |
---|
260 | public void setLastFault(TFault lastFault) { |
---|
261 | this.lastFault = lastFault; |
---|
262 | } |
---|
263 | |
---|
264 | //-------------------------------------------------------------------------- |
---|
265 | private final Map<String, Long> statistics = new TreeMap<String, Long>() { |
---|
266 | @Override |
---|
267 | public String toString() { |
---|
268 | StringBuilder sb = new StringBuilder(); |
---|
269 | for(Entry<String, Long> entry : entrySet()) { |
---|
270 | if(sb.length() > 0) { |
---|
271 | sb.append("\n"); |
---|
272 | } |
---|
273 | sb.append(entry.getKey()).append(": ").append(entry.getValue()); |
---|
274 | } |
---|
275 | return sb.toString(); |
---|
276 | } |
---|
277 | }; |
---|
278 | |
---|
279 | public void incStatistics(String name) { |
---|
280 | Long n = statistics.get(name); |
---|
281 | if(n == null) { |
---|
282 | statistics.put(name, 1L); |
---|
283 | } |
---|
284 | else { |
---|
285 | statistics.put(name, ++n); |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | public void clearStatistics() { |
---|
290 | statistics.clear(); |
---|
291 | } |
---|
292 | |
---|
293 | public Map<String, Long> getStatistics() { |
---|
294 | return statistics; |
---|
295 | } |
---|
296 | |
---|
297 | } |
---|