/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

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