Class Configurations


  • public class Configurations
    extends Object
    Utility class for getting configurations and things in them. Generally every call is done anew, so anything you want to keep you should store.

    Created by Jeff Gaynor
    on 1/19/12 at 11:20 AM

    • Constructor Summary

      Constructors 
      Constructor Description
      Configurations()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Collection<LocalTime> getAlarms​(org.apache.commons.configuration.tree.ConfigurationNode node, String tag)
      Get alarms that are in a given tag.
      static org.apache.commons.configuration.tree.ConfigurationNode getConfig​(org.apache.commons.configuration.XMLConfiguration cfg, String topNodeName, String configName)
      Loads the named configuration that resides under the topNodeName, e.g.
      protected static org.apache.commons.configuration.tree.ConfigurationNode getConfig​(org.apache.commons.configuration.XMLConfiguration cfg, String topNodeName, String configName, TreeSet<String> checkedAliases)  
      static org.apache.commons.configuration.XMLConfiguration getConfiguration​(File file)
      Loads the configuration from the given file.
      static org.apache.commons.configuration.XMLConfiguration getConfiguration​(URL resourceName)
      Gets the configuration via the resource name.
      static String getFirstAttribute​(org.apache.commons.configuration.tree.ConfigurationNode node, String name)
      Get the very first attribute with the given name from the configuration node.
      static boolean getFirstBooleanValue​(org.apache.commons.configuration.tree.ConfigurationNode node, String attrib, boolean defaultValue)  
      static long getFirstLongValue​(org.apache.commons.configuration.tree.ConfigurationNode node, String attrib, long defaultValue)
      Return the first long value of an attribute.
      static org.apache.commons.configuration.tree.ConfigurationNode getFirstNode​(org.apache.commons.configuration.tree.ConfigurationNode node, String name)
      Return the first named child configuration node of the given node or null if there is no such named child.
      static String getNodeValue​(org.apache.commons.configuration.tree.ConfigurationNode node, String name)
      Convenience method for getting the value of a single node, i.e.
      static String getNodeValue​(org.apache.commons.configuration.tree.ConfigurationNode node, String name, String defaultValue)
      Return the default value if none is found.
      static void killLog4J()
      Useful utility call.
      static org.apache.commons.configuration.XMLConfiguration resolveFileReferences​(File file)
      This will look for file references to include in the current XML configuration and load them all into a single XMLConfiguration
      protected static org.apache.commons.configuration.XMLConfiguration resolveFileReferences​(org.apache.commons.configuration.XMLConfiguration cfg, TreeSet<String> visitedFiles)
      All the actual work for loading files is done here.
    • Constructor Detail

      • Configurations

        public Configurations()
    • Method Detail

      • killLog4J

        public static void killLog4J()
        Useful utility call. This explicitly kills off logging from log 4 java. This is needed since some dependency (don't know which) has a misconfigured log4j properties file in its library which spews forth random messages. Invoke this at server start to definitely kill off any log4j activity. It is annoying to have to add a dependency to clean up after someone else!
      • getConfiguration

        public static org.apache.commons.configuration.XMLConfiguration getConfiguration​(File file)
        Loads the configuration from the given file. Various checks are done to give information about failures.
        Parameters:
        file -
        Returns:
      • resolveFileReferences

        public static org.apache.commons.configuration.XMLConfiguration resolveFileReferences​(File file)
        This will look for file references to include in the current XML configuration and load them all into a single XMLConfiguration
        Parameters:
        file -
        Returns:
      • resolveFileReferences

        protected static org.apache.commons.configuration.XMLConfiguration resolveFileReferences​(org.apache.commons.configuration.XMLConfiguration cfg,
                                                                                                 TreeSet<String> visitedFiles)
                                                                                          throws org.apache.commons.configuration.ConfigurationException
        All the actual work for loading files is done here.
        Parameters:
        cfg -
        visitedFiles -
        Returns:
        Throws:
        org.apache.commons.configuration.ConfigurationException
      • getConfiguration

        public static org.apache.commons.configuration.XMLConfiguration getConfiguration​(URL resourceName)
        Gets the configuration via the resource name.
        Parameters:
        resourceName -
        Returns:
      • getNodeValue

        public static String getNodeValue​(org.apache.commons.configuration.tree.ConfigurationNode node,
                                          String name)
        Convenience method for getting the value of a single node, i.e. the contents, so

        <foo>value</foo>

        would have name equal to 'foo' and return the string 'value'.
        Parameters:
        node -
        name -
        Returns:
      • getFirstNode

        public static org.apache.commons.configuration.tree.ConfigurationNode getFirstNode​(org.apache.commons.configuration.tree.ConfigurationNode node,
                                                                                           String name)
        Return the first named child configuration node of the given node or null if there is no such named child. Very useful if your specification only allows for a single child node.
        Parameters:
        node -
        name -
        Returns:
      • getFirstAttribute

        public static String getFirstAttribute​(org.apache.commons.configuration.tree.ConfigurationNode node,
                                               String name)
        Get the very first attribute with the given name from the configuration node. A null is returned if there is no such value.
        Parameters:
        node -
        name -
        Returns:
      • getFirstBooleanValue

        public static boolean getFirstBooleanValue​(org.apache.commons.configuration.tree.ConfigurationNode node,
                                                   String attrib,
                                                   boolean defaultValue)
      • getFirstLongValue

        public static long getFirstLongValue​(org.apache.commons.configuration.tree.ConfigurationNode node,
                                             String attrib,
                                             long defaultValue)
        Return the first long value of an attribute. Must have the default value specified. There is no promise as to what "first value" means thanks to the XML spec., so effectively you should restrict your XML to have at most one such value.
        Parameters:
        node -
        attrib -
        defaultValue -
        Returns:
      • getNodeValue

        public static String getNodeValue​(org.apache.commons.configuration.tree.ConfigurationNode node,
                                          String name,
                                          String defaultValue)
        Return the default value if none is found.
        Parameters:
        node -
        name -
        defaultValue -
        Returns:
      • getConfig

        public static org.apache.commons.configuration.tree.ConfigurationNode getConfig​(org.apache.commons.configuration.XMLConfiguration cfg,
                                                                                        String topNodeName,
                                                                                        String configName)
        Loads the named configuration that resides under the topNodeName, e.g. topNodeName might be 'config' and configName might be 'my-server'. This supports aliases too, so if the top level tag contains an alias="x" attribute, then the configuration named "x" is loaded instead. This allows for things like a "default" or "current" configuration which need never change once deployed. The aim is to make this all as configuration driven as possible.
        Parameters:
        cfg -
        topNodeName -
        configName -
        Returns:
      • getConfig

        protected static org.apache.commons.configuration.tree.ConfigurationNode getConfig​(org.apache.commons.configuration.XMLConfiguration cfg,
                                                                                           String topNodeName,
                                                                                           String configName,
                                                                                           TreeSet<String> checkedAliases)
        Parameters:
        cfg -
        topNodeName - The tag inside the outermost tag you want, e.g. "service", "client"
        configName - The name (i.e. value of name attributes)
        checkedAliases - Any aliases encountered so far.
        Returns:
      • getAlarms

        public static Collection<LocalTime> getAlarms​(org.apache.commons.configuration.tree.ConfigurationNode node,
                                                      String tag)
        Get alarms that are in a given tag. returns null if no alarms are set
        Parameters:
        node -
        tag -
        Returns: