Skip to content

Commit 9096093

Browse files
committed
#438: fail fast in case Transport claim failed on the connection pool. Otherwise, it becomes an NPE further down the chain.
1 parent fe2bcb5 commit 9096093

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

modules/batch-module/src/main/java/org/simplejavamail/internal/batchsupport/BatchException.java

+4
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ class BatchException extends RuntimeException {
77
BatchException(final String msg, final Throwable cause) {
88
super(msg, cause);
99
}
10+
11+
BatchException(final String msg) {
12+
super(msg);
13+
}
1014
}

modules/batch-module/src/main/java/org/simplejavamail/internal/batchsupport/BatchSupport.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import static java.lang.String.format;
2828
import static java.util.Objects.requireNonNull;
29+
import static java.util.Optional.ofNullable;
2930
import static java.util.concurrent.CompletableFuture.completedFuture;
3031
import static org.simplejavamail.internal.batchsupport.BatchException.ERROR_ACQUIRING_KEYED_POOLABLE;
3132
import static org.simplejavamail.internal.batchsupport.ClusterHelper.compareClusterConfig;
@@ -96,13 +97,20 @@ private void ensureClusterInitialized(@NotNull OperationalConfig operationalConf
9697
@Override
9798
@SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH", justification = "This is bullshit, Spotbugs. There's a requireNonNull() right in front of you, you numbnuts")
9899
public LifecycleDelegatingTransport acquireTransport(@NotNull final UUID clusterKey, @NotNull final Session session, boolean stickySession) {
100+
val smtpConnectionPool = requireNonNull(this.smtpConnectionPool, "Connection pool used before it was initialized. This shouldn't be possible.");
101+
checkConfigureOAuth2Token(session);
102+
103+
return ofNullable(getSessionTransportPoolableObject(smtpConnectionPool, clusterKey, session, stickySession))
104+
.map(LifecycleDelegatingTransportImpl::new)
105+
.orElseThrow(() -> new BatchException(format(ERROR_ACQUIRING_KEYED_POOLABLE, session)));
106+
}
107+
108+
@Nullable
109+
private PoolableObject<SessionTransport> getSessionTransportPoolableObject(SmtpConnectionPoolClustered smtpConnectionPool, UUID clusterKey, Session session, boolean stickySession) {
99110
try {
100-
requireNonNull(smtpConnectionPool, "Connection pool used before it was initialized. This shouldn't be possible.");
101-
checkConfigureOAuth2Token(session);
102-
final PoolableObject<SessionTransport> pooledTransport = stickySession
111+
return stickySession
103112
? smtpConnectionPool.claimResourceFromPool(new ResourceClusterAndPoolKey<>(clusterKey, session))
104113
: smtpConnectionPool.claimResourceFromCluster(clusterKey);
105-
return new LifecycleDelegatingTransportImpl(pooledTransport);
106114
} catch (InterruptedException e) {
107115
throw new BatchException(format(ERROR_ACQUIRING_KEYED_POOLABLE, session), e);
108116
}

0 commit comments

Comments
 (0)