1 5 package com.opensymphony.webwork.config; 6 7 import com.opensymphony.xwork.ObjectFactory; 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import java.util.Iterator ; 12 import java.util.Locale ; 13 import java.util.StringTokenizer ; 14 15 16 40 public class Configuration { 41 43 static Configuration configurationImpl; 44 static Configuration defaultImpl; 45 static Locale locale; private static final Log LOG = LogFactory.getLog(Configuration.class); 47 48 50 56 public static void setConfiguration(Configuration config) throws IllegalStateException { 57 configurationImpl = config; 58 locale = null; } 60 61 66 public static Configuration getConfiguration() { 67 return (configurationImpl == null) ? getDefaultConfiguration() : configurationImpl; 68 } 69 70 79 public static Locale getLocale() { 80 if (locale == null) { 81 try { 82 StringTokenizer localeTokens = new StringTokenizer (getString("webwork.locale"), "_"); 83 String lang = null; 84 String country = null; 85 86 if (localeTokens.hasMoreTokens()) { 87 lang = localeTokens.nextToken(); 88 } 89 90 if (localeTokens.hasMoreTokens()) { 91 country = localeTokens.nextToken(); 92 } 93 94 locale = new Locale (lang, country); 95 } catch (Throwable t) { 96 LOG.warn("Setting locale to the default locale"); 98 locale = Locale.getDefault(); 99 } 100 } 101 102 return locale; 103 } 104 105 112 public static boolean isSet(String name) { 113 return getConfiguration().isSetImpl(name); 114 } 115 116 124 public static String getString(String name) throws IllegalArgumentException { 125 String val = get(name).toString(); 126 127 return val; 128 } 129 130 138 public static Object get(String name) throws IllegalArgumentException { 139 Object val = getConfiguration().getImpl(name); 140 141 return val; 142 } 143 144 149 public static Iterator list() { 150 return getConfiguration().listImpl(); 151 } 152 153 158 public boolean isSetImpl(String name) { 159 return false; 162 } 163 164 173 public static void set(String name, Object value) throws IllegalArgumentException , UnsupportedOperationException { 174 getConfiguration().setImpl(name, value); 175 } 176 177 182 public void setImpl(String name, Object value) throws IllegalArgumentException , UnsupportedOperationException { 183 throw new UnsupportedOperationException ("This configuration does not support updating a setting"); 184 } 185 186 191 public Object getImpl(String aName) throws IllegalArgumentException { 192 return null; 193 } 194 195 200 public Iterator listImpl() { 201 throw new UnsupportedOperationException ("This configuration does not support listing the settings"); 202 } 203 204 private static Configuration getDefaultConfiguration() { 205 if (defaultImpl == null) { 206 defaultImpl = new DefaultConfiguration(); 208 209 try { 211 String className = getString("webwork.configuration"); 212 213 if (!className.equals(defaultImpl.getClass().getName())) { 214 try { 215 defaultImpl = (Configuration) ObjectFactory.getObjectFactory().buildBean(Thread.currentThread().getContextClassLoader().loadClass(className)); 216 } catch (Exception e) { 217 LOG.error("Could not instantiate configuration", e); 218 } 219 } 220 } catch (IllegalArgumentException ex) { 221 } 223 } 224 225 return defaultImpl; 226 } 227 228 public static void reset() { 229 defaultImpl = null; 230 configurationImpl = null; 231 } 232 } 233 | Popular Tags |