1 package org.igfay.jfig; 2 3 import org.apache.log4j.Logger; 4 import org.igfay.util.PropertyUtility; 5 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Map ; 11 import java.util.Properties ; 12 import java.util.TreeMap ; 13 14 37 public class JFig implements JFigIF { 38 private static Logger log = Logger.getLogger(JFig.class); 39 private static JFigIF configSingleton; 40 private JFigDictionary configDictionary; 41 private Map allConfigFiles; 42 private JFigLocatorIF configLocator; 43 private JFigParser configParser; 44 private List configListeners; 45 46 private JFig() { 48 this.configDictionary = new JFigDictionary(); 49 this.allConfigFiles = new HashMap (); 50 } 51 private JFig(JFigLocatorIF jfigLocator) throws JFigException { 52 if (jfigLocator == null) { 53 jfigLocator = new JFigLocator(null); 54 } 55 this.configLocator = jfigLocator; 56 this.configDictionary = new JFigDictionary(); 57 this.allConfigFiles = new HashMap (); 58 processConfig(); 59 } 60 61 public static void main(String [] args) { 62 JFig.getInstance().print(); 63 PropertyUtility.listProperties(); 64 } 65 66 70 public synchronized static JFigIF getInstance() { 71 return getInstance(null); 72 } 73 public static void setInstance(JFigIF jfig) { 74 configSingleton = jfig; 75 } 76 77 82 public synchronized static JFigIF getInstance(int dummy) throws JFigException { 83 return getConfigSingleton(null); 84 } 85 86 91 public synchronized static JFigIF getInstance(int dummy, JFigLocatorIF locator) throws JFigException { 92 return getConfigSingleton(locator); 93 } 94 95 99 public synchronized static JFigIF getInstance(JFigLocatorIF jfigLocator) { 100 JFigIF myConfigSingleton = new JFig(); 102 try { 103 myConfigSingleton = getConfigSingleton(jfigLocator); 104 } catch (JFigException e) { 105 log.error("*** Unable to create configuration dictionary. " + e.getMessage()+ " ***"); 106 } catch (NoClassDefFoundError e) { 107 log.error("*** Unable to create configuration dictionary. NoClassDefFoundError "+ e.getMessage() +"\nThis may be the result of incompatible XML jars. See JFIG documentation."); 108 } catch (Throwable e) { 109 log.error("*** Unhandled exception initializing JFig "+e.getMessage(),e); 110 } 111 setInstance(myConfigSingleton); 112 return myConfigSingleton; 113 } 114 115 private static JFigIF getConfigSingleton(JFigLocatorIF jfigLocator) throws JFigException { 116 if (configSingleton == null) { 117 log.debug("Create singleton object."); 118 configSingleton = new JFig(jfigLocator); 119 log.debug("Created singleton object"); 120 } 121 return configSingleton; 122 } 123 124 131 public void addConfigEventListener(JFigListener listener) { 132 log.debug("Add listener: " + listener); 133 getConfigListeners().add(listener); 134 log.debug("Number listeners: " + configListeners.size()); 135 } 136 137 141 protected List getConfigListeners() { 142 if (configListeners == null) { 143 configListeners = new ArrayList (); 144 } 145 146 return configListeners; 147 } 148 149 152 protected void fireConfigUpdateEvent() { 153 log.debug("Number listeners: " + getConfigListeners().size()); 154 155 JFigEvent event = new JFigEvent("Configuration Updated"); 156 157 for (int i = 0; i < getConfigListeners().size(); i++) { 159 log.debug("Notify listener"); 160 ((JFigListener) getConfigListeners().get(i)).configurationUpdate( 161 event); 162 } 163 } 164 165 168 public void print() { 169 getConfigDictionary().print(); 170 } 171 172 176 public void printConfigurationDictionary() { 177 print(); 178 } 179 180 183 protected void processConfig() throws JFigException { 184 log.debug("Processing file "+getConfigLocator().getConfigFileName()); 185 getParser().processConfig(); 186 187 getParser().resolveSymbolicValues(); 188 getParser().addPropertyValues(); 189 log.debug("Complete "); 190 log.debug(""); 191 } 192 193 196 public void reprocessConfiguration() throws JFigException { 197 reprocessConfiguration(null); 198 } 199 202 public void reprocessConfiguration(JFigLocator locator) throws JFigException { 203 initialize(locator); 204 fireConfigUpdateEvent(); 205 } 206 209 public static JFigIF initialize() throws JFigException { 210 return initialize(null); 211 } 212 213 220 public static JFigIF initialize(JFigLocator locator) throws JFigException { 221 JFig newConfigSingleton = new JFig(locator); 222 configSingleton = newConfigSingleton; 223 return configSingleton; 224 } 225 226 231 public JFigDictionary getConfigDictionary() { 232 return configDictionary; 233 } 234 235 241 public String [] getArrayValue(String section, String key) throws JFigException { 242 String value = getValue(section, key); 243 244 return JFigUtility.stringToArray(value); 245 } 246 247 250 public boolean getBooleanValue(String section, String key, String notFoundValue) { 251 String value = getValue(section, key, notFoundValue); 252 253 return Boolean.valueOf(value).booleanValue(); 254 } 255 256 265 public float getFloatValue(String section, String key, String notFoundValue) throws JFigException { 266 String value = getValue(section, key, notFoundValue); 267 268 try { 269 return Float.valueOf(value).floatValue(); 270 } catch (ArithmeticException e) { 271 throw new JFigException(e.getMessage()); 272 } 273 } 274 275 278 public int getIntegerValue(String section, String key) 279 throws JFigException { 280 String value = getValue(section, key); 281 282 try { 283 return Integer.parseInt(value); 284 } catch (ArithmeticException e) { 285 throw new JFigException(e.getMessage()); 286 } 287 } 288 289 292 public int getIntegerValue(String section, String key, String notFoundValue) { 293 String value = getValue(section, key, notFoundValue); 294 295 try { 296 return Integer.parseInt(value); 297 } catch (ArithmeticException e) { 298 log.info( 299 "Caught ArithmenticException for section " 300 + section 301 + " key " 302 + key 303 + " defaultValue " 304 + notFoundValue 305 + ". Returning 0"); 306 307 return 0; 308 } catch (NumberFormatException e) { 309 log.info( 310 "Caught NumberFormatException for section " 311 + section 312 + " key " 313 + key 314 + " defaultValue " 315 + notFoundValue 316 + ". Returning 0"); 317 318 return 0; 319 } 320 } 321 322 325 protected JFigParser getParser() { 326 if (configParser == null) { 327 if (isXML()) { 328 log.debug("Create XMLConfigParser"); 329 configParser = new XMLJFigParser(this, getConfigLocator()); 330 } else { 331 log.debug("create ConfigParser"); 332 configParser = new IniJFigParser(this, getConfigLocator()); 333 } 334 } 335 336 return configParser; 337 } 338 339 343 protected boolean isXML() { 344 return (getConfigFileName() != null) 345 && (getConfigFileName().indexOf("xml") > -1); 346 } 347 348 357 public String getValue(String section, String key, String defaultValue) { 358 String value = null; 359 360 try { 361 value = getValue(section, key); 362 } catch (JFigException e) { 363 log.debug("Return default value " + defaultValue); 364 365 return defaultValue; 366 } 367 368 return value; 369 } 370 371 382 public List getValuesStartingWith(String section, String key) { 383 log.debug("section " + section + " key " + key); 384 385 List list = new ArrayList (); 386 TreeMap map = getConfigDictionary().getSectionNamed(section, false); 387 388 if (map != null) { 389 Iterator iterator = map.keySet().iterator(); 390 391 while (iterator.hasNext()) { 392 String compareKey = (String ) iterator.next(); 393 log.debug("compareKey " + compareKey); 394 395 if (compareKey.startsWith(key)) { 396 list.add(map.get(compareKey)); 397 } 398 } 399 } 400 401 return list; 402 } 403 404 415 public Map getEntriesStartingWith(String section, String key) { 416 log.debug("section " + section + " key " + key); 417 418 List list = new ArrayList (); 419 TreeMap map = getConfigDictionary().getSectionNamed(section, false); 420 HashMap returnMap = new HashMap (); 421 if (map != null) { 422 Iterator iterator = map.keySet().iterator(); 423 424 while (iterator.hasNext()) { 425 String compareKey = (String ) iterator.next(); 426 log.debug("compareKey " + compareKey); 427 428 if (compareKey.startsWith(key)) { 429 returnMap.put(compareKey, map.get(compareKey)); 430 } 431 } 432 } 433 434 return returnMap; 435 } 436 437 440 public Map getSection(String section) { 441 log.debug("section " + section ); 442 return getConfigDictionary().getSectionNamed(section, false); 443 } 444 445 448 public Properties getSectionAsProperties(String section) { 449 return getSectionAsProperties(section, new Properties ()); 450 } 451 452 455 public Properties getSectionAsProperties(String section, Properties properties) { 456 log.debug("section " + section ); 457 if (properties == null) { 458 return null; 459 } 460 Map map = getSection(section); 462 Iterator iterator = map.keySet().iterator(); 463 while (iterator.hasNext()) { 464 String key = (String ) iterator.next(); 465 String value = (String ) map.get(key); 466 if (value != null) { 467 properties.put(key, value); 468 } 469 470 } 471 472 return properties; 473 } 474 475 479 public String getValue(String section, String key) throws JFigException { 480 String value = getConfigDictionary().getValue(section, key); 481 log.debug(value); 482 483 return value; 484 } 485 486 490 public void setConfigurationValue(String sectionName, String keyString, String valueString) { 491 getConfigDictionary().setConfigurationValue(sectionName, keyString, valueString); 492 } 493 494 497 public String [] getArrayValue(String section, String key, String notFoundValue) { 498 String value = getValue(section, key, notFoundValue); 499 500 return JFigUtility.stringToArray(value); 501 } 502 503 507 protected void setConfigDictionary(JFigDictionary configDictionary) { 508 this.configDictionary = configDictionary; 509 } 510 511 517 protected Map getAllConfigFiles() { 518 return allConfigFiles; 519 } 520 521 protected void setAllConfigFiles(HashMap map) { 522 allConfigFiles = map; 523 } 524 525 528 protected JFigLocatorIF getConfigLocator() { 529 return configLocator; 530 } 531 532 protected String getConfigFileName() { 533 return getConfigLocator().getConfigFileName(); 534 } 535 } 536 | Popular Tags |