Java Properties Get getProperty(String[] system_props, Properties props, String prop_name, boolean ignore_sysprops, String default_value)

Here you can find the source of getProperty(String[] system_props, Properties props, String prop_name, boolean ignore_sysprops, String default_value)

Description

Returns a value associated wither with one or more system properties, or found in the props map

License

LGPL

Parameter

Parameter Description
system_props a parameter
props List of properties read from the configuration file
prop_name The name of the property, will be removed from props if found
ignore_sysprops If true, system properties are not used and the values will only be retrieved from props (not system_props)
default_value Used to return a default value if the properties or system properties didn't have the value

Return

The value, or null if not found

Declaration

public static String getProperty(String[] system_props, Properties props, String prop_name,
        boolean ignore_sysprops, String default_value) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.*;

public class Main {
    /**// www  . j  av a 2s.  c  om
     * Returns a value associated wither with one or more system properties, or found in the props map
     * @param system_props
     * @param props List of properties read from the configuration file
     * @param prop_name The name of the property, will be removed from props if found
     * @param ignore_sysprops If true, system properties are not used and the values will only be retrieved from
     * props (not system_props)
     * @param default_value Used to return a default value if the properties or system properties didn't have the value
     * @return The value, or null if not found
     */
    public static String getProperty(String[] system_props, Properties props, String prop_name,
            boolean ignore_sysprops, String default_value) {
        String retval = null;
        if (props != null && prop_name != null) {
            retval = props.getProperty(prop_name);
            props.remove(prop_name);
        }

        if (!ignore_sysprops) {
            String tmp, prop;
            if (system_props != null) {
                for (int i = 0; i < system_props.length; i++) {
                    prop = system_props[i];
                    if (prop != null) {
                        try {
                            tmp = System.getProperty(prop);
                            if (tmp != null)
                                return tmp; // system properties override config file definitions
                        } catch (SecurityException ex) {
                        }
                    }
                }
            }
        }
        if (retval == null)
            return default_value;
        return retval;
    }

    public static String getProperty(String s) {
        String var, default_val, retval = null;
        int index = s.indexOf(":");
        if (index >= 0) {
            var = s.substring(0, index);
            default_val = s.substring(index + 1);
            if (default_val != null && default_val.length() > 0)
                default_val = default_val.trim();
            // retval=System.getProperty(var, default_val);
            retval = _getProperty(var, default_val);
        } else {
            var = s;
            // retval=System.getProperty(var);
            retval = _getProperty(var, null);
        }
        return retval;
    }

    /**
     * Parses a var which might be comma delimited, e.g. bla,foo:1000: if 'bla' is set, return its value. Else,
     * if 'foo' is set, return its value, else return "1000"
     * @param var
     * @param default_value
     * @return
     */
    private static String _getProperty(String var, String default_value) {
        if (var == null)
            return null;
        List<String> list = parseCommaDelimitedStrings(var);
        if (list == null || list.isEmpty()) {
            list = new ArrayList<String>(1);
            list.add(var);
        }
        String retval = null;
        for (String prop : list) {
            try {
                retval = System.getProperty(prop);
                if (retval != null)
                    return retval;
            } catch (Throwable e) {
            }
        }
        return default_value;
    }

    /** e.g. "bela,jeannette,michelle" --> List{"bela", "jeannette", "michelle"} */
    public static List<String> parseCommaDelimitedStrings(String l) {
        return parseStringList(l, ",");
    }

    public static List<String> parseStringList(String l, String separator) {
        List<String> tmp = new LinkedList<String>();
        StringTokenizer tok = new StringTokenizer(l, separator);
        String t;

        while (tok.hasMoreTokens()) {
            t = tok.nextToken();
            tmp.add(t.trim());
        }

        return tmp;
    }
}

Related

  1. getProperty(Properties properties, String context, String key, String def)
  2. getProperty(Properties props, String keyword)
  3. getProperty(Properties props, String prefix, String key)
  4. getProperty(Properties props, String propertyName)
  5. getProperty(Properties props, String propname)
  6. getPropertyDouble(Properties p, String key)
  7. getPropertyLong(Properties properties, String context, String key, Long def)
  8. getPropertyString(Properties properties, String propertyName)
  9. getPropertyText(String property, Object... replacements)