Java Properties Load from File loadEnv(final String configFilePath)

Here you can find the source of loadEnv(final String configFilePath)

Description

initialize environment using the incoming properties file (or defaults).

License

Open Source License

Parameter

Parameter Description
configFilePath the String representing the absolute file path of the jmx.properties or null (in this case defaults values are used)

Return

a map containing the initialized environment

Declaration

public static Map<String, String> loadEnv(final String configFilePath) 

Method Source Code


//package com.java2s;
/*//  w w  w . ja  v a  2s . c  om
 *  GeoBatch - Open Source geospatial batch processing system
 *  http://geobatch.geo-solutions.it/
 *  Copyright (C) 2007-2012 GeoSolutions S.A.S.
 *  http://www.geo-solutions.it
 *
 *  GPLv3 + Classpath exception
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Main {
    public final static String GB_JMXBEAN_KEY = "JMXServiceManager";
    public final static String GB_JMXBEAN_NAME = "JMXServiceManager";
    /**
     * connections parameters
     */
    public final static String GB_URL_KEY = "gb_jmx_url";
    public final static String GB_URL = "localhost";
    public final static String GB_PORT_KEY = "gb_jmx_port";
    public final static String GB_PORT = "1099";

    /**
     * initialize environment using the incoming properties file (or defaults). The properties file used to configure connection parameters may
     * contains at least: <br>
     * <ul>
     * <li><b>gb_jmx_url=localhost</b> - remote JMX server url</li>
     * <li><b>gb_jmx_port=1099</b> - remote JMX server port</li>
     * <li><b>JMXActionManager=JMXActionManager</b> - bean name which implements ActionManager interface</li>
     * </ul>
     * NOTE: above keywords are reserved keys<br>
     * 
     * @param configFilePath the String representing the absolute file path of the jmx.properties or null (in this case defaults values are used)
     * @return a map containing the initialized environment
     */
    public static Map<String, String> loadEnv(final String configFilePath) {
        // load from file
        final Properties props = new Properties();
        // if no external file configuration is found using defaults
        if (configFilePath != null) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(new File(configFilePath));
                props.load(fis);

            } catch (IOException e) {

                // if (LOGGER.isEnabledFor(Level.ERROR)){
                // LOGGER.error("Unable to run without a property file, check the path: "+path);
                // }

            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (Exception e) {
                    }
                }
            }
        }

        // return env
        final Map<String, String> env = new HashMap<String, String>();

        // init defaults
        // url
        if (!props.containsKey(GB_URL_KEY))
            env.put(GB_URL_KEY, GB_URL);
        else
            env.put(GB_URL_KEY, props.getProperty(GB_URL_KEY));

        // port
        if (!props.containsKey(GB_PORT_KEY))
            env.put(GB_PORT_KEY, GB_PORT);
        else
            env.put(GB_PORT_KEY, props.getProperty(GB_PORT_KEY));

        if (!props.containsKey(GB_JMXBEAN_KEY))
            env.put(GB_JMXBEAN_KEY, GB_JMXBEAN_NAME);
        else
            env.put(GB_JMXBEAN_KEY, props.getProperty(GB_JMXBEAN_KEY));

        // form properties env to map env
        final Set keySet = props.keySet();
        final Iterator it = keySet.iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            env.put(key, props.getProperty(key));
        }
        return env;
    }
}

Related

  1. loadDBProperties(String dbConn)
  2. loadDefault(String _file_path)
  3. loadDefaultConfiguration()
  4. loadDefaultProps(Properties deployProps)
  5. LoadDeployedObjects(String outputDirectory)
  6. loadErrorMap(InputStream resourceStream)
  7. loadEscaping(Properties prop, InputStream stream)
  8. loadFixedCommits()
  9. loadFixture(String fixtureId)