1 11 package org.eclipse.core.internal.preferences; 12 13 import java.io.*; 14 import java.lang.ref.WeakReference ; 15 import java.util.*; 16 import org.eclipse.core.internal.runtime.RuntimeLog; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.core.runtime.preferences.*; 19 import org.eclipse.osgi.util.NLS; 20 import org.osgi.framework.Bundle; 21 import org.osgi.framework.Constants; 22 import org.osgi.service.prefs.BackingStoreException; 23 import org.osgi.service.prefs.Preferences; 24 25 28 public class PreferencesService implements IPreferencesService { 29 33 private static final long STRING_SHARING_INTERVAL = 300000; 34 35 private static final String [] DEFAULT_DEFAULT_LOOKUP_ORDER = new String [] {"project", InstanceScope.SCOPE, ConfigurationScope.SCOPE, DefaultScope.SCOPE}; 41 private static final char EXPORT_ROOT_PREFIX = '!'; 42 private static final char BUNDLE_VERSION_PREFIX = '@'; 43 private static final float EXPORT_VERSION = 3; 44 private static final String VERSION_KEY = "file_export_version"; private static final String EMPTY_STRING = ""; 47 private static PreferencesService instance; 48 static final RootPreferences root = new RootPreferences(); 49 private static final Map defaultsRegistry = Collections.synchronizedMap(new HashMap()); 50 private Object registryHelper = null; 51 private Map defaultScopes = new HashMap(); 52 53 56 private long lastStringSharing = 0; 57 58 62 private static IStatus createStatusError(String message, Exception e) { 63 return new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, IStatus.ERROR, message, e); 64 } 65 66 69 public static PreferencesService getDefault() { 70 if (instance == null) 71 instance = new PreferencesService(); 72 return instance; 73 } 74 75 static void log(IStatus status) { 76 RuntimeLog.log(status); 77 } 78 79 PreferencesService() { 80 super(); 81 initializeDefaultScopes(); 82 } 83 84 87 public void applyPreferences(IEclipsePreferences tree, IPreferenceFilter[] filters) throws CoreException { 88 if (filters == null || filters.length == 0) 89 return; 90 try { 91 internalApply(tree, filters); 92 lastStringSharing = 0; 94 shareStrings(); 95 } catch (BackingStoreException e) { 96 throw new CoreException(createStatusError(PrefsMessages.preferences_applyProblems, e)); 97 } 98 } 99 100 103 public IStatus applyPreferences(IExportedPreferences preferences) throws CoreException { 104 if (preferences == null) 106 throw new IllegalArgumentException (); 107 108 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 109 PrefsMessages.message("Applying exported preferences: " + ((ExportedPreferences) preferences).toDeepDebugString()); 111 final MultiStatus result = new MultiStatus(PrefsMessages.OWNER_NAME, IStatus.OK, PrefsMessages.preferences_applyProblems, null); 112 113 IEclipsePreferences modifiedNode = firePreApplyEvent(preferences); 114 115 IPreferenceNodeVisitor visitor = new IPreferenceNodeVisitor() { 117 public boolean visit(IEclipsePreferences node) throws BackingStoreException { 118 IEclipsePreferences globalNode; 119 if (node.parent() == null) 120 globalNode = root; 121 else 122 globalNode = (IEclipsePreferences) root.node(node.absolutePath()); 123 ExportedPreferences epNode = (ExportedPreferences) node; 124 125 boolean removed = false; 128 if (epNode.isExportRoot()) { 129 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 130 PrefsMessages.message("Found export root: " + epNode.absolutePath()); globalNode.removeNode(); 133 removed = true; 134 } 135 136 String [] keys = epNode.properties.keys(); 139 if (keys.length > 0) { 140 if (removed) 142 globalNode = (IEclipsePreferences) root.node(node.absolutePath()); 143 for (int i = 0; i < keys.length; i++) { 144 String key = keys[i]; 145 key = key.intern(); 149 String value = node.get(key, null); 150 if (value != null) { 151 if (EclipsePreferences.DEBUG_PREFERENCE_SET) 152 PrefsMessages.message("Setting: " + globalNode.absolutePath() + '/' + key + '=' + value); globalNode.put(key, value); 154 } 155 } 156 } 157 158 return true; 160 } 161 }; 162 163 try { 164 modifiedNode.accept(visitor); 166 } catch (BackingStoreException e) { 167 throw new CoreException(createStatusError(PrefsMessages.preferences_applyProblems, e)); 168 } 169 170 try { 172 getRootNode().node(modifiedNode.absolutePath()).flush(); 173 } catch (BackingStoreException e) { 174 throw new CoreException(createStatusError(PrefsMessages.preferences_saveProblems, e)); 175 } 176 177 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 178 PrefsMessages.message("Current list of all settings: " + ((EclipsePreferences) getRootNode()).toDeepDebugString()); lastStringSharing = 0; 181 shareStrings(); 182 return result; 183 } 184 185 private boolean containsKeys(IEclipsePreferences aRoot) throws BackingStoreException { 186 final boolean result[] = new boolean[] {false}; 187 IPreferenceNodeVisitor visitor = new IPreferenceNodeVisitor() { 188 public boolean visit(IEclipsePreferences node) throws BackingStoreException { 189 if (node.keys().length != 0) 190 result[0] = true; 191 return !result[0]; 192 } 193 }; 194 aRoot.accept(visitor); 195 return result[0]; 196 } 197 198 204 private Properties convertFromLegacy(Properties properties) { 205 Properties result = new Properties(); 206 String prefix = IPath.SEPARATOR + InstanceScope.SCOPE + IPath.SEPARATOR; 207 for (Iterator i = properties.keySet().iterator(); i.hasNext();) { 208 String key = (String ) i.next(); 209 String value = properties.getProperty(key); 210 if (value != null) { 211 int index = key.indexOf(IPath.SEPARATOR); 212 if (index == -1) { 213 result.put(BUNDLE_VERSION_PREFIX + key, value); 214 result.put(EXPORT_ROOT_PREFIX + prefix + key, EMPTY_STRING); 215 } else { 216 String path = key.substring(0, index); 217 key = key.substring(index + 1); 218 result.put(EclipsePreferences.encodePath(prefix + path, key), value); 219 } 220 } 221 } 222 return result; 223 } 224 225 229 private IExportedPreferences convertFromProperties(Properties properties) { 230 IExportedPreferences result = ExportedPreferences.newRoot(); 231 for (Iterator i = properties.keySet().iterator(); i.hasNext();) { 232 String path = (String ) i.next(); 233 String value = properties.getProperty(path); 234 if (path.charAt(0) == EXPORT_ROOT_PREFIX) { 235 ExportedPreferences current = (ExportedPreferences) result.node(path.substring(1)); 236 current.setExportRoot(); 237 } else if (path.charAt(0) == BUNDLE_VERSION_PREFIX) { 238 ExportedPreferences current = (ExportedPreferences) result.node(InstanceScope.SCOPE).node(path.substring(1)); 239 current.setVersion(value); 240 } else { 241 String [] decoded = EclipsePreferences.decodePath(path); 242 path = decoded[0] == null ? EMPTY_STRING : decoded[0]; 243 ExportedPreferences current = (ExportedPreferences) result.node(path); 244 String key = decoded[1]; 245 current.put(key, value); 246 } 247 } 248 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 249 PrefsMessages.message("Converted preferences file to IExportedPreferences tree: " + ((ExportedPreferences) result).toDeepDebugString()); return result; 251 } 252 253 256 private Properties convertToProperties(IEclipsePreferences preferences, final String [] excludesList) throws BackingStoreException { 257 final Properties result = new Properties(); 258 final int baseLength = preferences.absolutePath().length(); 259 260 IPreferenceNodeVisitor visitor = new IPreferenceNodeVisitor() { 262 public boolean visit(IEclipsePreferences node) throws BackingStoreException { 263 String absolutePath = node.absolutePath(); 265 String scope = getScope(absolutePath); 266 if (DefaultScope.SCOPE.equals(scope)) 267 return false; 268 String path = absolutePath.length() <= baseLength ? EMPTY_STRING : EclipsePreferences.makeRelative(absolutePath.substring(baseLength)); 269 for (int i = 0; i < excludesList.length; i++) { 271 String exclusion = EclipsePreferences.makeRelative(excludesList[i]); 272 if (path.startsWith(exclusion)) 273 return false; 274 } 275 boolean needToAddVersion = InstanceScope.SCOPE.equals(scope); 276 String [] keys = node.keys(); 278 for (int i = 0; i < keys.length; i++) { 279 String key = keys[i]; 280 boolean ignore = false; 281 for (int j = 0; !ignore && j < excludesList.length; j++) 282 if (EclipsePreferences.encodePath(path, key).startsWith(EclipsePreferences.makeRelative(excludesList[j]))) 283 ignore = true; 284 if (!ignore) { 285 String value = node.get(key, null); 286 if (value != null) { 287 if (needToAddVersion) { 288 String bundle = getBundleName(absolutePath); 289 if (bundle != null) { 290 String version = getBundleVersion(bundle); 291 if (version != null) 292 result.put(BUNDLE_VERSION_PREFIX + bundle, version); 293 } 294 needToAddVersion = false; 295 } 296 result.put(EclipsePreferences.encodePath(absolutePath, key), value); 297 } 298 } 299 } 300 return true; 301 } 302 }; 303 304 preferences.accept(visitor); 306 307 return result; 309 } 310 311 320 void copyFromTo(Preferences source, Preferences destination, String [] keys, int depth) throws BackingStoreException { 321 String [] keysToCopy = keys == null ? source.keys() : keys; 322 for (int i = 0; i < keysToCopy.length; i++) { 323 String value = source.get(keysToCopy[i], null); 324 if (value != null) 325 destination.put(keysToCopy[i], value); 326 } 327 if (depth == 0) 328 return; 329 String [] children = source.childrenNames(); 330 for (int i = 0; i < children.length; i++) 331 copyFromTo(source.node(children[i]), destination.node(children[i]), keys, depth); 332 } 333 334 public WeakReference applyRuntimeDefaults(String name, WeakReference pluginReference) { 335 if (registryHelper == null) 336 return null; 337 return ((PreferenceServiceRegistryHelper) registryHelper).applyRuntimeDefaults(name, pluginReference); 338 } 339 340 private void initializeDefaultScopes() { 341 defaultScopes.put(DefaultScope.SCOPE, new DefaultPreferences()); 342 root.addChild(DefaultScope.SCOPE, null); 343 defaultScopes.put(InstanceScope.SCOPE, new InstancePreferences()); 344 root.addChild(InstanceScope.SCOPE, null); 345 defaultScopes.put(ConfigurationScope.SCOPE, new ConfigurationPreferences()); 346 root.addChild(ConfigurationScope.SCOPE, null); 347 } 348 349 public IEclipsePreferences createNode(String key) { 350 IScope scope = (IScope) defaultScopes.get(key); 351 if (scope == null) { 352 if (registryHelper == null) 353 return new EclipsePreferences(root, key); 354 return ((PreferenceServiceRegistryHelper) registryHelper).createNode(root, key); 355 } 356 return scope.create(root, key); 357 } 358 359 362 public void exportPreferences(IEclipsePreferences node, IPreferenceFilter[] filters, OutputStream stream) throws CoreException { 363 if (filters == null || filters.length == 0) 364 return; 365 try { 366 internalExport(node, filters, stream); 367 } catch (BackingStoreException e) { 368 throw new CoreException(createStatusError(PrefsMessages.preferences_exportProblems, e)); 369 } 370 } 371 372 375 public IStatus exportPreferences(IEclipsePreferences node, OutputStream output, String [] excludesList) throws CoreException { 376 if (node == null || output == null) 378 throw new IllegalArgumentException (); 379 Properties properties = null; 380 if (excludesList == null) 381 excludesList = new String [0]; 382 try { 383 properties = convertToProperties(node, excludesList); 384 if (properties.isEmpty()) 385 return Status.OK_STATUS; 386 properties.put(VERSION_KEY, Float.toString(EXPORT_VERSION)); 387 properties.put(EXPORT_ROOT_PREFIX + node.absolutePath(), EMPTY_STRING); 388 } catch (BackingStoreException e) { 389 throw new CoreException(createStatusError(e.getMessage(), e)); 390 } 391 try { 392 properties.store(output, null); 393 } catch (IOException e) { 394 throw new CoreException(createStatusError(PrefsMessages.preferences_exportProblems, e)); 395 } 396 return Status.OK_STATUS; 397 } 398 399 402 private IEclipsePreferences firePreApplyEvent(IEclipsePreferences tree) { 403 if (registryHelper == null) 404 return tree; 405 final IEclipsePreferences[] result = new IEclipsePreferences[] {tree}; 406 PreferenceModifyListener[] listeners = ((PreferenceServiceRegistryHelper) registryHelper).getModifyListeners(); 407 for (int i = 0; i < listeners.length; i++) { 408 final PreferenceModifyListener listener = listeners[i]; 409 ISafeRunnable job = new ISafeRunnable() { 410 public void handleException(Throwable exception) { 411 } 413 414 public void run() throws Exception { 415 result[0] = listener.preApply(result[0]); 416 } 417 }; 418 SafeRunner.run(job); 419 } 420 return result[0]; 421 } 422 423 426 public String get(String key, String defaultValue, Preferences[] nodes) { 427 if (nodes == null) 428 return defaultValue; 429 for (int i = 0; i < nodes.length; i++) { 430 Preferences node = nodes[i]; 431 if (node != null) { 432 String result = node.get(key, null); 433 if (result != null) 434 return result; 435 } 436 } 437 return defaultValue; 438 } 439 440 443 public boolean getBoolean(String qualifier, String key, boolean defaultValue, IScopeContext[] scopes) { 444 String result = get(EclipsePreferences.decodePath(key)[1], null, getNodes(qualifier, key, scopes)); 445 return result == null ? defaultValue : Boolean.valueOf(result).booleanValue(); 446 } 447 448 454 String getBundleName(String path) { 455 if (path.length() == 0 || path.charAt(0) != IPath.SEPARATOR) 456 return null; 457 int first = path.indexOf(IPath.SEPARATOR, 1); 458 if (first == -1) 459 return null; 460 int second = path.indexOf(IPath.SEPARATOR, first + 1); 461 return second == -1 ? path.substring(first + 1) : path.substring(first + 1, second); 462 } 463 464 468 String getBundleVersion(String bundleName) { 469 Bundle bundle = PreferencesOSGiUtils.getDefault().getBundle(bundleName); 470 if (bundle != null) { 471 Object version = bundle.getHeaders(EMPTY_STRING).get(Constants.BUNDLE_VERSION); 472 if (version != null && version instanceof String ) 473 return (String ) version; 474 } 475 return null; 476 } 477 478 481 public byte[] getByteArray(String qualifier, String key, byte[] defaultValue, IScopeContext[] scopes) { 482 String result = get(EclipsePreferences.decodePath(key)[1], null, getNodes(qualifier, key, scopes)); 483 return result == null ? defaultValue : result.getBytes(); 484 } 485 486 489 public String [] getDefaultLookupOrder(String qualifier, String key) { 490 LookupOrder order = (LookupOrder) defaultsRegistry.get(getRegistryKey(qualifier, key)); 491 return order == null ? null : order.getOrder(); 492 } 493 494 497 public double getDouble(String qualifier, String key, double defaultValue, IScopeContext[] scopes) { 498 String value = get(EclipsePreferences.decodePath(key)[1], null, getNodes(qualifier, key, scopes)); 499 if (value == null) 500 return defaultValue; 501 try { 502 return Double.parseDouble(value); 503 } catch (NumberFormatException e) { 504 return defaultValue; 505 } 506 } 507 508 511 public float getFloat(String qualifier, String key, float defaultValue, IScopeContext[] scopes) { 512 String value = get(EclipsePreferences.decodePath(key)[1], null, getNodes(qualifier, key, scopes)); 513 if (value == null) 514 return defaultValue; 515 try { 516 return Float.parseFloat(value); 517 } catch (NumberFormatException e) { 518 return defaultValue; 519 } 520 } 521 522 525 public int getInt(String qualifier, String key, int defaultValue, IScopeContext[] scopes) { 526 String value = get(EclipsePreferences.decodePath(key)[1], null, getNodes(qualifier, key, scopes)); 527 if (value == null) 528 return defaultValue; 529 try { 530 return Integer.parseInt(value); 531 } catch (NumberFormatException e) { 532 return defaultValue; 533 } 534 } 535 536 539 540 543 public long getLong(String qualifier, String key, long defaultValue, IScopeContext[] scopes) { 544 String value = get(EclipsePreferences.decodePath(key)[1], null, getNodes(qualifier, key, scopes)); 545 if (value == null) 546 return defaultValue; 547 try { 548 return Long.parseLong(value); 549 } catch (NumberFormatException e) { 550 return defaultValue; 551 } 552 } 553 554 557 public String [] getLookupOrder(String qualifier, String key) { 558 String [] order = getDefaultLookupOrder(qualifier, key); 559 if (order == null && key != null) 562 order = getDefaultLookupOrder(qualifier, null); 563 if (order == null) 564 order = DEFAULT_DEFAULT_LOOKUP_ORDER; 565 return order; 566 } 567 568 private Preferences[] getNodes(String qualifier, String key, IScopeContext[] contexts) { 569 String [] order = getLookupOrder(qualifier, key); 570 String childPath = EclipsePreferences.makeRelative(EclipsePreferences.decodePath(key)[0]); 571 ArrayList result = new ArrayList(); 572 for (int i = 0; i < order.length; i++) { 573 String scopeString = order[i]; 574 boolean found = false; 575 for (int j = 0; contexts != null && j < contexts.length; j++) { 576 IScopeContext context = contexts[j]; 577 if (context != null && context.getName().equals(scopeString)) { 578 Preferences node = context.getNode(qualifier); 579 if (node != null) { 580 found = true; 581 if (childPath != null) 582 node = node.node(childPath); 583 result.add(node); 584 } 585 } 586 } 587 if (!found) { 588 Preferences node = getRootNode().node(scopeString).node(qualifier); 589 if (childPath != null) 590 node = node.node(childPath); 591 result.add(node); 592 } 593 found = false; 594 } 595 return (Preferences[]) result.toArray(new Preferences[result.size()]); 596 } 597 598 601 private String getRegistryKey(String qualifier, String key) { 602 if (qualifier == null) 603 throw new IllegalArgumentException (); 604 if (key == null) 605 return qualifier; 606 return qualifier + '/' + key; 607 } 608 609 public IEclipsePreferences getRootNode() { 610 return root; 611 } 612 613 617 String getScope(String path) { 618 if (path == null || path.length() == 0) 619 return EMPTY_STRING; 620 int startIndex = path.indexOf(IPath.SEPARATOR); 621 if (startIndex == -1) 622 return path; 623 if (path.length() == 1) 624 return EMPTY_STRING; 625 int endIndex = path.indexOf(IPath.SEPARATOR, startIndex + 1); 626 if (endIndex == -1) 627 endIndex = path.length(); 628 return path.substring(startIndex + 1, endIndex); 629 } 630 631 634 public String getString(String qualifier, String key, String defaultValue, IScopeContext[] scopes) { 635 return get(EclipsePreferences.decodePath(key)[1], defaultValue, getNodes(qualifier, key, scopes)); 636 } 637 638 641 public IStatus importPreferences(InputStream input) throws CoreException { 642 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 643 PrefsMessages.message("Importing preferences..."); return applyPreferences(readPreferences(input)); 645 } 646 647 651 private void internalApply(IEclipsePreferences tree, IPreferenceFilter[] filters) throws BackingStoreException { 652 ArrayList trees = new ArrayList(); 653 for (int i = 0; i < filters.length; i++) 654 trees.add(trimTree(tree, filters[i])); 655 IEclipsePreferences toApply = mergeTrees((IEclipsePreferences[]) trees.toArray(new IEclipsePreferences[trees.size()])); 657 658 toApply = firePreApplyEvent(toApply); 660 661 IPreferenceNodeVisitor visitor = new IPreferenceNodeVisitor() { 663 public boolean visit(IEclipsePreferences node) throws BackingStoreException { 664 String [] keys = node.keys(); 665 if (keys.length == 0) 666 return true; 667 copyFromTo(node, getRootNode().node(node.absolutePath()), keys, 0); 668 return true; 669 } 670 }; 671 toApply.accept(visitor); 672 } 673 674 678 private void internalExport(IEclipsePreferences node, IPreferenceFilter filters[], OutputStream output) throws BackingStoreException, CoreException { 679 ArrayList trees = new ArrayList(); 680 for (int i = 0; i < filters.length; i++) 681 trees.add(trimTree(node, filters[i])); 682 IEclipsePreferences toExport = mergeTrees((IEclipsePreferences[]) trees.toArray(new IEclipsePreferences[trees.size()])); 683 exportPreferences(toExport, output, (String []) null); 684 } 685 686 690 private boolean internalMatches(IEclipsePreferences tree, IPreferenceFilter filter) throws BackingStoreException { 691 String [] scopes = filter.getScopes(); 692 if (scopes == null) 693 throw new IllegalArgumentException (); 694 String treePath = tree.absolutePath(); 695 for (int i = 0; i < scopes.length; i++) { 697 String scope = scopes[i]; 698 Map mapping = filter.getMapping(scope); 699 if (mapping == null) { 701 if (tree.parent() == null && tree.nodeExists(scope)) 703 return containsKeys((IEclipsePreferences) tree.node(scope)); 704 if (scopeMatches(scope, tree) && containsKeys(tree)) 706 return true; 707 continue; 708 } 709 for (Iterator iter = mapping.keySet().iterator(); iter.hasNext();) { 711 String nodePath = (String ) iter.next(); 712 String nodeFullPath = '/' + scope + '/' + nodePath; 713 if (!nodeFullPath.startsWith(treePath)) 715 continue; 716 String childPath = nodeFullPath.substring(treePath.length()); 718 childPath = EclipsePreferences.makeRelative(childPath); 719 if (tree.nodeExists(childPath)) { 720 PreferenceFilterEntry[] entries; 721 try { 723 entries = (PreferenceFilterEntry[]) mapping.get(nodePath); 724 } catch (ClassCastException e) { 725 log(createStatusError(PrefsMessages.preferences_classCastFilterEntry, e)); 726 continue; 727 } 728 Preferences child = tree.node(childPath); 731 if (entries == null) 732 return child.keys().length != 0 || child.childrenNames().length != 0; 733 for (int j = 0; j < entries.length; j++) { 735 if (entries[j] != null && child.get(entries[j].getKey(), null) != null) 736 return true; 737 } 738 } 739 } 740 } 741 return false; 742 } 743 744 747 private IPreferenceFilter[] internalMatches(IEclipsePreferences tree, IPreferenceFilter[] filters) throws BackingStoreException { 748 ArrayList result = new ArrayList(); 749 for (int i = 0; i < filters.length; i++) 750 if (internalMatches(tree, filters[i])) 751 result.add(filters[i]); 752 return (IPreferenceFilter[]) result.toArray(new IPreferenceFilter[result.size()]); 753 } 754 755 762 private boolean isLegacy(Properties properties) { 763 return properties.getProperty(VERSION_KEY) == null; 764 } 765 766 769 public IPreferenceFilter[] matches(IEclipsePreferences tree, IPreferenceFilter[] filters) throws CoreException { 770 if (filters == null || filters.length == 0) 771 return new IPreferenceFilter[0]; 772 try { 773 return internalMatches(tree, filters); 774 } catch (BackingStoreException e) { 775 throw new CoreException(createStatusError(PrefsMessages.preferences_matching, e)); 776 } 777 } 778 779 private IEclipsePreferences mergeTrees(IEclipsePreferences[] trees) throws BackingStoreException { 780 if (trees.length == 1) 781 return trees[0]; 782 final IEclipsePreferences result = ExportedPreferences.newRoot(); 783 if (trees.length == 0) 784 return result; 785 IPreferenceNodeVisitor visitor = new IPreferenceNodeVisitor() { 786 public boolean visit(IEclipsePreferences node) throws BackingStoreException { 787 Preferences destination = result.node(node.absolutePath()); 788 copyFromTo(node, destination, null, 0); 789 return true; 790 } 791 }; 792 for (int i = 0; i < trees.length; i++) 793 trees[i].accept(visitor); 794 return result; 795 } 796 797 800 public IExportedPreferences readPreferences(InputStream input) throws CoreException { 801 if (input == null) 802 throw new IllegalArgumentException (); 803 804 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 805 PrefsMessages.message("Reading preferences from stream..."); 807 Properties properties = new Properties(); 809 try { 810 properties.load(input); 811 } catch (IOException e) { 812 throw new CoreException(createStatusError(PrefsMessages.preferences_importProblems, e)); 813 } finally { 814 try { 815 input.close(); 816 } catch (IOException e) { 817 } 819 } 820 821 if (properties.isEmpty()) 823 throw new CoreException(createStatusError(PrefsMessages.preferences_invalidFileFormat, null)); 824 825 if (isLegacy(properties)) { 827 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 828 PrefsMessages.message("Read legacy preferences file, converting to 3.0 format..."); properties = convertFromLegacy(properties); 830 } else { 831 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 832 PrefsMessages.message("Read preferences file."); properties.remove(VERSION_KEY); 834 } 835 836 return convertFromProperties(properties); 838 } 839 840 843 private boolean scopeMatches(String scope, IEclipsePreferences tree) { 844 if (tree.parent() == null) 846 return false; 847 String path = tree.absolutePath(); 849 int index = path.indexOf('/', 1); 850 String sub = path.substring(1, index == -1 ? path.length() : index); 851 return scope.equals(sub); 852 } 853 854 857 public void setDefaultLookupOrder(String qualifier, String key, String [] order) { 858 String registryKey = getRegistryKey(qualifier, key); 859 if (order == null) 860 defaultsRegistry.remove(registryKey); 861 else { 862 LookupOrder obj = new LookupOrder(order); 863 defaultsRegistry.put(registryKey, obj); 864 } 865 } 866 867 public void setRegistryHelper(Object registryHelper) { 868 if (this.registryHelper != null && this.registryHelper != registryHelper) 869 ((PreferenceServiceRegistryHelper) this.registryHelper).stop(); 870 this.registryHelper = registryHelper; 871 } 872 873 876 void shareStrings() { 877 long now = System.currentTimeMillis(); 878 if (now - lastStringSharing < STRING_SHARING_INTERVAL) 879 return; 880 StringPool pool = new StringPool(); 881 root.shareStrings(pool); 882 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 883 System.out.println("Preference string sharing saved: " + pool.getSavedStringCount()); lastStringSharing = now; 885 } 886 887 890 private IEclipsePreferences trimTree(IEclipsePreferences tree, IPreferenceFilter filter) throws BackingStoreException { 891 IEclipsePreferences result = (IEclipsePreferences) ExportedPreferences.newRoot().node(tree.absolutePath()); 892 String [] scopes = filter.getScopes(); 893 if (scopes == null) 894 throw new IllegalArgumentException (); 895 String treePath = tree.absolutePath(); 896 for (int i = 0; i < scopes.length; i++) { 898 String scope = scopes[i]; 899 Map mapping = filter.getMapping(scope); 900 if (mapping == null) { 902 if (tree.parent() == null && tree.nodeExists(scope)) 904 copyFromTo(tree.node(scope), result.node(scope), null, -1); 905 else if (scopeMatches(scope, tree)) 907 copyFromTo(tree, result, null, -1); 908 continue; 909 } 910 for (Iterator iter = mapping.keySet().iterator(); iter.hasNext();) { 912 String nodePath = (String ) iter.next(); 913 String nodeFullPath = '/' + scope + '/' + nodePath; 914 if (!nodeFullPath.startsWith(treePath)) 916 continue; 917 String childPath = nodeFullPath.substring(treePath.length()); 919 childPath = EclipsePreferences.makeRelative(childPath); 920 if (tree.nodeExists(childPath)) { 921 PreferenceFilterEntry[] entries; 922 try { 924 entries = (PreferenceFilterEntry[]) mapping.get(nodePath); 925 } catch (ClassCastException e) { 926 log(createStatusError(PrefsMessages.preferences_classCastFilterEntry, e)); 927 continue; 928 } 929 String [] keys = null; 930 if (entries != null) { 931 ArrayList list = new ArrayList(); 932 for (int j = 0; j < entries.length; j++) { 933 if (entries[j] != null) 934 list.add(entries[j].getKey()); 935 } 936 keys = (String []) list.toArray(new String [list.size()]); 937 } 938 copyFromTo(tree.node(childPath), result.node(childPath), keys, keys == null ? -1 : 0); 940 } 941 } 942 } 943 return result; 944 } 945 946 960 IStatus validatePluginVersions(String bundle, PluginVersionIdentifier pref, PluginVersionIdentifier installed) { 961 if (installed.getMajorComponent() == pref.getMajorComponent() && installed.getMinorComponent() == pref.getMinorComponent()) 962 return null; 963 int severity; 964 if (installed.getMajorComponent() < pref.getMajorComponent()) 965 severity = IStatus.ERROR; 966 else 967 severity = IStatus.WARNING; 968 String msg = NLS.bind(PrefsMessages.preferences_incompatible, (new Object [] {pref, bundle, installed})); 969 return new Status(severity, PrefsMessages.OWNER_NAME, 1, msg, null); 970 } 971 972 public IStatus validateVersions(IPath path) { 973 final MultiStatus result = new MultiStatus(PrefsMessages.OWNER_NAME, IStatus.INFO, PrefsMessages.preferences_validate, null); 974 IPreferenceNodeVisitor visitor = new IPreferenceNodeVisitor() { 975 public boolean visit(IEclipsePreferences node) { 976 if (!(node instanceof ExportedPreferences)) 977 return false; 978 979 ExportedPreferences realNode = (ExportedPreferences) node; 981 String version = realNode.getVersion(); 982 if (version == null || !PluginVersionIdentifier.validateVersion(version).isOK()) 983 return true; 984 PluginVersionIdentifier versionInFile = new PluginVersionIdentifier(version); 985 986 String bundleName = getBundleName(node.absolutePath()); 988 if (bundleName == null) 989 return true; 990 String stringVersion = getBundleVersion(bundleName); 991 if (stringVersion == null || !PluginVersionIdentifier.validateVersion(stringVersion).isOK()) 992 return true; 993 PluginVersionIdentifier versionInMemory = new PluginVersionIdentifier(stringVersion); 994 995 IStatus verification = validatePluginVersions(bundleName, versionInFile, versionInMemory); 997 if (verification != null) 998 result.add(verification); 999 1000 return true; 1001 } 1002 }; 1003 1004 InputStream input = null; 1005 try { 1006 input = new BufferedInputStream(new FileInputStream(path.toFile())); 1007 IExportedPreferences prefs = readPreferences(input); 1008 prefs.accept(visitor); 1009 } catch (FileNotFoundException e) { 1010 } catch (CoreException e) { 1012 result.add(createStatusError(PrefsMessages.preferences_validationException, e)); 1013 } catch (BackingStoreException e) { 1014 result.add(createStatusError(PrefsMessages.preferences_validationException, e)); 1015 } 1016 return result; 1017 } 1018 1019} 1020 | Popular Tags |