FileDocCategorySizeDatePackage
Settings.javaAPI DocExample11777Mon Jul 23 13:26:54 BST 2007org.apache.struts2.config

Settings

public class Settings extends Object
Settings retrieves and exposes default values used by the framework. An application can override a factory default and provide its own value for a setting.

Implementation of the class is pluggable (the default implementation is {@link DefaultSettings}). Pluggability gives applications to ability to customize how settings are retrieved. As an example, an application may wish to check some custom property store before delegating to the usual configuration and property files.

Key methods:

  • {@link #getLocale()}
  • {@link #get(String)}
  • {@link #set(String, String)}
  • {@link #list()}

Key methods for subclasses (plugins):

  • {@link #getImpl(String)}
  • {@link #setImpl(String, String)}
  • {@link #listImpl()}
  • {@link #isSetImpl(String)}

Fields Summary
static Settings
settingsImpl
A pluggable implementation of Settings, provided through the {@link #setInstance} method.
static Settings
defaultImpl
An instance of {@link DefaultSettings} to use when another implementation is not provided (plugged in).
static Locale
locale
An instance of the default locale as specified by the struts.locale setting.
private static final Log
LOG
The Logging instance for this class.
Constructors Summary
Methods Summary
public static java.lang.Stringget(java.lang.String name)
Provides a setting value as a String.

The method will throw an IllegalArgumentException if an error occurs while retrieveing the property or if the property doesn't exist.

param
name the name of the setting to retrieve.
return
the setting value as a String.
throws
IllegalArgumentException if an error occurs retrieving the property or the property does not exist.

        return getInstance().getImpl(name);
    
private static org.apache.struts2.config.SettingsgetDefaultInstance()
Creates a default Settings object.

A default implementation may be specified by the struts.configuration setting; otherwise, this method instantiates {@link DefaultSettings} as the default implementation.

return
A default Settings object.

        if (defaultImpl == null) {
            // Create bootstrap implementation
            defaultImpl = new DefaultSettings();

            // Create default implementation
            try {
                String className = get(StrutsConstants.STRUTS_CONFIGURATION);

                if (!className.equals(defaultImpl.getClass().getName())) {
                    try {
                        // singleton instances shouldn't be built accessing request or session-specific context data
                        defaultImpl = (Settings) ObjectFactory.getObjectFactory().buildBean(Thread.currentThread().getContextClassLoader().loadClass(className), null);
                    } catch (Exception e) {
                        LOG.error("Settings: Could not instantiate the struts.configuration object, substituting the default implementation.", e);
                    }
                }
            } catch (IllegalArgumentException ex) {
                // ignore
            }
        }

        return defaultImpl;
    
public java.lang.StringgetImpl(java.lang.String name)
Implements the {@link #get(String)} method.

param
name The name of the setting value to retreive
return
The setting value as a String
throws
IllegalArgumentException if an error occurs when retrieving the value
see
#get(String)

        return null;
    
public static org.apache.struts2.config.SettingsgetInstance()
Provides the Settings object.

This method will substitute the default instance if another instance is not registered.

return
the Settings object.

        return (settingsImpl == null) ? getDefaultInstance() : settingsImpl;
    
public static java.util.LocalegetLocale()
Provides the Struts default locale.

This method utilizes the struts.locale setting, which should be given as the Java {@link java.util.Locale#toString() toString()} representation of a Locale object ("en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr_MAC", and so forth).

If a struts.locale setting is not registered, then the default virtual machine locale is substituted and cached.

return
the Struts default locale if specified or the VM default locale.
see
java.util.Locale#getDefault()

        if (locale == null) {
            try {
                StringTokenizer localeTokens = new StringTokenizer(get(StrutsConstants.STRUTS_LOCALE), "_");
                String lang = null;
                String country = null;

                if (localeTokens.hasMoreTokens()) {
                    lang = localeTokens.nextToken();
                }

                if (localeTokens.hasMoreTokens()) {
                    country = localeTokens.nextToken();
                }

                locale = new Locale(lang, country);
            } catch (Throwable t) {
                // Default
                LOG.warn("Settings: Could not parse struts.locale setting, substituting default VM locale");
                locale = Locale.getDefault();
            }
        }

        return locale;
    
public static com.opensymphony.xwork2.util.location.LocationgetLocation(java.lang.String name)
Provides the Location of a setting.

The Location is utilized as part of precise error reporting.

This method will throw an IllegalArgumentException if an error occurs while retrieving the value or if the setting doesn't exist.

param
name the name of the property to get.
return
the Location of a property.
throws
IllegalArgumentException if an error occurs retrieving the property or the property does not exist.

        return getInstance().getLocationImpl(name);
    
public com.opensymphony.xwork2.util.location.LocationgetLocationImpl(java.lang.String name)
Implements the {@link #getLocation(String)} method.

param
name Name of the setting to locate
return
The location of the setting
throws
IllegalArgumentException if an error occurs when retrieving the value
see
#getLocation(String)

        return null;
    
public static booleanisSet(java.lang.String name)
Determines whether or not a setting has a registered value.

This method is useful for testing for the existance of setting without throwing an IllegalArgumentException.

param
name the name of a setting to test.
return
true if the setting exists and has a value, false otherwise.

        return getInstance().isSetImpl(name);
    
public booleanisSetImpl(java.lang.String name)
Implements the {@link #isSet(String)} method.

param
name Identifier for the setting value to change
return
True if the setting exists and has a value, false otherwise.
see
#isSet(String)

        // this is dumb.. maybe it should just throw an unsupported op like the rest of the *Impl
        // methods in this class.
        return false;
    
public static java.util.Iteratorlist()
Provides an Iterator of all properties names.

return
an Iterator of all properties names.

        return getInstance().listImpl();
    
public java.util.IteratorlistImpl()
Implements the {@link #list()} method.

see
#list()
return
A list of the settings as an iterator

        throw new UnsupportedOperationException("Settings: This implementation does not support listing the registered settings");
    
public static voidreset()
Resets the default and any plugin Setting instance to null.

        defaultImpl = null;
        settingsImpl = null;
    
public static voidset(java.lang.String name, java.lang.String value)
Registers a value for a setting.

This method raises an exception if an error occurs when setting the value or if the settings implementation does not support setting values.

param
name the name of the setting.
param
value the value to register for the setting.
throws
IllegalArgumentException if an error occurs when setting the value.
throws
UnsupportedOperationException if the config implementation does not support setting values.

        getInstance().setImpl(name, value);
    
public voidsetImpl(java.lang.String name, java.lang.String value)
Implements the {@link #set(String, String)} method.

param
name Identifer for the setting to change.
param
value The new value for the setting.
throws
IllegalArgumentException if an error occurs when setting the value.
throws
UnsupportedOperationException if the config implementation does not support setting values.
see
#set(String, String)

        throw new UnsupportedOperationException("Settings: This implementation does not support setting a value.");
    
public static voidsetInstance(org.apache.struts2.config.Settings config)
Registers a custom Settings implementation (plugin), and resets the cached locale.

This method can only be called once.

param
config a Settings implementation
throws
IllegalStateException if an error occurs when setting the settings implementation.


                                            
           
        settingsImpl = config;
        locale = null;