1 18 package net.sf.uitags.tagutil; 19 20 import java.io.IOException ; 21 import java.util.Enumeration ; 22 import java.util.MissingResourceException ; 23 import java.util.Properties ; 24 import java.util.ResourceBundle ; 25 26 27 import net.sf.uitags.util.ArrayUtils; 28 import net.sf.uitags.util.PropertiesLoadingException; 29 import net.sf.uitags.util.StringUtils; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 59 public class TaglibProperties { 60 64 67 private static final String FACTORY_DEFAULT = "factory-default.properties"; 68 71 private static final String SITE_WIDE = "uitags"; 72 73 74 75 79 82 private static Log log = LogFactory.getLog(TaglibProperties.class); 83 84 88 private static Properties mergedProps = new Properties (); 89 90 95 public static final TaglibProperties MERGED; 96 97 101 private Properties runtimeProps; 102 103 104 105 109 115 static { 116 try { 118 mergedProps.load( 119 TaglibProperties.class.getResourceAsStream(FACTORY_DEFAULT)); 120 } 121 catch (IOException e) { 122 throw new PropertiesLoadingException(e, FACTORY_DEFAULT); 123 } 124 125 ResourceBundle rb = null; 127 try { 128 rb = ResourceBundle.getBundle(SITE_WIDE); 129 Enumeration keys = rb.getKeys(); 131 while (keys.hasMoreElements()) { 132 String key = (String ) keys.nextElement(); 133 mergedProps.setProperty(key, rb.getString(key)); 134 } 135 } 136 catch (MissingResourceException e) { 137 if (log.isDebugEnabled()) { 138 log.debug("Properties file '" + SITE_WIDE + "' not found."); 139 } 140 } 141 142 if (log.isDebugEnabled()) { 143 log.debug("Taglib properties loaded: " + mergedProps); 144 } 145 146 MERGED = new TaglibProperties() { 148 public void setRuntimeProperty(String key, String value) { 149 throw new IllegalStateException ("Object is immutable."); 150 } 151 }; 152 } 153 154 157 private TaglibProperties() { 158 this.runtimeProps = new Properties (); 159 } 160 161 167 public static TaglibProperties getInstance() { 168 return new TaglibProperties(); 169 } 170 171 172 173 177 186 public void setRuntimeProperty(String key, String value) { 187 if (value == null) { 188 return; 189 } 190 this.runtimeProps.setProperty(key, value); 191 } 192 193 200 public String get(String key) { 201 String ret = this.runtimeProps.getProperty(key); 203 204 if (ret == null) { 207 ret = mergedProps.getProperty(key); 208 } 209 210 if (ret == null) { 211 throw new IllegalStateException ( 212 "Property with key '" + key + "' is not set"); 213 } 214 return ret; 215 } 216 217 public void setRuntimeProperty(String key, String [] values) { 218 if (values == null) { 219 return; 220 } 221 this.runtimeProps.setProperty(key, StringUtils.join(values)); 222 } 223 224 public String [] getAsArray(String key) { 225 String stringValue = get(key); 226 return ArrayUtils.toArray(stringValue); 227 } 228 } 229 | Popular Tags |