| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.mail.internal.thread; |
| 21 |
|
|
| 22 |
|
import java.util.Collections; |
| 23 |
|
|
| 24 |
|
import javax.inject.Inject; |
| 25 |
|
import javax.inject.Named; |
| 26 |
|
import javax.inject.Provider; |
| 27 |
|
import javax.inject.Singleton; |
| 28 |
|
import javax.mail.MessagingException; |
| 29 |
|
import javax.mail.Session; |
| 30 |
|
import javax.mail.Transport; |
| 31 |
|
|
| 32 |
|
import org.apache.commons.lang.exception.ExceptionUtils; |
| 33 |
|
import org.xwiki.component.annotation.Component; |
| 34 |
|
import org.xwiki.context.ExecutionContext; |
| 35 |
|
import org.xwiki.context.ExecutionContextException; |
| 36 |
|
import org.xwiki.context.ExecutionContextManager; |
| 37 |
|
import org.xwiki.mail.ExtendedMimeMessage; |
| 38 |
|
import org.xwiki.mail.MailContentStore; |
| 39 |
|
import org.xwiki.mail.MailListener; |
| 40 |
|
|
| 41 |
|
import com.xpn.xwiki.XWikiContext; |
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
@version |
| 47 |
|
@since |
| 48 |
|
|
| 49 |
|
@Component |
| 50 |
|
@Named("send") |
| 51 |
|
@Singleton |
| |
|
| 88.9% |
Uncovered Elements: 8 (72) |
Complexity: 22 |
Complexity Density: 0.45 |
|
| 52 |
|
public class SendMailRunnable extends AbstractMailRunnable |
| 53 |
|
{ |
| 54 |
|
@Inject |
| 55 |
|
private MailQueueManager<SendMailQueueItem> sendMailQueueManager; |
| 56 |
|
|
| 57 |
|
@Inject |
| 58 |
|
@Named("filesystem") |
| 59 |
|
private MailContentStore mailContentStore; |
| 60 |
|
|
| 61 |
|
@Inject |
| 62 |
|
private ExecutionContextManager executionContextManager; |
| 63 |
|
|
| 64 |
|
@Inject |
| 65 |
|
private Provider<XWikiContext> contextProvider; |
| 66 |
|
|
| 67 |
|
private Transport currentTransport; |
| 68 |
|
|
| 69 |
|
private Session currentSession; |
| 70 |
|
|
| 71 |
|
private int count; |
| 72 |
|
|
| |
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 73 |
12 |
@Override... |
| 74 |
|
public void run() |
| 75 |
|
{ |
| 76 |
12 |
try { |
| 77 |
|
|
| 78 |
12 |
prepareContext(); |
| 79 |
|
|
| 80 |
12 |
runInternal(); |
| 81 |
|
} catch (ExecutionContextException e) { |
| 82 |
|
|
| 83 |
0 |
logger.error("Failed to initialize the send mail thread's execution context", e); |
| 84 |
|
} finally { |
| 85 |
12 |
closeTransport(); |
| 86 |
|
} |
| 87 |
|
} |
| 88 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 89 |
12 |
private void prepareContext() throws ExecutionContextException... |
| 90 |
|
{ |
| 91 |
|
|
| 92 |
12 |
ExecutionContext ec = new ExecutionContext(); |
| 93 |
12 |
this.executionContextManager.initialize(ec); |
| 94 |
|
} |
| 95 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 96 |
21 |
private void prepareContextForQueueItem(SendMailQueueItem mailItem)... |
| 97 |
|
{ |
| 98 |
|
|
| 99 |
|
|
| 100 |
21 |
XWikiContext xcontext = this.contextProvider.get(); |
| 101 |
21 |
xcontext.setWikiId(mailItem.getWikiId()); |
| 102 |
|
} |
| 103 |
|
|
| |
|
| 87.5% |
Uncovered Elements: 2 (16) |
Complexity: 5 |
Complexity Density: 0.42 |
|
| 104 |
12 |
private void runInternal()... |
| 105 |
|
{ |
| 106 |
12 |
do { |
| 107 |
8282 |
try { |
| 108 |
|
|
| 109 |
8282 |
if (this.sendMailQueueManager.hasMessage()) { |
| 110 |
|
|
| 111 |
|
|
| 112 |
21 |
SendMailQueueItem mailItem = this.sendMailQueueManager.peekMessage(); |
| 113 |
21 |
try { |
| 114 |
21 |
sendMail(mailItem); |
| 115 |
|
} finally { |
| 116 |
21 |
this.sendMailQueueManager.removeMessageFromQueue(mailItem); |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
21 |
waitSendWaitTime(); |
| 123 |
|
} |
| 124 |
|
|
| 125 |
8282 |
Thread.sleep(50L); |
| 126 |
|
} catch (InterruptedException e) { |
| 127 |
|
|
| 128 |
12 |
this.logger.debug("Mail Sender Thread was forcefully stopped", e); |
| 129 |
12 |
break; |
| 130 |
|
} catch (Exception e) { |
| 131 |
|
|
| 132 |
0 |
this.logger.error("Unexpected error in the Mail Sender Thread", e); |
| 133 |
|
} |
| 134 |
8270 |
} while (!this.shouldStop); |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
@param |
| 141 |
|
|
| |
|
| 87.5% |
Uncovered Elements: 4 (32) |
Complexity: 9 |
Complexity Density: 0.41 |
|
| 142 |
21 |
protected void sendMail(SendMailQueueItem item)... |
| 143 |
|
{ |
| 144 |
21 |
prepareContextForQueueItem(item); |
| 145 |
|
|
| 146 |
21 |
MailListener listener = item.getListener(); |
| 147 |
|
|
| 148 |
21 |
ExtendedMimeMessage message; |
| 149 |
21 |
try { |
| 150 |
|
|
| 151 |
21 |
message = this.mailContentStore.load(item.getSession(), item.getBatchId(), item.getUniqueMessageId()); |
| 152 |
|
} catch (Exception e) { |
| 153 |
2 |
if (listener != null) { |
| 154 |
2 |
listener.onSendMessageFatalError(item.getUniqueMessageId(), e, Collections.<String, Object>emptyMap()); |
| 155 |
|
} |
| 156 |
2 |
return; |
| 157 |
|
} |
| 158 |
|
|
| 159 |
19 |
try { |
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
19 |
if (item.getSession() != this.currentSession || (this.count % 100) == 0) { |
| 165 |
14 |
closeTransport(); |
| 166 |
14 |
this.currentSession = item.getSession(); |
| 167 |
14 |
this.currentTransport = this.currentSession.getTransport("smtp"); |
| 168 |
14 |
this.currentTransport.connect(); |
| 169 |
5 |
} else if (!this.currentTransport.isConnected()) { |
| 170 |
0 |
this.currentTransport.connect(); |
| 171 |
|
} |
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
18 |
this.currentTransport.sendMessage(message, message.getAllRecipients()); |
| 177 |
16 |
this.count++; |
| 178 |
|
|
| 179 |
|
|
| 180 |
16 |
if (listener != null) { |
| 181 |
14 |
listener.onSendMessageSuccess(message, Collections.<String, Object>emptyMap()); |
| 182 |
|
} |
| 183 |
|
} catch (Exception e) { |
| 184 |
|
|
| 185 |
3 |
if (listener != null) { |
| 186 |
3 |
listener.onSendMessageError(message, e, Collections.<String, Object>emptyMap()); |
| 187 |
|
} |
| 188 |
|
} |
| 189 |
|
} |
| 190 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 191 |
21 |
private void waitSendWaitTime() throws InterruptedException... |
| 192 |
|
{ |
| 193 |
21 |
long sendWaitTime = this.configuration.getSendWaitTime(); |
| 194 |
21 |
Thread.sleep(sendWaitTime); |
| 195 |
|
} |
| 196 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 197 |
26 |
private void closeTransport()... |
| 198 |
|
{ |
| 199 |
26 |
if (this.currentTransport != null) { |
| 200 |
14 |
try { |
| 201 |
14 |
this.currentTransport.close(); |
| 202 |
|
} catch (MessagingException e) { |
| 203 |
0 |
this.logger.warn("Failed to close JavaMail Transport connection. Reason [{}]", |
| 204 |
|
ExceptionUtils.getRootCauseMessage(e)); |
| 205 |
|
} |
| 206 |
|
} |
| 207 |
|
} |
| 208 |
|
} |