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

org.jlot.client.configuration.UserProperties Maven / Gradle / Ivy

There is a newer version: 1.2
Show newest version
package org.jlot.client.configuration;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

import javax.inject.Inject;

import org.springframework.stereotype.Component;

@Component
public class UserProperties
{
	private Properties			properties;

	@Inject
	private UserHomeDirectory	userHomeDirectory;

	public String get ( String key )
	{
		return (String) getUserProperties().get(key);
	}

	public void put ( String key, String value )
	{
		getUserProperties().put(key, value);
		save();
	}

	private Properties getUserProperties ( )
	{
		if (properties == null)
		{
			properties = load();
		}
		return properties;
	}

	public Properties load ( )
	{
		Properties properties = new Properties();
		File file = userHomeDirectory.getConfigFile();
		if (file.isFile() && file.canRead())
		{
			try
			{
				properties.load(new FileReader(file));
			}
			catch (FileNotFoundException e)
			{
				throw new IllegalStateException(e);
			}
			catch (IOException e)
			{
				throw new IllegalStateException(e);
			}
		}
		return properties;
	}

	public void save ( )
	{
		File file = userHomeDirectory.getConfigFile();
		try
		{
			properties.store(new FileWriter(file), "jlot user config");
		}
		catch (IOException e)
		{
			throw new IllegalStateException(e);
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy