
mailbox: Extract constants from configuration keys
This refactoring makes finding where a configuration value is used eaiser.
@f217fa2e41c3947807b835ccdde8d4311aef7122
--- app/mailbox/MailboxService.java
+++ app/mailbox/MailboxService.java
... | ... | @@ -74,8 +74,14 @@ |
74 | 74 |
private Thread idleThread; |
75 | 75 |
private Cancellable pollingSchedule; |
76 | 76 |
private boolean isStopping = false; |
77 |
- private static String IMAP_USE_KEY = "imap.use"; |
|
78 |
- private static boolean IMAP_USE_DEFAULT = true; |
|
77 |
+ private final static String IMAP_USE_KEY = "imap.use"; |
|
78 |
+ private final static boolean IMAP_USE_DEFAULT = true; |
|
79 |
+ private final static String IMAP_FOLDER_KEY = "imap.folder"; |
|
80 |
+ private final static String IMAP_FOLDER_DEFAULT = "inbox"; |
|
81 |
+ private final static String IMAP_HOST_KEY = "imap.host"; |
|
82 |
+ private final static String IMAP_PASSWORD_KEY = "imap.password"; |
|
83 |
+ private final static String IMAP_SSL_KEY = "imap.ssl"; |
|
84 |
+ private final static String IMAP_USER_KEY = "imap.user"; |
|
79 | 85 |
|
80 | 86 |
/** |
81 | 87 |
* Among the given {@code keys}, returns the keys that does not exist in |
... | ... | @@ -86,26 +92,21 @@ |
86 | 92 |
* @return the keys which don't have a matched value |
87 | 93 |
*/ |
88 | 94 |
private static Set<String> getMissingKeys(Configuration config, String... keys) { |
89 |
- Set<String> notConfigured = new HashSet<>(); |
|
90 |
- Map<String, Object> configMap = config.asMap(); |
|
91 |
- for (String key : keys) { |
|
92 |
- if (!configMap.containsKey(key)) { |
|
93 |
- notConfigured.add(key); |
|
94 |
- } |
|
95 |
- } |
|
96 |
- return notConfigured; |
|
95 |
+ Set<String> requiredKeys = new HashSet<>(Arrays.asList(keys)); |
|
96 |
+ requiredKeys.removeAll(config.keys()); |
|
97 |
+ return requiredKeys; |
|
97 | 98 |
} |
98 | 99 |
|
99 | 100 |
/** |
100 |
- * Connect to the IMAP server configured by the given {@code imapConfig}. |
|
101 |
+ * Connects to the configured IMAP server. |
|
101 | 102 |
* |
102 |
- * @param imapConfig |
|
103 | 103 |
* @return the store to be connected |
104 | 104 |
* @throws MessagingException |
105 | 105 |
*/ |
106 |
- private IMAPStore connect(Configuration imapConfig) throws MessagingException { |
|
106 |
+ private IMAPStore connect() throws MessagingException { |
|
107 |
+ final Configuration config = Configuration.root(); |
|
107 | 108 |
Set<String> missingKeys = |
108 |
- getMissingKeys(imapConfig, "host", "user", "password"); |
|
109 |
+ getMissingKeys(config, IMAP_HOST_KEY, IMAP_USER_KEY, IMAP_PASSWORD_KEY); |
|
109 | 110 |
|
110 | 111 |
if (missingKeys.size() > 0) { |
111 | 112 |
throw new IllegalStateException( |
... | ... | @@ -114,14 +115,14 @@ |
114 | 115 |
} |
115 | 116 |
|
116 | 117 |
Properties props = new Properties(); |
117 |
- String s = imapConfig.getBoolean("ssl", false) ? "s" : ""; |
|
118 |
+ String s = config.getBoolean(IMAP_SSL_KEY, false) ? "s" : ""; |
|
118 | 119 |
props.setProperty("mail.store.protocol", "imap" + s); |
119 | 120 |
|
120 | 121 |
Session session = getDefaultInstance(props, null); |
121 | 122 |
store = (IMAPStore) session.getStore(); |
122 |
- store.connect(imapConfig.getString("host"), |
|
123 |
- imapConfig.getString("user"), |
|
124 |
- imapConfig.getString("password")); |
|
123 |
+ store.connect(config.getString(IMAP_HOST_KEY), |
|
124 |
+ config.getString(IMAP_USER_KEY), |
|
125 |
+ config.getString(IMAP_PASSWORD_KEY)); |
|
125 | 126 |
return store; |
126 | 127 |
|
127 | 128 |
} |
... | ... | @@ -152,19 +153,19 @@ |
152 | 153 |
* Start Mailbox Service. |
153 | 154 |
*/ |
154 | 155 |
public void start() { |
155 |
- final Configuration imapConfig = Configuration.root().getConfig("imap"); |
|
156 |
- |
|
157 |
- if (imapConfig == null) { |
|
156 |
+ if (Configuration.root().getString(IMAP_HOST_KEY) == null) { |
|
158 | 157 |
play.Logger.info("Mailbox Service doesn't start because IMAP server is not configured."); |
159 | 158 |
return; |
160 | 159 |
} |
161 | 160 |
|
162 |
- if (!Configuration.root().getBoolean(IMAP_USE_KEY, IMAP_USE_DEFAULT)) { |
|
161 |
+ Configuration config = Configuration.root(); |
|
162 |
+ |
|
163 |
+ if (!config.getBoolean(IMAP_USE_KEY, IMAP_USE_DEFAULT)) { |
|
163 | 164 |
return; |
164 | 165 |
} |
165 | 166 |
|
166 | 167 |
List<User> users = User.find.where() |
167 |
- .ilike("email", imapConfig.getString("user") + "+%").findList(); |
|
168 |
+ .ilike("email", config.getString(IMAP_USER_KEY) + "+%").findList(); |
|
168 | 169 |
|
169 | 170 |
if (users.size() == 1) { |
170 | 171 |
Logger.warn("There is a user whose email is danger: " + users); |
... | ... | @@ -174,9 +175,9 @@ |
174 | 175 |
} |
175 | 176 |
|
176 | 177 |
try { |
177 |
- store = connect(imapConfig); |
|
178 |
+ store = connect(); |
|
178 | 179 |
folder = (IMAPFolder) store.getFolder( |
179 |
- imapConfig.getString("folder", "inbox")); |
|
180 |
+ config.getString(IMAP_FOLDER_KEY, IMAP_FOLDER_DEFAULT)); |
|
180 | 181 |
folder.open(Folder.READ_ONLY); |
181 | 182 |
} catch (Exception e) { |
182 | 183 |
play.Logger.error("Failed to open IMAP folder", e); |
... | ... | @@ -216,14 +217,12 @@ |
216 | 217 |
* @throws MessagingException |
217 | 218 |
*/ |
218 | 219 |
private IMAPFolder reopenFolder() throws MessagingException { |
219 |
- final Configuration imapConfig = Configuration.root().getConfig("imap"); |
|
220 |
- |
|
221 | 220 |
if (store == null || !store.isConnected()) { |
222 |
- store = connect(imapConfig); |
|
221 |
+ store = connect(); |
|
223 | 222 |
} |
224 | 223 |
|
225 | 224 |
IMAPFolder folder = (IMAPFolder) store.getFolder( |
226 |
- imapConfig.getString("folder", "inbox")); |
|
225 |
+ Configuration.root().getString(IMAP_FOLDER_KEY, IMAP_FOLDER_DEFAULT)); |
|
227 | 226 |
folder.open(Folder.READ_ONLY); |
228 | 227 |
|
229 | 228 |
return folder; |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?