PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks-mail/src/main/java/com/google/sitebricks/mail/SitebricksMail.java

http://github.com/dhanji/sitebricks
Java | 85 lines | 67 code | 15 blank | 3 comment | 11 complexity | 8f5b6095c274b9e9f0e2fd363df3e503 MD5 | raw file
 1package com.google.sitebricks.mail;
 2
 3import com.google.common.base.Preconditions;
 4import com.google.sitebricks.mail.Mail.AuthBuilder;
 5import com.google.sitebricks.mail.oauth.OAuth2Config;
 6import com.google.sitebricks.mail.oauth.OAuthConfig;
 7
 8import java.util.concurrent.ExecutorService;
 9import java.util.concurrent.Executors;
10import java.util.concurrent.TimeUnit;
11
12/**
13 * @author dhanji@gmail.com (Dhanji R. Prasanna)
14 */
15class SitebricksMail implements Mail, AuthBuilder {
16  private String host;
17  private int port;
18
19  private long timeout;
20
21  private ExecutorService bossPool;
22  private ExecutorService workerPool;
23
24  @Override
25  public AuthBuilder clientOf(String host, int port) {
26    Preconditions.checkArgument(null != host && !host.isEmpty(),
27        "Must specify a valid hostname");
28    Preconditions.checkArgument(port > 0,
29        "Must specify a valid (non-zero) port");
30    this.host = host;
31    this.port = port;
32    return this;
33  }
34
35  @Override
36  public AuthBuilder timeout(long amount, TimeUnit unit) {
37    this.timeout = unit.convert(amount, TimeUnit.MILLISECONDS);
38    return this;
39  }
40
41  @Override
42  public AuthBuilder executors(ExecutorService bossPool, ExecutorService workerPool) {
43    Preconditions.checkArgument(bossPool != null, "Boss executor cannot be null!");
44    Preconditions.checkArgument(workerPool != null, "Worker executor cannot be null!");
45    this.bossPool = bossPool;
46    this.workerPool = workerPool;
47    return this;
48  }
49
50  @Override
51  public MailClient prepare(Auth authType, String username, String password) {
52    Preconditions.checkArgument(authType != Auth.OAUTH, "Pleause use prepareOAuth() instead.");
53    if (null == bossPool) {
54      bossPool = Executors.newCachedThreadPool();
55      workerPool = Executors.newCachedThreadPool();
56    }
57
58    MailClientConfig config = new MailClientConfig(host, port, authType, username, password,
59        timeout);
60
61    return new NettyImapClient(config, bossPool, workerPool);
62  }
63
64  @Override
65  public MailClient prepareOAuth(String username, OAuthConfig config) {
66    if (null == bossPool) {
67      bossPool = Executors.newCachedThreadPool();
68      workerPool = Executors.newCachedThreadPool();
69    }
70
71    return new NettyImapClient(new MailClientConfig(host, port, username, config, timeout),
72        bossPool, workerPool);
73  }
74
75  @Override
76  public MailClient prepareOAuth2(String username, OAuth2Config config) {
77    if (null == bossPool) {
78      bossPool = Executors.newCachedThreadPool();
79      workerPool = Executors.newCachedThreadPool();
80    }
81
82    return new NettyImapClient(new MailClientConfig(host, port, username, config, timeout),
83        bossPool, workerPool);
84  }
85}