Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Merge branch 'feature/mailbox' into 'next'
Browse files Browse the repository at this point in the history
from pull-request 1531

* refs/heads/feature/mailbox:
  mailbox: Extract constants from configuration keys
  Revert "mailbox: Extract constants from configuration keys"

Reviewed-by: 채수원 <sw.chae@navercorp.com>
  • Loading branch information
doortts committed Mar 12, 2015
2 parents eb058c2 + f217fa2 commit 6339c73
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
38 changes: 22 additions & 16 deletions app/mailbox/MailboxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import play.Logger;
import play.libs.Akka;
import scala.concurrent.duration.Duration;
import utils.Config;
import utils.Diagnostic;
import utils.SimpleDiagnostic;

Expand Down Expand Up @@ -75,13 +74,14 @@ public class MailboxService {
private Thread idleThread;
private Cancellable pollingSchedule;
private boolean isStopping = false;

private final Config.Key<Boolean> USE = new Config.Key<>("imap.use", true);
private final Config.Key<String> FOLDER = new Config.Key<>("imap.folder", "inbox");
private final Config.Key<String> HOST = new Config.Key<>("imap.host");
private final Config.Key<String> PASSWORD = new Config.Key<>("imap.password");
private final Config.Key<Boolean> SSL = new Config.Key<>("imap.ssl", false);
private final Config.Key<String> USER = new Config.Key<>("imap.user");
private final static String IMAP_USE_KEY = "imap.use";
private final static boolean IMAP_USE_DEFAULT = true;
private final static String IMAP_FOLDER_KEY = "imap.folder";
private final static String IMAP_FOLDER_DEFAULT = "inbox";
private final static String IMAP_HOST_KEY = "imap.host";
private final static String IMAP_PASSWORD_KEY = "imap.password";
private final static String IMAP_SSL_KEY = "imap.ssl";
private final static String IMAP_USER_KEY = "imap.user";

/**
* Among the given {@code keys}, returns the keys that does not exist in
Expand All @@ -106,7 +106,7 @@ private static Set<String> getMissingKeys(Configuration config, String... keys)
private IMAPStore connect() throws MessagingException {
final Configuration config = Configuration.root();
Set<String> missingKeys =
getMissingKeys(config, HOST.getName(), USER.getName(), PASSWORD.getName());
getMissingKeys(config, IMAP_HOST_KEY, IMAP_USER_KEY, IMAP_PASSWORD_KEY);

if (missingKeys.size() > 0) {
throw new IllegalStateException(
Expand All @@ -115,12 +115,14 @@ private IMAPStore connect() throws MessagingException {
}

Properties props = new Properties();
String s = Config.get(SSL) ? "s" : "";
String s = config.getBoolean(IMAP_SSL_KEY, false) ? "s" : "";
props.setProperty("mail.store.protocol", "imap" + s);

Session session = getDefaultInstance(props, null);
store = (IMAPStore) session.getStore();
store.connect(Config.get(HOST), Config.get(USER), Config.get(PASSWORD));
store.connect(config.getString(IMAP_HOST_KEY),
config.getString(IMAP_USER_KEY),
config.getString(IMAP_PASSWORD_KEY));
return store;

}
Expand Down Expand Up @@ -151,17 +153,19 @@ public void stop() {
* Start Mailbox Service.
*/
public void start() {
if (Config.get(HOST) == null) {
if (Configuration.root().getString(IMAP_HOST_KEY) == null) {
play.Logger.info("Mailbox Service doesn't start because IMAP server is not configured.");
return;
}

if (!Config.get(USE)) {
Configuration config = Configuration.root();

if (!config.getBoolean(IMAP_USE_KEY, IMAP_USE_DEFAULT)) {
return;
}

List<User> users = User.find.where()
.ilike("email", Config.get(USER) + "+%").findList();
.ilike("email", config.getString(IMAP_USER_KEY) + "+%").findList();

if (users.size() == 1) {
Logger.warn("There is a user whose email is danger: " + users);
Expand All @@ -172,7 +176,8 @@ public void start() {

try {
store = connect();
folder = (IMAPFolder) store.getFolder(Config.get(FOLDER));
folder = (IMAPFolder) store.getFolder(
config.getString(IMAP_FOLDER_KEY, IMAP_FOLDER_DEFAULT));
folder.open(Folder.READ_ONLY);
} catch (Exception e) {
play.Logger.error("Failed to open IMAP folder", e);
Expand Down Expand Up @@ -216,7 +221,8 @@ private IMAPFolder reopenFolder() throws MessagingException {
store = connect();
}

IMAPFolder folder = (IMAPFolder) store.getFolder(Config.get(FOLDER));
IMAPFolder folder = (IMAPFolder) store.getFolder(
Configuration.root().getString(IMAP_FOLDER_KEY, IMAP_FOLDER_DEFAULT));
folder.open(Folder.READ_ONLY);

return folder;
Expand Down
27 changes: 0 additions & 27 deletions app/utils/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,33 +284,6 @@ public static Charset getCharset() {
return Charset.forName("UTF-8");
}

public static class Key<T> {
private final String name;
private final T defaultValue;

public Key(String name, T defaultValue) {
this.name = name;
this.defaultValue = defaultValue;
}

public Key(String name) {
this(name, null);
}

public String getName() {
return name;
}

public T getDefault() {
return defaultValue;
}
}

@SuppressWarnings("unchecked")
public static <T> T get(Key<T> key) {
return (T) Configuration.root().getObject(key.getName(), key.getDefault());
}

public static String getYobiHome() {
return System.getProperty("yobi.home");
}
Expand Down

0 comments on commit 6339c73

Please sign in to comment.