Class JKConfig

java.lang.Object
com.jk.core.config.JKConfig

public class JKConfig extends Object
This class provides an implementation of the configurations and provides ways to access and manage the properties of these configurations.
Version:
1.0
Author:
Dr. Jalal H. Kiswani
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static JKConfig
    Represents the default configuration instance.
    protected static boolean
    Represents a boolean value to show whether this object been initialized or not.
    protected static List<JKConfigListener>
    Represents the configuration listeners.
  • Constructor Summary

    Constructors
    Constructor
    Description
    This constructor creates a new instance of the JKConfig class.
    This constructor creates a new instance of the JKConfig class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    This method add a new configuration listener to the internal configuration listeners list.
    void
    This method adds all the properties specified to the configuration properties.
    void
    This method closes this JKConfig object and clears all the information and data stored within it.
    static JKConfig
    get()
    This method is used to access the default configuration instance.
    static JKConfig
    This method returns the default configuration instance, if its null, it initiate it first before returning it.
    This method gets the list of configuration listeners list.
    This method gets the property value that is associated with the property name specified, then returns the decrypted value.
    getPassword(String name, String defaultPassword)
    This method gets the property value that is associated with the property name specified, then returns the decrypted value.
    This method gets the configuration properties of the JKConfig object.
    This method gets the specified property value.
    getProperty(String name, String defaultValue)
    This method gets the specifies property value, or the default value in case the property had a null value.
    boolean
    This method gets the property value as boolean, using a default value of false.
    boolean
    getPropertyAsBoolean(String name, boolean defaultValue)
    This method gets the property value as boolean.
    getPropertyAsDate(String name, Date defaultValue)
    This method gets the property value as date.
    double
    getPropertyAsDouble(String name, double defaultValue)
    This method gets the property value as double.
    int
    This method gets the property value as integer with a default value of 0
    int
    getPropertyAsInteger(String name, int defaultValue)
    This method gets the property value as integer.
    This method gets the property value as time.
    getPropertyAsTime(String property, LocalTime defaultTime)
    This method gets the property value as time.
    This method gets a string associated with the given configuration property name with a default value of null
    getString(String name, String defaultValue)
    This method gets a string associated with the given configuration property name.
    static void
    This method initiate the default configuration instance by specifying all its needed attributes, while properly documenting the steps with the configuration logger.
    static boolean
    This method checks whether this JKConfig object is initiated or not.
    protected static Properties
    This method adds the configuration properties stored on cloud foundry..
    protected static Map
    loadFromFile(String fileName)
    This method loads up the file specified by the given URL.
    protected static Properties
    This method adds the configuration properties stored on a smart cloud environment variables.
    static void
    main(String[] args)
    This is the main method that tests the methods available in the JKConfig class.
    protected static Properties
    This method takes a set of pre-configured properties and performs processing to load additional configuration properties.
    static Properties
    readConfigFromLocalFolder(String appConfigFileName, File localPath)
    This method reads the configuration stored in a local file..
    static void
    This method resets this JKConfig object by clearing all the data and information stored within it and resetting it to its initiated state.
    static void
    setDefaultInstance(JKConfig defaultInstance)
    This method replaces the default configuration instance with the specified instance.
    void
    setPassword(String name, String value)
    This method encrypt the property value provided before adding it with the rest of the configuration properties.
    void
    setProperty(String key, String value)
    This method sets a property, this will replace any previously set values.
    void
    setString(String name, String value)
    This method sets a property, this will replace any previously set values.
    This method returns all the current configuration properties of the JKConfig object as Properties object.
    Returns a string representation of the object.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • defaultInstance

      protected static JKConfig defaultInstance
      Represents the default configuration instance.
    • listeners

      protected static List<JKConfigListener> listeners
      Represents the configuration listeners.
    • initilized

      protected static boolean initilized
      Represents a boolean value to show whether this object been initialized or not.
  • Constructor Details

    • JKConfig

      public JKConfig()
      This constructor creates a new instance of the JKConfig class.

      It creates an empty instance without any configurations.

    • JKConfig

      public JKConfig(Properties prop)
      This constructor creates a new instance of the JKConfig class.

      It uses the specified properties as the configuration properties of the initiated object.

      Parameters:
      prop - Specifies the object configuration properties.
  • Method Details

    • getDefaultInstance

      public static JKConfig getDefaultInstance()
      This method returns the default configuration instance, if its null, it initiate it first before returning it.
      Returns:
      the default configuration instance.
    • init

      public static void init()
      This method initiate the default configuration instance by specifying all its needed attributes, while properly documenting the steps with the configuration logger.
    • loadFromFile

      protected static Map loadFromFile(String fileName)
      This method loads up the file specified by the given URL.
      Parameters:
      fileName - Specifies the URL of the file needed to be loaded.
      Returns:
      the properties read in the loaded file. returns an empty map if the given URL doesn't point to a valid location.
    • processPreConfigToLoadConfig

      protected static Properties processPreConfigToLoadConfig(Properties allProperties)
      This method takes a set of pre-configured properties and performs processing to load additional configuration properties.
      Parameters:
      allProperties - Specifies The pre-configured properties to be processed.
      Returns:
      a new Properties object containing the pre-configured properties
    • readConfigFromLocalFolder

      public static Properties readConfigFromLocalFolder(String appConfigFileName, File localPath)
      This method reads the configuration stored in a local file..
      Parameters:
      appConfigFileName - Specifies the local file name that holds the configuration properties.
      localPath - Specifies the local file path.
      Returns:
      a new Properties object containing the configuration specified in the local file.
    • getProperty

      public String getProperty(String name)
      This method gets the specified property value.
      Parameters:
      name - Specifies the name of the property.
      Returns:
      the property that was associated with the specified name.
    • getProperty

      public String getProperty(String name, String defaultValue)
      This method gets the specifies property value, or the default value in case the property had a null value.
      Parameters:
      name - Specifies the name of the property.
      defaultValue - Specifies the default value of the property.
      Returns:
      the property value that is associated with the specified name. Returns the property default value if the property name points to a null property. returns null if there's no property with the specified name.
    • getPropertyAsInteger

      public int getPropertyAsInteger(String name, int defaultValue)
      This method gets the property value as integer.
      Parameters:
      name - Specifies the property name.
      defaultValue - Specifies the property default value.
      Returns:
      the property value as an integer.
      See Also:
    • getPropertyAsDouble

      public double getPropertyAsDouble(String name, double defaultValue)
      This method gets the property value as double.
      Parameters:
      name - Specifies the property name.
      defaultValue - Specifies the property default value.
      Returns:
      the property value as a double.
      See Also:
    • getPropertyAsBoolean

      public boolean getPropertyAsBoolean(String name)
      This method gets the property value as boolean, using a default value of false.
      Parameters:
      name - Specifies the property name.
      Returns:
      the property value as boolean.
      See Also:
    • getPropertyAsBoolean

      public boolean getPropertyAsBoolean(String name, boolean defaultValue)
      This method gets the property value as boolean.
      Parameters:
      name - Specifies the property name.
      defaultValue - Specifies the property default value.
      Returns:
      the property value as boolean.
      See Also:
    • getPropertyAsDate

      public Date getPropertyAsDate(String name, Date defaultValue)
      This method gets the property value as date.
      Parameters:
      name - Specifies the property name.
      defaultValue - Specifies the property default value.
      Returns:
      the property value as a date.
      See Also:
    • toString

      public String toString()
      Returns a string representation of the object.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the object.
    • toProperties

      public Properties toProperties()
      This method returns all the current configuration properties of the JKConfig object as Properties object.

      This method ignores all the properties that have null values.

      Returns:
      configuration properties which this object is currently configured to.
    • setString

      public void setString(String name, String value)
      This method sets a property, this will replace any previously set values.

      This is implicitly a call to clearProperty(name), addProperty(name, value).

      Parameters:
      name - Specifies the property name.
      value - Specifies the property value.
      See Also:
      • clearProperty(key)
      • addProperty(key, value)
    • setPassword

      public void setPassword(String name, String value)
      This method encrypt the property value provided before adding it with the rest of the configuration properties.

      This will replace any previously set values.

      Parameters:
      name - Specifies the name of the property.
      value - Specifies the property value to be encrypted.
      See Also:
    • getString

      public String getString(String name, String defaultValue)
      This method gets a string associated with the given configuration property name. If the property name doesn't map to an existing object, the defaultValue is returned.
      Parameters:
      name - Specifies the property name.
      defaultValue - Specifies the property default value.
      Returns:
      the string which the property name is associated with, or the default value if the property name specified doesn't map to an existing entry.
    • getPassword

      public String getPassword(String name)
      This method gets the property value that is associated with the property name specified, then returns the decrypted value.

      This method uses a null password to decrypt the value.

      Parameters:
      name - Specifies the property name.
      Returns:
      the decrypted value associated with the property name.
    • getPassword

      public String getPassword(String name, String defaultPassword)
      This method gets the property value that is associated with the property name specified, then returns the decrypted value.

      This method uses the provided password to decrypt the value.

      Parameters:
      name - Specifies the property name.
      defaultPassword - Specifies the property default password used for decrypting the value.
      Returns:
      the decrypted value.
    • loadFromFrameworkEnvironmentVariables

      protected static Properties loadFromFrameworkEnvironmentVariables()
      This method adds the configuration properties stored on a smart cloud environment variables.
      Returns:
      a new Properties object containing the configuration specified in the smart cloud environment variables..
    • loadFromCloudFoundry

      protected static Properties loadFromCloudFoundry()
      This method adds the configuration properties stored on cloud foundry..
      Returns:
      a new Properties object containing the configuration specified in the smart cloud environment variables.
    • main

      public static void main(String[] args)
      This is the main method that tests the methods available in the JKConfig class.
      Parameters:
      args - The command-line arguments.
    • getPropertyAsInteger

      public int getPropertyAsInteger(String name)
      This method gets the property value as integer with a default value of 0
      Parameters:
      name - Specifies the property name.
      Returns:
      the specified property value as integer
      See Also:
    • getString

      public String getString(String name)
      This method gets a string associated with the given configuration property name with a default value of null
      Parameters:
      name - Specifies the property name.
      Returns:
      a string associated with the specified property name.
    • setDefaultInstance

      public static void setDefaultInstance(JKConfig defaultInstance)
      This method replaces the default configuration instance with the specified instance.
      Parameters:
      defaultInstance - Specifies the new default configuration instance.
    • reset

      public static void reset()
      This method resets this JKConfig object by clearing all the data and information stored within it and resetting it to its initiated state.
    • close

      public void close()
      This method closes this JKConfig object and clears all the information and data stored within it.

      This method doesn't revert the object to its initiate state, to do that, check reset() instead.

    • addListner

      public static void addListner(JKConfigListener listener)
      This method add a new configuration listener to the internal configuration listeners list.
      Parameters:
      listener - Specifies the new listener to be added.
    • getListeners

      public static List<JKConfigListener> getListeners()
      This method gets the list of configuration listeners list.
      Returns:
      a list view of the configuration listeners.
    • get

      public static JKConfig get()
      This method is used to access the default configuration instance.
      Returns:
      the default configuration instance.
    • addProperties

      public void addProperties(Properties properties)
      This method adds all the properties specified to the configuration properties.

      This will replace the properties that share the same property name.

      Parameters:
      properties - Specifies the properties to be added to the configuration.
    • setProperty

      public void setProperty(String key, String value)
      This method sets a property, this will replace any previously set values.

      This is implicitly a call to clearProperty(key), addProperty(key, value).

      Parameters:
      key - Specifies the property name.
      value - Specifies the property value.
      See Also:
      • clearProperty(key)
      • addProperty(key, value)
    • getPropertyAsTime

      public LocalTime getPropertyAsTime(String property)
      This method gets the property value as time.
      Parameters:
      property - Specifies the property.
      Returns:
      the property value as time.
    • getPropertyAsTime

      public LocalTime getPropertyAsTime(String property, LocalTime defaultTime)
      This method gets the property value as time. returns the default time value if the property value is null.
      Parameters:
      property - Specifies the property.
      defaultTime - Specifies the default property value as time
      Returns:
      the property value as time. or the default time value if the property value is null.
    • isInitilized

      public static boolean isInitilized()
      This method checks whether this JKConfig object is initiated or not.
      Returns:
      true, if is initiated, false, otherwise.
    • getProperties

      public Properties getProperties(String prefix)
      This method gets the configuration properties of the JKConfig object.
      Parameters:
      prefix - Specifies the properties prefix.
      Returns:
      the properties stored within the JKConfig object.