1 22 package org.jboss.util.property; 23 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.security.AccessController ; 28 import java.security.PrivilegedAction ; 29 import java.util.List ; 30 import java.util.LinkedList ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import java.util.HashMap ; 35 import java.util.Set ; 36 import java.util.HashSet ; 37 import java.util.Collections ; 38 import java.util.Properties ; 39 import javax.naming.Context ; 40 import javax.naming.ldap.LdapContext ; 41 42 import org.jboss.util.NullArgumentException; 43 44 55 public class PropertyMap extends Properties 56 { 57 58 private static final long serialVersionUID = 8747802918099008518L; 59 60 61 public static final String PROPERTY_NAME_SEPARATOR = "."; 62 63 64 public static final String [] EMPTY_ARRAY_PROPERTY = new String [0]; 65 66 67 protected transient List unboundListeners; 68 69 70 protected transient Map boundListeners; 71 72 79 private transient Map jndiMap; 80 private final static Object NULL_VALUE = new Object (); 81 82 87 public PropertyMap(Properties defaults) 88 { 89 super(defaults); 90 init(); 91 } 92 93 96 public PropertyMap() 97 { 98 this(null); 99 } 100 101 103 private void init() 104 { 105 unboundListeners = Collections.synchronizedList(new ArrayList ()); 106 boundListeners = Collections.synchronizedMap(new HashMap ()); 107 jndiMap = new HashMap (); 108 109 PrivilegedAction action = new PrivilegedAction () 110 { 111 public Object run() 112 { 113 Object value = System.getProperty(Context.PROVIDER_URL); 114 if (value == null) value = NULL_VALUE; 115 jndiMap.put(Context.PROVIDER_URL, value); 116 117 value = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 118 if (value == null) value = NULL_VALUE; 119 jndiMap.put(Context.INITIAL_CONTEXT_FACTORY, value); 120 121 value = System.getProperty(Context.OBJECT_FACTORIES); 122 if (value == null) value = NULL_VALUE; 123 jndiMap.put(Context.OBJECT_FACTORIES, value); 124 125 value = System.getProperty(Context.URL_PKG_PREFIXES); 126 if (value == null) value = NULL_VALUE; 127 jndiMap.put(Context.URL_PKG_PREFIXES, value); 128 129 value = System.getProperty(Context.STATE_FACTORIES); 130 if (value == null) value = NULL_VALUE; 131 jndiMap.put(Context.STATE_FACTORIES, value); 132 133 value = System.getProperty(Context.DNS_URL); 134 if (value == null) value = NULL_VALUE; 135 jndiMap.put(Context.DNS_URL, value); 136 137 value = System.getProperty(LdapContext.CONTROL_FACTORIES); 138 if (value == null) value = NULL_VALUE; 139 jndiMap.put(LdapContext.CONTROL_FACTORIES, value); 140 return null; 141 } 142 }; 143 AccessController.doPrivileged(action); 144 } 145 146 150 private void updateJndiCache(String name, String value) 151 { 152 if( name == null ) 153 return; 154 155 boolean isJndiProperty = name.equals(Context.PROVIDER_URL) 156 || name.equals(Context.INITIAL_CONTEXT_FACTORY) 157 || name.equals(Context.OBJECT_FACTORIES) 158 || name.equals(Context.URL_PKG_PREFIXES) 159 || name.equals(Context.STATE_FACTORIES) 160 || name.equals(Context.DNS_URL) 161 || name.equals(LdapContext.CONTROL_FACTORIES) 162 ; 163 if( isJndiProperty == true ) 164 jndiMap.put(name, value); 165 } 166 167 174 public Object put(Object name, Object value) 175 { 176 if (name == null) 177 throw new NullArgumentException("name"); 178 180 boolean add = !containsKey(name); 182 Object prev = super.put(name, value); 183 184 PropertyEvent event = 185 new PropertyEvent(this, name.toString(), value.toString()); 186 187 if (add) 189 { 190 firePropertyAdded(event); 191 } 192 else 193 { 194 firePropertyChanged(event); 195 } 196 197 return prev; 198 } 199 200 206 public Object remove(Object name) 207 { 208 if (name == null) 209 throw new NullArgumentException("name"); 210 211 boolean contains = containsKey(name); 213 Object value = null; 214 215 if (contains) 216 { 217 value = super.remove(name); 218 if (defaults != null) 219 { 220 Object obj = defaults.remove(name); 221 if (value == null) 222 { 223 value = obj; 224 } 225 } 226 jndiMap.remove(name); 228 229 PropertyEvent event = new PropertyEvent(this, name.toString(), value.toString()); 230 firePropertyRemoved(event); 231 } 232 233 return value; 234 } 235 236 240 public Set keySet(final boolean includeDefaults) 241 { 242 if (includeDefaults) 243 { 244 Set set = new HashSet (); 245 set.addAll(defaults.keySet()); 246 set.addAll(super.keySet()); 247 return Collections.synchronizedSet(set); 248 } 249 250 return super.keySet(); 251 } 252 253 257 public Set entrySet(final boolean includeDefaults) 258 { 259 if (includeDefaults) 260 { 261 Set set = new HashSet (); 262 set.addAll(defaults.entrySet()); 263 set.addAll(super.entrySet()); 264 return Collections.synchronizedSet(set); 265 } 266 267 return super.entrySet(); 268 } 269 270 275 public void addPropertyListener(PropertyListener listener) 276 { 277 if (listener == null) 278 throw new NullArgumentException("listener"); 279 280 if (listener instanceof BoundPropertyListener) 281 { 282 addPropertyListener((BoundPropertyListener) listener); 283 } 284 else 285 { 286 if (!unboundListeners.contains(listener)) 288 unboundListeners.add(listener); 289 } 290 } 291 292 297 protected void addPropertyListener(BoundPropertyListener listener) 298 { 299 String name = listener.getPropertyName(); 301 302 List list = (List ) boundListeners.get(name); 304 305 if (list == null) 307 { 308 list = Collections.synchronizedList(new ArrayList ()); 309 boundListeners.put(name, list); 310 } 311 312 if (!list.contains(listener)) 314 { 315 list.add(listener); 316 listener.propertyBound(this); 318 } 319 } 320 321 326 public void addPropertyListeners(PropertyListener[] listeners) 327 { 328 if (listeners == null) 329 throw new NullArgumentException("listeners"); 330 331 for (int i = 0; i < listeners.length; i++) 332 { 333 addPropertyListener(listeners[i]); 334 } 335 } 336 337 343 public boolean removePropertyListener(PropertyListener listener) 344 { 345 if (listener == null) 346 throw new NullArgumentException("listener"); 347 348 boolean removed = false; 349 if (listener instanceof BoundPropertyListener) 350 { 351 removed = removePropertyListener((BoundPropertyListener) listener); 352 } 353 else 354 { 355 removed = unboundListeners.remove(listener); 356 } 357 358 return removed; 359 } 360 361 367 protected boolean removePropertyListener(BoundPropertyListener listener) 368 { 369 String name = listener.getPropertyName(); 371 372 List list = (List ) boundListeners.get(name); 374 boolean removed = false; 375 if (list != null) 376 { 377 removed = list.remove(listener); 378 379 if (removed) listener.propertyUnbound(this); 381 } 382 return removed; 383 } 384 385 391 private void firePropertyAdded(List list, PropertyEvent event) 392 { 393 if (list == null) return; 394 395 int size = list.size(); 396 for (int i = 0; i < size; i++) 397 { 398 PropertyListener listener = (PropertyListener) list.get(i); 399 listener.propertyAdded(event); 400 } 401 } 402 403 408 protected void firePropertyAdded(PropertyEvent event) 409 { 410 if (boundListeners != null) 412 { 413 List list = (List ) boundListeners.get(event.getPropertyName()); 414 if (list != null) 415 { 416 firePropertyAdded(list, event); 417 } 418 } 419 420 firePropertyAdded(unboundListeners, event); 422 } 423 424 430 private void firePropertyRemoved(List list, PropertyEvent event) 431 { 432 if (list == null) return; 433 434 int size = list.size(); 435 for (int i = 0; i < size; i++) 436 { 437 PropertyListener listener = (PropertyListener) list.get(i); 438 listener.propertyRemoved(event); 439 } 440 } 441 442 447 protected void firePropertyRemoved(PropertyEvent event) 448 { 449 if (boundListeners != null) 451 { 452 List list = (List ) boundListeners.get(event.getPropertyName()); 453 if (list != null) 454 { 455 firePropertyRemoved(list, event); 456 } 457 } 458 459 firePropertyRemoved(unboundListeners, event); 461 } 462 463 469 private void firePropertyChanged(List list, PropertyEvent event) 470 { 471 if (list == null) return; 472 473 int size = list.size(); 474 for (int i = 0; i < size; i++) 475 { 476 PropertyListener listener = (PropertyListener) list.get(i); 477 listener.propertyChanged(event); 478 } 479 } 480 481 486 protected void firePropertyChanged(PropertyEvent event) 487 { 488 if (boundListeners != null) 490 { 491 List list = (List ) boundListeners.get(event.getPropertyName()); 492 if (list != null) 493 { 494 firePropertyChanged(list, event); 495 } 496 } 497 498 firePropertyChanged(unboundListeners, event); 500 } 501 502 509 protected String makePrefixedPropertyName(String base, String prefix) 510 { 511 String name = base; 512 513 if (prefix != null) 514 { 515 StringBuffer buff = new StringBuffer (base); 516 if (prefix != null) 517 { 518 buff.insert(0, PROPERTY_NAME_SEPARATOR); 519 buff.insert(0, prefix); 520 } 521 return buff.toString(); 522 } 523 524 return name; 525 } 526 527 533 public void load(String prefix, Map map) throws PropertyException 534 { 535 if (map == null) 537 throw new NullArgumentException("map"); 538 539 Iterator iter = map.keySet().iterator(); 541 while (iter.hasNext()) 542 { 543 String key = String.valueOf(iter.next()); 545 String name = makePrefixedPropertyName(key, prefix); 546 String value = String.valueOf(map.get(name)); 547 548 setProperty(name, value); 550 } 551 } 552 553 558 public void load(Map map) throws PropertyException 559 { 560 load(null, map); 561 } 562 563 568 public void load(PropertyReader reader) throws PropertyException, IOException 569 { 570 if (reader == null) 571 throw new NullArgumentException("reader"); 572 573 load(reader.readProperties()); 574 } 575 576 581 public void load(String className) throws PropertyException, IOException 582 { 583 if (className == null) 584 throw new NullArgumentException("className"); 585 586 PropertyReader reader = null; 587 588 try 589 { 590 Class type = Class.forName(className); 591 reader = (PropertyReader) type.newInstance(); 592 } 593 catch (Exception e) 594 { 595 throw new PropertyException(e); 596 } 597 598 load(reader); 600 } 601 602 612 public Object setProperty(String name, String value) 613 { 614 updateJndiCache(name, value); 615 return put(name, value); 616 } 617 618 public String getProperty(String name) 619 { 620 Object value = jndiMap.get(name); 621 if (value != null) 622 { 623 return (value == NULL_VALUE) ? null : (String ) value; 625 } 626 return super.getProperty(name); 627 } 628 629 635 public String removeProperty(String name) 636 { 637 return (String ) remove(name); 638 } 639 640 641 648 protected String makeIndexPropertyName(String base, int index) 649 { 650 return base + PROPERTY_NAME_SEPARATOR + index; 651 } 652 653 666 public String [] getArrayProperty(String base, String [] defaultValues) 667 { 668 if (base == null) 669 throw new NullArgumentException("base"); 670 671 List list = new LinkedList (); 673 674 int i = 0; 675 while (true) 676 { 677 String name = makeIndexPropertyName(base, i); 679 680 String value = getProperty(name); 682 683 if (value != null) 684 { 685 list.add(value); 686 } 687 else if (i >= 0) 688 { 689 break; } 691 692 i++; 693 } 694 695 String values[] = defaultValues; 696 697 if (list.size() != 0) 699 { 700 values = (String []) list.toArray(new String [list.size()]); 701 } 702 703 return values; 704 } 705 706 712 public String [] getArrayProperty(String name) 713 { 714 return getArrayProperty(name, EMPTY_ARRAY_PROPERTY); 715 } 716 717 722 public Iterator names() 723 { 724 return keySet().iterator(); 725 } 726 727 733 public boolean containsProperty(String name) 734 { 735 return containsKey(name); 736 } 737 738 744 public PropertyGroup getPropertyGroup(String basename) 745 { 746 return new PropertyGroup(basename, this); 747 } 748 749 756 public PropertyGroup getPropertyGroup(String basename, int index) 757 { 758 String name = makeIndexPropertyName(basename, index); 759 return getPropertyGroup(name); 760 } 761 762 private void readObject(ObjectInputStream stream) 763 throws IOException , ClassNotFoundException 764 { 765 init(); 767 768 stream.defaultReadObject(); 769 } 770 771 private void writeObject(ObjectOutputStream stream) 772 throws IOException 773 { 774 stream.defaultWriteObject(); 775 } 776 } 777 | Popular Tags |