statoolinfosweb/src/fr/devinsy/statoolinfos/HtmlizerContext.java

185 lines
4.7 KiB
Java
Raw Normal View History

/*
* 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;
/**
* 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;
/**
* 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());
File htmlizeDirectory = this.configuration.getHtmlizeDirectory();
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(this.configuration.getHtmlizeInputURL(), 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;
}
/**
* 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;
}
}