1
0
Fork 0

ConfigFileLoader RE-DESIGN: Split into two parts, common settings and specific project ones, by inheritance and generics for the project's settings Enum

This commit is contained in:
Xavier Fontanet 2022-10-12 11:39:31 +02:00
parent 954c02141e
commit bec734b64c
8 changed files with 103 additions and 186 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/.idea/ /.idea/
/target/ /target/
.DS_Store

View File

@ -0,0 +1,65 @@
package tv.mangrana.config;
import tv.mangrana.exception.IncorrectWorkingReferencesException;
import tv.mangrana.utils.yml.FakeYmlLoader;
import java.io.File;
import java.util.EnumMap;
public abstract class CommonConfigFileLoader<P extends Enum<P>> {
private static final String CONFIG_FOLDER = "/config";
public enum CommonProjectConfiguration {
RADARR_API_KEY,
RADARR_API_HOST,
SONARR_API_KEY,
SONARR_API_HOST,
PLEX_TOKEN,
PLEX_HOST,
PLEX_SECTIONS_LIST_URI,
PLEX_SECTION_REFRESH_URI,
SONARR_PATHS_STARTER,
PLEX_PATHS_STARTER
}
private EnumMap<CommonProjectConfiguration, String> commonConfigurationsMap;
private final Class<P> projectConfigEnumType;
private EnumMap<P, String> configurationsMap;
public CommonConfigFileLoader(Class<P> projectConfigEnumType) throws IncorrectWorkingReferencesException {
this.projectConfigEnumType = projectConfigEnumType;
loadAllConfigFromFile(false);
}
public CommonConfigFileLoader<P> refresh() throws IncorrectWorkingReferencesException {
return loadAllConfigFromFile(true);
}
private CommonConfigFileLoader<P> loadAllConfigFromFile(boolean silently) throws IncorrectWorkingReferencesException {
commonConfigurationsMap = new EnumMap<>(CommonProjectConfiguration.class);
commonConfigurationsMap.putAll( loadFromFile(CommonProjectConfiguration.class, silently) );
configurationsMap = new EnumMap<>(projectConfigEnumType);
configurationsMap.putAll( loadFromFile(projectConfigEnumType, silently) );
return this;
}
private <E extends Enum<E>> EnumMap<E, String> loadFromFile(Class<E> enumType, boolean silently) throws IncorrectWorkingReferencesException {
File configFile = new File(System.getProperty("user.dir")
+ CONFIG_FOLDER.concat("/").concat(getConfigFileName()));
return FakeYmlLoader.getEnumMapFromFile(configFile, enumType, silently);
}
public String getConfig(P key) {
return configurationsMap.get(key);
}
public String getCommonConfig(CommonProjectConfiguration key) {
return commonConfigurationsMap.get(key);
}
protected abstract String getConfigFileName();
}

View File

@ -1,65 +0,0 @@
package tv.mangrana.config;
import tv.mangrana.exception.IncorrectWorkingReferencesException;
import tv.mangrana.utils.yml.FakeYmlLoader;
import java.io.File;
import java.util.EnumMap;
public class ConfigFileLoader {
private static final String CONFIG_FOLDER = "/config";
private static final String CONFIG_FILE = "AfterDownloadCarerConfig.yml";
public enum ProjectConfiguration {
MANAGE_FAILED_DOWNLOADS,
IMMORTAL_PROCESS,
GRABBED_FILE_IDENTIFIER_REGEX,
RADARR_API_KEY,
RADARR_API_HOST,
SONARR_API_KEY,
SONARR_API_HOST,
DOWNLOADS_TEAM_DRIVE_ID,
DOWNLOADS_SERIES_FOLDER_ID,
MOVIES_TEAM_DRIVE_ID,
SERIES_TEAM_DRIVE_ID,
PLEX_TOKEN,
PLEX_HOST,
PLEX_SECTIONS_LIST_URI,
PLEX_SECTION_REFRESH_URI,
GOOGLE_RETRY_INTERVAL,
SONARR_RETRY_INTERVAL,
RADARR_RETRY_INTERVAL,
SONARR_PATHS_STARTER,
PLEX_PATHS_STARTER
}
private EnumMap<ProjectConfiguration, String> configurationsMap;
public ConfigFileLoader() throws IncorrectWorkingReferencesException {
loadFromFile(false);
}
private ConfigFileLoader(boolean silently) throws IncorrectWorkingReferencesException {
loadFromFile(silently);
}
public ConfigFileLoader refresh() throws IncorrectWorkingReferencesException {
return loadFromFile(true);
}
public static ConfigFileLoader getFreshConfig() throws IncorrectWorkingReferencesException {
return new ConfigFileLoader(true);
}
private ConfigFileLoader loadFromFile(boolean silently) throws IncorrectWorkingReferencesException {
File configFile = new File(System.getProperty("user.dir")
+ CONFIG_FOLDER.concat("/").concat(CONFIG_FILE));
configurationsMap = FakeYmlLoader.getEnumMapFromFile(configFile, ProjectConfiguration.class, silently);
return this;
}
public String getConfig(ProjectConfiguration key) {
return configurationsMap.get(key);
}
}

