Class FWPropertyProvider

java.lang.Object
net.simplace.core.property.FWPropertyProvider

public final class FWPropertyProvider extends Object
Provider for properties. It centralizes access to properties throughout the whole application. In addition to making direct access to property files obsolete it also caches whole sets of properties (i.e. property files) by keeping them in memory. A property set is loaded into memory when any of the properties contained in it is being accessed for the first time.

A property set name is read from name_config.xml, first from the user cache directory ~/tmp/properties, then from the classpath. It also gives access to environment, registry and Java runtime variables.

Author:
Andreas Enders
  • Method Details

    • getInstance

      public static FWPropertyProvider getInstance()
      Get Singleton instance of PropertyResourceManager.
      Returns:
      the Instance of PropertyProvider (singleton)
    • getProperty

      public String getProperty(String aPropertySet, String aKey, String aDefault)
      Get the value of a property in a specific set. If the set doesnt contain a value for the given property return the specified default value.
      Parameters:
      aPropertySet - name of the property set
      aKey - the property key
      aDefault - value returned if the property is missing
      Returns:
      the property value with system variables replaced (see replaceSystemVariables(String)), or the default
    • getProperty

      public String getProperty(String aPropertySet, String aKey)
      Get the value of a property in a specific set. If the set doesn't contain a value for the given property or if the set doesn't exist anyway return .
      Parameters:
      aPropertySet - name of the property set
      aKey - the property key
      Returns:
      the property value with system variables replaced, or null (a warning is logged)
    • getSystemEnvironmentVariable

      public String getSystemEnvironmentVariable(String aKey)
      Returns an environment variable, or else the Java system property with this name.
      Parameters:
      aKey - the variable name
      Returns:
      the value, or null if neither exists or the key is null
    • getSystemRegistryVariable

      public static String getSystemRegistryVariable(String aKey)
      Gets the value for a given registry key. A registry key is composed of a registry root key and path. If the registry key is not present or can not be found (non-Windows systems) null is returned. Windows registry values are stored by root key and path. The parameter registryKey contains the following format: ROOT_KEY\PATH\VALUE for a specific value or ROOT_KEY\PATH\ to return the default value. Example registryKey values: HKEY_CURRENT_USER\Environment\JAVA_HOME HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Search\SearchAssistant HKEY_CLASSES_ROOT\Shell\shell\open\command\ Supported root key values are: HKEY_CLASSES_ROOT HKEY_CURRENT_CONFIG HKEY_CURRENT_USER HKEY_DYN_DATA HKEY_LOCAL_MACHINE HKEY_PERFORMANCE_DATA HKEY_USERS Calling getSystemRegistryVariable("HKEY_CLASSES_ROOT\Shell\shell\open\command\") would return %SystemRoot%\Explorer.exe /idlist,%I,%L on a typical Windows installation.
      Parameters:
      aKey - the registry key ROOT_KEY\PATH\VALUE
      Returns:
      the system registry variable value, or null
    • getJavaRuntimeVariable

      public static String getJavaRuntimeVariable(String aKey)
      Returns a Java system property with forward slashes; for java.io.tmpdir trailing separators are removed.
      Parameters:
      aKey - name of the system property
      Returns:
      the value, or null for unknown properties (was a NullPointerException)
    • setSystemEnvironmentVariable

      @Deprecated public static boolean setSystemEnvironmentVariable(String aKey, String aValue)
      Deprecated.
      has no effect
      Not supported: a Java process cannot change its own environment variables (running set in a shell only affected that child process). A warning is logged.
      Parameters:
      aKey - the variable name
      aValue - the value
      Returns:
      always false
    • setProperty

      public boolean setProperty(String aPropertySet, String aKey, String aValue)
      Set the value for a specified key within a specified property set and make those settings persistent, so the next time the application is started the set value is still available. This is essential for adding user settings capabilities to applications. The key given as a parameter to this method must exist in the (original) property set. No new keys can be added. If the key does not exist the method will return false like in any other case of an error.
      Parameters:
      aPropertySet - name of the property set
      aKey - the property key (must exist)
      aValue - the new value; null removes the property
      Returns:
      true if successful
    • setProperty

      public boolean setProperty(String aPropertySet, String aKey, String aValue, boolean aNewValue)
      Set the value for a specified key within a specified property set and make those settings persistent, so the next time the application is started the set value is still available. Line breaks are stored as \n. Unless aNewValue is set, the key must exist in the property set. If the key does not exist the method will return false like in any other case of an error.
      Parameters:
      aPropertySet - name of the property set
      aKey - the property key
      aValue - the new value; null removes the property
      aNewValue - true to allow adding a new key
      Returns:
      true if successful
    • getPropertyContainer

      public FWPropertyContainer getPropertyContainer(String aPropertySet)
      Return the complete Properties object. Can cause data corruption in property resource manager, so better use getPropertyNames(...) to get an enumeration of property names and then iterate over this enumeration and invoke getProperty(...) for each name.
      Parameters:
      aPropertySet - name of the property set
      Returns:
      the property container for this property set (loaded on first access), or null if it cannot be found or read
    • getSystemPropertyContainer

      public FWPropertyContainer getSystemPropertyContainer()
      Return the complete Properties object. Can cause data corruption in property resource manager, so better use getPropertyNames(...) to get an enumeration of property names and then iterate over this enumeration and invoke getProperty(...) for each name.
      Returns:
      the property container of the application (named after the locale prefix of the layout, or LAPClient without GUI)
    • getPropertyNames

      public Set<String> getPropertyNames(String aPropertySet)
      Return the property names contained in the specified property set. Return null if the property set cant be found.
      Parameters:
      aPropertySet - name of the property set
      Returns:
      the property keys (the live key set)
    • getPropertySetURL

      public URL getPropertySetURL(String aPropertySet)
      Get the URL the specified property set has been loaded from. Return null if the property set can't be found.
      Parameters:
      aPropertySet - name of the property set
      Returns:
      URL of the property set
    • getEnvVars

      public FWPropertyContainer getEnvVars()
      Returns the environment-variables.
      Returns:
      the property container of the EnvVars
    • replaceSystemVariables

      public String replaceSystemVariables(String aPropertyValue)
      Replaces one system variable in a property value: %{registry key} (registry), %NAME% (environment), ${name} (Java runtime) or $NAME$ (environment). Only the first kind found is replaced.
      Parameters:
      aPropertyValue - the property value
      Returns:
      the value with the variable replaced, or the unchanged value on errors (logged)
    • getPropertiesAsString

      public String getPropertiesAsString(String aPropertyset)
      Returns a property set as XML text.
      Parameters:
      aPropertyset - name of the property set
      Returns:
      the XML text, or an empty string on errors (logged)
    • savePropertyContainer

      public void savePropertyContainer(FWPropertyContainer aPropertiesContainer)
      Saves a property set as name_config.xml in the cache directory (which locatePropertySet(String) prefers when loading) and keeps it in memory. The original file in the class path (application directory or jar) is never overwritten. Errors are logged.
      Parameters:
      aPropertiesContainer - the property container