Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.*;

public class Main {
    /**
     * 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 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,
            String default_value) {
        String retval = null;
        if (props != null && prop_name != null) {
            retval = props.getProperty(prop_name);
            props.remove(prop_name);
        }

        String tmp, prop;
        if (retval == null && 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;
                    } 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.isEmpty())
                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;
    }
}