Java tutorial
/* * Copyright (C) 2017 GetMangos * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package eu.mangos.configuration.dao.impl; import eu.mangos.configuration.dao.AuthConfigurationDAO; import eu.mangos.configuration.enums.BanType; import eu.mangos.configuration.enums.LogColor; import eu.mangos.configuration.enums.LogLevel; import eu.mangos.configuration.enums.ProcessPriority; import eu.mangos.configuration.model.AuthConfiguration; import eu.mangos.utils.file.FileConfiguration; import java.io.File; import javax.inject.Inject; import org.apache.commons.configuration2.Configuration; import org.apache.commons.configuration2.FileBasedConfiguration; import org.apache.commons.configuration2.PropertiesConfiguration; import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; import org.apache.commons.configuration2.builder.fluent.Parameters; import org.apache.commons.configuration2.ex.ConfigurationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author GetMangos */ public class AuthConfigurationDAOImpl implements AuthConfigurationDAO { private static final Logger logger = LoggerFactory.getLogger(AuthConfigurationDAOImpl.class); private FileBasedConfigurationBuilder<FileBasedConfiguration> builder; private final FileConfiguration configuration; @Inject public AuthConfigurationDAOImpl(FileConfiguration configuration) { this.configuration = configuration; } @Override public AuthConfiguration readConfiguration() throws ConfigurationException { AuthConfiguration authConfig = new AuthConfiguration(); Parameters params = new Parameters(); this.builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class) .configure(params.properties().setFile(new File(this.configuration.getSourcePath()))); Configuration config = this.builder.getConfiguration(); authConfig.setVersion(config.getString("ConfVersion")); if (authConfig.getVersion() == null) { logger.error("The selected file does not contain any ConfVersion property."); throw new ConfigurationException("The selected file does not contain any ConfVersion property."); } if (Long.parseLong(authConfig.getVersion()) != AuthConfiguration.CONF_VERSION) { logger.error("The selected file has a version number not compatible with this manager."); throw new ConfigurationException("The selected file is not compatible with this manager."); } String[] loginDBInfo = (config.getString("LoginDatabaseInfo", "").replace("\"", "")).split(";"); if (loginDBInfo.length != 5) { logger.warn("The selected file has a LoginDatabaseInfo property with not enough parameters."); } authConfig.setIp(loginDBInfo.length >= 1 ? loginDBInfo[0] : ""); authConfig.setPort(loginDBInfo.length >= 2 ? Integer.parseInt(loginDBInfo[1]) : 0); authConfig.setUser(loginDBInfo.length >= 3 ? loginDBInfo[2] : ""); authConfig.setPassword(loginDBInfo.length >= 4 ? loginDBInfo[3] : ""); authConfig.setSchema(loginDBInfo.length >= 5 ? loginDBInfo[4] : ""); authConfig.setLogsDirectory(config.getString("LogsDir", "").replace("\"", "")); authConfig.setPidFile(config.getString("PidFile", "").replace("\"", "")); authConfig.setMaxPing(config.getInt("MaxPingTime", 30)); authConfig.setRealmServerPort(config.getInt("RealmServerPort", 3724)); authConfig.setBindIP(config.getString("BindIP", "0.0.0.0").replace("\"", "")); authConfig.setLogConsoleLevel(LogLevel.convert(config.getInt("LogLevel"))); authConfig.setIncludeLogTime((config.getInt("LogTime", 0) != 0)); authConfig.setLogFile(config.getString("LogFile", "realm-list.log").replace("\"", "")); authConfig.setIncludeTimestamp((config.getInt("LogTimestamp", 0) != 0)); authConfig.setLogFileLevel(LogLevel.convert(config.getInt("LogFileLevel"))); String[] logColors = (config.getString("LogColors", "")).replace("\"", "").split(" "); if (logColors.length != 4) { logger.warn("The selected file has a LogColors property with not enough parameters."); } authConfig.setLogNormalColor( logColors.length >= 1 ? LogColor.convert(Integer.parseInt(logColors[0])) : LogColor.NONE); authConfig.setLogDetailsColor( logColors.length >= 2 ? LogColor.convert(Integer.parseInt(logColors[1])) : LogColor.NONE); authConfig.setLogDebugColor( logColors.length >= 3 ? LogColor.convert(Integer.parseInt(logColors[2])) : LogColor.NONE); authConfig.setLogErrorColor( logColors.length >= 4 ? LogColor.convert(Integer.parseInt(logColors[3])) : LogColor.NONE); authConfig.setUseProcessors(config.getInt("UseProcessors", 0)); authConfig.setProcessPriority(ProcessPriority.convert(config.getInt("ProcessPriority", 1))); authConfig.setWaitAtStartupError(config.getInt("WaitAtStartupError", 0) != 0); authConfig.setRealmStateUpdateDelay(config.getInt("RealmsStateUpdateDelay", 20)); authConfig.setNbWrongPass(config.getInt("WrongPass.MaxCount", 3)); authConfig.setBanTime(config.getInt("WrongPass.BanTime", 300)); authConfig.setBanType(BanType.convert(config.getInt("WrongPass.BanType", 0))); logger.info("Configuration file " + this.configuration.getName() + " has been loaded succesfully !"); return authConfig; } @Override public void saveConfiguration(AuthConfiguration authConfig) throws ConfigurationException { Configuration config; try { config = this.builder.getConfiguration(); config.setProperty("[RealmdConf]", ""); ((PropertiesConfiguration) config).getLayout().setSeparator("[RealmdConf]", ""); config.setProperty("LoginDatabaseInfo", authConfig.getIp() + ";" + authConfig.getPort() + ";" + authConfig.getUser() + ";" + authConfig.getPassword() + ";" + authConfig.getSchema()); config.setProperty("LogsDir", authConfig.getLogsDirectory()); config.setProperty("PidFile", authConfig.getPidFile()); config.setProperty("MaxPingTime", "" + authConfig.getMaxPing()); config.setProperty("RealmServerPort", "" + authConfig.getRealmServerPort()); config.setProperty("BindIP", authConfig.getBindIP()); config.setProperty("LogLevel", "" + authConfig.getLogConsoleLevel().getCode()); config.setProperty("LogTime", authConfig.isIncludeLogTime() ? "1" : "0"); config.setProperty("LogFile", authConfig.getLogFile()); config.setProperty("LogTimestamp", authConfig.isIncludeLogTime() ? "1" : "0"); config.setProperty("LogFileLevel", "" + authConfig.getLogConsoleLevel().getCode()); config.setProperty("LogColors", "" + authConfig.getLogNormalColor().getCode() + " " + authConfig.getLogDetailsColor().getCode() + " " + authConfig.getLogDebugColor().getCode() + " " + authConfig.getLogErrorColor().getCode()); config.setProperty("UseProcessors", "" + authConfig.getUseProcessors()); config.setProperty("ProcessPriority", "" + authConfig.getProcessPriority().getCode()); config.setProperty("WaitAtStartupError", authConfig.isWaitAtStartupError() ? "1" : "0"); config.setProperty("RealmsStateUpdateDelay", "" + authConfig.getRealmStateUpdateDelay()); config.setProperty("WrongPass.MaxCount", "" + authConfig.getNbWrongPass()); config.setProperty("WrongPass.BanTime", "" + authConfig.getBanTime()); config.setProperty("WrongPass.BanType", "" + authConfig.getBanType().getCode()); logger.debug("Saving the configuration " + this.configuration.getSourcePath()); builder.save(); } catch (ConfigurationException ex) { System.out.println(ex.toString()); logger.error("An error happened during the save of the configuration"); throw ex; } } @Override public void setConfiguration(String file) { this.configuration.setSourcePath(file); } @Override public String getConfiguration() { return this.configuration.getSourcePath(); } @Override public void setName(String name) { this.configuration.setName(name); } @Override public String getName() { return this.configuration.getName(); } }