BrightSide Workbench Full Report + Source Code
MailProviders.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.mail.provider;
20 
21 import java.io.IOException;
22 import org.turro.elephant.context.Application;
23 import org.turro.elephant.context.ElephantContext;
24 import org.turro.elephant.context.IConstructor;
25 import org.turro.file.Document;
26 import org.turro.log.WebLoggers;
27 import org.turro.mail.pool.MailMessagePool;
28 
33 public class MailProviders {
34 
35  public static final String GENERIC = "Generic";
36 
39  }
40 
41  public MailMessagePool getGeneric(IConstructor constructor) {
42  return getPool(constructor, GENERIC);
43  }
44 
45  public MailMessagePool getPool(IConstructor constructor, String name) {
46  if(providers != null) {
47  MailProvider provider = providers.get(name);
48  if(provider != null) {
49  return MailMessagePool.of(constructor, provider);
50  }
51  }
52  return null;
53  }
54 
55  public MailProvider getProvider(String name) {
56  if(providers != null) {
57  return providers.get(name);
58  }
59  return null;
60  }
61 
62  /* Factory */
63 
64  public static MailProviders instance() {
65  return new MailProviders();
66  }
67 
68  private static final String PROVIDERS_FILE = "/WEB-INF/elephant/conf/mail/providers.json";
69 
70  private final MailProviderSet providers;
71 
72  private MailProviders() {
73  providers = loadProviders();
74  }
75 
76  private MailProviderSet loadProviders() {
77  Document providersFile = Document.from(ElephantContext.getRealPath(PROVIDERS_FILE));
78  if(providersFile.exists()) {
79  try {
80  return MailProviderSet.from(providersFile.content());
81  } catch (IOException ex) {
82  WebLoggers.severe(this).exception(ex).log();
83  }
84  } else {
85  MailProviderSet mailProviderSet = MailProviderMigration.migrate();
86  try {
87  providersFile.content(mailProviderSet.toJson());
88  return mailProviderSet;
89  } catch (IOException ex) {
90  WebLoggers.severe(this).exception(ex).log();
91  }
92  }
93  return null;
94  }
95 
96 }
MailProvider getProvider(String name)
MailMessagePool getPool(IConstructor constructor, String name)
MailMessagePool getGeneric(IConstructor constructor)