X-Git-Url: https://git.hedgecode.org/?p=chesshog.git;a=blobdiff_plain;f=chesshog-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2FChessHogProperties.java;fp=chesshog-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2FChessHogProperties.java;h=06428907466ded4261c79f594bfcf1815af0ad38;hp=0000000000000000000000000000000000000000;hb=b723e3cbfc1fce59187408a95de47568b1e5e88e;hpb=6f233e90e111f983e0ef401697859bb677bc0929 diff --git a/chesshog-core/src/main/java/org/hedgecode/chess/ChessHogProperties.java b/chesshog-core/src/main/java/org/hedgecode/chess/ChessHogProperties.java new file mode 100644 index 0000000..0642890 --- /dev/null +++ b/chesshog-core/src/main/java/org/hedgecode/chess/ChessHogProperties.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2018-2019. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +/** + * ChessHog properties holder. + * + * @author Dmitry Samoshin aka gotty + */ +public class ChessHogProperties { + + /** Properties file. */ + private static final String PROPERTIES_FILE = "chesshog.properties"; + + /** Properties. */ + private static final Properties PROPERTIES; + + /** + * Static properties initialization. + */ + static { + PROPERTIES = new Properties(); + readProperties(PROPERTIES_FILE, PROPERTIES, true); + } + + /** + * @param propsFile Properties file name. + * @param props Properties. + * @param throwExc Flag indicating whether to throw an exception or not. + */ + public static void readProperties(String propsFile, Properties props, boolean throwExc) { + try (InputStream is = ChessHogProperties.class.getClassLoader().getResourceAsStream(propsFile)) { + if (is == null) { + if (throwExc) + throw new RuntimeException("Failed to find properties file: " + propsFile); + else + return; + } + + props.load(is); + } + catch (IOException e) { + throw new RuntimeException("Failed to read properties file: " + propsFile, e); + } + } + + /** + * Empty string for not found properties. + */ + private static final String EMPTY = ""; + + /** + * Gets property value. + * + * @param key Property key. + * @return Property value (possibly empty string, but never {@code null}). + */ + public static String get(String key) { + return PROPERTIES.getProperty(key, EMPTY); + } + + /** + * Private constructor. + */ + private ChessHogProperties() { + } + +}