/* * Copyright (C) 2020 Christian Pierre MOMON * * This file is part of StatoolInfos, simple service statistics tool. * * StatoolInfos is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * StatoolInfos is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with StatoolInfos. If not, see . */ package fr.devinsy.statoolinfos; import java.io.File; import java.io.IOException; import fr.devinsy.statoolinfos.core.Categories; import fr.devinsy.statoolinfos.core.Configuration; import fr.devinsy.statoolinfos.core.Factory; import fr.devinsy.statoolinfos.core.Federation; import fr.devinsy.statoolinfos.core.StatoolInfosException; import fr.devinsy.statoolinfos.crawl.CrawlCache; import fr.devinsy.statoolinfos.crawl.CrawlJournal; import fr.devinsy.statoolinfos.crawl.CrawlJournalFile; import fr.devinsy.statoolinfos.crawl.Crawler; /** * The Class Manager. */ public class HtmlizerContext { private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(HtmlizerContext.class); private static class SingletonHolder { private static final HtmlizerContext instance = new HtmlizerContext(); } private Configuration configuration; private Federation federation; private Categories categories; private CrawlCache cache; private CrawlJournal crawlJournal; /** * Instantiates a new manager. */ private HtmlizerContext() { } /** * Configure. * * @param configurationFile * the configuration file * @throws IOException * @throws StatoolInfosException */ public void configure(final File configurationFile) throws StatoolInfosException, IOException { this.configuration = Factory.loadConfiguration(configurationFile); logger.info("Cache setting: {}", this.configuration.getCrawlCachePath()); logger.info("Htmlize input setting: {}", this.configuration.getHtmlizeInputURL()); logger.info("Htmlize directory setting: {}", this.configuration.getHtmlizeDirectoryPath()); this.cache = new CrawlCache(this.configuration.getCrawlCacheDirectory()); this.crawlJournal = CrawlJournalFile.load(this.cache.restoreFile(Crawler.getJournalURL())); File htmlizeInputFile = this.cache.restoreFile(this.configuration.getHtmlizeInputURL()); File htmlizeDirectory = this.configuration.getHtmlizeDirectory(); if (htmlizeInputFile == null) { throw new IllegalArgumentException("Htmlize input undefined."); } else if (!htmlizeInputFile.exists()) { throw new IllegalArgumentException("Htmlize input is missing."); } else if (htmlizeInputFile.isDirectory()) { throw new IllegalArgumentException("Htmlize input is a directory."); } else if (htmlizeDirectory == null) { throw new IllegalArgumentException("Htmlize directory undefined."); } else if (!htmlizeDirectory.exists()) { throw new IllegalArgumentException("Htmlize directory is missing."); } else if (!htmlizeDirectory.isDirectory()) { throw new IllegalArgumentException("Htmlize directory is not a directory."); } else { if (this.configuration.isFederation()) { this.federation = Factory.loadFederation(htmlizeInputFile, this.cache); this.categories = Factory.loadCategories(this.configuration.getCategoryFile(), this.federation); } else { throw new IllegalArgumentException("Not a federation configuration."); } } } /** * Gets the cache. * * @return the cache */ public CrawlCache getCache() { CrawlCache result; result = this.cache; // return result; } /** * Gets the categories. * * @return the categories */ public Categories getCategories() { Categories result; result = this.categories; // return result; } /** * Gets the configuration. * * @return the configuration */ public Configuration getConfiguration() { Configuration result; result = this.configuration; // return result; } public CrawlJournal getCrawlJournal() { return this.crawlJournal; } /** * Gets the federation. * * @return the federation */ public Federation getFederation() { Federation result; result = this.federation; // return result; } /** * Htmlize directory. * * @return the file */ public File getHtmlizeDirectory() { File result; result = this.configuration.getHtmlizeDirectory(); // return result; } /** * Instance. * * @return the manager */ public static HtmlizerContext instance() { return SingletonHolder.instance; } }