All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.poshi.runner.util.PropsUtil Maven / Gradle / Ivy

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library 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 Lesser General Public License for more
 * details.
 */

package com.liferay.poshi.runner.util;

import java.io.InputStream;

import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

/**
 * @author Brian Wing Shun Chan
 */
public class PropsUtil {

	public static String get(String key) {
		return _propsUtil._get(key);
	}

	public static Properties getProperties() {
		return _propsUtil._props;
	}

	public static void set(String key, String value) {
		_propsUtil._set(key, value);
	}

	private PropsUtil() {
		try {
			String[] propertiesFileNames = {
				"poshi-runner.properties", "poshi-runner-ext.properties"
			};

			for (String propertiesFileName : propertiesFileNames) {
				Class clazz = getClass();

				ClassLoader classLoader = clazz.getClassLoader();

				InputStream inputStream = classLoader.getResourceAsStream(
					propertiesFileName);

				if (inputStream != null) {
					_props.load(inputStream);
				}
			}

			_printProperties(false);
		}
		catch (Exception exception) {
			exception.printStackTrace();
		}
	}

	private String _get(String key) {
		String value = System.getProperty(key);

		if (Validator.isNull(value)) {
			value = _props.getProperty(key);
		}

		return value;
	}

	private void _printProperties(boolean update) {
		List keys = Collections.list(
			(Enumeration)_props.propertyNames());

		keys = ListUtil.sort(keys);

		if (update) {
			System.out.println("-- updated properties --");
		}
		else {
			System.out.println("-- listing properties --");
		}

		for (String key : keys) {
			System.out.println(key + "=" + _get(key));
		}

		System.out.println("");
	}

	private void _set(String key, String value) {
		_props.setProperty(key, value);
	}

	private static final PropsUtil _propsUtil = new PropsUtil();

	private final Properties _props = new Properties();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy