1 2 24 25 package com.lutris.util; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.io.PrintWriter ; 32 import java.util.HashMap ; 33 import java.util.Hashtable ; 34 import java.util.Iterator ; 35 import java.util.Set ; 36 import java.util.Vector ; 37 38 import org.enhydra.util.AbsConfigFile; 39 import org.enhydra.util.ConfConfiguration; 40 import org.enhydra.util.JNDIAdapter; 41 42 93 public class ConfigFile extends AbsConfigFile{ 94 95 100 public ConfigFile(InputStream inputStream) throws ConfigException { 101 config = new Config(); 102 order = new Vector (); 103 comments = new Hashtable (); 104 ConfigParser parser = new ConfigParser(inputStream); 105 try { 106 parser.process(this); 107 } 108 catch (ParseException e) { 109 throw new ConfigException(ConfigException.SYNTAX, e.getMessage()); 110 } 111 112 try { 114 readJndi(); 115 } catch (Exception e){} 116 117 } 118 119 public ConfigFile () { 120 super(); 121 } 122 123 130 public ConfigFile (File file) throws ConfigException, IOException { 131 super(file); 132 } 133 134 139 public ConfigFile(KeywordValueTable kvt) throws ConfigException { 140 super(kvt); 141 } 142 143 146 protected void readJndi() throws ConfigException { 147 try { 148 if (jndiAdapt == null){ 149 try { 150 jndiAdapt = new JNDIAdapter(this.file.getAbsolutePath(), "org.enhydra.spi.conf.ConfFileInitialContextFactory"); 151 } 152 catch(Exception e){ 153 jndiAdapt = new JNDIAdapter(); 154 } 155 } 156 157 String [] leafKeys = jndiAdapt.leafKeys(); 158 if (leafKeys != null) { 159 int leafKeysLength = leafKeys.length; 160 String newKey = null; 161 String stringValue; 162 String [] stringValues; 163 boolean has_configuration_keys = false; 164 ConfConfiguration configuration_value = null; 165 for (int i=0; i<leafKeysLength; i++){ 166 String leafKey = leafKeys[i]; 167 if (leafKey != null) { 168 if (leafKey.equals("configuration/ConfConfigurationFactory")) { 169 has_configuration_keys = true; 170 configuration_value = (ConfConfiguration) jndiAdapt.get("configuration/ConfConfigurationFactory"); 171 } 172 else { 173 if (jndiAdapt.isArray(leafKey)){ 174 newKey = jndiAdapt.removeArrayMark(leafKey); 175 if (newKey !=null) { 176 stringValues = jndiAdapt.getStrings(newKey); 177 newKey = jndiAdapt.makeConfigString(newKey); 178 config.set(newKey, stringValues); 179 if (!order.contains(newKey)) 180 order.addElement(newKey); 181 comments.put(newKey, ""); 182 } 183 } 184 else { 185 Object ovalue = jndiAdapt.get(leafKey); 186 newKey = jndiAdapt.makeConfigString(leafKey); 187 188 if (ovalue instanceof java.lang.String ){ 190 stringValue = (String )ovalue; 191 if (stringValue.startsWith("jndi:")){ 192 193 stringValue = stringValue.substring(5); 194 Object resource = jndiAdapt.getResource(stringValue); 195 if (resource != null) { 196 config.set(newKey, resource); 197 jndiParameterNames.put(newKey,stringValue); 198 } 199 else { 200 config.set(newKey, "jndi:"+stringValue); 201 } 202 } 203 else { 204 config.set(newKey, stringValue); 205 } 206 if ( (newKey !=null) && (!order.contains(newKey))) { 207 order.addElement(newKey); 208 } 209 comments.put(newKey, ""); 210 } 211 else { 212 } 213 } } } } if (has_configuration_keys && (configuration_value != null)) { 218 HashMap elems = configuration_value.getResults(); 219 Set set1 = elems.keySet(); 220 Iterator iter1 = set1.iterator(); 221 while (iter1.hasNext()){ 222 String key = (String )iter1.next(); 223 if (jndiAdapt.isArray(key)) { 224 stringValues = (String [])elems.get(key); 225 newKey = jndiAdapt.removeArrayMark(key); 226 newKey = jndiAdapt.makeConfigString(newKey); 227 config.set(newKey, stringValues); 228 if (!order.contains(newKey)) 229 order.addElement(newKey); 230 comments.put(newKey, ""); 231 } 232 else { 233 stringValue = (String )elems.get(key); 234 235 newKey = jndiAdapt.makeConfigString(key); 236 237 if (stringValue.startsWith("jndi:")){ 238 stringValue = stringValue.substring(5); 239 Object resource = jndiAdapt.getResource(stringValue); 240 if (resource != null) { 241 config.set(newKey, resource); 242 jndiParameterNames.put(newKey,stringValue); 243 } 244 else { 245 config.set(newKey, "jndi:"+stringValue); 246 } 247 } 248 else { 249 config.set(newKey, stringValue); 250 } 251 if (!order.contains(newKey)) 252 order.addElement(newKey); 253 comments.put(newKey, ""); 254 } 255 } } 258 259 } 260 } catch (Exception e){ 262 throw new ConfigException("Error in reading JNDI configuration parameters."); 265 } 266 } 267 268 273 public void write(OutputStream outputStream) { 274 PrintWriter out = new PrintWriter (outputStream, true); 275 boolean isArray = false; 276 String key; 277 String comment; 278 String [] values; 279 Hashtable remaining = new Hashtable (); 280 String [] remainingkeys = config.leafKeys(); 281 282 for (int i = 0; i < remainingkeys.length; i++) 285 remaining.put(remainingkeys[i], "X"); 286 287 for (int i = 0; i < order.size(); i++) { 289 key = (String ) order.elementAt(i); 290 comment = (String ) comments.get(key); 291 isArray = false; 292 try { 293 Object o = config.get(key); 294 if (o == null) { 295 continue; 296 } 297 isArray = o.getClass().isArray(); 298 if (isArray) { 299 Object [] oa = (Object [])o; 300 if ((oa.length > 0) && (oa[0] instanceof java.lang.String )) 301 values = (String []) o; 302 else { 303 values = new String [oa.length]; 304 for (int k = 0; k < oa.length; k++) 305 values[k] = oa[k].toString(); 306 } 307 } 308 else { 309 values = new String [1]; 310 if (o instanceof java.lang.String ) 311 values[0] = (String ) o; 312 else 313 values[0] = o.toString(); 314 } 315 } 316 catch (KeywordValueException e) { 317 values = null; 318 } 319 if ((values == null) || (values.length == 0)) { 321 if ((comment != null) && !(comment.equals(""))) { 322 if (comment.endsWith("\n")) 323 out.print(comment); 324 else 325 out.println(comment); 326 } 327 out.print(key); 328 if (isArray) out.print("[]"); 329 out.println(" ="); 330 } 331 else { 332 if ((comment != null) && !(comment.equals(""))) { 333 if (comment.endsWith("\n")) 334 out.print(comment); 335 else 336 out.println(comment); 337 } 338 if (isArray) 339 out.print(key + "[] = " + quoteStr(values[0])); 340 else { 341 if (jndiParameterNames.containsKey(key)){ 342 String newValueString = "jndi:" + (String )jndiParameterNames.get(key); 343 out.print(key + " = " + quoteStr(newValueString)); 344 } 346 else { 347 out.print(key + " = " + quoteStr(values[0])); 348 } 349 350 } 351 for (int j = 1; j < values.length; j++) 352 out.print(", " + quoteStr(values[j])); 353 out.println(); 354 } 355 remaining.remove(key); 356 } 357 358 comment = (String ) comments.get(TRAILING_COMMENT); 360 if ((comment != null) && !(comment.equals(""))) { 361 if (comment.endsWith("\n")) { 362 out.print(comment); 363 } 364 else { 365 out.println(comment); 366 } 367 } 368 remaining.remove(TRAILING_COMMENT); 369 370 371 384 385 386 int i=0; 387 String lastWord = ""; 388 while (i != remainingkeys.length) { 389 key = remainingkeys[i]; 390 i++; 391 if (remaining.get(key) != null) { 393 isArray = false; 394 if (!key.startsWith(lastWord)) 395 out.println(""); 396 int dot = key.indexOf('.'); 397 if (dot == -1) 398 dot = key.length(); 399 lastWord = key.substring(0,dot); 400 401 try { 402 Object o = config.get(key); 403 if (o == null) 404 continue; 405 isArray = o.getClass().isArray(); 406 if (isArray) { 407 Object [] oa = (Object [])o; 408 if (oa[0] instanceof java.lang.String ) 409 values = (String []) o; 410 else { 411 values = new String [oa.length]; 412 for (int k = 0; k < oa.length; k++) 413 values[k] = oa[k].toString(); 414 } 415 } 416 else { 417 values = new String [1]; 418 if (o instanceof java.lang.String ) 419 values[0] = (String ) o; 420 else 421 values[0] = o.toString(); 422 } 423 } 424 catch (KeywordValueException e) { 425 values = null; 426 } 427 if ((values == null) || (values.length == 0)) { 429 out.println(key + " ="); 430 } 431 else { 432 if (isArray) 433 out.print(key + "[] = " + quoteStr(values[0])); 434 else 435 out.print(key + " = " + quoteStr(values[0])); 436 437 for (int j = 1; j < values.length; j++) 438 out.print(", " + quoteStr(values[j])); out.println(); 441 } 442 } 443 } 444 } 445 446 447 private boolean containsWhiteSpace(String str) { 448 if (str.indexOf(" ") != -1) { 449 return true; 450 } 451 else if (str.indexOf("\t") != -1) { 452 return true; 453 } 454 return false; 455 } 456 457 458 private static final String quoteStr(String s) { 459 if ((s == null) || (s.length() < 1)) return ""; 460 char[] chars = s.toCharArray(); 461 StringBuffer sb = new StringBuffer (); 462 boolean needQuotes = false; 463 for (int i=0; i<chars.length; i++) { 464 switch (chars[i]) { 465 case '\n': 467 needQuotes = true; 468 sb.append("\\n"); 469 break; 470 case '\b': 471 needQuotes = true; 472 sb.append("\\b"); 473 break; 474 case '\r': 475 needQuotes = true; 476 sb.append("\\r"); 477 break; 478 case '\f': 479 needQuotes = true; 480 sb.append("\\f"); 481 break; 482 case '"': 483 needQuotes = true; 484 sb.append("\\\""); 485 break; 486 case '\\': 487 needQuotes = true; 488 sb.append("\\\\"); 489 break; 490 491 case '\t': case ' ': case '!': case '#': case '$': 494 case '%': case '&': case '\'': case '(': case ')': 495 case '*': case '+': case ',': case '/': case ':': 496 case ';': case '<': case '=': case '>': case '?': 497 case '[': case ']': case '^': case '`': case '{': 498 case '|': case '}': case '~': 499 needQuotes = true; 500 sb.append(chars[i]); 501 break; 502 503 default: 505 if ((chars[i] < ' ') || (chars[i] == 0x7f)) { 506 needQuotes = true; 507 int ival = (int) chars[i]; 508 sb.append('\\'); 509 sb.append(digits[(ival & 0xc0) >> 6]); 510 sb.append(digits[(ival & 0x38) >> 3]); 511 sb.append(digits[(ival & 0x07)]); 512 } 513 else if (chars[i] > 0x7f) { 514 needQuotes = true; 515 int ival = (int) chars[i]; 516 sb.append("\\u"); 517 sb.append(digits[(ival & 0xf000) >> 12]); 518 sb.append(digits[(ival & 0x0f00) >> 8]); 519 sb.append(digits[(ival & 0x00f0) >> 4]); 520 sb.append(digits[(ival & 0x000f)]); 521 } 522 else 523 sb.append(chars[i]); 524 } 525 } 526 if (needQuotes) 527 return( "\"" + sb.toString() + "\""); 528 return sb.toString(); 529 } 530 531 private static final char[] digits = { 532 '0', '1', '2', '3', '4', '5', '6', '7', 533 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 534 }; 535 536 } 537 538 | Popular Tags |