1 19 20 package org.netbeans.api.debugger; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.io.OutputStream ; 27 import java.io.PrintWriter ; 28 import java.lang.reflect.Array ; 29 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Collections ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 import java.util.Set ; 37 import org.openide.ErrorManager; 38 39 import org.openide.filesystems.FileLock; 40 import org.openide.filesystems.FileSystem; 41 import org.openide.filesystems.Repository; 42 import org.openide.filesystems.FileObject; 43 import org.openide.util.RequestProcessor; 44 45 46 51 public abstract class Properties { 52 53 54 private static Properties defaultProperties; 55 56 61 public static synchronized Properties getDefault () { 62 if (defaultProperties == null) 63 defaultProperties = new PropertiesImpl (); 64 return defaultProperties; 65 } 66 67 74 public abstract String getString (String propertyName, String defaultValue); 75 76 82 public abstract void setString (String propertyName, String value); 83 84 91 public abstract int getInt (String propertyName, int defaultValue); 92 93 99 public abstract void setInt (String propertyName, int value); 100 101 108 public abstract char getChar (String propertyName, char defaultValue); 109 110 116 public abstract void setChar (String propertyName, char value); 117 118 125 public abstract float getFloat (String propertyName, float defaultValue); 126 127 133 public abstract void setFloat (String propertyName, float value); 134 135 142 public abstract long getLong (String propertyName, long defaultValue); 143 144 150 public abstract void setLong (String propertyName, long value); 151 152 159 public abstract double getDouble (String propertyName, double defaultValue); 160 161 167 public abstract void setDouble (String propertyName, double value); 168 169 176 public abstract boolean getBoolean (String propertyName, boolean defaultValue); 177 178 184 public abstract void setBoolean (String propertyName, boolean value); 185 186 193 public abstract byte getByte (String propertyName, byte defaultValue); 194 195 201 public abstract void setByte (String propertyName, byte value); 202 203 210 public abstract short getShort (String propertyName, short defaultValue); 211 212 218 public abstract void setShort (String propertyName, short value); 219 226 227 public abstract Object getObject (String propertyName, Object defaultValue); 228 229 235 public abstract void setObject (String propertyName, Object value); 236 237 244 public abstract Object [] getArray (String propertyName, Object [] defaultValue); 245 246 252 public abstract void setArray (String propertyName, Object [] value); 253 254 261 public abstract Collection getCollection (String propertyName, Collection defaultValue); 262 263 269 public abstract void setCollection (String propertyName, Collection value); 270 271 278 public abstract Map getMap (String propertyName, Map defaultValue); 279 280 286 public abstract void setMap (String propertyName, Map value); 287 288 294 public abstract Properties getProperties (String propertyName); 295 296 297 299 305 public interface Reader { 306 307 312 public String [] getSupportedClassNames (); 313 314 322 public Object read (String className, Properties properties); 323 324 330 public void write (Object object, Properties properties); 331 } 332 333 private final static class PrimitiveRegister { 334 335 private HashMap properties = new HashMap (); 336 private boolean isInitialized = false; 337 338 339 public synchronized String getProperty (String propertyName, String defaultValue) { 340 if (!isInitialized) load (); 341 isInitialized = true; 342 String value = (String ) properties.get (propertyName); 343 if (value != null) return value; 344 return defaultValue; 345 } 346 347 public synchronized void setProperty (String propertyName, String value) { 348 if (!isInitialized) load (); 349 isInitialized = true; 350 properties.put (propertyName, value); 351 save (); 352 } 353 354 private void load () { 355 BufferedReader br = null; 356 try { 357 FileObject fo = findSettings(); 358 InputStream is = fo.getInputStream (); 359 br = new BufferedReader (new InputStreamReader (is)); 360 361 String l = br.readLine (); 362 while (l != null) { 363 int i = l.indexOf (':'); 364 if (i > 0) 365 properties.put (l.substring (0, i), l.substring (i + 1)); 366 l = br.readLine (); 367 } 368 br.close (); 369 } catch (IOException ex) { 370 ErrorManager.getDefault().notify(ex); 371 } 372 } 373 374 private RequestProcessor.Task task; 377 378 private synchronized void save () { 379 if (task == null) { 380 task = new RequestProcessor("Debugger Properties Save RP", 1).create( 381 new Runnable () { 382 public void run () { 383 saveIn (); 384 } 385 } 386 ); 387 } 388 task.schedule(4000); 389 } 390 391 private void saveIn () { 392 PrintWriter pw = null; 393 FileLock lock = null; 394 try { 395 FileObject fo = findSettings(); 396 lock = fo.lock (); 397 OutputStream os = fo.getOutputStream (lock); 398 pw = new PrintWriter (os); 399 400 Set s = properties.keySet (); 401 ArrayList l = new ArrayList (s); 402 Collections.sort (l); 403 int i, k = l.size (); 404 for (i = 0; i < k; i++) { 405 String key = (String ) l.get (i); 406 Object value = properties.get (key); 407 if (value != null) { 408 pw.println ("" + key + ":" + value); 410 } 411 } 412 pw.flush (); 413 } catch (IOException ex) { 414 ErrorManager.getDefault().notify( 415 ErrorManager.getDefault().annotate(ex, 416 "Can not save debugger settings.")); 417 } finally { 418 if (pw != null) { 419 pw.close (); 420 } 421 if (lock != null) { 422 lock.releaseLock (); 423 } 424 } 425 } 426 427 private static FileObject findSettings() throws IOException { 428 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 429 FileObject r = fs.findResource("Services"); if (r == null) { 431 r = fs.getRoot ().createFolder("Services"); } 433 FileObject fo = r.getFileObject 434 ("org-netbeans-modules-debugger-Settings", "properties"); if (fo == null) { 436 fo = r.createData 437 ("org-netbeans-modules-debugger-Settings", "properties"); } 439 return fo; 440 } 441 } 442 443 private static class PropertiesImpl extends Properties { 444 445 private static final Object BAD_OBJECT = new Object (); 446 private static final String BAD_STRING = ""; 447 private static final Map BAD_MAP = new HashMap (); 448 private static final Collection BAD_COLLECTION = new ArrayList (); 449 private static final Object [] BAD_ARRAY = new Object [0]; 450 451 private HashMap register; 452 453 454 private PrimitiveRegister impl = new PrimitiveRegister (); 455 456 private void initReaders () { 457 register = new HashMap (); 458 Iterator i = DebuggerManager.getDebuggerManager().lookup(null, Reader .class).iterator (); 459 while (i.hasNext ()) { 460 Reader r = (Reader ) i.next (); 461 String [] ns = r.getSupportedClassNames (); 462 int j, jj = ns.length; 463 for (j = 0; j < jj; j++) 464 register.put (ns [j], r); 465 } 466 } 467 468 private Reader findReader (String typeID) { 469 synchronized (this) { 470 if (register == null) { 471 initReaders (); 472 } 473 } 474 475 Reader r = (Reader ) register.get (typeID); 476 if (r != null) return r; 477 478 Class c = null; 479 try { 480 c = getClassLoader ().loadClass (typeID); 481 } catch (ClassNotFoundException e) { 482 ErrorManager.getDefault().notify(e); 483 return null; 484 } 485 while ((c != null) && (register.get (c.getName ()) == null)) { 486 c = c.getSuperclass (); 487 } 488 if (c != null) 489 r = (Reader ) register.get (c.getName ()); 490 return r; 491 } 492 493 494 495 496 498 public String getString (String propertyName, String defaultValue) { 499 String value = impl.getProperty (propertyName, null); 500 if (value == null) return defaultValue; 501 if (!value.startsWith ("\"")) { 502 System.out.println("Can not read string " + value + "."); 503 return defaultValue; 504 } 505 return value.substring (1, value.length () - 1); 506 } 507 508 public void setString (String propertyName, String value) { 509 if (value != null) { 510 impl.setProperty (propertyName, "\"" + value + "\""); 511 } else { 512 impl.setProperty (propertyName, value); 513 } 514 } 515 516 public int getInt (String propertyName, int defaultValue) { 517 String value = impl.getProperty (propertyName, null); 518 if (value == null) return defaultValue; 519 int val = Integer.parseInt (value); 520 return val; 521 } 522 523 public void setInt (String propertyName, int value) { 524 impl.setProperty (propertyName, Integer.toString (value)); 525 } 526 527 public char getChar (String propertyName, char defaultValue) { 528 String value = impl.getProperty (propertyName, null); 529 if (value == null) return defaultValue; 530 char val = value.charAt (0); 531 return val; 532 } 533 534 public void setChar (String propertyName, char value) { 535 impl.setProperty (propertyName, "" + value); 536 } 537 538 public float getFloat (String propertyName, float defaultValue) { 539 String value = impl.getProperty (propertyName, null); 540 if (value == null) return defaultValue; 541 float val = Float.parseFloat (value); 542 return val; 543 } 544 545 public void setFloat (String propertyName, float value) { 546 impl.setProperty (propertyName, Float.toString (value)); 547 } 548 549 public long getLong (String propertyName, long defaultValue) { 550 String value = impl.getProperty (propertyName, null); 551 if (value == null) return defaultValue; 552 long val = Long.parseLong (value); 553 return val; 554 } 555 556 public void setLong (String propertyName, long value) { 557 impl.setProperty (propertyName, Long.toString (value)); 558 } 559 560 public double getDouble (String propertyName, double defaultValue) { 561 String value = impl.getProperty (propertyName, null); 562 if (value == null) return defaultValue; 563 double val = Double.parseDouble (value); 564 return val; 565 } 566 567 public void setDouble (String propertyName, double value) { 568 impl.setProperty (propertyName, Double.toString (value)); 569 } 570 571 public boolean getBoolean (String propertyName, boolean defaultValue) { 572 String value = impl.getProperty (propertyName, null); 573 if (value == null) return defaultValue; 574 boolean val = value.equals ("true"); 575 return val; 576 } 577 578 public void setBoolean (String propertyName, boolean value) { 579 impl.setProperty (propertyName, value ? "true" : "false"); 580 } 581 582 public byte getByte (String propertyName, byte defaultValue) { 583 String value = impl.getProperty (propertyName, null); 584 if (value == null) return defaultValue; 585 byte val = Byte.parseByte (value); 586 return val; 587 } 588 589 public void setByte (String propertyName, byte value) { 590 impl.setProperty (propertyName, Byte.toString (value)); 591 } 592 593 public short getShort (String propertyName, short defaultValue) { 594 String value = impl.getProperty (propertyName, null); 595 if (value == null) return defaultValue; 596 short val = Short.parseShort (value); 597 return val; 598 } 599 600 public void setShort (String propertyName, short value) { 601 impl.setProperty (propertyName, Short.toString (value)); 602 } 603 604 public Object getObject (String propertyName, Object defaultValue) { 605 String typeID = impl.getProperty (propertyName, null); 606 if (typeID == null) return defaultValue; 607 if (typeID.equals ("# null")) 608 return null; 609 if (!typeID.startsWith ("# ")) { 610 if (typeID.startsWith ("\"")) { 611 String s = getString (propertyName, BAD_STRING); 612 if (s == BAD_STRING) return defaultValue; 613 return s; 614 } 615 System.out.println("Can not read object " + typeID + ". No reader registered for type " + typeID + "."); 616 return defaultValue; 617 } 618 typeID = typeID.substring (2); 619 Class c = null; 620 try { 621 c = Class.forName (typeID); 622 } catch (ClassNotFoundException e) { 623 } 624 if (c != null) { 625 if (Map .class.isAssignableFrom (c)) { 626 Map m = getMap (propertyName, BAD_MAP); 627 if (m == BAD_MAP) return defaultValue; 628 return m; 629 } 630 if (Object [].class.isAssignableFrom (c)) { 631 Object [] os = getArray (propertyName, BAD_ARRAY); 632 if (os == BAD_ARRAY) return defaultValue; 633 return os; 634 } 635 if (Collection .class.isAssignableFrom (c)) { 636 Collection co = getCollection (propertyName, BAD_COLLECTION); 637 if (co == BAD_COLLECTION) return defaultValue; 638 return co; 639 } 640 } 641 Reader r = findReader (typeID); 642 if (r == null) { 643 System.out.println("Can not read object. No reader registered for type " + typeID + "."); 644 return defaultValue; 645 } 646 return r.read (typeID, getProperties (propertyName)); 647 } 648 649 public void setObject (String propertyName, Object value) { 650 if (value == null) { 651 impl.setProperty (propertyName, "# null"); 652 return; 653 } 654 if (value instanceof String ) { 655 setString (propertyName, (String ) value); 656 return; 657 } 658 if (value instanceof Map ) { 659 setMap (propertyName, (Map ) value); 660 return; 661 } 662 if (value instanceof Collection ) { 663 setCollection (propertyName, (Collection ) value); 664 return; 665 } 666 if (value instanceof Object []) { 667 setArray (propertyName, (Object []) value); 668 return; 669 } 670 671 Reader r = findReader (value.getClass ().getName ()); 673 if (r == null) { 674 System.out.println ("Can not write object " + value); 675 return; 676 } 677 678 r.write (value, getProperties (propertyName)); 680 impl.setProperty (propertyName, "# " + value.getClass ().getName ()); 681 } 682 683 public Object [] getArray (String propertyName, Object [] defaultValue) { 684 String typeID = impl.getProperty (propertyName, null); 685 String arrayType = impl.getProperty (propertyName + ".array_type", null); 686 Properties p = getProperties (propertyName); 687 int l = p.getInt ("length", -1); 688 if (l < 0) return defaultValue; 689 Object [] os = null; 690 try { 691 os = (Object []) Array.newInstance ( 692 getClassLoader ().loadClass (arrayType), 693 l 694 ); 695 } catch (ClassNotFoundException ex) { 696 ErrorManager.getDefault().notify(ex); 697 os = new Object [l]; 698 } 699 for (int i = 0; i < l; i++) { 700 Object o = p.getObject ("" + i, BAD_OBJECT); 701 if (o == BAD_OBJECT) return defaultValue; 702 os [i] = o; 703 } 704 return os; 705 } 706 707 public void setArray (String propertyName, Object [] value) { 708 impl.setProperty (propertyName, "# array"); 709 impl.setProperty (propertyName + ".array_type", value.getClass ().getComponentType ().getName ()); 710 Properties p = getProperties (propertyName); 711 int i, k = value.length; 712 p.setInt ("length", k); 713 for (i = 0; i < k; i++) 714 p.setObject ("" + i, value [i]); 715 } 716 717 public Collection getCollection (String propertyName, Collection defaultValue) { 718 String typeID = impl.getProperty (propertyName, null); 719 if (typeID == null) return defaultValue; 720 if (!typeID.startsWith ("# ")) return defaultValue; 721 Collection c = null; 722 try { 723 c = (Collection ) Class.forName (typeID.substring (2)).newInstance (); 724 } catch (ClassNotFoundException ex) { 725 return defaultValue; 726 } catch (InstantiationException ex) { 727 return defaultValue; 728 } catch (IllegalAccessException ex) { 729 return defaultValue; 730 } 731 Properties p = getProperties (propertyName); 732 int i, k = p.getInt ("length", 0); 733 for (i = 0; i < k; i++) { 734 Object o = p.getObject ("" + i, BAD_OBJECT); 735 if (o == BAD_OBJECT) return defaultValue; 736 c.add (o); 737 } 738 return c; 739 } 740 741 public void setCollection (String propertyName, Collection value) { 742 if (value == null) return; 743 impl.setProperty (propertyName, "# " + value.getClass ().getName ()); 744 Properties p = getProperties (propertyName); 745 Iterator it = value.iterator (); 746 int i = 0; 747 p.setInt ("length", value.size ()); 748 while (it.hasNext ()) { 749 p.setObject ("" + i, it.next ()); 750 i++; 751 } 752 } 753 754 public Map getMap (String propertyName, Map defaultValue) { 755 String typeID = impl.getProperty (propertyName, null); 756 if (typeID == null) return defaultValue; 757 if (!typeID.startsWith ("# ")) return defaultValue; 758 Map m = null; 759 try { 760 m = (Map ) Class.forName (typeID.substring (2)).newInstance (); 761 } catch (ClassNotFoundException ex) { 762 return defaultValue; 763 } catch (InstantiationException ex) { 764 return defaultValue; 765 } catch (IllegalAccessException ex) { 766 return defaultValue; 767 } 768 Properties p = getProperties (propertyName); 769 int i, k = p.getInt ("length", 0); 770 for (i = 0; i < k; i++) { 771 Object key = p.getObject ("" + i + "-key", BAD_OBJECT); 772 if (key == BAD_OBJECT) return defaultValue; 773 Object value = p.getObject ("" + i + "-value", BAD_OBJECT); 774 if (value == BAD_OBJECT) return defaultValue; 775 m.put (key, value); 776 } 777 return m; 778 } 779 780 public void setMap (String propertyName, Map value) { 781 if (value == null) return; 782 impl.setProperty (propertyName, "# " + value.getClass ().getName ()); 783 Properties p = getProperties (propertyName); 784 Iterator it = value.keySet ().iterator (); 785 int i = 0; 786 p.setInt ("length", value.size ()); 787 while (it.hasNext ()) { 788 Object o = it.next (); 789 p.setObject ("" + i + "-key", o); 790 p.setObject ("" + i + "-value", value.get (o)); 791 i++; 792 } 793 } 794 795 public Properties getProperties (String propertyName) { 796 return new DelegatingProperties (this, propertyName); 797 } 798 799 private static ClassLoader classLoader; 800 private static ClassLoader getClassLoader () { 801 if (classLoader == null) 802 classLoader = (ClassLoader ) org.openide.util.Lookup. 804 getDefault ().lookup (ClassLoader .class); 805 return classLoader; 806 } 807 } 808 809 private static class DelegatingProperties extends Properties { 810 811 private Properties delegatingProperties; 812 private String root; 813 814 815 DelegatingProperties (Properties properties, String root) { 816 delegatingProperties = properties; 817 this.root = root; 818 } 819 820 public String getString (String propertyName, String defaultValue) { 821 return delegatingProperties.getString (root + '.' + propertyName, defaultValue); 822 } 823 824 public void setString (String propertyName, String value) { 825 delegatingProperties.setString (root + '.' + propertyName, value); 826 } 827 828 public int getInt (String propertyName, int defaultValue) { 829 return delegatingProperties.getInt (root + '.' + propertyName, defaultValue); 830 } 831 832 public void setInt (String propertyName, int value) { 833 delegatingProperties.setInt (root + '.' + propertyName, value); 834 } 835 836 public byte getByte (String propertyName, byte defaultValue) { 837 return delegatingProperties.getByte (root + '.' + propertyName, defaultValue); 838 } 839 840 public void setByte (String propertyName, byte value) { 841 delegatingProperties.setByte (root + '.' + propertyName, value); 842 } 843 844 public char getChar (String propertyName, char defaultValue) { 845 return delegatingProperties.getChar (root + '.' + propertyName, defaultValue); 846 } 847 848 public void setChar (String propertyName, char value) { 849 delegatingProperties.setChar (root + '.' + propertyName, value); 850 } 851 852 public boolean getBoolean (String propertyName, boolean defaultValue) { 853 return delegatingProperties.getBoolean (root + '.' + propertyName, defaultValue); 854 } 855 856 public void setBoolean (String propertyName, boolean value) { 857 delegatingProperties.setBoolean (root + '.' + propertyName, value); 858 } 859 860 public short getShort (String propertyName, short defaultValue) { 861 return delegatingProperties.getShort (root + '.' + propertyName, defaultValue); 862 } 863 864 public void setShort (String propertyName, short value) { 865 delegatingProperties.setShort (root + '.' + propertyName, value); 866 } 867 868 public long getLong (String propertyName, long defaultValue) { 869 return delegatingProperties.getLong (root + '.' + propertyName, defaultValue); 870 } 871 872 public void setLong (String propertyName, long value) { 873 delegatingProperties.setLong (root + '.' + propertyName, value); 874 } 875 876 public double getDouble (String propertyName, double defaultValue) { 877 return delegatingProperties.getDouble (root + '.' + propertyName, defaultValue); 878 } 879 880 public void setDouble (String propertyName, double value) { 881 delegatingProperties.setDouble (root + '.' + propertyName, value); 882 } 883 884 public float getFloat (String propertyName, float defaultValue) { 885 return delegatingProperties.getFloat (root + '.' + propertyName, defaultValue); 886 } 887 888 public void setFloat (String propertyName, float value) { 889 delegatingProperties.setFloat (root + '.' + propertyName, value); 890 } 891 892 public Object getObject (String propertyName, Object defaultValue) { 893 return delegatingProperties.getObject (root + '.' + propertyName, defaultValue); 894 } 895 896 public void setObject (String propertyName, Object value) { 897 delegatingProperties.setObject (root + '.' + propertyName, value); 898 } 899 900 public Object [] getArray (String propertyName, Object [] defaultValue) { 901 return delegatingProperties.getArray (root + '.' + propertyName, defaultValue); 902 } 903 904 public void setArray (String propertyName, Object [] value) { 905 delegatingProperties.setArray (root + '.' + propertyName, value); 906 } 907 908 public Collection getCollection (String propertyName, Collection defaultValue) { 909 return delegatingProperties.getCollection (root + '.' + propertyName, defaultValue); 910 } 911 912 public void setCollection (String propertyName, Collection value) { 913 delegatingProperties.setCollection (root + '.' + propertyName, value); 914 } 915 916 public Map getMap (String propertyName, Map defaultValue) { 917 return delegatingProperties.getMap (root + '.' + propertyName, defaultValue); 918 } 919 920 public void setMap (String propertyName, Map value) { 921 delegatingProperties.setMap (root + '.' + propertyName, value); 922 } 923 924 public Properties getProperties (String propertyName) { 925 return new DelegatingProperties (delegatingProperties, root + '.' + propertyName); 926 } 927 } 928 } 929 | Popular Tags |