Class Configuration

    • Constructor Detail

      • Configuration

        public Configuration()
        A new configuration.
      • Configuration

        public Configuration​(boolean loadDefaults)
        A new configuration where the behavior of reading from the default resources can be turned off. If the parameter loadDefaults is false, the new instance will not load resources from the default files.
        Parameters:
        loadDefaults - specifies whether to load from the default files
      • Configuration

        public Configuration​(Configuration other)
        A new configuration with the same settings cloned from another.
        Parameters:
        other - the configuration from which to clone settings.
    • Method Detail

      • addDefaultResource

        public static void addDefaultResource​(String name)
        Add a default resource. Resources are loaded in the order of the resources added.
        Parameters:
        name - file name. File should be present in the classpath.
      • addResource

        public void addResource​(String name)
        Add a configuration resource. The properties of this resource will override properties of previously added resources, unless they were marked final.
        Parameters:
        name - resource to be added, the classpath is examined for a file with that name.
      • addResource

        public void addResource​(URL url)
        Add a configuration resource. The properties of this resource will override properties of previously added resources, unless they were marked final.
        Parameters:
        url - url of the resource to be added, the local filesystem is examined directly to find the resource, without referring to the classpath.
      • addResource

        public void addResource​(InputStream in)
        Add a configuration resource. The properties of this resource will override properties of previously added resources, unless they were marked final.
        Parameters:
        in - InputStream to deserialize the object from.
      • reloadConfiguration

        public void reloadConfiguration()
        Reload configuration from previously added resources. This method will clear all the configuration read from the added resources, and final parameters. This will make the resources to be read again before accessing the values. Values that are added via set methods will overlay values read from the resources.
      • get

        public String get​(String name)
        Get the value of the name property, null if no such property exists. Values are processed for variable expansion before being returned.
        Parameters:
        name - the property name.
        Returns:
        the value of the name property, or null if no such property exists.
      • getRaw

        public String getRaw​(String name)
        Get the value of the name property, without doing variable expansion.
        Parameters:
        name - the property name.
        Returns:
        the value of the name property, or null if no such property exists.
      • getChar

        public char getChar​(String name)
        Get the char value of the name property, null if no such property exists. Values are processed for variable expansion before being returned.
        Parameters:
        name - the property name.
        Returns:
        the value of the name property, or null if no such property exists.
      • getChar

        public char getChar​(String name,
                            char defaultValue)
        Get the char value of the name property, null if no such property exists. Values are processed for variable expansion before being returned.
        Parameters:
        name - the property name.
        Returns:
        the value of the name property, or null if no such property exists.
      • set

        public void set​(String name,
                        String value)
        Set the value of the name property.
        Parameters:
        name - property name.
        value - property value.
      • setIfUnset

        public void setIfUnset​(String name,
                               String value)
        Sets a property if it is currently unset.
        Parameters:
        name - the property name
        value - the new value
      • get

        public String get​(String name,
                          String defaultValue)
        Get the value of the name property. If no such property exists, then defaultValue is returned.
        Parameters:
        name - property name.
        defaultValue - default value.
        Returns:
        property value, or defaultValue if the property doesn't exist.
      • getInt

        public int getInt​(String name,
                          int defaultValue)
        Get the value of the name property as an int. If no such property exists, or if the specified value is not a valid int, then defaultValue is returned.
        Parameters:
        name - property name.
        defaultValue - default value.
        Returns:
        property value as an int, or defaultValue.
      • setInt

        public void setInt​(String name,
                           int value)
        Set the value of the name property to an int.
        Parameters:
        name - property name.
        value - int value of the property.
      • getLong

        public long getLong​(String name,
                            long defaultValue)
        Get the value of the name property as a long. If no such property is specified, or if the specified value is not a valid long, then defaultValue is returned.
        Parameters:
        name - property name.
        defaultValue - default value.
        Returns:
        property value as a long, or defaultValue.
      • setLong

        public void setLong​(String name,
                            long value)
        Set the value of the name property to a long.
        Parameters:
        name - property name.
        value - long value of the property.
      • getFloat

        public float getFloat​(String name,
                              float defaultValue)
        Get the value of the name property as a float. If no such property is specified, or if the specified value is not a valid float, then defaultValue is returned.
        Parameters:
        name - property name.
        defaultValue - default value.
        Returns:
        property value as a float, or defaultValue.
      • setFloat

        public void setFloat​(String name,
                             float value)
        Set the value of the name property to a float.
        Parameters:
        name - property name.
        value - property value.
      • getBoolean

        public boolean getBoolean​(String name,
                                  boolean defaultValue)
        Get the value of the name property as a boolean. If no such property is specified, or if the specified value is not a valid boolean, then defaultValue is returned.
        Parameters:
        name - property name.
        defaultValue - default value.
        Returns:
        property value as a boolean, or defaultValue.
      • setBoolean

        public void setBoolean​(String name,
                               boolean value)
        Set the value of the name property to a boolean.
        Parameters:
        name - property name.
        value - boolean value of the property.
      • setBooleanIfUnset

        public void setBooleanIfUnset​(String name,
                                      boolean value)
        Set the given property, if it is currently unset.
        Parameters:
        name - property name
        value - new value
      • getPattern

        public Pattern getPattern​(String name,
                                  Pattern defaultValue)
        Get the value of the name property as a Pattern. If no such property is specified, or if the specified value is not a valid Pattern, then DefaultValue is returned.
        Parameters:
        name - property name
        defaultValue - default value
        Returns:
        property value as a compiled Pattern, or defaultValue
      • setPattern

        public void setPattern​(String name,
                               Pattern pattern)
        Set the given property to Pattern. If the pattern is passed as null, sets the empty pattern which results in further calls to getPattern(...) returning the default value.
        Parameters:
        name - property name
        pattern - new value
      • write

        public void write​(DataOutput out)
                   throws IOException
        Description copied from interface: Writable
        Serialize the fields of this object to out.
        Specified by:
        write in interface Writable
        Parameters:
        out - DataOuput to serialize this object into.
        Throws:
        IOException
      • readFields

        public void readFields​(DataInput in)
                        throws IOException
        Description copied from interface: Writable
        Deserialize the fields of this object from in.

        For efficiency, implementations should attempt to re-use storage in the existing object where possible.

        Specified by:
        readFields in interface Writable
        Parameters:
        in - DataInput to deseriablize this object from.
        Throws:
        IOException
      • getRange

        public Configuration.IntegerRanges getRange​(String name,
                                                    String defaultValue)
        Parse the given attribute as a set of integer ranges
        Parameters:
        name - the attribute name
        defaultValue - the default value if it is not set
        Returns:
        a new set of ranges from the configured value
      • getStringCollection

        public Collection<String> getStringCollection​(String name)
        Get the comma delimited values of the name property as a collection of Strings. If no such property is specified then empty collection is returned.

        This is an optimized version of getStrings(String)

        Parameters:
        name - property name.
        Returns:
        property value as a collection of Strings.
      • getStrings

        public String[] getStrings​(String name)
        Get the comma delimited values of the name property as an array of Strings. If no such property is specified then null is returned.
        Parameters:
        name - property name.
        Returns:
        property value as an array of Strings, or null.
      • getStrings

        public String[] getStrings​(String name,
                                   String... defaultValue)
        Get the comma delimited values of the name property as an array of Strings. If no such property is specified then default value is returned.
        Parameters:
        name - property name.
        defaultValue - The default value
        Returns:
        property value as an array of Strings, or default value.
      • getTrimmedStringCollection

        public Collection<String> getTrimmedStringCollection​(String name)
        Get the comma delimited values of the name property as a collection of Strings, trimmed of the leading and trailing whitespace. If no such property is specified then empty Collection is returned.
        Parameters:
        name - property name.
        Returns:
        property value as a collection of Strings, or empty Collection
      • getTrimmedStrings

        public String[] getTrimmedStrings​(String name)
        Get the comma delimited values of the name property as an array of Strings, trimmed of the leading and trailing whitespace. If no such property is specified then an empty array is returned.
        Parameters:
        name - property name.
        Returns:
        property value as an array of trimmed Strings, or empty array.
      • getTrimmedStrings

        public String[] getTrimmedStrings​(String name,
                                          String... defaultValue)
        Get the comma delimited values of the name property as an array of Strings, trimmed of the leading and trailing whitespace. If no such property is specified then default value is returned.
        Parameters:
        name - property name.
        defaultValue - The default value
        Returns:
        property value as an array of trimmed Strings, or default value.
      • setStrings

        public void setStrings​(String name,
                               String... values)
        Set the array of string values for the name property as as comma delimited values.
        Parameters:
        name - property name.
        values - The values
      • getClasses

        public Class<?>[] getClasses​(String name,
                                     Class<?>... defaultValue)
        Get the value of the name property as an array of Class. The value of the property specifies a list of comma separated class names. If no such property is specified, then defaultValue is returned.
        Parameters:
        name - the property name.
        defaultValue - default value.
        Returns:
        property value as a Class[], or defaultValue.
      • getClass

        public Class<?> getClass​(String name,
                                 Class<?> defaultValue)
        Get the value of the name property as a Class. If no such property is specified, then defaultValue is returned.
        Parameters:
        name - the class name.
        defaultValue - default value.
        Returns:
        property value as a Class, or defaultValue.
      • getClass

        public <U> Class<? extends U> getClass​(String name,
                                               Class<? extends U> defaultValue,
                                               Class<U> xface)
        Get the value of the name property as a Class implementing the interface specified by xface. If no such property is specified, then defaultValue is returned. An exception is thrown if the returned class does not implement the named interface.
        Parameters:
        name - the class name.
        defaultValue - default value.
        xface - the interface implemented by the named class.
        Returns:
        property value as a Class, or defaultValue.
      • getInstances

        public <U> List<U> getInstances​(String name,
                                        Class<U> xface)
        Get the value of the name property as a List of objects implementing the interface specified by xface. An exception is thrown if any of the classes does not exist, or if it does not implement the named interface.
        Parameters:
        name - the property name.
        xface - the interface implemented by the classes named by name.
        Returns:
        a List of objects implementing xface.
      • setClass

        public void setClass​(String name,
                             Class<?> theClass,
                             Class<?> xface)
        Set the value of the name property to the name of a theClass implementing the given interface xface. An exception is thrown if theClass does not implement the interface xface.
        Parameters:
        name - property name.
        theClass - property value.
        xface - the interface implemented by the named class.
      • getFile

        public File getFile​(String dirsProp,
                            String path)
                     throws IOException
        Get a local file name under a directory named in dirsProp with the given path. If dirsProp contains multiple directories, then one is chosen based on path's hash code. If the selected directory does not exist, an attempt is made to create it.
        Parameters:
        dirsProp - directory in which to locate the file.
        path - file-path.
        Returns:
        local file under the directory with the given path.
        Throws:
        IOException
      • getResource

        public URL getResource​(String name)
        Get the URL for the named resource.
        Parameters:
        name - resource name.
        Returns:
        the url for the named resource.
      • getConfResourceAsInputStream

        public InputStream getConfResourceAsInputStream​(String name)
        Get an input stream attached to the configuration resource with the given name.
        Parameters:
        name - configuration resource name.
        Returns:
        an input stream attached to the resource.
      • getConfResourceAsReader

        public Reader getConfResourceAsReader​(String name)
        Get a Reader attached to the configuration resource with the given name.
        Parameters:
        name - configuration resource name.
        Returns:
        a reader attached to the resource.
      • size

        public int size()
        Return the number of keys in the configuration.
        Returns:
        number of keys in the configuration.
      • clear

        public void clear()
        Clears all keys from the configuration.
      • dumpConfiguration

        public static void dumpConfiguration​(Configuration conf,
                                             Writer out)
                                      throws IOException
        Writes out all the parameters and their properties (final and resource) to the given Writer The format of the output would be { "properties" : [ {key1,value1,key1.isFinal,key1.resource}, {key2,value2, key2.isFinal,key2.resource}... ] } It does not output the parameters of the configuration object which is loaded from an input stream.
        Parameters:
        out - the Writer to write to
        Throws:
        IOException
      • getClassLoader

        public ClassLoader getClassLoader()
        Get the ClassLoader for this job.
        Returns:
        the correct class loader.
      • setClassLoader

        public void setClassLoader​(ClassLoader classLoader)
        Set the class loader that will be used to load the various objects.
        Parameters:
        classLoader - the new class loader.
      • setQuietMode

        public void setQuietMode​(boolean quietmode)
        Set the quietness-mode. In the quiet-mode, error and informational messages might not be logged.
        Parameters:
        quietmode - true to set quiet-mode on, false to turn it off.
      • main

        public static void main​(String[] args)
                         throws Exception
        For debugging. List non-default properties to the terminal and exit.
        Throws:
        Exception
      • toDouble

        public double toDouble()
        Description copied from interface: Writable
        Convert Writable to double. Whether this is supported depends on the specific writable.
        Specified by:
        toDouble in interface Writable
      • toFloat

        public float toFloat()
        Description copied from interface: Writable
        Convert Writable to float. Whether this is supported depends on the specific writable.
        Specified by:
        toFloat in interface Writable
      • toInt

        public int toInt()
        Description copied from interface: Writable
        Convert Writable to int. Whether this is supported depends on the specific writable.
        Specified by:
        toInt in interface Writable
      • toLong

        public long toLong()
        Description copied from interface: Writable
        Convert Writable to long. Whether this is supported depends on the specific writable.
        Specified by:
        toLong in interface Writable