1 package net.myvietnam.mvncore.configuration; 2 55 import java.util.ArrayList ; 56 import java.util.Iterator ; 57 import java.util.List ; 58 import java.util.NoSuchElementException ; 59 import java.util.Properties ; 60 import java.util.Vector ; 61 import javax.naming.Binding ; 62 import javax.naming.Context ; 63 import javax.naming.InitialContext ; 64 import javax.naming.NamingEnumeration ; 65 import javax.naming.NamingException ; 66 import org.apache.commons.lang.StringUtils; 67 import org.apache.commons.logging.Log; 68 import org.apache.commons.logging.LogFactory; 69 75 public class JNDIConfiguration 76 extends BaseConfiguration 77 implements Configuration 78 { 79 private static Log log = LogFactory.getLog(JNDIConfiguration.class); 80 private String prefix; 81 private Context envCtx; 82 private List clearedProperties = new ArrayList (); 83 87 public JNDIConfiguration() 88 { 89 } 90 96 public void addProperty(String key, Object token) 97 { 98 throw new Error ("This operation is not supported"); 99 } 100 109 private void recursiveGetKeys( 110 List keys, 111 NamingEnumeration enumeration, 112 String key) 113 throws NamingException 114 { 115 while (enumeration.hasMoreElements()) 116 { 117 Binding binding = (Binding ) enumeration.next(); 118 StringBuffer newKey = new StringBuffer (); 119 newKey.append(key); 120 if (newKey.length() > 0) 121 { 122 newKey.append("."); 123 } 124 newKey.append(binding.getName()); 125 if (binding.getObject() instanceof Context ) 126 { 127 Context c = (Context ) binding.getObject(); 128 NamingEnumeration enum2 = c.listBindings(""); 129 recursiveGetKeys(keys, enum2, newKey.toString()); 130 } 131 else 132 { 133 if (!keys.contains(newKey.toString())) 134 { 135 keys.add(newKey.toString()); 136 } 137 } 138 } 139 } 140 146 public Iterator getKeys() 147 { 148 return getKeys(""); 149 } 150 157 public Iterator getKeys(String key) 158 { 159 List keys = new ArrayList (); 160 try 161 { 162 String [] splitKeys = StringUtils.split(key, "."); 163 for (int i = 0; i < splitKeys.length; i++) 164 { 165 keys.add(splitKeys[i]); 166 } 167 Context context = null; 168 if (keys.size() == 0) 169 { 170 context = getContext(); 171 } 172 else 173 { 174 context = 175 getStartingContextPoint( 176 keys, 177 getContext().listBindings("")); 178 } 179 if (context != null) 180 { 181 NamingEnumeration enumeration = context.listBindings(""); 182 recursiveGetKeys(keys, enumeration, key); 183 } 184 } 185 catch (NamingException ne) 186 { 187 log.warn(ne); 188 } 189 return keys.iterator(); 190 } 191 200 private Context getStartingContextPoint(List keys, NamingEnumeration enumeration) 201 throws NamingException 202 { 203 String keyToSearchFor = (String ) keys.get(0); 204 log.debug("Key to search for is " + keyToSearchFor); 205 while (enumeration.hasMoreElements()) 206 { 207 Binding binding = (Binding ) enumeration.next(); 208 log.debug( 209 "Binding for name: " 210 + binding.getName() 211 + ", object:" 212 + binding.getObject() 213 + ", class:" 214 + binding.getClassName()); 215 if (binding.getObject() instanceof Context 216 && binding.getName().equals(keyToSearchFor)) 217 { 218 keys.remove(0); 219 Context c = (Context ) binding.getObject(); 220 if (keys.size() > 0) 221 { 222 return getStartingContextPoint(keys, c.listBindings("")); 223 } 224 else 225 { 226 return c; 227 } 228 } 229 } 230 return null; 231 } 232 244 public Properties getProperties(String key) 245 { 246 throw new Error ("This operation is not supported"); 247 } 248 public boolean isEmpty() 249 { 250 try 251 { 252 NamingEnumeration enumeration = getContext().listBindings(""); 253 return !enumeration.hasMore(); 254 } 255 catch (NamingException ne) 256 { 257 log.warn(ne); 258 return true; 259 } 260 } 261 268 public Object getProperty(String key) 269 { 270 throw new Error ("This operation is not supported"); 271 } 272 280 public void setProperty(String key, Object value) 281 { 282 throw new Error ("This operation is not supported"); 283 } 284 290 public void clearProperty(String key) 291 { 292 if (!clearedProperties.contains(key)) 293 { 294 clearedProperties.add(key); 295 } 296 } 297 301 public boolean containsKey(String key) 302 { 303 if (clearedProperties.contains(key)) 304 { 305 return false; 306 } 307 key = StringUtils.replace(key, ".", "/"); 308 try 309 { 310 getContext().lookup(key); 312 return true; 313 } 314 catch (javax.naming.NamingException ne) 315 { 316 return false; 317 } 318 } 319 326 public Configuration subset(String prefix) 327 { 328 BaseConfiguration c = new BaseConfiguration(); 329 Iterator keys = this.getKeys(); 330 boolean validSubset = false; 331 while (keys.hasNext()) 332 { 333 Object key = keys.next(); 334 if (key instanceof String && ((String ) key).startsWith(prefix)) 335 { 336 if (!validSubset) 337 { 338 validSubset = true; 339 } 340 String newKey = null; 341 346 if (((String ) key).length() == prefix.length()) 347 { 348 newKey = prefix; 349 } 350 else 351 { 352 newKey = ((String ) key).substring(prefix.length() + 1); 353 } 354 358 Object value = getValueFromJNDI(key.toString()); 359 if (value instanceof String ) 360 { 361 c.addPropertyDirect(newKey, interpolate((String ) value)); 362 } 363 else 364 { 365 c.addPropertyDirect(newKey, value); 366 } 367 } 368 } 369 if (validSubset) 370 { 371 return c; 372 } 373 else 374 { 375 return null; 376 } 377 } 378 379 389 public Boolean getBoolean(String key, Boolean defaultValue) 390 { 391 Object value = getValueFromJNDI(key); 392 if (value instanceof Boolean ) 393 { 394 return (Boolean ) value; 395 } 396 else if (value instanceof String ) 397 { 398 return testBoolean((String ) value); 399 } 400 else if (value == null) 401 { 402 if (defaults != null) 403 { 404 return defaults.getBoolean(key, defaultValue); 405 } 406 else 407 { 408 return defaultValue; 409 } 410 } 411 else 412 { 413 throw new ClassCastException ( 414 '\'' + key + "' doesn't map to a Boolean object"); 415 } 416 } 417 418 430 public Byte getByte(String key, Byte defaultValue) 431 { 432 Object value = getValueFromJNDI(key); 433 if (value instanceof Byte ) 434 { 435 return (Byte ) value; 436 } 437 else if (value instanceof String ) 438 { 439 Byte b = new Byte ((String ) value); 440 return b; 441 } 442 else if (value == null) 443 { 444 return defaultValue; 445 } 446 else 447 { 448 throw new ClassCastException ( 449 '\'' + key + "' doesn't map to a Byte object"); 450 } 451 } 452 453 465 public Double getDouble(String key, Double defaultValue) 466 { 467 Object value = this.getValueFromJNDI(key); 468 if (value instanceof Double ) 469 { 470 return (Double ) value; 471 } 472 else if (value instanceof String ) 473 { 474 Double d = new Double ((String ) value); 475 return d; 476 } 477 else if (value == null) 478 { 479 return defaultValue; 480 } 481 else 482 { 483 throw new ClassCastException ( 484 '\'' + key + "' doesn't map to a Double object"); 485 } 486 } 487 488 500 public Float getFloat(String key, Float defaultValue) 501 { 502 Object value = getValueFromJNDI(key); 503 if (value instanceof Float ) 504 { 505 return (Float ) value; 506 } 507 else if (value instanceof String ) 508 { 509 Float f = new Float ((String ) value); 510 return f; 511 } 512 else if (value == null) 513 { 514 return defaultValue; 515 } 516 else 517 { 518 throw new ClassCastException ( 519 '\'' + key + "' doesn't map to a Float object"); 520 } 521 } 522 523 535 public Integer getInteger(String key, Integer defaultValue) 536 { 537 Object value = getValueFromJNDI(key); 538 if (value instanceof Integer ) 539 { 540 return (Integer ) value; 541 } 542 else if (value instanceof String ) 543 { 544 Integer i = new Integer ((String ) value); 545 return i; 546 } 547 else if (value == null) 548 { 549 return defaultValue; 550 } 551 else 552 { 553 throw new ClassCastException ( 554 '\'' + key + "' doesn't map to a Integer object"); 555 } 556 } 557 558 570 public Long getLong(String key, Long defaultValue) 571 { 572 Object value = getValueFromJNDI(key); 573 if (value instanceof Long ) 574 { 575 return (Long ) value; 576 } 577 else if (value instanceof String ) 578 { 579 Long l = new Long ((String ) value); 580 return l; 581 } 582 else if (value == null) 583 { 584 return defaultValue; 585 } 586 else 587 { 588 throw new ClassCastException ( 589 '\'' + key + "' doesn't map to a Long object"); 590 } 591 } 592 593 605 public Short getShort(String key, Short defaultValue) 606 { 607 Object value = getValueFromJNDI(key); 608 if (value instanceof Short ) 609 { 610 return (Short ) value; 611 } 612 else if (value instanceof String ) 613 { 614 Short s = new Short ((String ) value); 615 return s; 616 } 617 else if (value == null) 618 { 619 return defaultValue; 620 } 621 else 622 { 623 throw new ClassCastException ( 624 '\'' + key + "' doesn't map to a Short object"); 625 } 626 } 627 628 637 public String getString(String key, String defaultValue) 638 { 639 try 640 { 641 Object o = getValueFromJNDI(key); 642 if (o == null) 643 { 644 return defaultValue; 645 } 646 else 647 { 648 return (String ) o; 649 } 650 } 651 catch (NoSuchElementException nsee) 652 { 653 return defaultValue; 654 } 655 } 656 665 public String [] getStringArray(String key) 666 { 667 Object value = getValueFromJNDI(key); 668 String [] tokens; 669 if (value instanceof String ) 670 { 671 tokens = new String [1]; 672 tokens[0] = interpolate((String ) value); 673 } 674 else if (value instanceof Container) 675 { 676 tokens = new String [((Container) value).size()]; 677 for (int i = 0; i < tokens.length; i++) 678 { 679 tokens[i] = interpolate((String ) ((Container) value).get(i)); 680 } 681 } 682 else if (value == null) 683 { 684 tokens = new String [0]; 685 } 686 else 687 { 688 throw new ClassCastException ( 689 '\'' + key + "' doesn't map to a String/Vector object"); 690 } 691 return tokens; 692 } 693 694 703 public Vector getVector(String key, Vector defaultValue) 704 { 705 try 706 { 707 Object value = this.getValueFromJNDI(key); 708 if (value != null) 709 { 710 Vector v = new Vector (1); 711 v.add(value.toString()); 712 return v; 713 } 714 else 715 { 716 if (defaultValue == null) 717 { 718 defaultValue = new Vector (); 719 } 720 return defaultValue; 721 } 722 } 723 catch (NoSuchElementException nsse) 724 { 725 return defaultValue; 726 } 727 } 728 731 public String getPrefix() 732 { 733 return prefix; 734 } 735 739 public void setPrefix(String prefix) 740 { 741 this.prefix = prefix; 742 } 743 private Object getValueFromJNDI(String key) 744 { 745 if (clearedProperties.contains(key)) 746 { 747 return null; 748 } 749 try 750 { 751 key = StringUtils.replace(key, ".", "/"); 752 return getContext().lookup(key); 753 } 754 catch (java.util.NoSuchElementException nsse) 755 { 756 return null; 757 } 758 catch (NamingException ne) 759 { 760 return null; 761 } 762 } 763 private Context getContext() throws NamingException 764 { 765 if (envCtx == null) 766 { 767 Context initCtx = new InitialContext (); 768 envCtx = (Context ) initCtx.lookup(getPrefix()); 769 } 770 return envCtx; 771 } 772 } 773 | Popular Tags |