Ticket #15451: plugin-prefs.patch
File plugin-prefs.patch, 22.1 KB (added by , 7 years ago) |
---|
-
src/org/openstreetmap/josm/Main.java
48 48 import org.openstreetmap.josm.io.FileWatcher; 49 49 import org.openstreetmap.josm.io.OnlineResource; 50 50 import org.openstreetmap.josm.io.OsmApi; 51 import org.openstreetmap.josm.plugins.PluginHandler; 51 52 import org.openstreetmap.josm.spi.preferences.Config; 52 53 import org.openstreetmap.josm.tools.CheckParameterUtil; 53 54 import org.openstreetmap.josm.tools.ImageProvider; … … 761 762 } catch (IOException ex) { 762 763 Logging.log(Logging.LEVEL_WARN, tr("Failed to save default preferences."), ex); 763 764 } 765 PluginHandler.shutdown(); 764 766 if (!GraphicsEnvironment.isHeadless()) { 765 767 ImageProvider.shutdown(true); 766 768 } -
src/org/openstreetmap/josm/data/Preferences.java
5 5 import static org.openstreetmap.josm.tools.I18n.tr; 6 6 7 7 import java.awt.Color; 8 import java.awt.GraphicsEnvironment;9 import java.awt.Toolkit;10 8 import java.io.File; 11 9 import java.io.IOException; 12 10 import java.io.PrintWriter; … … 14 12 import java.io.StringWriter; 15 13 import java.lang.annotation.Retention; 16 14 import java.lang.annotation.RetentionPolicy; 17 import java.lang.reflect.Field;18 15 import java.nio.charset.StandardCharsets; 19 16 import java.util.ArrayList; 20 17 import java.util.Collection; … … 27 24 import java.util.Map; 28 25 import java.util.Map.Entry; 29 26 import java.util.Optional; 30 import java.util.ResourceBundle;31 27 import java.util.Set; 32 28 import java.util.SortedMap; 33 29 import java.util.TreeMap; … … 45 41 import org.openstreetmap.josm.data.preferences.DoubleProperty; 46 42 import org.openstreetmap.josm.data.preferences.IntegerProperty; 47 43 import org.openstreetmap.josm.data.preferences.ColorInfo; 44 import org.openstreetmap.josm.data.preferences.JosmBaseDirectories; 48 45 import org.openstreetmap.josm.data.preferences.LongProperty; 49 46 import org.openstreetmap.josm.data.preferences.NamedColorProperty; 50 47 import org.openstreetmap.josm.data.preferences.PreferencesReader; … … 102 99 103 100 private static final long MAX_AGE_DEFAULT_PREFERENCES = TimeUnit.DAYS.toSeconds(50); 104 101 105 /** 106 * Internal storage for the preference directory. 107 * Do not access this variable directly! 108 * @see #getPreferencesDirectory() 109 */ 110 private File preferencesDir; 102 private final IBaseDirectories dirs; 111 103 112 104 /** 113 * Internal storage for the cache directory.114 */115 private File cacheDir;116 117 /**118 * Internal storage for the user data directory.119 */120 private File userdataDir;121 122 /**123 105 * Determines if preferences file is saved each time a property is changed. 124 106 */ 125 107 private boolean saveOnPut = true; … … 246 228 */ 247 229 public Preferences() { 248 230 // Default constructor 231 this.dirs = new JosmBaseDirectories(); 249 232 } 250 233 234 public Preferences(IBaseDirectories dirs) { 235 this.dirs = dirs; 236 } 237 251 238 /** 252 239 * Constructs a new {@code Preferences} from an existing instance. 253 240 * @param pref existing preferences to copy … … 255 242 */ 256 243 @SuppressWarnings("deprecation") 257 244 public Preferences(Preferences pref) { 245 this(pref.dirs); 258 246 settingsMap.putAll(pref.settingsMap); 259 247 defaultsMap.putAll(pref.defaultsMap); 260 248 colornames.putAll(pref.colornames); … … 431 419 432 420 @Override 433 421 public File getPreferencesDirectory(boolean createIfMissing) { 434 if (preferencesDir == null) { 435 String path; 436 path = System.getProperty("josm.pref"); 437 if (path != null) { 438 preferencesDir = new File(path).getAbsoluteFile(); 439 } else { 440 path = System.getProperty("josm.home"); 441 if (path != null) { 442 preferencesDir = new File(path).getAbsoluteFile(); 443 } else { 444 preferencesDir = Main.platform.getDefaultPrefDirectory(); 445 } 446 } 447 } 448 if (createIfMissing && !preferencesDir.exists() && !preferencesDir.mkdirs()) { 449 Logging.warn(tr("Failed to create missing preferences directory: {0}", preferencesDir.getAbsoluteFile())); 450 JOptionPane.showMessageDialog( 451 Main.parent, 452 tr("<html>Failed to create missing preferences directory: {0}</html>", preferencesDir.getAbsoluteFile()), 453 tr("Error"), 454 JOptionPane.ERROR_MESSAGE 455 ); 456 } 457 return preferencesDir; 422 return dirs.getPreferencesDirectory(createIfMissing); 458 423 } 459 424 460 425 /** … … 471 436 472 437 @Override 473 438 public File getUserDataDirectory(boolean createIfMissing) { 474 if (userdataDir == null) { 475 String path; 476 path = System.getProperty("josm.userdata"); 477 if (path != null) { 478 userdataDir = new File(path).getAbsoluteFile(); 479 } else { 480 path = System.getProperty("josm.home"); 481 if (path != null) { 482 userdataDir = new File(path).getAbsoluteFile(); 483 } else { 484 userdataDir = Main.platform.getDefaultUserDataDirectory(); 485 } 486 } 487 } 488 if (createIfMissing && !userdataDir.exists() && !userdataDir.mkdirs()) { 489 Logging.warn(tr("Failed to create missing user data directory: {0}", userdataDir.getAbsoluteFile())); 490 JOptionPane.showMessageDialog( 491 Main.parent, 492 tr("<html>Failed to create missing user data directory: {0}</html>", userdataDir.getAbsoluteFile()), 493 tr("Error"), 494 JOptionPane.ERROR_MESSAGE 495 ); 496 } 497 return userdataDir; 439 return dirs.getUserDataDirectory(createIfMissing); 498 440 } 499 441 500 442 /** … … 536 478 537 479 @Override 538 480 public File getCacheDirectory(boolean createIfMissing) { 539 if (cacheDir == null) { 540 String path = System.getProperty("josm.cache"); 541 if (path != null) { 542 cacheDir = new File(path).getAbsoluteFile(); 543 } else { 544 path = System.getProperty("josm.home"); 545 if (path != null) { 546 cacheDir = new File(path, "cache"); 547 } else { 548 path = get("cache.folder", null); 549 if (path != null) { 550 cacheDir = new File(path).getAbsoluteFile(); 551 } else { 552 cacheDir = Main.platform.getDefaultCacheDirectory(); 553 } 554 } 555 } 556 } 557 if (createIfMissing && !cacheDir.exists() && !cacheDir.mkdirs()) { 558 Logging.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile())); 559 JOptionPane.showMessageDialog( 560 Main.parent, 561 tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()), 562 tr("Error"), 563 JOptionPane.ERROR_MESSAGE 564 ); 565 } 566 return cacheDir; 481 return dirs.getCacheDirectory(createIfMissing); 567 482 } 568 483 569 484 private static void addPossibleResourceDir(Set<String> locations, String s) { … … 781 696 if (!defaults) { 782 697 /* currently unused, but may help to fix configuration issues in future */ 783 698 putInt("josm.version", Version.getInstance().getVersion()); 784 785 updateSystemProperties();786 699 } 787 700 788 701 File backupFile = new File(prefFile + "_backup"); … … 836 749 reader.parse(); 837 750 settingsMap.clear(); 838 751 settingsMap.putAll(reader.getSettings()); 839 updateSystemProperties();840 752 removeObsolete(reader.getVersion()); 841 753 } 842 754 … … 983 895 */ 984 896 public void resetToInitialState() { 985 897 resetToDefault(); 986 preferencesDir = null;987 cacheDir = null;988 userdataDir = null;989 898 saveOnPut = true; 990 899 initSuccessful = false; 991 900 } … … 1504 1413 } 1505 1414 1506 1415 /** 1507 * Updates system properties with the current values in the preferences.1508 */1509 public void updateSystemProperties() {1510 if ("true".equals(get("prefer.ipv6", "auto")) && !"true".equals(Utils.updateSystemProperty("java.net.preferIPv6Addresses", "true"))) {1511 // never set this to false, only true!1512 Logging.info(tr("Try enabling IPv6 network, prefering IPv6 over IPv4 (only works on early startup)."));1513 }1514 Utils.updateSystemProperty("http.agent", Version.getInstance().getAgentString());1515 Utils.updateSystemProperty("user.language", get("language"));1516 // Workaround to fix a Java bug. This ugly hack comes from Sun bug database: https://bugs.openjdk.java.net/browse/JDK-62927391517 // Force AWT toolkit to update its internal preferences (fix #6345).1518 // Does not work anymore with Java 9, to remove with Java 9 migration1519 if (Utils.getJavaVersion() < 9 && !GraphicsEnvironment.isHeadless()) {1520 try {1521 Field field = Toolkit.class.getDeclaredField("resources");1522 Utils.setObjectsAccessible(field);1523 field.set(null, ResourceBundle.getBundle("sun.awt.resources.awt"));1524 } catch (ReflectiveOperationException | RuntimeException e) { // NOPMD1525 // Catch RuntimeException in order to catch InaccessibleObjectException, new in Java 91526 Logging.warn(e);1527 }1528 }1529 // Possibility to disable SNI (not by default) in case of misconfigured https servers1530 // See #9875 + http://stackoverflow.com/a/14884941/22571721531 // then https://josm.openstreetmap.de/ticket/12152#comment:5 for details1532 if (getBoolean("jdk.tls.disableSNIExtension", false)) {1533 Utils.updateSystemProperty("jsse.enableSNIExtension", "false");1534 }1535 }1536 1537 /**1538 1416 * Replies the collection of plugin site URLs from where plugin lists can be downloaded. 1539 1417 * @return the collection of plugin site URLs 1540 1418 * @see #getOnlinePluginSites -
src/org/openstreetmap/josm/data/preferences/JosmBaseDirectories.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.data.preferences; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 import java.io.File; 7 8 import javax.swing.JOptionPane; 9 10 import org.openstreetmap.josm.Main; 11 import org.openstreetmap.josm.spi.preferences.IBaseDirectories; 12 import org.openstreetmap.josm.tools.Logging; 13 14 /** 15 * 16 * @since xxx 17 */ 18 public class JosmBaseDirectories implements IBaseDirectories { 19 /** 20 * Internal storage for the preference directory. 21 */ 22 private File preferencesDir; 23 24 /** 25 * Internal storage for the cache directory. 26 */ 27 private File cacheDir; 28 29 /** 30 * Internal storage for the user data directory. 31 */ 32 private File userdataDir; 33 34 @Override 35 public File getPreferencesDirectory(boolean createIfMissing) { 36 if (preferencesDir == null) { 37 String path; 38 path = System.getProperty("josm.pref"); 39 if (path != null) { 40 preferencesDir = new File(path).getAbsoluteFile(); 41 } else { 42 path = System.getProperty("josm.home"); 43 if (path != null) { 44 preferencesDir = new File(path).getAbsoluteFile(); 45 } else { 46 preferencesDir = Main.platform.getDefaultPrefDirectory(); 47 } 48 } 49 } 50 if (createIfMissing && !preferencesDir.exists() && !preferencesDir.mkdirs()) { 51 Logging.warn(tr("Failed to create missing preferences directory: {0}", preferencesDir.getAbsoluteFile())); 52 JOptionPane.showMessageDialog( 53 Main.parent, 54 tr("<html>Failed to create missing preferences directory: {0}</html>", preferencesDir.getAbsoluteFile()), 55 tr("Error"), 56 JOptionPane.ERROR_MESSAGE 57 ); 58 } 59 return preferencesDir; 60 } 61 62 @Override 63 public File getUserDataDirectory(boolean createIfMissing) { 64 if (userdataDir == null) { 65 String path; 66 path = System.getProperty("josm.userdata"); 67 if (path != null) { 68 userdataDir = new File(path).getAbsoluteFile(); 69 } else { 70 path = System.getProperty("josm.home"); 71 if (path != null) { 72 userdataDir = new File(path).getAbsoluteFile(); 73 } else { 74 userdataDir = Main.platform.getDefaultUserDataDirectory(); 75 } 76 } 77 } 78 if (createIfMissing && !userdataDir.exists() && !userdataDir.mkdirs()) { 79 Logging.warn(tr("Failed to create missing user data directory: {0}", userdataDir.getAbsoluteFile())); 80 JOptionPane.showMessageDialog( 81 Main.parent, 82 tr("<html>Failed to create missing user data directory: {0}</html>", userdataDir.getAbsoluteFile()), 83 tr("Error"), 84 JOptionPane.ERROR_MESSAGE 85 ); 86 } 87 return userdataDir; 88 } 89 90 @Override 91 public File getCacheDirectory(boolean createIfMissing) { 92 if (cacheDir == null) { 93 String path = System.getProperty("josm.cache"); 94 if (path != null) { 95 cacheDir = new File(path).getAbsoluteFile(); 96 } else { 97 path = System.getProperty("josm.home"); 98 if (path != null) { 99 cacheDir = new File(path, "cache"); 100 } else { 101 cacheDir = Main.platform.getDefaultCacheDirectory(); 102 } 103 } 104 } 105 if (createIfMissing && !cacheDir.exists() && !cacheDir.mkdirs()) { 106 Logging.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile())); 107 JOptionPane.showMessageDialog( 108 Main.parent, 109 tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()), 110 tr("Error"), 111 JOptionPane.ERROR_MESSAGE 112 ); 113 } 114 return cacheDir; 115 } 116 117 } -
src/org/openstreetmap/josm/gui/MainApplication.java
Property changes on: src/org/openstreetmap/josm/data/preferences/JosmBaseDirectories.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property
10 10 import java.awt.Font; 11 11 import java.awt.GraphicsEnvironment; 12 12 import java.awt.GridBagLayout; 13 import java.awt.Toolkit; 13 14 import java.awt.event.KeyEvent; 14 15 import java.io.File; 15 16 import java.io.IOException; 16 17 import java.io.InputStream; 18 import java.lang.reflect.Field; 17 19 import java.net.Authenticator; 18 20 import java.net.Inet6Address; 19 21 import java.net.InetAddress; … … 37 39 import java.util.Map; 38 40 import java.util.Objects; 39 41 import java.util.Optional; 42 import java.util.ResourceBundle; 40 43 import java.util.Set; 41 44 import java.util.TreeSet; 42 45 import java.util.concurrent.Callable; … … 147 150 import org.openstreetmap.josm.plugins.PluginHandler; 148 151 import org.openstreetmap.josm.plugins.PluginInformation; 149 152 import org.openstreetmap.josm.spi.preferences.Config; 153 import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent; 154 import org.openstreetmap.josm.spi.preferences.PreferenceChangedListener; 150 155 import org.openstreetmap.josm.tools.FontsManager; 151 156 import org.openstreetmap.josm.tools.GBC; 152 157 import org.openstreetmap.josm.tools.HttpClient; … … 932 937 if (!language.isPresent()) { 933 938 I18n.set(Config.getPref().get("language", null)); 934 939 } 935 Main.pref.updateSystemProperties(); 940 updateSystemProperties(); 941 Main.pref.addPreferenceChangeListener(new PreferenceChangedListener() { 942 @Override 943 public void preferenceChanged(PreferenceChangeEvent e) { 944 updateSystemProperties(); 945 } 946 }); 936 947 937 948 checkIPv6(); 938 949 … … 1072 1083 } 1073 1084 1074 1085 /** 1086 * Updates system properties with the current values in the preferences. 1087 */ 1088 public static void updateSystemProperties() { 1089 if ("true".equals(Config.getPref().get("prefer.ipv6", "auto")) 1090 && !"true".equals(Utils.updateSystemProperty("java.net.preferIPv6Addresses", "true"))) { 1091 // never set this to false, only true! 1092 Logging.info(tr("Try enabling IPv6 network, prefering IPv6 over IPv4 (only works on early startup).")); 1093 } 1094 Utils.updateSystemProperty("http.agent", Version.getInstance().getAgentString()); 1095 Utils.updateSystemProperty("user.language", Config.getPref().get("language")); 1096 // Workaround to fix a Java bug. This ugly hack comes from Sun bug database: https://bugs.openjdk.java.net/browse/JDK-6292739 1097 // Force AWT toolkit to update its internal preferences (fix #6345). 1098 // Does not work anymore with Java 9, to remove with Java 9 migration 1099 if (Utils.getJavaVersion() < 9 && !GraphicsEnvironment.isHeadless()) { 1100 try { 1101 Field field = Toolkit.class.getDeclaredField("resources"); 1102 Utils.setObjectsAccessible(field); 1103 field.set(null, ResourceBundle.getBundle("sun.awt.resources.awt")); 1104 } catch (ReflectiveOperationException | RuntimeException e) { // NOPMD 1105 // Catch RuntimeException in order to catch InaccessibleObjectException, new in Java 9 1106 Logging.warn(e); 1107 } 1108 } 1109 // Possibility to disable SNI (not by default) in case of misconfigured https servers 1110 // See #9875 + http://stackoverflow.com/a/14884941/2257172 1111 // then https://josm.openstreetmap.de/ticket/12152#comment:5 for details 1112 if (Config.getPref().getBoolean("jdk.tls.disableSNIExtension", false)) { 1113 Utils.updateSystemProperty("jsse.enableSNIExtension", "false"); 1114 } 1115 } 1116 1117 /** 1075 1118 * Setup the sources for NTV2 grid shift files for projection support. 1076 1119 * @since 12795 1077 1120 */ -
src/org/openstreetmap/josm/plugins/Plugin.java
16 16 import java.util.List; 17 17 18 18 import org.openstreetmap.josm.Main; 19 import org.openstreetmap.josm.data.Preferences; 19 20 import org.openstreetmap.josm.gui.MapFrame; 20 21 import org.openstreetmap.josm.gui.MapFrameListener; 21 22 import org.openstreetmap.josm.gui.download.DownloadSelection; … … 22 23 import org.openstreetmap.josm.gui.preferences.PreferenceSetting; 23 24 import org.openstreetmap.josm.spi.preferences.Config; 24 25 import org.openstreetmap.josm.spi.preferences.IBaseDirectories; 26 import org.openstreetmap.josm.spi.preferences.IPreferences; 25 27 import org.openstreetmap.josm.tools.Logging; 26 28 import org.openstreetmap.josm.tools.Utils; 27 29 … … 57 59 */ 58 60 private PluginInformation info; 59 61 60 private final IBaseDirectories pluginBaseDirectories = new PluginBaseDirectories(); 62 private final IBaseDirectories pluginBaseDirectories; 63 final Preferences pluginPref; 64 boolean pluginPrefInitialized; 61 65 62 66 private class PluginBaseDirectories implements IBaseDirectories { 63 67 private File preferencesDir; … … 108 112 */ 109 113 public Plugin(PluginInformation info) { 110 114 this.info = info; 115 this.pluginBaseDirectories = new PluginBaseDirectories(); 116 this.pluginPref = new Preferences(pluginBaseDirectories); 111 117 } 112 118 113 119 /** … … 148 154 return new File(Main.pref.getPluginsDirectory(), info.name).getPath(); 149 155 } 150 156 157 public IPreferences getPref() { 158 if (!pluginPrefInitialized) { 159 // lazy initialization, so it will not create directories and files 160 // when preferences is not accessed by the plugin 161 pluginPref.init(false); 162 pluginPrefInitialized = true; 163 } 164 return pluginPref; 165 } 166 151 167 @Override 152 168 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {} 153 169 -
src/org/openstreetmap/josm/plugins/PluginHandler.java
1554 1554 return result; 1555 1555 } 1556 1556 1557 public static void shutdown() { 1558 for (PluginProxy pp : pluginList) { 1559 Utils.instanceOfAndCast(pp.getPlugin(), Plugin.class) 1560 .filter(plugin -> plugin.pluginPrefInitialized).ifPresent(plugin -> { 1561 try { 1562 plugin.pluginPref.saveDefaults(); 1563 } catch (IOException ex) { 1564 Logging.log(Logging.LEVEL_WARN, 1565 tr("Failed to save default preferences for plugin {0}.", plugin.getPluginInformation().name), ex); 1566 } 1567 }); 1568 } 1569 } 1570 1557 1571 private static class UpdatePluginsMessagePanel extends JPanel { 1558 1572 private final JMultilineLabel lblMessage = new JMultilineLabel(""); 1559 1573 private final JCheckBox cbDontShowAgain = new JCheckBox(