Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Creative commons Attribution-NonCommercial license. http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
  
  package base.jee.api;
  
 import base.KeyValue;
 
 import java.util.Date;
 import java.util.Map;

Standard mechanism for getting and setting configuration settings.
 
 public class Settings {

Cached version of the configuration settings
 
 	private Map<StringStringsetting = new Hashtable<>();
 
 	private API api;

Indicates at what time the cached setting data will expire
 
 	private long expires = (new Date()).getTime()-60*1000;
 
 	public Settings(API apithrows IOException {
 		if(api == null) {
 			throw new IllegalArgumentException("Valid data source parameter is required.");
 		}
 
 		this. = api;
 	}

Retrieve a configuration setting. First checks if the configuration setting is cached in memory, otherwise the configuration setting is loaded from the database.

Parameters:
name
Returns:
Throws:
java.io.IOException Throws an IOException when there is a problem with the database or if the name parameter does not correspond to a valid database entry
 
 	public String get(String namethrows IOException {
 		if( < (new Date()).getTime()) {
 		}
 		String value = .get(name);
 		if(value == null) {
 			throw new IOException("Required configuration parameter not specified: " + name);
 		}
 		return value;
 	}

Retrieve a configuration setting. First checks if the configuration setting is cached in memory, otherwise the configuration setting is loaded from the database.

Parameters:
name
Returns:
Throws:
java.io.IOException
 
 	public String get(String nameString defaultValuethrows IOException {
 		if( < (new Date()).getTime()) {
 		}
 		String value = .get(name);
 		if(value != null) {
 			return value;
 		}
 		return defaultValue;
 	}

Returns a map of all the current setting key value pairs. The settings returned by this method are cached and hence at times may be inconsistent with the database.

Returns:
All current settings.
Throws:
java.io.IOException
 
 	public Map<StringStringgetAll() throws IOException {
 		Map<StringStringall = new Hashtable<>();
 		if( < (new Date()).getTime()) {
 		}
 
 		while(i.hasNext()) {
 			String key = (String)i.next();
 			all.put(key.get(key));
 		}
 
		return all;
	}

Force clear the cache of setting information to ensure the next read of a setting returns the most up to date settings.

	public void refresh() throws IOException {
		 = 0;
	}

Update a configuration setting in memory and in the database itself. If there is a problem writing to the database, the value for this setting will not be changed in memory or in the database.

Parameters:
name
value
Throws:
java.io.IOException Thrown when there is a problem writing to the database
	public void set(String nameString valuethrows IOException {
		if( < (new Date()).getTime()) {
		}
		if(.get(name) != null && .get(name).equals(value)) {
			return;
		}
		.upsertSetting(nullnamevalue);
		.put(namevalue);
	}
	public void remove(String namethrows IOException {
		.deleteSetting(nullname);
	}

Reload the configuration settings from the database. If any problems occur while the settings are being loaded, the previous set of cached settings are retained.

Throws:
java.io.IOException Thrown when there is a problem reading from the database.
	private void loadSettings() throws IOException {
		Map<StringStringnewSettings = new Hashtable<>();
		for(KeyValue r : .getSettings(null)) {
			if(r.getValue() == null) {
				newSettings.put(r.getKey(), "");
else {
				newSettings.put(r.getKey(), r.getValue());
			}
		}
		// Reset the data expire time
		try {
			 = (new Date()).getTime() + Long.parseLong(newSettings.get("setting.cache.timeout"))*1000;
catch(Exception e) {
			// Set a default time to expire in 60 seconds
			 = (new Date()).getTime() + 60*1000;
		}
		 = newSettings;
	}
	public void deleteAll() throws IOException {
		 = new Hashtable<>();
	}
New to GrepCode? Check out our FAQ X