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

br.com.jhonsapp.util.loader.PropertiesLoader Maven / Gradle / Ivy

Go to download

This project provides useful classes that facilitate the construction of new components.

There is a newer version: 1.0.8
Show newest version
package br.com.jhonsapp.util.loader;

import java.io.IOException;
import java.io.Serializable;
import java.nio.file.FileSystemNotFoundException;
import java.util.Properties;

import br.com.jhonsapp.util.verifier.StringVerifier;

/**
 * This class allow to load properties from file properties.
 * 
 * @author Jhonathan Camacho
 *
 */
//@RequestScoped
public class PropertiesLoader implements Serializable {

	/**
	 * Generated serial version id created at 08 July 2017.
	 */
	private static final long serialVersionUID = 502401706618110190L;

	/***
	 * Verify if the property file exists in the project.
	 * 
	 * @param propertiesName
	 *            is the property file name.
	 * @return true if the properties file exists and false otherwise.
	 */
	public boolean hasPropertiesFile(String propertiesName) {

		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		return loader.getResourceAsStream(propertiesName) != null;
	}

	/***
	 * Load a specific property file into memory, if it
	 * exists.
	 * 
	 * @param propertiesName
	 *            is the property file name.
	 * @return the properties file if exists and FileSystemNotFoundException if
	 *         not.
	 */
	public Properties getPropertiesFile(String propertiesName) {

		Properties properties = null;

		try {
			properties = new Properties();
			ClassLoader loader = Thread.currentThread().getContextClassLoader();

			properties.load(loader.getResourceAsStream(propertiesName));
		} catch (IOException e) {
			throw new FileSystemNotFoundException("Property file '" + propertiesName + "' not found in the classpath.");
		}

		return properties;
	}

	/***
	 * Searchs a specific property in a propertie file, if
	 * it exists.
	 * 
	 * @param properties
	 *            the properties file.
	 * @param propertieKey
	 *            the name of the specific property in a properties file.
	 * @return the value of the specific property.
	 */
	public String getProperty(Properties properties, String propertieKey) {

		verifyIfKeyExists(properties, propertieKey);

		return properties.getProperty(propertieKey);
	}

	private boolean verifyIfKeyExists(Properties properties, String propertieKey) {

		if (StringVerifier.isBlanck(propertieKey))
			throw new IllegalArgumentException("The key '" + propertieKey + "' is blanck.");

		if (!properties.containsKey(propertieKey))
			throw new IllegalArgumentException("The key '" + propertieKey + "' doesn't exist.");

		return true;
	}

	/***
	 * Verify if a specific property has a specific value.
	 * 
	 * @param properties
	 *            the properties file.
	 * @param propertieValue
	 *            the value of the propertie.
	 * @return the true if value exists and IllegalArgumentException if not.
	 */
	public boolean verifyIfValueExists(Properties properties, String propertieValue) {

		if (StringVerifier.isBlanck(getProperty(properties, propertieValue)))
			throw new IllegalArgumentException("The value '" + propertieValue + "' doesn't exist.");

		return true;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy