1 package net.myvietnam.mvncore.configuration; 2 55 56 import java.util.ArrayList ; 57 import java.util.Iterator ; 58 import java.util.LinkedList ; 59 import java.util.List ; 60 import java.util.ListIterator ; 61 import java.util.NoSuchElementException ; 62 import java.util.Properties ; 63 import java.util.Vector ; 64 65 76 public class CompositeConfiguration implements Configuration 77 { 78 79 private LinkedList configList = new LinkedList (); 80 81 85 private BaseConfiguration inMemoryConfiguration; 86 87 91 public CompositeConfiguration() 92 { 93 clear(); 94 } 95 96 public void addConfiguration(Configuration config) 97 { 98 if (!configList.contains(config)) 99 { 100 configList.add(configList.indexOf(inMemoryConfiguration), config); 105 } 106 } 107 public void removeConfiguration(Configuration config) 108 { 109 if (!config.equals(inMemoryConfiguration)) 112 { 113 configList.remove(config); 114 } 115 } 116 public int getNumberOfConfigurations() 117 { 118 return configList.size(); 119 } 120 public void clear() 121 { 122 configList.clear(); 123 inMemoryConfiguration = new BaseConfiguration(); 125 configList.addLast(inMemoryConfiguration); 126 } 127 133 public void addProperty(String key, Object token) 134 { 135 inMemoryConfiguration.addProperty(key, token); 136 } 137 143 public Iterator getKeys() 144 { 145 List keys = new ArrayList (); 146 for (ListIterator i = configList.listIterator(); i.hasNext();) 147 { 148 Configuration config = (Configuration) i.next(); 149 for (Iterator j = config.getKeys(); j.hasNext();) 150 { 151 String key = (String ) j.next(); 152 if (!keys.contains(key)) 153 { 154 keys.add(key); 155 } 156 } 157 } 158 return keys.iterator(); 159 } 160 166 public Iterator getKeys(String key) 167 { 168 List keys = new ArrayList (); 169 for (ListIterator i = configList.listIterator(); i.hasNext();) 170 { 171 Configuration config = (Configuration) i.next(); 172 for (Iterator j = config.getKeys(key); j.hasNext();) 173 { 174 String newKey = (String ) j.next(); 175 if (!keys.contains(newKey)) 176 { 177 keys.add(newKey); 178 } 179 } 180 } 181 return keys.iterator(); 182 } 183 194 public Properties getProperties(String key) 195 { 196 return getFirstMatchingConfig(key).getProperties(key); 197 } 198 public boolean isEmpty() 199 { 200 boolean isEmpty = true; 201 for (ListIterator i = configList.listIterator(); i.hasNext();) 202 { 203 Configuration config = (Configuration) i.next(); 204 if (!config.isEmpty()) 205 { 206 return false; 207 } 208 } 209 return isEmpty; 210 } 211 218 public Object getProperty(String key) 219 { 220 return getFirstMatchingConfig(key).getProperty(key); 221 } 222 230 public void setProperty(String key, Object value) 231 { 232 clearProperty(key); 233 addProperty(key, value); 234 } 235 240 public void clearProperty(String key) 241 { 242 for (ListIterator i = configList.listIterator(); i.hasNext();) 243 { 244 Configuration config = (Configuration) i.next(); 245 config.clearProperty(key); 246 } 247 } 248 251 public boolean containsKey(String key) 252 { 253 for (ListIterator i = configList.listIterator(); i.hasNext();) 254 { 255 Configuration config = (Configuration) i.next(); 256 if (config.containsKey(key)) 257 { 258 return true; 259 } 260 } 261 return false; 262 } 263 270 public Configuration subset(String prefix) 271 { 272 CompositeConfiguration subsetCompositeConfiguration = 273 new CompositeConfiguration(); 274 Configuration subConf = null; 275 int count = 0; 276 for (ListIterator i = configList.listIterator(); i.hasNext();) 277 { 278 Configuration config = (Configuration) i.next(); 279 Configuration subset = config.subset(prefix); 280 if (subset != null) 281 { 282 subsetCompositeConfiguration.addConfiguration(subset); 283 subConf = subset; 284 count++; 285 } 286 } 287 return (count == 1) ? subConf : subsetCompositeConfiguration; 288 } 289 301 public float getFloat(String key) 302 { 303 return getFirstMatchingConfig(key).getFloat(key); 304 } 305 315 public boolean getBoolean(String key) 316 { 317 return getFirstMatchingConfig(key).getBoolean(key); 318 } 319 328 public boolean getBoolean(String key, boolean defaultValue) 329 { 330 return getBoolean(key, new Boolean (defaultValue)).booleanValue(); 331 } 332 342 public Boolean getBoolean(String key, Boolean defaultValue) 343 { 344 try 345 { 346 return getFirstMatchingConfig(key).getBoolean(key, defaultValue); 347 } 348 catch (NoSuchElementException nsee) 349 { 350 return defaultValue; 351 } 352 } 353 365 public byte getByte(String key) 366 { 367 return getFirstMatchingConfig(key).getByte(key); 368 } 369 380 public byte getByte(String key, byte defaultValue) 381 { 382 return getByte(key, new Byte (defaultValue).byteValue()); 383 } 384 396 public Byte getByte(String key, Byte defaultValue) 397 { 398 try 399 { 400 return getFirstMatchingConfig(key).getByte(key, defaultValue); 401 } 402 catch (NoSuchElementException nsee) 403 { 404 return defaultValue; 405 } 406 } 407 419 public double getDouble(String key) 420 { 421 return getFirstMatchingConfig(key).getDouble(key); 422 } 423 434 public double getDouble(String key, double defaultValue) 435 { 436 return getDouble(key, new Double (defaultValue)).doubleValue(); 437 } 438 450 public Double getDouble(String key, Double defaultValue) 451 { 452 try 453 { 454 return getFirstMatchingConfig(key).getDouble(key, defaultValue); 455 } 456 catch (NoSuchElementException nsee) 457 { 458 return defaultValue; 459 } 460 } 461 472 public float getFloat(String key, float defaultValue) 473 { 474 return getFloat(key, new Float (defaultValue)).floatValue(); 475 } 476 488 public Float getFloat(String key, Float defaultValue) 489 { 490 try 491 { 492 return getFirstMatchingConfig(key).getFloat(key, defaultValue); 493 } 494 catch (NoSuchElementException nsee) 495 { 496 return defaultValue; 497 } 498 } 499 511 public int getInt(String key) 512 { 513 return getFirstMatchingConfig(key).getInt(key); 514 } 515 526 public int getInt(String key, int defaultValue) 527 { 528 return getInteger(key, new Integer (defaultValue)).intValue(); 529 } 530 542 public Integer getInteger(String key, Integer defaultValue) 543 { 544 try 545 { 546 return getFirstMatchingConfig(key).getInteger(key, defaultValue); 547 } 548 catch (NoSuchElementException nsee) 549 { 550 return defaultValue; 551 } 552 } 553 565 public long getLong(String key) 566 { 567 return getFirstMatchingConfig(key).getLong(key); 568 } 569 580 public long getLong(String key, long defaultValue) 581 { 582 return getLong(key, new Long (defaultValue)).longValue(); 583 } 584 596 public Long getLong(String key, Long defaultValue) 597 { 598 try 599 { 600 return getFirstMatchingConfig(key).getLong(key, defaultValue); 601 } 602 catch (NoSuchElementException nsee) 603 { 604 return defaultValue; 605 } 606 } 607 619 public short getShort(String key) 620 { 621 return getFirstMatchingConfig(key).getShort(key); 622 } 623 634 public short getShort(String key, short defaultValue) 635 { 636 return getShort(key, new Short (defaultValue)).shortValue(); 637 } 638 650 public Short getShort(String key, Short defaultValue) 651 { 652 try 653 { 654 return getFirstMatchingConfig(key).getShort(key, defaultValue); 655 } 656 catch (NoSuchElementException nsee) 657 { 658 return defaultValue; 659 } 660 } 661 669 public String getString(String key) 670 { 671 return getString(key, null); 672 } 673 682 public String getString(String key, String defaultValue) 683 { 684 try 685 { 686 return getFirstMatchingConfig(key).getString(key, defaultValue); 687 } 688 catch (NoSuchElementException nsee) 689 { 690 return defaultValue; 691 } 692 } 693 702 public String [] getStringArray(String key) 703 { 704 Vector v = getVector(key); 705 return (String []) v.toArray(new String [0]); 706 } 707 708 716 public Vector getVector(String key) 717 { 718 Vector v = new Vector (); 719 720 for (ListIterator li = configList.listIterator(); li.hasNext();) 721 { 722 Configuration config = (Configuration) li.next(); 723 if (config.containsKey(key)) 724 { 725 v.addAll(config.getVector(key)); 726 } 727 } 728 729 return v; 730 } 731 732 741 public Vector getVector(String key, Vector defaultValue) 742 { 743 Vector v = getVector(key); 744 745 return (v.size() == 0) ? defaultValue : v; 746 } 747 748 private Configuration getFirstMatchingConfig(String key) 749 { 750 for (ListIterator i = configList.listIterator(); i.hasNext();) 751 { 752 Configuration config = (Configuration) i.next(); 753 if (config.containsKey(key)) 754 { 755 return config; 756 } 757 } 758 throw new NoSuchElementException ( 759 '\'' + key + "' doesn't map to an existing object"); 760 } 761 762 public Configuration getConfiguration(int index) 763 { 764 return (Configuration) configList.get(index); 765 } 766 } 767 | Popular Tags |