1 19 20 package org.netbeans.api.registry; 21 22 import org.netbeans.modules.registry.ApiContextFactory; 23 import org.netbeans.modules.registry.OrderingSupport; 24 import org.netbeans.spi.registry.BasicContext; 25 import org.netbeans.spi.registry.MergedContextProvider; 26 import org.netbeans.spi.registry.ResettableContext; 27 import org.netbeans.spi.registry.SpiUtils; 28 import org.openide.ErrorManager; 29 import org.openide.util.Lookup; 30 import org.openide.util.Mutex; 31 import org.openide.util.NbBundle; 32 33 import java.awt.*; 34 import java.beans.PropertyChangeListener ; 35 import java.lang.ref.WeakReference ; 36 import java.net.MalformedURLException ; 37 import java.net.URL ; 38 import java.util.*; 39 import java.util.List ; 40 41 95 public final class Context { 96 97 98 BasicContext delegate; 99 100 102 private static WeakHashMap contextCache = new WeakHashMap(); 103 104 private static final Mutex.Privileged privilegedMutex = new Mutex.Privileged(); 110 private static final Mutex mutex = new Mutex (privilegedMutex); 111 112 static { 113 ApiContextFactory.DEFAULT = new ApiContextFactoryImpl(); 114 } 115 116 private static BasicContext defaultRootContext; 117 118 private static final ErrorManager errorManager = ErrorManager.getDefault().getInstance("org.netbeans.api.registry"); 119 120 private Context(BasicContext delegate) { 121 this.delegate = delegate; 122 } 123 124 128 138 public static Context getDefault() { 139 return Context.getApiContext(getRC()); 140 } 141 142 143 149 public static Context merge (final Context[] delegates) { 150 BasicContext mergedBasic = SpiUtils.merge(new MergedContextProvider() { 151 public void addPropertyChangeListener(PropertyChangeListener listener) { 152 } 153 154 public void removePropertyChangeListener(PropertyChangeListener listener) { 155 } 156 157 public BasicContext[] getDelegates() { 158 List basicDelegates = new ArrayList(); 159 for (int i = 0; i < delegates.length; i++) { 160 Context delegate = delegates[i]; 161 basicDelegates.add(delegate.delegate); 162 } 163 164 return (BasicContext[])basicDelegates.toArray(new BasicContext[0]); 165 } 166 }); 167 return SpiUtils.createContext(mergedBasic); 168 } 169 170 175 public String getContextName() { 176 return delegate.getContextName(); 177 } 178 179 184 public Context getRootContext() { 185 return getApiContext(delegate.getRootContext()); 186 } 187 188 194 public String getAbsoluteContextName() { 195 BasicContext ctx = delegate; 196 StringBuffer sb = new StringBuffer (ctx.getContextName()); 197 while (ctx.getParentContext() != null) { 198 ctx = ctx.getParentContext(); 199 if (ctx.getContextName().equals("/")) { 200 sb.insert(0, ctx.getContextName()); 201 } else { 202 sb.insert(0, "/"); 203 sb.insert(0, ctx.getContextName()); 204 } 205 } 206 return sb.toString(); 207 } 208 209 216 public Context getSubcontext(String subcontextName) { 217 Mutex.Privileged mp = getMutexPrivileged(); 218 try { 219 mp.enterReadAccess(); 220 StringTokenizer tok = new StringTokenizer(subcontextName, "/"); BasicContext ctx = delegate; 222 while (tok.hasMoreTokens() && ctx != null) { 223 String name = tok.nextToken(); 224 ctx = ctx.getSubcontext(name); 225 } 226 return getApiContext(ctx); 227 } finally { 228 mp.exitReadAccess(); 229 } 230 } 231 232 237 public Context getParentContext() { 238 Mutex.Privileged mp = getMutexPrivileged(); 239 try { 240 mp.enterReadAccess(); 241 return getApiContext(delegate.getParentContext()); 242 } finally { 243 mp.exitReadAccess(); 244 } 245 } 246 247 257 public Context createSubcontext(String subcontextName) throws ContextException { 258 Mutex.Privileged mp = getMutexPrivileged(); 259 try { 260 mp.enterWriteAccess(); 261 StringTokenizer tok = new StringTokenizer(subcontextName, "/"); BasicContext ctx = delegate; 263 while (tok.hasMoreTokens()) { 264 String name = tok.nextToken(); 265 BasicContext ctx2 = ctx.getSubcontext(name); 266 if (ctx2 != null) { 267 ctx = ctx2; 268 } else { 269 ctx = ctx.createSubcontext(name); 270 } 271 } 272 return getApiContext(ctx); 273 } finally { 274 mp.exitWriteAccess(); 275 } 276 } 277 278 287 public void destroySubcontext(String subcontextName) throws ContextException { 288 int index = subcontextName.lastIndexOf('/'); 289 BasicContext ctx = delegate; 290 if (index != -1) { 291 ctx = getSubcontext(subcontextName.substring(0, index)).delegate; 292 subcontextName = subcontextName.substring(index+1); 293 } 294 Mutex.Privileged mp = getMutexPrivileged(); 295 try { 296 mp.enterWriteAccess(); 297 ctx.destroySubcontext(subcontextName); 298 } finally { 299 mp.exitWriteAccess(); 300 } 301 } 302 303 304 308 314 public Collection getSubcontextNames() { 315 Mutex.Privileged mp = getMutexPrivileged(); 316 try { 317 mp.enterReadAccess(); 318 return delegate.getSubcontextNames(); 319 } finally { 320 mp.exitReadAccess(); 321 } 322 } 323 324 330 public Collection getBindingNames() { 331 Mutex.Privileged mp = getMutexPrivileged(); 332 try { 333 mp.enterReadAccess(); 334 return delegate.getBindingNames(); 335 } finally { 336 mp.exitReadAccess(); 337 } 338 } 339 340 346 public Collection getAttributeNames(String bindingName) { 347 Mutex.Privileged mp = getMutexPrivileged(); 348 try { 349 mp.enterReadAccess(); 350 return delegate.getAttributeNames(bindingName); 351 } finally { 352 mp.exitReadAccess(); 353 } 354 } 355 356 360 370 public void orderContext(List names) { 371 StringBuffer value = new StringBuffer (); 372 Iterator it = names.iterator(); 373 while (it.hasNext()) { 374 Object o = it.next(); 375 if (!(o instanceof String )) { 376 throw new IllegalArgumentException ("OrderContext: Passed list contains item which is not String - "+o); 377 } 378 String item = (String )o; 379 checkItem(item); 384 value.append(item); 385 value.append(","); 386 } 387 if (value.length() > 0) { 388 value.setLength(value.length()-1); 389 } 390 setAttribute(null, "fullorder", value.toString()); 391 } 392 393 private void checkItem(String name) { 394 if (name.endsWith("/")) { 395 name = name.substring(0, name.length()-1); 396 if (getSubcontext(name) == null) { 397 throw new IllegalArgumentException ("OrderContext: Passed list contains non-existing subcontext - "+name); 398 } 399 } else { 400 if (!getBindingNames().contains(name)) { 401 throw new IllegalArgumentException ("OrderContext: Passed list contains non-existing binding - "+name); 402 } 403 } 404 } 405 406 419 public List getOrderedObjects() { 420 Mutex.Privileged mp = getMutexPrivileged(); 421 try { 422 mp.enterReadAccess(); 423 List ar = new ArrayList(); 424 Iterator it = OrderingSupport.DEFAULT.getOrderedNames(this).iterator(); 425 while (it.hasNext()) { 426 String item = (String )it.next(); 427 if (item.endsWith("/")) { 428 Context ctx = getSubcontext(item.substring(0, item.length()-1)); 429 if (ctx != null) { 430 ar.add(ctx); 431 } 432 } else { 433 Object o = getObject(item, null); 434 if (o != null) { 435 ar.add(o); 436 } 437 } 438 } 439 return ar; 440 } finally { 441 mp.exitReadAccess(); 442 } 443 } 444 445 460 public List getOrderedNames() { 461 Mutex.Privileged mp = getMutexPrivileged(); 462 try { 463 mp.enterReadAccess(); 464 return OrderingSupport.DEFAULT.getOrderedNames(this); 465 } finally { 466 mp.exitReadAccess(); 467 } 468 } 469 470 471 475 489 public Object getObject(String bindingName, Object defaultValue) { 490 Object o = getObject(bindingName); 491 492 if (o instanceof ObjectRef) { 493 Object origin = o; 495 while (o != null && o instanceof ObjectRef) { 496 o = ((ObjectRef)o).getObject(); 497 } 498 if (o == null) { 500 o = origin; 501 } 502 } 503 504 if (o == null) { 505 return defaultValue; 506 } else { 507 return o; 508 } 509 } 510 511 518 public ObjectRef getRef(String bindingName) { 519 Object o = getObject(bindingName); 520 521 if (o instanceof ObjectRef) { 522 return (ObjectRef)o; 523 } else { 524 return null; 525 } 526 } 527 528 private Object getObject(String bindingName) { 529 Object o = null; 530 Mutex.Privileged mp = getMutexPrivileged(); 531 try { 532 mp.enterReadAccess(); 533 o = delegate.lookupObject(bindingName); 534 } catch (ContextException ex) { 535 if (errorManager.isLoggable(ErrorManager.INFORMATIONAL)) { 536 errorManager.annotate(ex, 537 NbBundle.getMessage(Context.class, "MSG_get_object", bindingName, getAbsoluteContextName())); 538 errorManager.notify(ErrorManager.INFORMATIONAL, ex); 539 } 540 } finally { 541 mp.exitReadAccess(); 542 } 543 544 return o; 545 } 546 547 558 public void putObject(String bindingName, Object value) { 559 Mutex.Privileged mp = getMutexPrivileged(); 560 try { 561 mp.enterWriteAccess(); 562 delegate.bindObject(bindingName, value); 563 } catch (ContextException ex) { 564 if (errorManager.isLoggable(ErrorManager.INFORMATIONAL)) { 565 errorManager.annotate(ex, 566 NbBundle.getMessage(Context.class, "MSG_put_object", bindingName, getAbsoluteContextName())); 567 errorManager.notify(ErrorManager.INFORMATIONAL, ex); 568 } 569 } finally { 570 mp.exitWriteAccess(); 571 } 572 } 573 574 575 582 public String getString(String bindingName, String defaultValue) { 583 Object o = getObject(bindingName, null); 584 if (o == null || !(o instanceof String ) ) { 585 return defaultValue; 586 } else { 587 return (String )o; 588 } 589 } 590 591 598 public void putString(String bindingName, String value) { 599 putObject(bindingName, value); 600 } 601 602 609 public int getInt(String bindingName, int defaultValue) { 610 Object o = getObject(bindingName, null); 611 if (o == null) { 612 return defaultValue; 613 } else if (o instanceof Integer ) { 614 return ((Integer )o).intValue(); 615 } else if (o instanceof String ) { 616 return Integer.parseInt((String )o); 617 } else { 618 return defaultValue; 619 } 620 } 621 622 630 public void putInt(String bindingName, int value) { 631 putObject(bindingName, new Integer (value)); 632 } 633 634 641 public long getLong(String bindingName, long defaultValue) { 642 Object o = getObject(bindingName, null); 643 if (o == null) { 644 return defaultValue; 645 } else if (o instanceof Long ) { 646 return ((Long )o).longValue(); 647 } else if (o instanceof String ) { 648 return Long.parseLong((String )o); 649 } else { 650 return defaultValue; 651 } 652 } 653 654 662 public void putLong(String bindingName, long value) { 663 putObject(bindingName, new Long (value)); 664 } 665 666 673 public boolean getBoolean(String bindingName, boolean defaultValue) { 674 Object o = getObject(bindingName, null); 675 if (o == null) { 676 return defaultValue; 677 } else if (o instanceof Boolean ) { 678 return ((Boolean )o).booleanValue(); 679 } else if (o instanceof String ) { 680 return Boolean.valueOf((String )o).booleanValue(); 681 } else { 682 return defaultValue; 683 } 684 } 685 686 694 public void putBoolean(String bindingName, boolean value) { 695 putObject(bindingName, Boolean.valueOf(value)); 696 } 697 698 705 public float getFloat(String bindingName, float defaultValue) { 706 Object o = getObject(bindingName, null); 707 if (o == null) { 708 return defaultValue; 709 } else if (o instanceof Float ) { 710 return ((Float )o).floatValue(); 711 } else if (o instanceof String ) { 712 return Float.parseFloat((String )o); 713 } else { 714 return defaultValue; 715 } 716 } 717 718 726 public void putFloat(String bindingName, float value) { 727 putObject(bindingName, new Float (value)); 728 } 729 730 737 public double getDouble(String bindingName, double defaultValue) { 738 Object o = getObject(bindingName, null); 739 if (o == null) { 740 return defaultValue; 741 } else if (o instanceof Double ) { 742 return ((Double )o).doubleValue(); 743 } else if (o instanceof String ) { 744 return Double.parseDouble((String )o); 745 } else { 746 return defaultValue; 747 } 748 } 749 750 758 public void putDouble(String bindingName, double value) { 759 putObject(bindingName, new Double (value)); 760 } 761 762 769 public Font getFont(String bindingName, Font defaultValue) { 770 Object o = getObject(bindingName, null); 771 if (o == null) { 772 return defaultValue; 773 } else if (o instanceof Font) { 774 return (Font)o; 775 } else if (o instanceof String ) { 776 return Font.decode((String )o); 777 } else { 778 return defaultValue; 779 } 780 } 781 782 789 public void putFont(String bindingName, Font value) { 790 putObject(bindingName, value); 791 } 792 793 800 public Color getColor(String bindingName, Color defaultValue) { 801 Object o = getObject(bindingName, null); 802 if (o == null) { 803 return defaultValue; 804 } else if (o instanceof Color) { 805 return (Color)o; 806 } else if (o instanceof String ) { 807 return Color.decode((String )o); 808 } else { 809 return defaultValue; 810 } 811 } 812 813 820 public void putColor(String bindingName, Color value) { 821 putObject(bindingName, value); 822 } 823 824 831 public URL getURL(String bindingName, URL defaultValue) { 832 Object o = getObject(bindingName, null); 833 if (o == null) { 834 return defaultValue; 835 } else if (o instanceof URL ) { 836 return (URL )o; 837 } else if (o instanceof String ) { 838 try { 839 return new URL ((String )o); 840 } catch (MalformedURLException ex) { 841 return defaultValue; 842 } 843 } else { 844 return defaultValue; 845 } 846 } 847 848 855 public void putURL(String bindingName, URL value) { 856 putObject(bindingName, value); 857 } 858 859 868 public String [] getStringArray(String bindingName, char separator, String [] defaultValue) { 869 String value = getString(bindingName, null); 870 if (value == null) { 871 return defaultValue; 872 } 873 StringTokenizer tok = new StringTokenizer(value, Character.toString(separator)); 874 String sa[] = new String [tok.countTokens()]; 875 int index = 0; 876 while (tok.hasMoreTokens()) { 877 sa[index] = tok.nextToken(); 878 index++; 879 } 880 return sa; 881 } 882 883 892 public void putStringArray(String bindingName, char separator, String [] value) { 893 if (value == null) { 894 putString(bindingName, null); 895 return; 896 } 897 StringBuffer sb = new StringBuffer (); 898 for (int i=0; i<value.length; i++) { 899 sb.append(value[i]); 900 if (i+1<value.length) { 901 sb.append(separator); 902 } 903 } 904 putString(bindingName, sb.toString()); 905 } 906 907 908 912 922 public String getAttribute(String bindingName, String attributeName, String defaultValue) { 923 Mutex.Privileged mp = getMutexPrivileged(); 924 String value = null; 925 try { 926 mp.enterReadAccess(); 927 value = delegate.getAttribute(bindingName, attributeName); 928 } catch (ContextException ex) { 929 if (errorManager.isLoggable(ErrorManager.INFORMATIONAL)) { 930 errorManager.annotate(ex, 931 NbBundle.getMessage(Context.class, "MSG_get_attr", 932 bindingName == null ? attributeName : bindingName+"\\"+attributeName, 933 getAbsoluteContextName())); 934 errorManager.notify(ErrorManager.INFORMATIONAL, ex); 935 } 936 } finally { 937 mp.exitReadAccess(); 938 } 939 if (value == null) { 940 value = defaultValue; 941 } 942 return value; 943 } 944 945 955 public void setAttribute(String bindingName, String attributeName, String value) { 956 Mutex.Privileged mp = getMutexPrivileged(); 957 try { 958 mp.enterWriteAccess(); 959 delegate.setAttribute(bindingName, attributeName, value); 960 } catch (ContextException ex) { 961 if (errorManager.isLoggable(ErrorManager.INFORMATIONAL)) { 962 errorManager.annotate(ex, 963 NbBundle.getMessage(Context.class, "MSG_put_attr", 964 bindingName == null ? attributeName : bindingName+"\\"+attributeName, 965 getAbsoluteContextName())); 966 errorManager.notify(ErrorManager.INFORMATIONAL, ex); 967 } 968 } finally { 969 mp.exitWriteAccess(); 970 } 971 } 972 973 974 978 990 public synchronized void addContextListener(ContextListener l) { 991 delegate.addContextListener(l); 992 } 993 994 1000 public synchronized void removeContextListener(ContextListener l) { 1001 delegate.removeContextListener(l); 1002 } 1003 1004 1005 1006 1010 1011 1018 public boolean hasDefault(String bindingName) { 1019 if (delegate instanceof ResettableContext) { 1020 return ((ResettableContext)delegate).hasDefault(bindingName); 1021 } 1022 return false; 1023 } 1024 1025 1035 public boolean isModified(String bindingName) { 1036 if (delegate instanceof ResettableContext) { 1037 Mutex.Privileged mp = getMutexPrivileged(); 1038 try { 1039 mp.enterReadAccess(); 1040 return ((ResettableContext)delegate).isModified(bindingName); 1041 } finally { 1042 mp.exitReadAccess(); 1043 } 1044 } 1045 return true; 1046 } 1047 1048 1059 public void revert(String bindingName) throws ContextException { 1060 Mutex.Privileged mp = getMutexPrivileged(); 1061 if (delegate instanceof ResettableContext) { 1062 try { 1063 mp.enterWriteAccess(); 1064 ((ResettableContext)delegate).revert(bindingName); 1065 } finally { 1066 mp.exitWriteAccess(); 1067 } 1068 return; 1069 } 1070 if (bindingName != null) { 1072 putObject(bindingName, null); 1073 } else { 1074 try { 1075 mp.enterWriteAccess(); 1076 BasicContext ctx = delegate.getParentContext(); 1077 String name = delegate.getContextName(); 1078 ctx.destroySubcontext(name); 1079 ctx.createSubcontext(name); 1080 } finally { 1081 mp.exitWriteAccess(); 1082 } 1083 } 1088 } 1089 1090 1097 public static synchronized Mutex getMutex() { 1098 return mutex; 1100 } 1101 1102 public String toString() { 1103 return "Context: [absoluteName="+getAbsoluteContextName()+"] " + super.toString(); 1104 } 1105 1106 1110 1111 static synchronized Context getApiContext(BasicContext ctx) { 1112 if (ctx == null) { 1113 return null; 1114 } 1115 1116 WeakReference weakRef = (WeakReference )contextCache.get(ctx); 1117 Context apiCtx = (weakRef != null) ? (Context)weakRef.get() : null; 1118 if (apiCtx == null) { 1119 apiCtx = new Context(ctx); 1120 contextCache.put(ctx, new WeakReference (apiCtx)); 1121 } 1122 return apiCtx; 1123 } 1124 1125 1126 private Mutex.Privileged getMutexPrivileged() { 1127 return privilegedMutex; 1128 } 1129 1130 1131 private static synchronized BasicContext getRC() { 1132 if (defaultRootContext == null) { 1133 defaultRootContext = (BasicContext)Lookup.getDefault().lookup(BasicContext.class);; 1134 if (defaultRootContext == null) { 1135 errorManager.log(ErrorManager.EXCEPTION, 1136 "FATAL ERROR: RootContext was not found in the default lookup! "+ "All Registry API operations will fail!! "+ "CAUSE: Either the org-netbeans-core-registry-1-?.?.jar module does not exist "+ "or its implementation dependency on org-netbeans-modules-registry-1-?.?.jar "+ "could not be fulfilled and therefore it was not installed. "+ "SOLUTION: rebuild both modules together."); } 1143 } 1144 return defaultRootContext; 1145 } 1146 1147} 1148 | Popular Tags |