View File

@ -1,8 +1,5 @@
package tv.mangrana.plex.url; package tv.mangrana.plex.url;
import tv.mangrana.config.ConfigFileLoader;
import tv.mangrana.exception.IncorrectWorkingReferencesException;
import tv.mangrana.utils.EasyLogger;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.HttpUriRequest;
@ -11,6 +8,8 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import tv.mangrana.config.CommonConfigFileLoader;
import tv.mangrana.utils.EasyLogger;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
@ -22,26 +21,26 @@ import java.net.URISyntaxException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static tv.mangrana.config.ConfigFileLoader.ProjectConfiguration.*; import static tv.mangrana.config.CommonConfigFileLoader.CommonProjectConfiguration.*;
import static tv.mangrana.utils.Output.log; import static tv.mangrana.utils.Output.log;
import static tv.mangrana.utils.rest.APIInterface.ProtocolURLMark.HTTPS; import static tv.mangrana.utils.rest.APIInterface.ProtocolURLMark.HTTPS;
public class PlexCommandLauncher { public class PlexCommandLauncher {
private final EasyLogger logger; private final EasyLogger logger;
private final ConfigFileLoader config; private final CommonConfigFileLoader<?> config;
private final PlexLibrarySectionsResolver sectionResolver; private final PlexLibrarySectionsResolver sectionResolver;
public PlexCommandLauncher(ConfigFileLoader config) { public PlexCommandLauncher(CommonConfigFileLoader<?> config) {
this.config = config; this.config = config;
this.logger = new EasyLogger(); this.logger = new EasyLogger();
this.sectionResolver = new PlexLibrarySectionsResolver(this, config); this.sectionResolver = new PlexLibrarySectionsResolver(this, config);
} }
public static void main(String[] args) throws IncorrectWorkingReferencesException { // public static void main(String[] args) throws IncorrectWorkingReferencesException {
String toRefresh="/tv/Series/C/City on a Hill (2019)"; // String toRefresh="/tv/Series/C/City on a Hill (2019)";
new PlexCommandLauncher(new ConfigFileLoader()).scanByPath(toRefresh); // new PlexCommandLauncher(new CommonConfigFileLoader()).scanByPath(toRefresh);
} // }
public void scanByPath(String fullDestinationPath) { public void scanByPath(String fullDestinationPath) {
String plexPathToRefresh = getPlexUrlPath2Refresh(fullDestinationPath); String plexPathToRefresh = getPlexUrlPath2Refresh(fullDestinationPath);
@ -51,11 +50,11 @@ public class PlexCommandLauncher {
HttpUriRequest httpGET = RequestBuilder.get() HttpUriRequest httpGET = RequestBuilder.get()
.setUri(new URI(plexRefreshURL)) .setUri(new URI(plexRefreshURL))
.addParameter("path", plexPathToRefresh) .addParameter("path", plexPathToRefresh)
.addParameter("X-Plex-Token", config.getConfig(PLEX_TOKEN)) .addParameter("X-Plex-Token", config.getCommonConfig(PLEX_TOKEN))
.build(); .build();
httpclient.execute(httpGET); httpclient.execute(httpGET);
@SuppressWarnings("unused") @SuppressWarnings("unused")
String urlWithTokenHidden = httpGET.getURI().toString().replaceFirst(config.getConfig(PLEX_TOKEN), "__plex_token__"); String urlWithTokenHidden = httpGET.getURI().toString().replaceFirst(config.getCommonConfig(PLEX_TOKEN), "__plex_token__");
logger.nLog("Launched URL command: {0}", httpGET.getURI().toString()); logger.nLog("Launched URL command: {0}", httpGET.getURI().toString());
} catch (Exception e) { } catch (Exception e) {
logger.nHLog("Some error has happened using the URL <{0}>", plexRefreshURL); logger.nHLog("Some error has happened using the URL <{0}>", plexRefreshURL);
@ -64,10 +63,10 @@ public class PlexCommandLauncher {
} }
public String getPlexUrlPath2Refresh(String fullDestinationPath) { public String getPlexUrlPath2Refresh(String fullDestinationPath) {
Pattern p = Pattern.compile(config.getConfig(SONARR_PATHS_STARTER).concat("(.+/.+ \\(\\d{4}\\))")); Pattern p = Pattern.compile(config.getCommonConfig(SONARR_PATHS_STARTER).concat("(.+/.+ \\(\\d{4}\\))"));
Matcher m = p.matcher(fullDestinationPath); Matcher m = p.matcher(fullDestinationPath);
if (m.find()) { if (m.find()) {
String pathInPlexDockerStart = config.getConfig(PLEX_PATHS_STARTER); String pathInPlexDockerStart = config.getCommonConfig(PLEX_PATHS_STARTER);
return pathInPlexDockerStart.concat(m.group(1)); return pathInPlexDockerStart.concat(m.group(1));
} }
return null; return null;
@ -77,7 +76,7 @@ public class PlexCommandLauncher {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) { try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpUriRequest httpGET = RequestBuilder.get() HttpUriRequest httpGET = RequestBuilder.get()
.setUri(new URI(getPlexSectionsURL())) .setUri(new URI(getPlexSectionsURL()))
.addParameter("X-Plex-Token", config.getConfig(PLEX_TOKEN)) .addParameter("X-Plex-Token", config.getCommonConfig(PLEX_TOKEN))
.build(); .build();
try (CloseableHttpResponse httpResponse = httpclient.execute(httpGET)) { try (CloseableHttpResponse httpResponse = httpclient.execute(httpGET)) {
final HttpEntity entity = httpResponse.getEntity(); final HttpEntity entity = httpResponse.getEntity();
@ -91,7 +90,7 @@ public class PlexCommandLauncher {
} }
} }
} }
log("launched url command: "+httpGET.getURI().toString().replaceFirst(config.getConfig(PLEX_TOKEN), "__plex_token__")); log("launched url command: "+httpGET.getURI().toString().replaceFirst(config.getCommonConfig(PLEX_TOKEN), "__plex_token__"));
} catch (URISyntaxException | IOException e) { } catch (URISyntaxException | IOException e) {
log("could not refresh plex artist because of "+e.getMessage()); log("could not refresh plex artist because of "+e.getMessage());
e.printStackTrace(); e.printStackTrace();
@ -102,15 +101,15 @@ public class PlexCommandLauncher {
private String getPlexRefreshURL(String fullDestinationPath) { private String getPlexRefreshURL(String fullDestinationPath) {
String sectionId = sectionResolver.resolveSectionByPath(fullDestinationPath); String sectionId = sectionResolver.resolveSectionByPath(fullDestinationPath);
if (sectionId==null) return null; if (sectionId==null) return null;
String host = config.getConfig(PLEX_HOST); String host = config.getCommonConfig(PLEX_HOST);
String uriFormat = config.getConfig(PLEX_SECTION_REFRESH_URI); String uriFormat = config.getCommonConfig(PLEX_SECTION_REFRESH_URI);
String uri = uriFormat.replaceFirst("\\{section_id}", sectionId); String uri = uriFormat.replaceFirst("\\{section_id}", sectionId);
return HTTPS.getMark() + host + uri; return HTTPS.getMark() + host + uri;
} }
private String getPlexSectionsURL() { private String getPlexSectionsURL() {
String host = HTTPS.getMark() + config.getConfig(PLEX_HOST); String host = HTTPS.getMark() + config.getCommonConfig(PLEX_HOST);
String uri = config.getConfig(PLEX_SECTIONS_LIST_URI); String uri = config.getCommonConfig(PLEX_SECTIONS_LIST_URI);
return host + uri; return host + uri;
} }

View File

@ -1,7 +1,7 @@
package tv.mangrana.plex.url; package tv.mangrana.plex.url;
import tv.mangrana.config.ConfigFileLoader; import tv.mangrana.config.CommonConfigFileLoader;
import tv.mangrana.utils.Output; import tv.mangrana.utils.Output;
import com.sun.org.apache.xerces.internal.dom.DeferredElementImpl; import com.sun.org.apache.xerces.internal.dom.DeferredElementImpl;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@ -13,20 +13,20 @@ import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathFactory;
import static tv.mangrana.config.ConfigFileLoader.ProjectConfiguration.PLEX_PATHS_STARTER; import static tv.mangrana.config.CommonConfigFileLoader.CommonProjectConfiguration.PLEX_PATHS_STARTER;
public class PlexLibrarySectionsResolver { public class PlexLibrarySectionsResolver {
private final PlexCommandLauncher commandLauncher; private final PlexCommandLauncher commandLauncher;
private final ConfigFileLoader config; private final CommonConfigFileLoader<?> config;
public PlexLibrarySectionsResolver(PlexCommandLauncher commandLauncher, ConfigFileLoader config) { public PlexLibrarySectionsResolver(PlexCommandLauncher commandLauncher, CommonConfigFileLoader<?> config) {
this.commandLauncher = commandLauncher; this.commandLauncher = commandLauncher;
this.config = config; this.config = config;
} }
public String resolveSectionByPath(String fullDestinationPath) { public String resolveSectionByPath(String fullDestinationPath) {
final String plexPathStarter = config.getConfig(PLEX_PATHS_STARTER); final String plexPathStarter = config.getCommonConfig(PLEX_PATHS_STARTER);
String keyFolder = fullDestinationPath.replaceFirst(plexPathStarter,"").split("/")[1]; String keyFolder = fullDestinationPath.replaceFirst(plexPathStarter,"").split("/")[1];
Document xmlDocument = commandLauncher.retrieveSectionsInfo(); Document xmlDocument = commandLauncher.retrieveSectionsInfo();
XPath xPath = XPathFactory.newInstance().newXPath(); XPath xPath = XPathFactory.newInstance().newXPath();

View File

@ -1,13 +1,13 @@
package tv.mangrana.radarr.api.client.gateway; package tv.mangrana.radarr.api.client.gateway;
import tv.mangrana.config.ConfigFileLoader; import tv.mangrana.config.CommonConfigFileLoader;
import tv.mangrana.radarr.api.schema.command.RefreshMoviesCommand; import tv.mangrana.radarr.api.schema.command.RefreshMoviesCommand;
import tv.mangrana.radarr.api.schema.movie.MovieResource; import tv.mangrana.radarr.api.schema.movie.MovieResource;
import tv.mangrana.radarr.api.schema.queue.QueueResourcePagingResource; import tv.mangrana.radarr.api.schema.queue.QueueResourcePagingResource;
import tv.mangrana.utils.rest.APIProxyBuilderSingleton; import tv.mangrana.utils.rest.APIProxyBuilderSingleton;
import static tv.mangrana.config.ConfigFileLoader.ProjectConfiguration.RADARR_API_HOST; import static tv.mangrana.config.CommonConfigFileLoader.CommonProjectConfiguration.RADARR_API_HOST;
import static tv.mangrana.config.ConfigFileLoader.ProjectConfiguration.RADARR_API_KEY; import static tv.mangrana.config.CommonConfigFileLoader.CommonProjectConfiguration.RADARR_API_KEY;
import static tv.mangrana.utils.Output.log; import static tv.mangrana.utils.Output.log;
public class RadarrApiGateway { public class RadarrApiGateway {
@ -15,9 +15,9 @@ public class RadarrApiGateway {
private final String apiKey; private final String apiKey;
private final RadarrAPIInterface proxy; private final RadarrAPIInterface proxy;
public RadarrApiGateway(ConfigFileLoader config) { public RadarrApiGateway(CommonConfigFileLoader<?> config) {
apiKey = config.getConfig(RADARR_API_KEY); apiKey = config.getCommonConfig(RADARR_API_KEY);
proxy = APIProxyBuilderSingleton.getRadarrInterface(config.getConfig(RADARR_API_HOST)); proxy = APIProxyBuilderSingleton.getRadarrInterface(config.getCommonConfig(RADARR_API_HOST));
} }
public QueueResourcePagingResource getQueue() { public QueueResourcePagingResource getQueue() {

View File

@ -1,6 +1,6 @@
package tv.mangrana.sonarr.api.client.gateway; package tv.mangrana.sonarr.api.client.gateway;
import tv.mangrana.config.ConfigFileLoader; import tv.mangrana.config.CommonConfigFileLoader;
import tv.mangrana.sonarr.api.schema.command.RefreshSerieCommand; import tv.mangrana.sonarr.api.schema.command.RefreshSerieCommand;
import tv.mangrana.sonarr.api.schema.history.SonarrHistory; import tv.mangrana.sonarr.api.schema.history.SonarrHistory;
import tv.mangrana.sonarr.api.schema.queue.SonarrQueue; import tv.mangrana.sonarr.api.schema.queue.SonarrQueue;
@ -9,8 +9,8 @@ import tv.mangrana.utils.EasyLogger;
import tv.mangrana.utils.Output; import tv.mangrana.utils.Output;
import tv.mangrana.utils.rest.APIProxyBuilderSingleton; import tv.mangrana.utils.rest.APIProxyBuilderSingleton;
import static tv.mangrana.config.ConfigFileLoader.ProjectConfiguration.SONARR_API_HOST; import static tv.mangrana.config.CommonConfigFileLoader.CommonProjectConfiguration.SONARR_API_HOST;
import static tv.mangrana.config.ConfigFileLoader.ProjectConfiguration.SONARR_API_KEY; import static tv.mangrana.config.CommonConfigFileLoader.CommonProjectConfiguration.SONARR_API_KEY;
public class SonarrApiGateway { public class SonarrApiGateway {
@ -18,9 +18,9 @@ public class SonarrApiGateway {
private final SonarrAPIInterface proxy; private final SonarrAPIInterface proxy;
private final EasyLogger logger; private final EasyLogger logger;
public SonarrApiGateway(ConfigFileLoader config) { public SonarrApiGateway(CommonConfigFileLoader<?> config) {
apiKey = config.getConfig(SONARR_API_KEY); apiKey = config.getCommonConfig(SONARR_API_KEY);
proxy = APIProxyBuilderSingleton.getSonarrInterface(config.getConfig(SONARR_API_HOST)); proxy = APIProxyBuilderSingleton.getSonarrInterface(config.getCommonConfig(SONARR_API_HOST));
logger = new EasyLogger(); logger = new EasyLogger();
} }

View File

@ -1,83 +0,0 @@
package tv.mangrana.utils.schema;
import tv.mangrana.config.ConfigFileLoader;
import tv.mangrana.exception.IncorrectWorkingReferencesException;
import java.io.IOException;
import static tv.mangrana.config.ConfigFileLoader.ProjectConfiguration.*;
import static tv.mangrana.utils.rest.APIInterface.ProtocolURLMark.HTTPS;
/**
* @deprecated Once the generated classes for client schema are located in the project, this utility is not needed anymore
*/
@Deprecated
public class ClientSchemaGenerator {
ConfigFileLoader configFileLoader;
private ClientSchemaGenerator() throws IncorrectWorkingReferencesException {
configFileLoader = new ConfigFileLoader();
}
public static void main(String[] args) throws IncorrectWorkingReferencesException, IOException {
new ClientSchemaGenerator().generateRadarrMovieClientSchema();
}
@SuppressWarnings("unused")
private void generateRadarrQueueClientSchema() throws IOException {
generate(
configFileLoader.getConfig(RADARR_API_HOST),
"/api/v3/queue?includeMovie=true?apikey=",
configFileLoader.getConfig(RADARR_API_KEY),
"tv.mangrana.radarr.api.schema.queue",
"QueueResourcePagingResource");
}
@SuppressWarnings("unused")
private void generateRadarrMovieClientSchema() throws IOException {
generate(
configFileLoader.getConfig(RADARR_API_HOST),
"/api/v3/movie/9216?apikey=",
configFileLoader.getConfig(RADARR_API_KEY),
"tv.mangrana.radarr.api.schema.movie",
"MovieResource");
}
@SuppressWarnings("unused")
private void generateSonarrQueueClientSchema() throws IOException {
generate(
configFileLoader.getConfig(SONARR_API_HOST),
"/api/v3/queue?apikey=",
configFileLoader.getConfig(SONARR_API_KEY),
"tv.mangrana.sonarr.api.schema.queue",
"SonarrQueue");
}
@SuppressWarnings("unused")
private void generateSonarrSeriesClientSchema() throws IOException {
generate(
configFileLoader.getConfig(SONARR_API_HOST),
"/api/v3/series/2220?apikey=",
configFileLoader.getConfig(SONARR_API_KEY),
"tv.mangrana.sonarr.api.schema.series",
"SonarrSeries");
}
@SuppressWarnings("unused")
private void generateSonarrHistoryClientSchema() throws IOException {
generate(
configFileLoader.getConfig(SONARR_API_HOST),
"/api/v3/history?sortKey=date&apikey=",
configFileLoader.getConfig(SONARR_API_KEY),
"tv.mangrana.sonarr.api.schema.history",
"SonarrHistory");
}
private void generate(String host, String uri, String apiKey, String pckg, String className) throws IOException {
String schemaUrl = HTTPS.getMark()+host.concat(uri.concat(apiKey));
ClassGeneratorFromJson generatorFromJson = new ClassGeneratorFromJson();
generatorFromJson.generateSchema(schemaUrl, pckg, className);
}
}