All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mycore.common.config.MCRConfiguration Maven / Gradle / Ivy

There is a newer version: 2024.05
Show newest version
/*
 * This file is part of ***  M y C o R e  ***
 * See http://www.mycore.de/ for details.
 *
 * MyCoRe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MyCoRe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MyCoRe.  If not, see .
 */

package org.mycore.common.config;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.mycore.common.MCRClassTools;

/**
 * Provides methods to manage and read all configuration properties from the MyCoRe configuration files.
 * The Properties used by this class are initialized via {@link MCRConfigurationLoaderFactory}.
 * The class is implemented using the singleton pattern. Using this class is very easy, here is an example:
 *
 * 
 * // Get a configuration property as a String:
 * String driver = MCRConfiguration.instance().getString("MCR.JDBC.Driver");
 *
 * // Get a configuration property as an int, use 500 as default if not set:
 * int max = MCRConfiguration.instance().getInt("MCR.Cache.Size", 500);
 * 
* * As you see, the class provides methods to get configuration properties as different data types and allows you to * specify defaults. All MyCoRe configuration properties should start with "MCR." * * The class also provides methods for listing and saving all properties to an OutputStream. * Using the set methods allows * client code to set new configuration properties or overwrite existing ones with new values. * @see #list(PrintStream) * @see #store * @author Frank Lützenkirchen * @deprecated Please do use {@link MCRConfiguration2} instead */ @Deprecated public class MCRConfiguration { /** * The single instance of this class that will be used at runtime */ private static MCRConfiguration singleton; static { createSingleton(); } /** * Returns the single instance of this class that can be used to read and manage the configuration properties. * * @return the single instance of MCRConfiguration to be used */ public static MCRConfiguration instance() { return singleton; } /** * Use this method as a default value for {@link #getStrings(String, List)}. * * @return an empty list of Strings * @see Collections#emptyList() */ public static List emptyList() { return Collections.emptyList(); } /** * Instantiates the singleton by calling the protected constructor. */ protected static void createSingleton() { try { singleton = new MCRConfiguration(); } catch (IOException e) { throw new MCRConfigurationException("Could not instantiate MCRConfiguration.", e); } MCRConfigurationBase.systemModified(); } /** * return the given properties sorted by keys * @param props - properties to be sorted * if props is null - an empty properties object that supports sorting by key will be created * @return a new properties object sorted by keys */ @Deprecated public static Properties sortProperties(Properties props) { Properties sortedProps = new Properties() { private static final long serialVersionUID = 1L; @Override public synchronized Enumeration keys() { return Collections.enumeration(new TreeSet<>(super.keySet())); } }; if (props != null) { sortedProps.putAll(props); } return sortedProps; } /** * returns the last point in time when the MyCoRe system was last modified. This method can help you to validate * caches not under your controll, e.g. client caches. * * @see System#currentTimeMillis() */ @Deprecated public final long getSystemLastModified() { return MCRConfigurationBase.getSystemLastModified(); } /** * signalize that the system state has changed. Call this method when ever you changed the persistency layer. */ @Deprecated public final void systemModified() { MCRConfigurationBase.systemModified(); } /** * Protected constructor to create the singleton instance */ protected MCRConfiguration() throws IOException { } private MCRProperties getResolvedProperties() { return MCRConfigurationBase.getResolvedProperties(); } public MCRProperties getDeprecatedProperties() { return MCRConfigurationBase.getDeprecatedProperties(); } @Deprecated public Map getPropertiesMap() { return MCRConfiguration2.getPropertiesMap(); } /** * Returns all the properties beginning with the specified string * * @param startsWith * the string all the returned properties start with * @return the list of properties */ @Deprecated public Map getPropertiesMap(final String startsWith) { return MCRConfiguration2.getPropertiesMap() .entrySet() .stream() .filter(p -> p.getKey().startsWith(startsWith)) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } /** * Returns a new instance of the class specified in the configuration property with the given name. * * @param name * the non-null and non-empty qualified name of the configuration property * @param defaultname * the qualified class name * @return Instance of the value of the configuration property * @throws MCRConfigurationException * if the property is not set or the class can not be loaded or instantiated */ @Deprecated public T getInstanceOf(String name, String defaultname) throws MCRConfigurationException { return defaultname == null ? MCRConfiguration2.getOrThrow(name, MCRConfiguration2::instantiateClass) : MCRConfiguration2.getInstanceOf(name) .orElseGet(() -> MCRConfiguration2.instantiateClass(defaultname)); } /** * Returns a new instance of the class specified in the configuration property with the given name. * * @param name * the non-null and non-empty qualified name of the configuration property * @param defaultObj * the default object; * @return Instance of the value of the configuration property * @throws MCRConfigurationException * if the property is not set or the class can not be loaded or instantiated */ @Deprecated public T getInstanceOf(String name, T defaultObj) { return MCRConfiguration2.getInstanceOf(name).orElse(defaultObj); } /** * Loads a Java Class defined in property name. * @param name Name of the property * @param Supertype of class defined in name * @return non null Class asignable to <T> * @throws MCRConfigurationException if property is not defined or class could not be loaded */ @Deprecated public Class getClass(String name) throws MCRConfigurationException { return MCRConfiguration2.getClass(name) .orElseThrow(() -> MCRConfiguration2.createConfigurationException(name)); } /** * Loads a Java Class defined in property name. * @param name Name of the property * @param defaultClass default value if property is not defined * @param Supertype of class defined in name * @return non null Class asignable to <T> */ @Deprecated public Class getClass(String name, Class defaultClass) { return MCRConfiguration2.getClass(name).orElse(defaultClass); } /** * Returns a new instance of the class specified in the configuration property with the given name. * * @param name * the non-null and non-empty name of the configuration property * @return the value of the configuration property as a String, or null * @throws MCRConfigurationException * if the property is not set or the class can not be loaded or instantiated */ @Deprecated public T getInstanceOf(String name) throws MCRConfigurationException { return MCRConfiguration2.getOrThrow(name, MCRConfiguration2::instantiateClass); } /** * Returns a instance of the class specified in the configuration property with the given name. If the class was * previously instantiated by this method this instance is returned. * * @param name * the non-null and non-empty name of the configuration property * @return the instance of the class named by the value of the configuration property * @throws MCRConfigurationException * if the property is not set or the class can not be loaded or instantiated */ @Deprecated public T getSingleInstanceOf(String name, String defaultname) throws MCRConfigurationException { try { return MCRConfiguration2.getSingleInstanceOf(name, MCRClassTools.forName(defaultname)) .orElseThrow(() -> MCRConfiguration2.createConfigurationException(name)); } catch (ClassNotFoundException e) { throw MCRConfiguration2.createConfigurationException(name); } } /** * Returns a instance of the class specified in the configuration property with the given name. If the class was * prevously instantiated by this method this instance is returned. * * @param name * non-null and non-empty name of the configuration property * @return the instance of the class named by the value of the configuration property * @throws MCRConfigurationException * if the property is not set or the class can not be loaded or instantiated */ @Deprecated public T getSingleInstanceOf(String name) { return MCRConfiguration2.getSingleInstanceOf(name) .orElseThrow(() -> MCRConfiguration2.createConfigurationException(name)); } /** * Returns the configuration property with the specified name as a String. * * @param name * the non-null and non-empty name of the configuration property * @return the value of the configuration property as a String * @throws MCRConfigurationException * if the property with this name is not set */ @Deprecated public String getString(String name) { return MCRConfigurationBase.getString(name) .map(String::trim) .orElseThrow(() -> MCRConfiguration2.createConfigurationException(name)); } /** * Returns the configuration property with the specified name as a list of strings. Values should be delimited by * ',' * * @param name * the non-null and non-empty name of the configuration property * @return the value of the configuration property as a unmodifiable list of strings. * @throws MCRConfigurationException * if the property with this name is not set */ @Deprecated public List getStrings(String name) { return MCRConfigurationBase.getString(name) .map(MCRConfiguration2::splitValue) .orElseThrow(() -> MCRConfiguration2.createConfigurationException(name)) .collect(Collectors.toList()); } /** * Returns the configuration property with the specified name as a list of strings. Values should be delimited by * ',' * * @param name * the non-null and non-empty name of the configuration property * @param defaultValue * the value to return if the configuration property is not set * @return the value of the configuration property as a unmodifiable list of strings or defaultValue. */ @Deprecated public List getStrings(String name, List defaultValue) { return MCRConfigurationBase.getString(name) .map(MCRConfiguration2::splitValue) .map(s -> s.collect(Collectors.toList())) .orElse(defaultValue); } /** * Returns the configuration property with the specified name as a String, or returns a given default value if the * property is not set. * * @param name * the non-null and non-empty name of the configuration property * @param defaultValue * the value to return if the configuration property is not set * @return the value of the configuration property as a String */ @Deprecated public String getString(String name, String defaultValue) { return MCRConfiguration2.getString(name).orElse(defaultValue); } /** * Returns the configuration property with the specified name as an * int value. * * @param name * the non-null and non-empty name of the configuration property * @return the value of the configuration property as an int value * @throws NumberFormatException * if the configuration property is not an int value * @throws MCRConfigurationException * if the property with this name is not set */ @Deprecated public int getInt(String name) throws NumberFormatException { return MCRConfiguration2.getOrThrow(name, Integer::parseInt); } /** * Returns the configuration property with the specified name as an * int value, or returns a given default value if the property is not set. * * @param name * the non-null and non-empty name of the configuration property /** Returns the configuration property * with the specified name as an int value, or returns a given default value if the property * is not set. * @param defaultValue * the value to return if the configuration property is not set * @return the value of the specified property as an int value * @throws NumberFormatException * if the configuration property is set but is not an int * value */ @Deprecated public int getInt(String name, int defaultValue) throws NumberFormatException { return MCRConfiguration2.getInt(name).orElse(defaultValue); } /** * Returns the configuration property with the specified name as a * long value. * * @param name * the non-null and non-empty name of the configuration property * @return the value of the configuration property as a long value * @throws NumberFormatException * if the configuration property is not a long value * @throws MCRConfigurationException * if the property with this name is not set */ @Deprecated public long getLong(String name) throws NumberFormatException { return MCRConfiguration2.getOrThrow(name, Long::parseLong); } /** * Returns the configuration property with the specified name as a * long value, or returns a given default value if the property is not set. * * @return the value of the specified property as a long value * @param name * the non-null and non-empty name of the configuration property * @param defaultValue * the value to return if the configuration property is not set * @throws NumberFormatException * if the configuration property is set but is not a long * value */ @Deprecated public long getLong(String name, long defaultValue) throws NumberFormatException { return MCRConfiguration2.getLong(name).orElse(defaultValue); } /** * Returns the configuration property with the specified name as a * float value. * * @param name * the non-null and non-empty name of the configuration property * @return the value of the configuration property as a float value * @throws NumberFormatException * if the configuration property is not a float value * @throws MCRConfigurationException * if the property with this name is not set */ @Deprecated public float getFloat(String name) throws NumberFormatException { return MCRConfiguration2.getOrThrow(name, Float::parseFloat); } /** * Returns the configuration property with the specified name as a * float value, or returns a given default value if the property is not set. * * @return the value of the specified property as a float value * @param name * the non-null and non-empty name of the configuration property * @param defaultValue * the value to return if the configuration property is not set * @throws NumberFormatException * if the configuration property is set but is not a * float value */ @Deprecated public float getFloat(String name, float defaultValue) throws NumberFormatException { return MCRConfiguration2.getFloat(name).orElse(defaultValue); } /** * Returns the configuration property with the specified name as a * double value. * * @param name * the non-null and non-empty name of the configuration property * @return the value of the configuration property as a double * value * @throws NumberFormatException * if the configuration property is not a double value * @throws MCRConfigurationException * if the property with this name is not set */ @Deprecated public double getDouble(String name) throws NumberFormatException { return MCRConfiguration2.getOrThrow(name, Double::parseDouble); } /** * Returns the configuration property with the specified name as a * double value, or returns a given default value if the property is not set. * * @return the value of the specified property as a double value * @param name * the non-null and non-empty name of the configuration property * @param defaultValue * the value to return if the configuration property is not set * @throws NumberFormatException * if the configuration property is set but is not a * double value */ @Deprecated public double getDouble(String name, double defaultValue) throws NumberFormatException { return MCRConfiguration2.getDouble(name).orElse(defaultValue); } /** * Returns the configuration property with the specified name as a * boolean value. * * @param name * the non-null and non-empty name of the configuration property * @return true, if and only if the specified property has the value true * @throws MCRConfigurationException * if the property with this name is not set */ @Deprecated public boolean getBoolean(String name) { return MCRConfiguration2.getOrThrow(name, Boolean::parseBoolean); } /** * Returns the configuration property with the specified name as a * boolean value, or returns a given default value if the property is not set. If the property is set and its * value is not true * , then false is returned. * * @return the value of the specified property as a boolean value * @param name * the non-null and non-empty name of the configuration property * @param defaultValue * the value to return if the configuration property is not set */ @Deprecated public boolean getBoolean(String name, boolean defaultValue) { return MCRConfiguration2.getBoolean(name).orElse(defaultValue); } /** * Sets the configuration property with the specified name to a new * String value. If the parameter value is * null, the property will be deleted. * * @param name * the non-null and non-empty name of the configuration property * @param value * the new value of the configuration property, possibly * null */ @Deprecated public void set(String name, String value) { MCRConfiguration2.set(name, value); } /** * use {@link MCRConfigurationBase#initialize(Map, Map, boolean)} */ @Deprecated public synchronized void initialize(Map props, boolean clear) { throw new UnsupportedOperationException(); } /** * Sets the configuration property with the specified name to a new * int value. * * @param name * the non-null and non-empty name of the configuration property * @param value * the new value of the configuration property */ @Deprecated public void set(String name, int value) { MCRConfiguration2.set(name, String.valueOf(value)); } /** * Sets the configuration property with the specified name to a new * long value. * * @param name * the non-null and non-empty name of the configuration property * @param value * the new value of the configuration property */ @Deprecated public void set(String name, long value) { MCRConfiguration2.set(name, String.valueOf(value)); } /** * Sets the configuration property with the specified name to a new * float value. * * @param name * the non-null and non-empty name of the configuration property * @param value * the new value of the configuration property */ @Deprecated public void set(String name, float value) { MCRConfiguration2.set(name, String.valueOf(value)); } /** * Sets the configuration property with the specified name to a new * double value. * * @param name * the non-null and non-empty name of the configuration property * @param value * the new value of the configuration property */ @Deprecated public void set(String name, double value) { MCRConfiguration2.set(name, String.valueOf(value)); } /** * Sets the configuration property with the specified name to a new * boolean value. * * @param name * the non-null and non-empty name of the configuration property * @param value * the new value of the configuration property */ @Deprecated public void set(String name, boolean value) { MCRConfiguration2.set(name, String.valueOf(value)); } /** * Lists all configuration properties currently set to a PrintStream. Useful for debugging, e. g. by calling *

* MCRConfiguration.instance().list( System.out ); *

* * @see java.util.Properties#list( PrintStream ) * @param out * the PrintStream to list the configuration properties on */ @Deprecated public void list(PrintStream out) { MCRConfigurationBase.getResolvedProperties().list(out); } /** * Lists all configuration properties currently set to a PrintWriter. Useful for debugging. * * @see java.util.Properties#list( PrintWriter ) * @param out * the PrintWriter to list the configuration properties on */ @Deprecated public void list(PrintWriter out) { MCRConfigurationBase.getResolvedProperties().list(out); } /** * Stores all configuration properties currently set to an OutputStream. * * @see java.util.Properties#store * @param out * the OutputStream to write the configuration properties to * @param header * the header to prepend before writing the list of properties * @throws IOException * if writing to the OutputStream throws an IOException * */ @Deprecated public void store(OutputStream out, String header) throws IOException { MCRConfigurationBase.getResolvedProperties().store(out, header); } /** * Returns a String containing the configuration properties currently set. Useful for debugging, e. g. by calling *

* System.out.println( MCRConfiguration.instance() ); *

* * @return a String containing the configuration properties currently set */ @Override public String toString() { return MCRConfigurationBase.getResolvedProperties().toString(); } }