Java tutorial
/** * Copyright (c) 2016-2020 Weibo, Inc. * All rights reserved. * * This software is the confidential and proprietary information of Weibo, * Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into with Weibo. */ package com.weibo.datasys.common.conf; import java.io.File; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.XMLConfiguration; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; /** * *?? * **/ public class ConfigFactory { private static final String CONFIG_FILE_DEFAULT_PATH = "./conf/config.xml"; private static XMLConfiguration config = null; private static File configDirPath; private ConfigFactory() { } public static void init(String configFilePath) { // ?? if (configFilePath == null) { configFilePath = CONFIG_FILE_DEFAULT_PATH; } try { System.out.println(new Date() + " - [ConfigInit] - ConfigPath= " + configFilePath); config = new XMLConfiguration(configFilePath); config.setReloadingStrategy(new FileChangedReloadingStrategy()); configDirPath = new File(configFilePath).getParentFile(); } catch (ConfigurationException e) { System.out.println(new Date() + " - [FatalError] - Init ConfigFactory Error. System Exit."); System.exit(1); } } /** * @return ? */ public static File getConfigDirPath() { return configDirPath; } /** * ?? * * @param configXPath * ? * @return */ public static String getString(String configXPath) { return config.getString(configXPath, null); } /** * ?? * * @param configXPath * ? * @param defaultValue * ? * @return */ public static String getString(String configXPath, String defaultValue) { return config.getString(configXPath, defaultValue); } /** * ??double * * @param configXPath * ? * @param defaultValue * ? * @return */ public static double getDouble(String configXPath, double defaultValue) { return config.getDouble(configXPath, defaultValue); } /** * ?? * * @param configXPath * ? * @param defaultValue * ? * @return */ public static float getFloat(String configXPath, float defaultValue) { return config.getFloat(configXPath, defaultValue); } /** * ?? * * @param configXPath * ? * @param defaultValue * ? * @return */ public static int getInt(String configXPath, int defaultValue) { return config.getInt(configXPath, defaultValue); } /** * ?? * * @param configXPath * ? * @param defaultValue * ? * @return */ public static long getLong(String configXPath, long defaultValue) { return config.getLong(configXPath, defaultValue); } /** * ??boolean * * @param configXPath * ? * @param defaultValue * ? * @return */ public static boolean getBoolean(String configXPath, boolean defaultValue) { return config.getBoolean(configXPath, defaultValue); } /** * ??List * * @param configXPath * ? * @return */ public static List<String> getList(String configXPath) { List<String> values = new ArrayList<String>(); for (Object o : config.getList(configXPath)) { values.add(o.toString()); } return values; } /** * * String? * * @param configXPath * @param value */ public static void setString(String configXPath, String value) { config.setProperty(configXPath, value); } /** * * int? * * @param configXPath * @param value */ public static void setInt(String configXPath, int value) { config.setProperty(configXPath, value); } /** * * Float? * * @param configXPath * @param value */ public static void setFloat(String configXPath, float value) { config.setProperty(configXPath, value); } /** * * Long? * * @param configXPath * @param value */ public static void setLong(String configXPath, long value) { config.setProperty(configXPath, value); } /** * * Boolean? * * @param configXPath * @param value */ public static void setBoolean(String configXPath, boolean value) { config.setProperty(configXPath, value); } /** * ? * * @param key */ public static void remove(String key) { config.clearTree(key); } /** * ?? */ public static void save() { try { config.save(); } catch (ConfigurationException e) { e.printStackTrace(); } } }