001 package com.sptci.echo2;
002
003 import java.util.Locale;
004 import java.util.HashMap;
005 import java.util.MissingResourceException;
006 import java.util.ResourceBundle;
007 import java.util.logging.Level;
008 import java.util.logging.Logger;
009
010 import nextapp.echo2.app.ApplicationInstance;
011
012 /**
013 * A utility class that is used to load and retrieve the localised
014 * configurable properties for UI components. This class needs
015 * an active <code>ApplicationInstance</code> to be properly
016 * initialised.
017 *
018 * <p>Copyright 2006 Sans Pareil Technologies, Inc.</p>
019 * @author Rakesh Vidyadharan 2006-03-01
020 * @version $Id: Configuration.java 3026 2007-03-02 14:02:28Z rakesh $
021 */
022 public class Configuration
023 {
024 /**
025 * The name of the resource bundle to load.
026 *
027 * {@value}
028 */
029 private static final String BUNDLE_NAME =
030 "META-INF.resource.localisation.Configuration";
031
032 /**
033 * The logger used to log errors.
034 */
035 private static final Logger logger =
036 Logger.getLogger( Configuration.class.getName() );
037
038 /**
039 * The resource bundle that represents the appropriate properties
040 * file.
041 */
042 private static HashMap<Locale, ResourceBundle> resource =
043 new HashMap<Locale, ResourceBundle>();
044
045 /**
046 * Cannot be instantiated.
047 */
048 private Configuration() {}
049
050 /**
051 * Returns localised configured text for the key for the specified
052 * source object. Look for a key that is named after the fully
053 * qualified class name of the source object.
054 *
055 * @see #getString( String )
056 * @param source The object for which the localised text is to be
057 * retrieved.
058 * @param key The key of the text to be returned
059 * @return The appropriate localised text (if the key is not defined,
060 * the string "!key!" is returned)
061 */
062 public static final String getString( Object source, String key )
063 {
064 return getString( source.getClass().getName() + "." + key );
065 }
066
067 /**
068 * Returns localised configured text for the key.
069 *
070 * @param key The key of the text to be returned
071 * @return The appropriate localised text (if the key is not defined,
072 * the string "!key!" is returned)
073 */
074 public static final String getString( String key )
075 {
076 String result = null;
077 try
078 {
079 result = fetchResourceBundle().getString( key );
080 }
081 catch ( MissingResourceException mrex )
082 {
083 logger.warning( "No resource configured in " +
084 BUNDLE_NAME + " for key " + key );
085 result = '!' + key + '!';
086 }
087
088 return result;
089 }
090
091 /**
092 * Returns configured integer value for the key for the specified
093 * source object. Look for a key that is named after the fully
094 * qualified class name of the source object.
095 *
096 * @see #getInt( String )
097 * @param source The object whose configured localised value is
098 * to be retrieved.
099 * @param key The key of the integer value to be returned
100 * @return The appropriate value (if the key is not defined,
101 * the value 0 is returned)
102 */
103 public static final int getInt( Object source, String key )
104 {
105 return getInt( source.getClass().getName() + "." + key );
106 }
107
108 /**
109 * Returns configured integer value for the key.
110 *
111 * @param key The key of the integer value to be returned
112 * @return The appropriate value (if the key is not defined,
113 * the value 0 is returned)
114 */
115 public static final int getInt( String key )
116 {
117 int result = 0;
118 try
119 {
120 result = Integer.parseInt( fetchResourceBundle().getString( key ) );
121 }
122 catch ( MissingResourceException mrex )
123 {
124 logger.warning( "No resource configured in " +
125 BUNDLE_NAME + " for key " + key );
126 }
127
128 return result;
129 }
130
131 /**
132 * Fetch the appropriate <code>ResourceBundle</code> based upon the
133 * <code>Locale</code> from {@link #resource}.
134 *
135 * @return ResourceBundle The appropriate resource bundle for the
136 * locale.
137 */
138 private static final ResourceBundle fetchResourceBundle()
139 {
140 Locale locale = Locale.getDefault();
141
142 if ( ApplicationInstance.getActive() != null )
143 {
144 locale = ApplicationInstance.getActive().getLocale();
145 }
146
147 ResourceBundle bundle = resource.get( locale );
148 if ( bundle == null )
149 {
150 bundle = ResourceBundle.getBundle( BUNDLE_NAME, locale );
151 resource.put( locale, bundle );
152 }
153
154 return bundle;
155 }
156 }