org.openbp.common.setting
Class SettingResolver

java.lang.Object
  extended by org.openbp.common.setting.SettingResolver
All Implemented Interfaces:
java.lang.Cloneable

public class SettingResolver
extends java.lang.Object
implements java.lang.Cloneable

The setting resolver manages a chain of setting providers. The providers may retrieve or save settings. The setting resolver will access the registered setting providers in order to resolve (i. e. retrieve or store) the setting.
A setting provider may be e. g. a provider that store settings in a property file, but also a provider that may store them in a database under the name of the current user. When a provider registers with the resolver, it can be assigned a priority. The priority will determine the order in which the resolver will ask the providers if they can resolve the setting. A setting is identified by its name, which can include sub setting specifications (sections), which are separated by the '.' character, e. g. "section1.section2.name".
The setting name may optionally include the name of the setting provider, e. g. "user:section1.section2.name". Some setting providers may save the settings automatically if one of the set methods is called, whereas others may cache the settings and save them to the persistent storage after the saveSettings(java.lang.String) method has been called.

Author:
Heiko Erhardt

Field Summary
static char PROVIDER_SEPARATOR
          Provider separator
 
Constructor Summary
SettingResolver()
          Constructor.
 
Method Summary
 void addProvider(java.lang.String name, SettingProvider provider, int priority)
          Adds a provider.
 void clearProvider(java.lang.String name)
          Clears the specified setting provider.
 java.lang.Object clone()
          Creates a clone of this object.
 SettingProvider getProvider(java.lang.String name)
          Gets a provider by its name.
 java.util.List getProviderInfoList()
          Gets the provider list.
 java.lang.Object getSetting(java.lang.String name, java.lang.Object dflt)
          Gets a setting value or the default.
 boolean loadSettings(java.lang.String providerName)
          Loads the current setting values of a setting provider.
 boolean saveSettings(java.lang.String providerName)
          Saves the current setting values of a setting provider.
 SettingProvider setSetting(java.lang.String name, java.lang.Object value)
          Sets a setting.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PROVIDER_SEPARATOR

public static final char PROVIDER_SEPARATOR
Provider separator

See Also:
Constant Field Values
Constructor Detail

SettingResolver

public SettingResolver()
Constructor.

Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Creates a clone of this object. Copies the list of providers, but not the providers and the ProviderInfo objects themself.

Overrides:
clone in class java.lang.Object
Returns:
The clone (a deep copy of this object)
Throws:
java.lang.CloneNotSupportedException - Never

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getSetting

public java.lang.Object getSetting(java.lang.String name,
                                   java.lang.Object dflt)
Gets a setting value or the default.

Parameters:
name - Name of the setting
dflt - Default value of the setting
Returns:
The setting value or the default value if the setting does not exist

setSetting

public SettingProvider setSetting(java.lang.String name,
                                  java.lang.Object value)
Sets a setting.

Parameters:
name - Name of the setting
If the setting name contains a provider specification, the specified provider will be used. Otherwise, the first provider (according to their priority) that can save the setting, will be used.
value - Value of the setting
Returns:
The setting provider that saved the setting or null if no provider was able to save this setting

loadSettings

public boolean loadSettings(java.lang.String providerName)
Loads the current setting values of a setting provider.

Parameters:
providerName - Name of the provider to save or null for all
Returns:
true If the setting were successfully loaded. false If there was an error loading the properties of at least one provider.

saveSettings

public boolean saveSettings(java.lang.String providerName)
Saves the current setting values of a setting provider.

Parameters:
providerName - Name of the provider to save or null for all
Returns:
true If the setting were successfully saved or the provider is not save-capable. false If there was an error saving the properties of at least one provider.

addProvider

public void addProvider(java.lang.String name,
                        SettingProvider provider,
                        int priority)
Adds a provider. Also calls SettingProvider.loadSettings().

Parameters:
name - Name of the provider
provider - The provider to add
priority - Priority of the provider; 0 is top priority

clearProvider

public void clearProvider(java.lang.String name)
Clears the specified setting provider.

Parameters:
name - Name of the provider to remove

getProvider

public SettingProvider getProvider(java.lang.String name)
Gets a provider by its name.

Parameters:
name - Name of the provider
Returns:
The provider or null if not found

getProviderInfoList

public java.util.List getProviderInfoList()
Gets the provider list.



Copyright © 2011. All Rights Reserved.