|
| 1 | +/** |
| 2 | + * Copyright (c) 2023 Source Auditor Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.spdx; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.util.Properties; |
| 23 | + |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +/** |
| 28 | + * The configuration class for the Spdx-Java-Library. When a caller attempts to retrieve a configuration property, it |
| 29 | + * will first be checked in the Java system properties (i.e. set via `-D` command line options to the JVM, or by |
| 30 | + * programmatic calls to `System.setProperty()` in code), and will then fallback on a properties file in the classpath. |
| 31 | + * That file must be called `/resources/spdx-java-library.properties`. |
| 32 | + * |
| 33 | + * Please see the documentation for specifics on what configuration options Spdx-Java-Library supports, and how they |
| 34 | + * impact the library's behavior. |
| 35 | + */ |
| 36 | +public final class Configuration { |
| 37 | + private static final Logger logger = LoggerFactory.getLogger(Configuration.class.getName()); |
| 38 | + private static final String PROPERTIES_DIR = "resources"; |
| 39 | + private static final String CONFIGURATION_PROPERTIES_FILENAME = PROPERTIES_DIR + "/" + "spdx-java-library.properties"; |
| 40 | + private static final String DEPRECATED_CONFIGURATION_PROPERTIES_FILENAME = PROPERTIES_DIR + "/" + "licenses.properties"; // Deprecated filename |
| 41 | + |
| 42 | + private static Configuration singleton; |
| 43 | + private final Properties properties; |
| 44 | + |
| 45 | + private Configuration() { |
| 46 | + Properties tmpProperties = loadProperties(CONFIGURATION_PROPERTIES_FILENAME); |
| 47 | + if (tmpProperties == null) { |
| 48 | + // This is to preserve backwards compatibility with version 1.1.7 of the library and earlier |
| 49 | + tmpProperties = loadProperties(DEPRECATED_CONFIGURATION_PROPERTIES_FILENAME); |
| 50 | + if (tmpProperties != null) { |
| 51 | + logger.warn("You are using a deprecated configuration properties filename ('" + DEPRECATED_CONFIGURATION_PROPERTIES_FILENAME + "'). Please consider migrating to the new name ('" + CONFIGURATION_PROPERTIES_FILENAME + "')."); |
| 52 | + } |
| 53 | + } |
| 54 | + properties = tmpProperties; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return The singleton instance of the Configuration class. |
| 59 | + */ |
| 60 | + public static Configuration getInstance() { |
| 61 | + if (singleton == null) { |
| 62 | + singleton = new Configuration(); |
| 63 | + } |
| 64 | + return singleton; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param propertyName The name of the configuration property to retrieve. |
| 69 | + * @return The value of the given property name, or null if it wasn't found. |
| 70 | + */ |
| 71 | + public String getProperty(final String propertyName) { |
| 72 | + return getProperty(propertyName, null); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param propertyName The name of the configuration property to retrieve. |
| 77 | + * @param defaultValue The default value to return, if the property isn't found. |
| 78 | + * @return The value of the given property name, or defaultValue if it wasn't found. |
| 79 | + */ |
| 80 | + public String getProperty(final String propertyName, final String defaultValue) { |
| 81 | + return System.getProperty(propertyName, properties == null ? defaultValue : properties.getProperty(propertyName, defaultValue)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Tries to load properties from the CLASSPATH, using the provided filename, ignoring errors |
| 86 | + * encountered during the process (e.g., the properties file doesn't exist, etc.). |
| 87 | + * |
| 88 | + * @param propertiesFileName the name of the file to load, including path (if any) |
| 89 | + * @return a (possibly empty) set of properties, or null if the properties file doesn't exist on the CLASSPATH |
| 90 | + */ |
| 91 | + private static Properties loadProperties(final String propertiesFileName) { |
| 92 | + Properties result = null; |
| 93 | + if (propertiesFileName != null) { |
| 94 | + InputStream in = null; |
| 95 | + try { |
| 96 | + in = Configuration.class.getResourceAsStream("/" + propertiesFileName); |
| 97 | + if (in != null) { |
| 98 | + result = new Properties(); |
| 99 | + result.load(in); |
| 100 | + } |
| 101 | + } catch (IOException e) { |
| 102 | + // Ignore it and fall through |
| 103 | + logger.warn("IO Exception reading configuration properties file '" + propertiesFileName + "': " + e.getMessage(), e); |
| 104 | + result = null; |
| 105 | + } finally { |
| 106 | + if (in != null) { |
| 107 | + try { |
| 108 | + in.close(); |
| 109 | + } catch (IOException e) { |
| 110 | + // Ignore it and fall through |
| 111 | + logger.warn("Unable to close configuration properties file '" + propertiesFileName + "': " + e.getMessage(), e); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return result; |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments