agirstatool/src/org/april/agirstatool/cli/AgirStatoolConfigFile.java

220 lines
4.3 KiB
Java

/*
* Copyright (C) 2020 Christian Pierre MOMON <christian.momon@devinsy.fr>
*
* This file is part of AgirStatool, simple key value database.
*
* AgirStatool 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.
*
* AgirStatool 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 AgirStatool. If not, see <http://www.gnu.org/licenses/>.
*/
package org.april.agirstatool.cli;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.april.agirstatool.core.AgirStatoolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class AgirStatoolConfigFile.
*/
public class AgirStatoolConfigFile extends Properties
{
private static final long serialVersionUID = -6816730915507062072L;
private static final Logger logger = LoggerFactory.getLogger(AgirStatoolConfigFile.class);
private static final String TIME_PATTERN = "^\\d\\dh\\d\\d$";
/**
* Instantiates a new agir statool config file.
*
* @param source
* the source
* @throws AgirStatoolException
* the agir statool exception
*/
public AgirStatoolConfigFile(final File configurationFile) throws AgirStatoolException
{
super();
try
{
if (configurationFile == null)
{
throw new AgirStatoolException("Configuration file undefined.");
}
else if (!configurationFile.exists())
{
throw new AgirStatoolException("Configuration file does not exist.");
}
else if (!configurationFile.isFile())
{
throw new AgirStatoolException("Configuration file is not a file.");
}
else
{
Properties config = loadProperties(configurationFile);
this.putAll(config);
}
}
catch (IOException exception)
{
throw new AgirStatoolException("IO error.", exception);
}
}
/**
* Gets the dabatase login.
*
* @return the dabatase login
*/
public String getDabataseLogin()
{
String result;
result = getProperty("database.login");
//
return result;
}
/**
* Gets the dabatase name.
*
* @return the dabatase name
*/
public String getDabataseName()
{
String result;
result = getProperty("database.name");
//
return result;
}
/**
* Gets the dabatase password.
*
* @return the dabatase password
*/
public String getDabatasePassword()
{
String result;
result = getProperty("database.password");
//
return result;
}
/**
* Gets the dabatase url.
*
* @return the dabatase url
*/
public String getDabataseUrl()
{
String result;
result = getProperty("database.url");
//
return result;
}
/**
* Gets the target directory.
*
* @return the target directory
*/
public String getTargetDirectory()
{
String result;
result = getProperty("targetDirectory");
//
return result;
}
/**
* Checks if is valid.
*
* @return true, if is valid
*/
public boolean isValid()
{
boolean result;
if (StringUtils.isBlank(getProperty("database.url")))
{
result = false;
}
else if (StringUtils.isBlank(getProperty("database.name")))
{
result = false;
}
else if (StringUtils.isBlank(getProperty("database.login")))
{
result = false;
}
else if (StringUtils.isBlank(getProperty("targetDirectory")))
{
result = false;
}
else
{
result = true;
}
//
return result;
}
/**
* Load properties.
*
* @param source
* the source
* @return the properties
* @throws IOException
* Signals that an I/O exception has occurred.
*/
private static Properties loadProperties(final File source) throws IOException
{
Properties result;
System.setProperty("file.encoding", "UTF-8");
result = new Properties();
FileReader reader = null;
try
{
reader = new FileReader(source);
result.load(reader);
}
finally
{
IOUtils.closeQuietly(reader);
}
//
return result;
}
}