1 2 24 25 package com.lutris.util; 26 27 28 import java.lang.reflect.Array ; 29 import java.lang.reflect.Constructor ; 30 import java.util.Hashtable ; 31 32 import org.enhydra.util.ConfigFileInterface; 33 34 45 public class Config extends KeywordValueTable { 46 47 51 private static final int GET_ALL = -1; 52 53 56 private ConfigFileInterface configFile = null; 57 58 61 public Config() { 62 super(); 63 } 64 65 69 public Config(KeywordValueTable kvt) { 70 super(); 71 String [] keys = kvt.keys(); 72 for (int i = 0; i < keys.length; i++) { 73 try { 74 set(keys[i], kvt.get(keys[i])); 75 } 76 catch (KeywordValueException e) { 77 throw new FatalExceptionError(e); 79 } 80 } 81 } 82 83 90 public Config(KeywordValueTable kvt, ConfigFileInterface configFile) { 91 this(kvt); 92 this.configFile = configFile; 93 } 94 95 99 public Config(ConfigFileInterface configFile) { 100 this(); 101 this.configFile = configFile; 102 } 103 104 112 public Config getClonedConfig() throws KeywordValueException{ 114 Config returnConfig = this.getClonedConfigParams(); 115 116 123 ConfigFileInterface cf = null; 124 Class classObj = this.getConfigFile().getClass(); 125 String className = classObj.getName(); 126 127 Class [] classParam = new Class [1]; 128 Constructor constr = null; 129 Object [] arguments = new Object [1]; 130 try { 131 classParam[0] = Class.forName("com.lutris.util.Config"); 132 constr = classObj.getConstructor(classParam); 133 arguments[0] = (Object )(this); 134 cf = (ConfigFileInterface)constr.newInstance(arguments); 135 cf.setFile(this.getConfigFile().getFile()); 136 returnConfig.setConfigFile(cf); 137 } 138 catch (Exception ex){ 139 returnConfig.setConfigFile(cf); 140 } 141 return returnConfig; 142 } 143 144 145 155 private Config getClonedConfigParams() throws KeywordValueException{ 157 Config returnConfig = new Config(); 158 String [] keys = this.keys(); 159 160 for (int i = 0; i < keys.length; i++) { 161 Object tempValue = this.get(keys[i]); 162 163 if(tempValue instanceof String ) { 164 returnConfig.set( new String (keys[i]), new String ((String )tempValue) ); 165 } 166 else if (tempValue instanceof KeywordValueTable) { Config kvtTempConfig = (new Config( (KeywordValueTable)tempValue)).getClonedConfigParams(); 168 returnConfig.set( new String (keys[i]), (KeywordValueTable)kvtTempConfig ); 169 } 170 else if (tempValue instanceof Config) { Config tempConfig = ((Config)tempValue).getClonedConfigParams(); 172 returnConfig.set( new String (keys[i]), tempConfig ); 173 } 174 else if( tempValue.getClass().isArray() ) { 175 int len = Array.getLength(tempValue); 176 String [] newTemp = new String [len]; 177 for (int k=0; k<len; k++) { 178 newTemp[k] = new String ( Array.get(tempValue,k).toString() ); 179 } 180 returnConfig.set( new String (keys[i]), newTemp ); 181 } 182 } 183 return returnConfig; 184 } 185 193 public Hashtable allConfigParams(String prefix) throws KeywordValueException{ 194 195 196 String [] keys = this.keys(); 197 Hashtable ht=new Hashtable (); 198 for (int i = 0; i < keys.length; i++) { 199 Object tempValue = this.get(keys[i]); 200 String name; 201 202 if(prefix != null && prefix.length()>0){ 203 name=prefix+"_"+keys[i]; 204 }else{ 205 name=keys[i]; 206 } 207 208 if(tempValue instanceof String ) { 209 ht.put(name, (String )tempValue); 210 } 211 else if (tempValue instanceof KeywordValueTable) { ht.putAll(((Config)tempValue).allConfigParams(name)); 213 } 214 else if (tempValue instanceof Config) { ht.putAll(((Config)tempValue).allConfigParams(name)); 216 } 217 else if( tempValue.getClass().isArray() ) { 218 int len = Array.getLength(tempValue); 219 String temp = ""; 220 if (len>0){ 221 for (int k=0; k<len; k++) { 222 temp=temp+","+(String )Array.get(tempValue,k).toString(); 223 } 224 ht.put(name+"_Array",temp.substring(1)); 225 } else { 226 ht.put(name+"_Array",temp); 227 } 228 } 229 } 230 return ht; 231 } 232 240 public void importConfig(Config config) throws KeywordValueException{ 242 String [] original = this.keys(); 243 String [] in = config.keys(); 244 245 for(int i=0; i<in.length; i++) 247 this.set( in[i], config.get(in[i]) ); 248 for(int i=0; i<original.length; i++) { 250 if(!config.containsKey(original[i])) 251 this.remove( in[i] ); 252 } 253 } 254 255 256 262 protected KeywordValueTable newSection() { 263 return new Config(configFile); 264 } 265 266 272 public ConfigFileInterface getConfigFile() { 273 return configFile; 274 } 275 276 282 public void setConfigFile(ConfigFileInterface configFile) { 283 this.configFile = configFile; 284 } 285 286 296 public synchronized Config getConfig(String keyword) 297 throws KeywordValueException { 298 return (Config) getSection(keyword); 299 } 300 301 314 public synchronized KeywordValueTable getSection(String keyword) 315 throws KeywordValueException { 316 KeywordValueTable kvt = super.getSection(keyword); 317 if (kvt == null) { 318 return null; 319 } 320 if (kvt instanceof Config) { 321 return kvt; 322 } 323 else { 324 return new Config(kvt, configFile); 325 } 326 } 327 328 334 public boolean containsKey(String key) { 335 boolean result = false; 336 try { 337 result = super.containsKey(key); 338 } 339 catch (KeywordValueException e) { 340 result = false; 341 } 342 return result; 343 } 344 345 353 public int containsCount(String key) throws ConfigException { 354 Object valObj = null; 355 try { 356 valObj = get(key); 357 if (valObj == null) return -1; 358 return Array.getLength(valObj); 359 } 360 catch (KeywordValueException e) { 361 throw new ConfigException(e.getMessage()); 362 } 363 catch (IllegalArgumentException e) { 364 } 366 if (valObj == null) return -1; 368 return 1; 369 } 370 371 382 public boolean isArray(String key) throws ConfigException { 383 Object valObj = null; 384 try { 385 valObj = get(key); 386 if (valObj == null) 387 throw new ConfigException("Key \"" + key + "\" not found."); 388 Array.getLength(valObj); 390 return true; 392 } 393 catch (KeywordValueException e) { 394 throw new ConfigException(e.getMessage()); 395 } 396 catch (IllegalArgumentException e) { 397 return false; 399 } 400 } 401 402 423 private final long[] getLongsInternal(String key, int count) 424 throws ConfigException { 425 426 Object obj; 427 try { 428 obj = get(key); 429 } 430 catch (KeywordValueException e) { 431 throw new ConfigException(e.getMessage()); 432 } 433 if (obj == null) { 434 throw new ConfigException(ConfigException.NOT_FOUND, 435 "Key \"" + key 436 + "\" not found in configuration."); 437 } 438 long[] la = null; 439 if (obj.getClass().isArray()) { 440 int len = Array.getLength(obj); 441 la = new long[len]; 442 for (int i=0; i<len; i++) { 443 try { 444 la[i] = (long) Long.parseLong(Array.get(obj,i).toString()); 445 } 446 catch (Throwable e) { 447 throw new ConfigException("Element " + i + 448 " is not a long."); 449 } 450 } 451 } 452 else { 453 la = new long[1]; 454 try { 455 la[0] = Long.parseLong(obj.toString()); 456 } 457 catch (Throwable e) { 458 throw new ConfigException("Element 0 is not a long."); 459 } 460 } 461 if ((count != GET_ALL) && (la.length != count)) { 462 throw new ConfigException(ConfigException.COUNT, 463 "Key \"" + key 464 + "\" has " + la.length + " elements. (expected " 465 + count + ")"); 466 } 467 return la; 468 } 469 470 481 public long getLong(String key) throws ConfigException 482 { 483 return (getLongsInternal(key, 1))[0]; 484 } 485 486 501 public long getLong(String key, long defaultValue) throws ConfigException { 502 try { 503 return (getLongsInternal(key, 1))[0]; 504 } 505 catch (ConfigException e) { 506 if (e.reason != e.NOT_FOUND) { 507 throw e; 508 } 509 return defaultValue; 510 } 511 } 512 513 524 public long[] getLongs(String key) throws ConfigException 525 { 526 return getLongsInternal(key, GET_ALL); 527 } 528 529 542 public long[] getLongs(String key, long[] defaultValue) 543 throws ConfigException 544 { 545 try { 546 return getLongsInternal(key, GET_ALL); 547 } 548 catch (ConfigException e) { 549 if (e.reason != e.NOT_FOUND) { 550 throw e; 551 } 552 return defaultValue; 553 } 554 } 555 556 576 private final int[] getIntsInternal(String key, int count) 577 throws ConfigException 578 { 579 Object obj; 580 try { 581 obj = get(key); 582 } 583 catch (KeywordValueException e) { 584 throw new ConfigException(e.getMessage()); 585 } 586 if (obj == null) { 587 throw new ConfigException(ConfigException.NOT_FOUND, 588 "Key \"" + key 589 + "\" not found in configuration."); 590 } 591 int[] ia = null; 592 if (obj.getClass().isArray()) { 593 int len = Array.getLength(obj); 594 ia = new int[len]; 595 for (int i=0; i<len; i++) { 596 try { 597 ia[i] = (int)Integer.parseInt(Array.get(obj,i).toString()); 598 } 599 catch (Throwable e) { 600 throw new ConfigException("Element " + i + 601 " is not an integer."); 602 } 603 } 604 } 605 else { 606 ia = new int[1]; 607 try { 608 ia[0] = Integer.parseInt(obj.toString()); 609 } 610 catch (Throwable e) { 611 throw new ConfigException("Element 0 is not an integer."); 612 } 613 } 614 if ((count != GET_ALL) && (ia.length != count)) { 615 throw new ConfigException(ConfigException.COUNT, 616 "Key \"" + key 617 + "\" has " + ia.length + " elements. (expected " 618 + count + ")"); 619 } 620 return ia; 621 } 622 623 634 public int getInt(String key) throws ConfigException 635 { 636 return (getIntsInternal(key, 1))[0]; 637 } 638 639 652 public int getInt(String key, int defaultValue) throws ConfigException 653 { 654 try { 655 return (getIntsInternal(key, 1))[0]; 656 } 657 catch (ConfigException e) { 658 if (e.reason != e.NOT_FOUND) { 659 throw e; 660 } 661 return defaultValue; 662 } 663 } 664 665 676 public int[] getInts(String key) throws ConfigException 677 { 678 return getIntsInternal(key, GET_ALL); 679 } 680 681 694 public int[] getInts(String key, int[] defaultValue) throws ConfigException 695 { 696 try { 697 return getIntsInternal(key, GET_ALL); 698 } 699 catch (ConfigException e) { 700 if (e.reason != e.NOT_FOUND) { 701 throw e; 702 } 703 return defaultValue; 704 } 705 } 706 707 724 private final String [] getStringsInternal(String key, int count) 725 throws ConfigException 726 { 727 Object obj; 728 try { 729 obj = get(key); 730 } 731 catch (KeywordValueException e) { 732 throw new ConfigException(e.getMessage()); 733 } 734 if (obj == null) { 735 throw new ConfigException(ConfigException.NOT_FOUND, 736 "Key \"" + key 737 + "\" not found in configuration."); 738 } 739 String [] sa = null; 740 if (obj.getClass().isArray()) { 741 int len = Array.getLength(obj); 742 sa = new String [len]; 743 for (int i=0; i<len; i++) { 744 try { 745 sa[i] = Array.get(obj,i).toString(); 746 } 747 catch (Throwable e) { 748 throw new ConfigException("Element " + i + 749 " is not a String."); 750 } 751 } 752 } 753 else { 754 sa = new String [1]; 755 try { 756 sa[0] = obj.toString(); 757 } 758 catch (Throwable e) { 759 throw new ConfigException("Element 0 is not a String."); 760 } 761 } 762 if ((count != GET_ALL) && (sa.length != count)) { 763 throw new ConfigException(ConfigException.COUNT, 764 "Key \"" + key 765 + "\" has " + sa.length + " elements. (expected " 766 + count + ")"); 767 } 768 return sa; 769 } 770 771 782 public String getString(String key) 783 throws ConfigException 784 { 785 return (getStringsInternal(key, 1))[0]; 786 } 787 788 801 public String getString(String key, String defaultValue) 802 throws ConfigException 803 { 804 try { 805 return (getStringsInternal(key, 1))[0]; 806 } 807 catch (ConfigException e) { 808 if (e.reason != e.NOT_FOUND) { 809 throw e; 810 } 811 return defaultValue; 812 } 813 } 814 815 826 public String [] getStrings(String key) throws ConfigException 827 { 828 return getStringsInternal(key, GET_ALL); 829 } 830 831 844 public String [] getStrings(String key, String [] defaultValue) 845 throws ConfigException 846 { 847 try { 848 return getStringsInternal(key, GET_ALL); 849 } 850 catch (ConfigException e) { 851 if (e.reason != e.NOT_FOUND) { 852 throw e; 853 } 854 return defaultValue; 855 } 856 } 857 858 880 private final boolean[] getBooleansInternal(String key, int count) 881 throws ConfigException 882 { 883 Object obj; 884 try { 885 obj = get(key); 886 } 887 catch (KeywordValueException e) { 888 throw new ConfigException(e.getMessage()); 889 } 890 if (obj == null) { 891 throw new ConfigException(ConfigException.NOT_FOUND, 892 "Key \"" + key 893 + "\" not found in configuration."); 894 } 895 boolean[] ba = null; 896 if (obj.getClass().isArray()) { 897 int len = Array.getLength(obj); 898 ba = new boolean[len]; 899 for (int i=0; i<len; i++) { 900 try { 901 ba[i] = Boolean.valueOf( 902 Array.get(obj,i).toString().toLowerCase()).booleanValue(); 903 } catch (Throwable e) { 904 throw new ConfigException("Element " + i + 905 " is not a boolean."); 906 } 907 } 908 } 909 else { 910 ba = new boolean[1]; 911 try { 912 ba[0] = 913 Boolean.valueOf(obj.toString().toLowerCase()).booleanValue(); 914 } 915 catch (Throwable e) { 916 throw new ConfigException("Element 0 is not a boolean."); 917 } 918 } 919 if ((count != GET_ALL) && (ba.length != count)) { 920 throw new ConfigException(ConfigException.COUNT, 921 "Key \"" + key 922 + "\" has " + ba.length + " elements. (expected " 923 + count + ")"); 924 } 925 return ba; 926 } 927 928 939 public boolean getBoolean(String key) throws ConfigException 940 { 941 return (getBooleansInternal(key, 1))[0]; 942 } 943 944 957 public boolean getBoolean(String key, boolean defaultValue) 958 throws ConfigException 959 { 960 try { 961 return(getBooleansInternal(key, 1)[0]); 962 } 963 catch (ConfigException e) { 964 if (e.reason != e.NOT_FOUND) { 965 throw e; 966 } 967 return defaultValue; 968 } 969 } 970 971 982 public boolean[] getBooleans(String key) throws ConfigException 983 { 984 return getBooleansInternal(key, GET_ALL); 985 } 986 987 1000 public boolean[] getBooleans(String key, boolean[] defaultValue) 1001 throws ConfigException 1002 { 1003 try { 1004 return getBooleansInternal(key, GET_ALL); 1005 } 1006 catch (ConfigException e) { 1007 if (e.reason != e.NOT_FOUND) { 1008 throw e; 1009 } 1010 return defaultValue; 1011 } 1012 } 1013 1014 1034 private final double[] getDoublesInternal(String key, int count) 1035 throws ConfigException 1036 { 1037 Object obj; 1038 try { 1039 obj = get(key); 1040 } 1041 catch (KeywordValueException e) { 1042 throw new ConfigException(e.getMessage()); 1043 } 1044 if (obj == null) { 1045 throw new ConfigException(ConfigException.NOT_FOUND, 1046 "Key \"" + key 1047 + "\" not found in configuration."); 1048 } 1049 double[] da = null; 1050 if (obj.getClass().isArray()) { 1051 int len = Array.getLength(obj); 1052 da = new double[len]; 1053 for (int i=0; i<len; i++) { 1054 try { 1055 da[i] = Double.valueOf( 1056 Array.get(obj,i).toString()).doubleValue(); 1057 } 1058 catch (Throwable e) { 1059 throw new ConfigException("Element " + i + 1060 " is not a double."); 1061 } 1062 } 1063 } 1064 else { 1065 da = new double[1]; 1066 try { 1067 da[0] = Double.valueOf(obj.toString()).doubleValue(); 1068 } 1069 catch (Throwable e) { 1070 throw new ConfigException("Element 0 is not a long."); 1071 } 1072 } 1073 if ((count != GET_ALL) && (da.length != count)) { 1074 throw new ConfigException(ConfigException.COUNT, 1075 "Key \"" + key 1076 + "\" has " + da.length + " elements. (expected " 1077 + count + ")"); 1078 } 1079 return da; 1080 } 1081 1082 1092 public double getDouble(String key) throws ConfigException 1093 { 1094 return (getDoublesInternal(key, 1))[0]; 1095 } 1096 1097 1110 public double getDouble(String key, double defaultValue) 1111 throws ConfigException 1112 { 1113 try { 1114 return(getDoublesInternal(key, 1)[0]); 1115 } 1116 catch (ConfigException e) { 1117 if (e.reason != e.NOT_FOUND) { 1118 throw e; 1119 } 1120 return defaultValue; 1121 } 1122 } 1123 1124 1135 public double[] getDoubles(String key) throws ConfigException 1136 { 1137 return getDoublesInternal(key, GET_ALL); 1138 } 1139 1140 1153 public double[] getDoubles(String key, double[] defaultValue) 1154 throws ConfigException 1155 { 1156 try { 1157 return getDoublesInternal(key, GET_ALL); 1158 } 1159 catch (ConfigException e) { 1160 if (e.reason != e.NOT_FOUND) { 1161 throw e; 1162 } 1163 return defaultValue; 1164 } 1165 } 1166 1174 public Object getDataSource(String key) throws ConfigException { 1175 Object obj; 1176 try { 1177 obj = get(key); 1178 } 1179 catch (KeywordValueException e) { 1180 throw new ConfigException(e.getMessage()); 1181 } 1182 if (obj == null) { 1183 throw new ConfigException(ConfigException.NOT_FOUND, 1184 "Key \"" + key 1185 + "\" not found in configuration."); 1186 } 1187 Object ds = null; 1188 if (obj.getClass().isArray()) { 1189 throw new ConfigException("The value is an array. The method returns a single DataStruct Object."); 1190 } 1191 else { 1192 1193 try { 1194 ds = obj; 1195 } 1196 catch (Throwable e) { 1197 throw new ConfigException("Element is not a DataSource."); 1198 } 1199 } 1200 return ds; 1201 } 1202 1203 1216 public Object getDataSource(String key, Object defaultValue) 1217 throws ConfigException { 1218 try { 1219 return getDataSource(key); 1220 } 1221 catch (ConfigException e) { 1222 if (e.reason != e.NOT_FOUND) { 1223 throw e; 1224 } 1225 return defaultValue; 1226 } 1227 } 1228 1229 1230} 1231 1232 | Popular Tags |