diff --git a/pom.xml b/pom.xml index e82f116..3880b96 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.umutayb Utilities - 1.6.7 + 1.6.8 jar Java-Utilities @@ -53,6 +53,7 @@ 1.9.12 2.22.1 3.8.0 + 1.0.0 1.8 1.8 4.8.0 @@ -73,6 +74,13 @@ + + + io.github.umutayb + context-store + ${context.store.version} + + com.squareup.okhttp3 diff --git a/src/main/java/context/ContextStore.java b/src/main/java/context/ContextStore.java deleted file mode 100644 index 60aeea8..0000000 --- a/src/main/java/context/ContextStore.java +++ /dev/null @@ -1,171 +0,0 @@ -package context; - -import properties.PropertyUtilities; -import java.util.Collections; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -import static properties.PropertyUtilities.*; - -/** - * The ContextStore class provides a thread-safe storage for key-value pairs in a ConcurrentHashMap. - * Each thread has its own map, which avoids concurrency issues and ensures thread safety. - * - * @author Umut Ay Bora - * @version 1.4.0 (Documented in 1.4.0, released in an earlier version) - */ -@SuppressWarnings({"unused", "unchecked"}) -public class ContextStore { - - /** - * ThreadLocal variable to store a ConcurrentHashMap for each thread. - */ - private static final ThreadLocal> map = ThreadLocal.withInitial(ConcurrentHashMap::new); - - /** - * Associates the specified value with the specified key in the ContextStore. - * If the key or value is null, the operation is skipped. - * This method is synchronized to ensure thread safety during the put operation. - * - * @param The type of the keys in the ContextStore. - * @param The type of the values in the ContextStore. - * @param key The key with which the specified value is to be associated. - * @param value The value to be associated with the specified key. - * @throws IllegalArgumentException If either the provided key or value is null. - * @see ContextStore#map - */ - public static synchronized void put(K key, V value) { - if (key != null && value != null) { - map.get().put(key, value); - } - } - - /** - * Removes the entry with the specified key from the ContextStore. - * If the key is not present in the ContextStore, null is returned. - * This method is synchronized to ensure thread safety during the removal process. - * - * @param The type of the keys in the ContextStore. - * @param The type of the values in the ContextStore. - * @param key The key whose associated entry is to be removed. - * @throws IllegalArgumentException If the provided key is null. - * @see ContextStore#map - */ - public static synchronized void remove(K key) { - ((ConcurrentHashMap) map.get()).remove(key); - } - - /** - * Retrieves the value associated with the specified key from the ContextStore. - * If the key is not present in the ContextStore, null is returned. - * This method is synchronized to ensure thread safety during the retrieval process. - * - * @param The type of the keys in the map. - * @param The type of the values in the map. - * @param key The key whose associated value is to be retrieved. - * @return The value associated with the specified key, or null if the key is not present. - * @throws IllegalArgumentException If the provided key is null. - * @see ContextStore#map - */ - public static synchronized V get(K key) { - return key != null ? ((ConcurrentHashMap) map.get()).get(key) : null; - } - - /** - * Retrieves the value associated with the specified key from the ContextStore. - * If the key is not present in the ContextStore, the provided defaultValue is returned. - * This method is synchronized to ensure thread safety during the retrieval process. - * - * @param The type of the keys in the map. - * @param The type of the values in the map. - * @param key The key whose associated value is to be retrieved. - * @param defaultValue The default value to be returned if the key is not present in the map. - * @return The value associated with the specified key, or the defaultValue if the key is not present. - * @throws IllegalArgumentException If the provided key is null. - * @see ContextStore#map - */ - public static synchronized V get(K key, V defaultValue) { - return key != null && map.get().get(key) != null ? ((ConcurrentHashMap) map.get()).get(key) : defaultValue; - } - - /** - * Retrieves an unmodifiable set view of the keys contained in the ContextStore. - * This method is synchronized to ensure thread safety during the retrieval process. - * - * @param The type of the keys in the ContextStore. - * @param The type of the values in the ContextStore. - * @return An unmodifiable set view of the keys in the ContextStore. - * @see ContextStore#map - */ - public static synchronized Set items() { - return Collections.unmodifiableSet(((ConcurrentHashMap) map.get()).keySet()); - } - - /** - * Clears all key-value mappings from the ContextStore. - * This method is synchronized to ensure thread safety during the clear operation. - * - * @see ContextStore#map - */ - public static synchronized void clear() { - map.get().clear(); - } - - /** - * Checks whether the ContextStore contains the specified key. - * - * @param The type of the key in the ContextStore. - * @param key The key to check for existence in the ContextStore. - * @return true if the ContextStore contains the key, otherwise false. - */ - public static synchronized boolean has(K key){ - return map.get().get(key) != null; - } - - /** - * Updates the value associated with the specified key in the ContextStore. - * If the key or value is null, the update operation is skipped. - * This method is synchronized to ensure thread safety during the update process. - * - * @param The type of the keys in the ContextStore. - * @param The type of the values in the ContextStore. - * @param key The key whose associated value is to be updated. - * @param value The new value to be associated with the specified key. - * @throws IllegalArgumentException If either the provided key or value is null. - * @see ContextStore#map - */ - public static synchronized void update(K key, V value) { - if (key != null && value != null) { - ((ConcurrentHashMap) map.get()).computeIfPresent(key, (k, oldValue) -> value); - } - } - - /** - * Merges the entries from the specified map into the ContextStore. - * This method is synchronized to ensure thread safety during the merge operation. - * - * @param maps Maps containing entries to be merged into the ContextStore. - * @see ContextStore#map - */ - public static synchronized void merge(Map... maps) { - for (Map map:maps) ContextStore.map.get().putAll(map); - } - - /** - * Loads properties from one or more property files, merging them into the ContextStore for the current thread. - * The method is synchronized to ensure thread safety during the loading and merging process. - * - * @param propertyNames An array of property file names or paths to be loaded and merged. - * @throws IllegalArgumentException If the provided array of property names is null or empty. - * @throws RuntimeException If an error occurs during the loading or merging of properties. - * This can be an IOException or any other runtime exception. - * The specific exception details are logged for further investigation. - * @see PropertyUtilities#fromPropertyFile(String) fromPropertyFile(String) - * @see ContextStore#merge(Map...) UtilityPropertiesMap.merge(Map) - * @see ContextStore#map - */ - public static synchronized void loadProperties(String... propertyNames) { - for (String propertyName : propertyNames) merge(fromPropertyFile(propertyName)); - } -} diff --git a/src/main/java/properties/Constants.java b/src/main/java/properties/Constants.java deleted file mode 100644 index 60c737e..0000000 --- a/src/main/java/properties/Constants.java +++ /dev/null @@ -1,5 +0,0 @@ -package properties; - -public class Constants { - public static final String UTILITY_PROPERTIES_FILE_NAME = "utility.properties"; -} diff --git a/src/main/java/properties/PropertiesReader.java b/src/main/java/properties/PropertiesReader.java deleted file mode 100644 index b93881e..0000000 --- a/src/main/java/properties/PropertiesReader.java +++ /dev/null @@ -1,40 +0,0 @@ -package properties; - -import utils.Printer; -import utils.StringUtilities; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; - -public class PropertiesReader { - - Printer log = new Printer(StringUtilities.class); - - private Properties properties; - - /** - * Constructs a new instance of the PropertiesReader class with the specified property file name. - * - * @param propertyFileName the name of the property file to read - */ - public PropertiesReader(String propertyFileName){ - try { - InputStream inputStream = getClass().getClassLoader() - .getResourceAsStream(propertyFileName); - this.properties = new Properties(); - this.properties.load(inputStream); - } - catch (IOException exception) {log.error(exception.getMessage(), exception);} - } - - /** - * Returns the value of the specified property. - * - * @param propertyName the name of the property to retrieve the value for - * @return the value of the property with the specified name as a String - */ - public String getProperty(String propertyName) { - return this.properties.getProperty(propertyName); - } -} diff --git a/src/main/java/properties/PropertyUtilities.java b/src/main/java/properties/PropertyUtilities.java deleted file mode 100644 index 2e98eca..0000000 --- a/src/main/java/properties/PropertyUtilities.java +++ /dev/null @@ -1,149 +0,0 @@ -package properties; - -import utils.Printer; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; - -import static properties.Constants.UTILITY_PROPERTIES_FILE_NAME; - -/** - * A utility class for loading properties from a file. - */ -public abstract class PropertyUtilities { - - private static final Printer log = new Printer(PropertyUtilities.class); - - /** - * The default Properties object containing the loaded default properties. - */ - public static final Properties properties = new Properties(); - - /** - * Loads the default properties from the default property file. - * - * @return The Properties object containing the loaded default properties. - */ - public static Properties loadPropertyFile(String propertyFileName) { - try (InputStream inputStream = PropertyUtilities.class.getResourceAsStream("/" + propertyFileName)) { - if (inputStream != null) - properties.load(inputStream); - else - log.warning(propertyFileName + " file not found!"); - } - catch (IOException | NullPointerException e) { - log.error(propertyFileName + " could not be loaded", e); - } - return properties; - } - - /** - * Retrieves the value of a specific property from the default properties. - * - * @param key The key of the property to retrieve. - * @return The value of the property, or null if the property is not found. - */ - public static String getProperty(String key) { - return properties.getProperty(key); - } - - /** - * Retrieves the value of a specific property from the default properties. - * If the property is not found, it will return the provided default value. - * - * @param key The key of the property to retrieve. - * @param defaultValue The value to return if the property is not found. - * @return The value of the property, or the default value if the property is not found. - */ - public static String getProperty(String key, String defaultValue) { - return properties.getProperty(key, defaultValue); - } - - /** - * Retrieves properties from the default property file. - * - * @return A Map containing properties loaded from the default property file. - */ - public static Map fromPropertyFile() { - return fromPropertyFile(UTILITY_PROPERTIES_FILE_NAME); - } - - /** - * Loads properties from a specified file and creates a Map with key-value pairs. - * If an IOException occurs while attempting to load properties from the file, - * it falls back to an alternative method of loading the properties. - * - * @param propertyFile The path to the property file to be loaded. - * @return A Map containing key-value pairs from the loaded properties file. - * @throws IllegalArgumentException If the provided propertyFile is null or empty. - * @throws RuntimeException If an error occurs during the loading of properties. - * This can be an IOException or any other runtime exception. - * The specific exception details are logged for further investigation. - * @see UtilityPropertiesMap#create(Properties) UtilityPropertiesMap.create(Properties) - */ - public static Map fromPropertyFile(String propertyFile) { - try { - loadPropertiesFromPath(propertyFile); - } catch (IOException e) { - // If IOException occurs, fall back to an alternative method - loadPropertyFile(propertyFile); - } - return UtilityPropertiesMap.create(properties); - } - - /** - * Retrieves properties from one or more property files and returns them as a merged Map. - * - * @param propertyNames An array of property file names or paths to be loaded and merged. - * @return A Map containing key-value pairs from the loaded properties files. - * @throws IllegalArgumentException If the provided array of property names is null or empty. - * @throws RuntimeException If an error occurs during the loading of properties. - * This can be an IOException or any other runtime exception. - * The specific exception details are logged for further investigation. - * @see #fromPropertyFile(String) fromPropertyFile(String) - */ - public static Map getProperties(String... propertyNames) { - Map properties = new HashMap<>(); - for (String propertyName : propertyNames) { - properties.putAll(fromPropertyFile(propertyName)); - } - return properties; - } - - /** - * Retrieves PropertyUtility properties. - * - * @return A Map containing properties retrieved from the environment. - */ - public static Properties getProperties(){ - return properties; - } - - /** - * Retrieves properties from the environment. - * - * @return A Map containing properties retrieved from the environment. - */ - public static Map fromEnvironment() { - return new UtilityPropertiesMap(System.getenv()); - } - - /** - * Retrieves properties from system properties. - * - * @return A Map containing properties retrieved from system properties. - */ - public static Map fromSystemProperties() { - Properties systemProperties = System.getProperties(); - return UtilityPropertiesMap.create(systemProperties); - } - - /** - * Loads properties from a specified property file. - * - */ - public static void loadPropertiesFromPath(String path) throws IOException { - properties.load(new FileReader(path)); - } -} diff --git a/src/main/java/properties/UtilityPropertiesMap.java b/src/main/java/properties/UtilityPropertiesMap.java deleted file mode 100644 index e5b5520..0000000 --- a/src/main/java/properties/UtilityPropertiesMap.java +++ /dev/null @@ -1,73 +0,0 @@ -package properties; - -import java.util.*; - -import static java.util.Objects.requireNonNull; - -public class UtilityPropertiesMap extends AbstractMap { - - final UtilityPropertiesMap parent; - final Map delegate; - - UtilityPropertiesMap(UtilityPropertiesMap parent, Map delegate) { - this.delegate = requireNonNull(delegate); - this.parent = parent; - } - - UtilityPropertiesMap(Map delegate) { - this(null, delegate); - } - - @Override - public Set> entrySet() { - return delegate.entrySet(); - } - - /** - * Creates a UtilityPropertiesMap from a Properties object. - * - * @param p The Properties object to create the map from. - * @return A UtilityPropertiesMap created from the Properties object. - */ - static UtilityPropertiesMap create(Properties p) { - Map copy = new HashMap<>(); - p.stringPropertyNames().forEach(s -> copy.put(s, p.getProperty(s))); - return new UtilityPropertiesMap(copy); - } - - @Override - public String get(Object key) { - String exactMatch = super.get(key); - if (exactMatch != null) { - return exactMatch; - } - - if (!(key instanceof String keyString)) { - return null; - } - - String formattedKey = formatKey(keyString); - String formattedMatch = super.get(formattedKey); - if (formattedMatch != null) { - return formattedMatch; - } - - if (parent == null) { - return null; - } - return parent.get(key); - } - - /** - * Formats a key to handle variations in case and characters. - * - * @param key The key to format. - * @return The formatted key. - */ - String formatKey(String key) { - return key - .replace(".", "_") - .replace("-", "_") - .toLowerCase(Locale.ENGLISH); - } -} \ No newline at end of file