[LIB-13] Lazy scanners init, scan by URL and rename API functions
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / ChessHogScannerProperties.java
1 /*
2  * Copyright (c) 2019-2020. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.scanner;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.Properties;
22
23 /**
24  * ChessHogScanner properties holder.
25  *
26  * @author Dmitry Samoshin aka gotty
27  */
28 public class ChessHogScannerProperties {
29
30     /** Properties file. */
31     private static final String PROPERTIES_FILE = "scanner.properties";
32
33     /** Properties. */
34     private static final Properties PROPERTIES;
35
36     /**
37      * Static properties initialization.
38      */
39     static {
40         PROPERTIES = new Properties();
41         readProperties(PROPERTIES_FILE, PROPERTIES, true);
42     }
43
44     /**
45      * @param propsFile Properties file name.
46      * @param props Properties.
47      * @param throwExc Flag indicating whether to throw an exception or not.
48      */
49     public static void readProperties(String propsFile, Properties props, boolean throwExc) {
50         try (InputStream is = ChessHogScannerProperties.class.getClassLoader().getResourceAsStream(propsFile))
51         {
52             if (is == null) {
53                 if (throwExc) {
54                     throw new RuntimeException(
55                             String.format("Failed to find properties file: %s", propsFile)
56                     );
57                 } else {
58                     return;
59                 }
60             }
61             props.load(is);
62         } catch (IOException cause) {
63             throw new RuntimeException(
64                     String.format("Failed to read properties file: %s", propsFile),
65                     cause
66             );
67         }
68     }
69
70     /**
71      * Empty string for not found properties.
72      */
73     private static final String EMPTY = "";
74
75     /**
76      * Gets string property value.
77      *
78      * @param key Property key.
79      * @return Property value (possibly empty string, but never {@code null}).
80      */
81     public static String get(String key) {
82         return PROPERTIES.getProperty(key, EMPTY);
83     }
84
85     /**
86      * Gets boolean property value.
87      *
88      * @param key Property key.
89      * @return Property value ({@code false} by default).
90      */
91     public static boolean is(String key) {
92         return Boolean.parseBoolean(
93                 PROPERTIES.getProperty(key, Boolean.FALSE.toString())
94         );
95     }
96
97     /**
98      * Zero integer for not found properties.
99      */
100     private static final String ZERO = "0";
101
102     /**
103      * Gets integer property value.
104      *
105      * @param key Property key.
106      * @return Property value ({@code 0} by default).
107      */
108     public static int getInt(String key) {
109         return Integer.parseInt(
110                 PROPERTIES.getProperty(key, ZERO)
111         );
112     }
113
114     /**
115      * Private constructor.
116      */
117     private ChessHogScannerProperties() {
118     }
119
120 }