2020-10-28 15:32:05 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Christian Pierre MOMON <christian@momon.org>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
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;
|
2021-05-17 13:24:10 +02:00
|
|
|
import fr.devinsy.statoolinfos.crawl.CrawlJournal;
|
|
|
|
import fr.devinsy.statoolinfos.crawl.CrawlJournalFile;
|
|
|
|
import fr.devinsy.statoolinfos.crawl.Crawler;
|
2020-10-28 15:32:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2021-05-17 13:24:10 +02:00
|
|
|
private CrawlJournal crawlJournal;
|
2020-10-28 15:32:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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());
|
2021-05-17 13:24:10 +02:00
|
|
|
logger.info("Htmlize input setting: {}", this.configuration.getHtmlizeInputURL());
|
2020-10-28 15:32:05 +01:00
|
|
|
logger.info("Htmlize directory setting: {}", this.configuration.getHtmlizeDirectoryPath());
|
|
|
|
|
2021-05-17 13:24:10 +02:00
|
|
|
this.cache = new CrawlCache(this.configuration.getCrawlCacheDirectory());
|
|
|
|
this.crawlJournal = CrawlJournalFile.load(this.cache.restoreFile(Crawler.getJournalURL()));
|
|
|
|
|
|
|
|
File htmlizeInputFile = this.cache.restoreFile(this.configuration.getHtmlizeInputURL());
|
2020-10-28 15:32:05 +01:00
|
|
|
File htmlizeDirectory = this.configuration.getHtmlizeDirectory();
|
2021-05-17 13:24:10 +02:00
|
|
|
if (htmlizeInputFile == null)
|
2020-10-28 15:32:05 +01:00
|
|
|
{
|
|
|
|
throw new IllegalArgumentException("Htmlize input undefined.");
|
|
|
|
}
|
2021-05-17 13:24:10 +02:00
|
|
|
else if (!htmlizeInputFile.exists())
|
2020-10-28 15:32:05 +01:00
|
|
|
{
|
|
|
|
throw new IllegalArgumentException("Htmlize input is missing.");
|
|
|
|
}
|
2021-05-17 13:24:10 +02:00
|
|
|
else if (htmlizeInputFile.isDirectory())
|
2020-10-28 15:32:05 +01:00
|
|
|
{
|
|
|
|
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())
|
|
|
|
{
|
2021-05-17 13:24:10 +02:00
|
|
|
this.federation = Factory.loadFederation(htmlizeInputFile, this.cache);
|
2020-10-28 15:32:05 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-17 13:24:10 +02:00
|
|
|
public CrawlJournal getCrawlJournal()
|
|
|
|
{
|
|
|
|
return this.crawlJournal;
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:32:05 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|