source: josm/trunk/src/org/openstreetmap/josm/data/Preferences.java@ 12867

Last change on this file since 12867 was 12865, checked in by Don-vip, 7 years ago

see #11390 - SonarQube - squid:S3824 - "Map.get" and value test should be replaced with single method call

  • Property svn:eol-style set to native
File size: 59.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Color;
8import java.awt.GraphicsEnvironment;
9import java.awt.Toolkit;
10import java.io.File;
11import java.io.IOException;
12import java.io.PrintWriter;
13import java.io.Reader;
14import java.io.StringWriter;
15import java.lang.annotation.Retention;
16import java.lang.annotation.RetentionPolicy;
17import java.lang.reflect.Field;
18import java.nio.charset.StandardCharsets;
19import java.util.AbstractMap;
20import java.util.ArrayList;
21import java.util.Collection;
22import java.util.Collections;
23import java.util.HashMap;
24import java.util.HashSet;
25import java.util.Iterator;
26import java.util.LinkedList;
27import java.util.List;
28import java.util.Map;
29import java.util.Map.Entry;
30import java.util.Optional;
31import java.util.ResourceBundle;
32import java.util.Set;
33import java.util.SortedMap;
34import java.util.TreeMap;
35import java.util.concurrent.TimeUnit;
36import java.util.function.Predicate;
37import java.util.regex.Matcher;
38import java.util.regex.Pattern;
39import java.util.stream.Collectors;
40import java.util.stream.Stream;
41
42import javax.swing.JOptionPane;
43import javax.xml.stream.XMLStreamException;
44
45import org.openstreetmap.josm.Main;
46import org.openstreetmap.josm.data.preferences.BooleanProperty;
47import org.openstreetmap.josm.data.preferences.ColorProperty;
48import org.openstreetmap.josm.data.preferences.DoubleProperty;
49import org.openstreetmap.josm.data.preferences.IntegerProperty;
50import org.openstreetmap.josm.data.preferences.ListListSetting;
51import org.openstreetmap.josm.data.preferences.ListSetting;
52import org.openstreetmap.josm.data.preferences.LongProperty;
53import org.openstreetmap.josm.data.preferences.MapListSetting;
54import org.openstreetmap.josm.data.preferences.PreferencesReader;
55import org.openstreetmap.josm.data.preferences.PreferencesWriter;
56import org.openstreetmap.josm.data.preferences.Setting;
57import org.openstreetmap.josm.data.preferences.StringSetting;
58import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
59import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
60import org.openstreetmap.josm.io.OfflineAccessException;
61import org.openstreetmap.josm.io.OnlineResource;
62import org.openstreetmap.josm.spi.preferences.AbstractPreferences;
63import org.openstreetmap.josm.spi.preferences.IBaseDirectories;
64import org.openstreetmap.josm.spi.preferences.IPreferences;
65import org.openstreetmap.josm.tools.CheckParameterUtil;
66import org.openstreetmap.josm.tools.ColorHelper;
67import org.openstreetmap.josm.tools.I18n;
68import org.openstreetmap.josm.tools.ListenerList;
69import org.openstreetmap.josm.tools.Logging;
70import org.openstreetmap.josm.tools.Utils;
71import org.xml.sax.SAXException;
72
73/**
74 * This class holds all preferences for JOSM.
75 *
76 * Other classes can register their beloved properties here. All properties will be
77 * saved upon set-access.
78 *
79 * Each property is a key=setting pair, where key is a String and setting can be one of
80 * 4 types:
81 * string, list, list of lists and list of maps.
82 * In addition, each key has a unique default value that is set when the value is first
83 * accessed using one of the get...() methods. You can use the same preference
84 * key in different parts of the code, but the default value must be the same
85 * everywhere. A default value of null means, the setting has been requested, but
86 * no default value was set. This is used in advanced preferences to present a list
87 * off all possible settings.
88 *
89 * At the moment, you cannot put the empty string for string properties.
90 * put(key, "") means, the property is removed.
91 *
92 * @author imi
93 * @since 74
94 */
95public class Preferences extends AbstractPreferences implements IBaseDirectories {
96
97 private static final String COLOR_PREFIX = "color.";
98
99 private static final String[] OBSOLETE_PREF_KEYS = {
100 "imagery.layers.addedIds", /* remove entry after June 2017 */
101 "projection", /* remove entry after Nov. 2017 */
102 "projection.sub", /* remove entry after Nov. 2017 */
103 };
104
105 private static final long MAX_AGE_DEFAULT_PREFERENCES = TimeUnit.DAYS.toSeconds(50);
106
107 /**
108 * Internal storage for the preference directory.
109 * Do not access this variable directly!
110 * @see #getPreferencesDirectory()
111 */
112 private File preferencesDir;
113
114 /**
115 * Internal storage for the cache directory.
116 */
117 private File cacheDir;
118
119 /**
120 * Internal storage for the user data directory.
121 */
122 private File userdataDir;
123
124 /**
125 * Determines if preferences file is saved each time a property is changed.
126 */
127 private boolean saveOnPut = true;
128
129 /**
130 * Maps the setting name to the current value of the setting.
131 * The map must not contain null as key or value. The mapped setting objects
132 * must not have a null value.
133 */
134 protected final SortedMap<String, Setting<?>> settingsMap = new TreeMap<>();
135
136 /**
137 * Maps the setting name to the default value of the setting.
138 * The map must not contain null as key or value. The value of the mapped
139 * setting objects can be null.
140 */
141 protected final SortedMap<String, Setting<?>> defaultsMap = new TreeMap<>();
142
143 private final Predicate<Entry<String, Setting<?>>> NO_DEFAULT_SETTINGS_ENTRY =
144 e -> !e.getValue().equals(defaultsMap.get(e.getKey()));
145
146 /**
147 * Maps color keys to human readable color name
148 */
149 protected final SortedMap<String, String> colornames = new TreeMap<>();
150
151 /**
152 * Indicates whether {@link #init(boolean)} completed successfully.
153 * Used to decide whether to write backup preference file in {@link #save()}
154 */
155 protected boolean initSuccessful;
156
157 /**
158 * Event triggered when a preference entry value changes.
159 */
160 public interface PreferenceChangeEvent {
161 /**
162 * Returns the preference key.
163 * @return the preference key
164 */
165 String getKey();
166
167 /**
168 * Returns the old preference value.
169 * @return the old preference value
170 */
171 Setting<?> getOldValue();
172
173 /**
174 * Returns the new preference value.
175 * @return the new preference value
176 */
177 Setting<?> getNewValue();
178 }
179
180 /**
181 * Listener to preference change events.
182 * @since 10600 (functional interface)
183 */
184 @FunctionalInterface
185 public interface PreferenceChangedListener {
186 /**
187 * Trigerred when a preference entry value changes.
188 * @param e the preference change event
189 */
190 void preferenceChanged(PreferenceChangeEvent e);
191 }
192
193 private static class DefaultPreferenceChangeEvent implements PreferenceChangeEvent {
194 private final String key;
195 private final Setting<?> oldValue;
196 private final Setting<?> newValue;
197
198 DefaultPreferenceChangeEvent(String key, Setting<?> oldValue, Setting<?> newValue) {
199 this.key = key;
200 this.oldValue = oldValue;
201 this.newValue = newValue;
202 }
203
204 @Override
205 public String getKey() {
206 return key;
207 }
208
209 @Override
210 public Setting<?> getOldValue() {
211 return oldValue;
212 }
213
214 @Override
215 public Setting<?> getNewValue() {
216 return newValue;
217 }
218 }
219
220 private final ListenerList<PreferenceChangedListener> listeners = ListenerList.create();
221
222 private final HashMap<String, ListenerList<PreferenceChangedListener>> keyListeners = new HashMap<>();
223
224 /**
225 * Constructs a new {@code Preferences}.
226 */
227 public Preferences() {
228 // Default constructor
229 }
230
231 /**
232 * Constructs a new {@code Preferences} from an existing instance.
233 * @param pref existing preferences to copy
234 * @since 12634
235 */
236 public Preferences(Preferences pref) {
237 settingsMap.putAll(pref.settingsMap);
238 defaultsMap.putAll(pref.defaultsMap);
239 colornames.putAll(pref.colornames);
240 }
241
242 /**
243 * Adds a new preferences listener.
244 * @param listener The listener to add
245 */
246 @Override
247 public void addPreferenceChangeListener(PreferenceChangedListener listener) {
248 if (listener != null) {
249 listeners.addListener(listener);
250 }
251 }
252
253 /**
254 * Removes a preferences listener.
255 * @param listener The listener to remove
256 */
257 @Override
258 public void removePreferenceChangeListener(PreferenceChangedListener listener) {
259 listeners.removeListener(listener);
260 }
261
262 /**
263 * Adds a listener that only listens to changes in one preference
264 * @param key The preference key to listen to
265 * @param listener The listener to add.
266 * @since 10824
267 */
268 @Override
269 public void addKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
270 listenersForKey(key).addListener(listener);
271 }
272
273 /**
274 * Adds a weak listener that only listens to changes in one preference
275 * @param key The preference key to listen to
276 * @param listener The listener to add.
277 * @since 10824
278 */
279 public void addWeakKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
280 listenersForKey(key).addWeakListener(listener);
281 }
282
283 private ListenerList<PreferenceChangedListener> listenersForKey(String key) {
284 return keyListeners.computeIfAbsent(key, k -> ListenerList.create());
285 }
286
287 /**
288 * Removes a listener that only listens to changes in one preference
289 * @param key The preference key to listen to
290 * @param listener The listener to add.
291 */
292 @Override
293 public void removeKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
294 Optional.ofNullable(keyListeners.get(key)).orElseThrow(
295 () -> new IllegalArgumentException("There are no listeners registered for " + key))
296 .removeListener(listener);
297 }
298
299 protected void firePreferenceChanged(String key, Setting<?> oldValue, Setting<?> newValue) {
300 final PreferenceChangeEvent evt = new DefaultPreferenceChangeEvent(key, oldValue, newValue);
301 listeners.fireEvent(listener -> listener.preferenceChanged(evt));
302
303 ListenerList<PreferenceChangedListener> forKey = keyListeners.get(key);
304 if (forKey != null) {
305 forKey.fireEvent(listener -> listener.preferenceChanged(evt));
306 }
307 }
308
309 /**
310 * Get the base name of the JOSM directories for preferences, cache and
311 * user data.
312 * Default value is "JOSM", unless overridden by system property "josm.dir.name".
313 * @return the base name of the JOSM directories for preferences, cache and
314 * user data
315 */
316 public String getJOSMDirectoryBaseName() {
317 String name = System.getProperty("josm.dir.name");
318 if (name != null)
319 return name;
320 else
321 return "JOSM";
322 }
323
324 /**
325 * Returns the user defined preferences directory, containing the preferences.xml file
326 * @return The user defined preferences directory, containing the preferences.xml file
327 * @since 7834
328 * @deprecated use {@link #getPreferencesDirectory(boolean)}
329 */
330 @Deprecated
331 public File getPreferencesDirectory() {
332 return getPreferencesDirectory(false);
333 }
334
335 @Override
336 public File getPreferencesDirectory(boolean createIfMissing) {
337 if (preferencesDir == null) {
338 String path;
339 path = System.getProperty("josm.pref");
340 if (path != null) {
341 preferencesDir = new File(path).getAbsoluteFile();
342 } else {
343 path = System.getProperty("josm.home");
344 if (path != null) {
345 preferencesDir = new File(path).getAbsoluteFile();
346 } else {
347 preferencesDir = Main.platform.getDefaultPrefDirectory();
348 }
349 }
350 }
351 if (createIfMissing && !preferencesDir.exists() && !preferencesDir.mkdirs()) {
352 Logging.warn(tr("Failed to create missing preferences directory: {0}", preferencesDir.getAbsoluteFile()));
353 JOptionPane.showMessageDialog(
354 Main.parent,
355 tr("<html>Failed to create missing preferences directory: {0}</html>", preferencesDir.getAbsoluteFile()),
356 tr("Error"),
357 JOptionPane.ERROR_MESSAGE
358 );
359 }
360 return preferencesDir;
361 }
362
363 /**
364 * Returns the user data directory, containing autosave, plugins, etc.
365 * Depending on the OS it may be the same directory as preferences directory.
366 * @return The user data directory, containing autosave, plugins, etc.
367 * @since 7834
368 * @deprecated use {@link #getUserDataDirectory(boolean)}
369 */
370 @Deprecated
371 public File getUserDataDirectory() {
372 return getUserDataDirectory(false);
373 }
374
375 @Override
376 public File getUserDataDirectory(boolean createIfMissing) {
377 if (userdataDir == null) {
378 String path;
379 path = System.getProperty("josm.userdata");
380 if (path != null) {
381 userdataDir = new File(path).getAbsoluteFile();
382 } else {
383 path = System.getProperty("josm.home");
384 if (path != null) {
385 userdataDir = new File(path).getAbsoluteFile();
386 } else {
387 userdataDir = Main.platform.getDefaultUserDataDirectory();
388 }
389 }
390 }
391 if (createIfMissing && !userdataDir.exists() && !userdataDir.mkdirs()) {
392 Logging.warn(tr("Failed to create missing user data directory: {0}", userdataDir.getAbsoluteFile()));
393 JOptionPane.showMessageDialog(
394 Main.parent,
395 tr("<html>Failed to create missing user data directory: {0}</html>", userdataDir.getAbsoluteFile()),
396 tr("Error"),
397 JOptionPane.ERROR_MESSAGE
398 );
399 }
400 return userdataDir;
401 }
402
403 /**
404 * Returns the user preferences file (preferences.xml).
405 * @return The user preferences file (preferences.xml)
406 */
407 public File getPreferenceFile() {
408 return new File(getPreferencesDirectory(false), "preferences.xml");
409 }
410
411 /**
412 * Returns the cache file for default preferences.
413 * @return the cache file for default preferences
414 */
415 public File getDefaultsCacheFile() {
416 return new File(getCacheDirectory(true), "default_preferences.xml");
417 }
418
419 /**
420 * Returns the user plugin directory.
421 * @return The user plugin directory
422 */
423 public File getPluginsDirectory() {
424 return new File(getUserDataDirectory(false), "plugins");
425 }
426
427 /**
428 * Get the directory where cached content of any kind should be stored.
429 *
430 * If the directory doesn't exist on the file system, it will be created by this method.
431 *
432 * @return the cache directory
433 * @deprecated use {@link #getCacheDirectory(boolean)}
434 */
435 @Deprecated
436 public File getCacheDirectory() {
437 return getCacheDirectory(true);
438 }
439
440 @Override
441 public File getCacheDirectory(boolean createIfMissing) {
442 if (cacheDir == null) {
443 String path = System.getProperty("josm.cache");
444 if (path != null) {
445 cacheDir = new File(path).getAbsoluteFile();
446 } else {
447 path = System.getProperty("josm.home");
448 if (path != null) {
449 cacheDir = new File(path, "cache");
450 } else {
451 path = get("cache.folder", null);
452 if (path != null) {
453 cacheDir = new File(path).getAbsoluteFile();
454 } else {
455 cacheDir = Main.platform.getDefaultCacheDirectory();
456 }
457 }
458 }
459 }
460 if (createIfMissing && !cacheDir.exists() && !cacheDir.mkdirs()) {
461 Logging.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile()));
462 JOptionPane.showMessageDialog(
463 Main.parent,
464 tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()),
465 tr("Error"),
466 JOptionPane.ERROR_MESSAGE
467 );
468 }
469 return cacheDir;
470 }
471
472 private static void addPossibleResourceDir(Set<String> locations, String s) {
473 if (s != null) {
474 if (!s.endsWith(File.separator)) {
475 s += File.separator;
476 }
477 locations.add(s);
478 }
479 }
480
481 /**
482 * Returns a set of all existing directories where resources could be stored.
483 * @return A set of all existing directories where resources could be stored.
484 */
485 public Collection<String> getAllPossiblePreferenceDirs() {
486 Set<String> locations = new HashSet<>();
487 addPossibleResourceDir(locations, getPreferencesDirectory(false).getPath());
488 addPossibleResourceDir(locations, getUserDataDirectory(false).getPath());
489 addPossibleResourceDir(locations, System.getenv("JOSM_RESOURCES"));
490 addPossibleResourceDir(locations, System.getProperty("josm.resources"));
491 if (Main.isPlatformWindows()) {
492 String appdata = System.getenv("APPDATA");
493 if (appdata != null && System.getenv("ALLUSERSPROFILE") != null
494 && appdata.lastIndexOf(File.separator) != -1) {
495 appdata = appdata.substring(appdata.lastIndexOf(File.separator));
496 locations.add(new File(new File(System.getenv("ALLUSERSPROFILE"),
497 appdata), "JOSM").getPath());
498 }
499 } else {
500 locations.add("/usr/local/share/josm/");
501 locations.add("/usr/local/lib/josm/");
502 locations.add("/usr/share/josm/");
503 locations.add("/usr/lib/josm/");
504 }
505 return locations;
506 }
507
508 /**
509 * Gets all normal (string) settings that have a key starting with the prefix
510 * @param prefix The start of the key
511 * @return The key names of the settings
512 */
513 public synchronized Map<String, String> getAllPrefix(final String prefix) {
514 final Map<String, String> all = new TreeMap<>();
515 for (final Entry<String, Setting<?>> e : settingsMap.entrySet()) {
516 if (e.getKey().startsWith(prefix) && (e.getValue() instanceof StringSetting)) {
517 all.put(e.getKey(), ((StringSetting) e.getValue()).getValue());
518 }
519 }
520 return all;
521 }
522
523 /**
524 * Gets all list settings that have a key starting with the prefix
525 * @param prefix The start of the key
526 * @return The key names of the list settings
527 */
528 public synchronized List<String> getAllPrefixCollectionKeys(final String prefix) {
529 final List<String> all = new LinkedList<>();
530 for (Map.Entry<String, Setting<?>> entry : settingsMap.entrySet()) {
531 if (entry.getKey().startsWith(prefix) && entry.getValue() instanceof ListSetting) {
532 all.add(entry.getKey());
533 }
534 }
535 return all;
536 }
537
538 /**
539 * Gets all known colors (preferences starting with the color prefix)
540 * @return All colors
541 */
542 public synchronized Map<String, String> getAllColors() {
543 final Map<String, String> all = new TreeMap<>();
544 for (final Entry<String, Setting<?>> e : defaultsMap.entrySet()) {
545 if (e.getKey().startsWith(COLOR_PREFIX) && e.getValue() instanceof StringSetting) {
546 StringSetting d = (StringSetting) e.getValue();
547 if (d.getValue() != null) {
548 all.put(e.getKey().substring(6), d.getValue());
549 }
550 }
551 }
552 for (final Entry<String, Setting<?>> e : settingsMap.entrySet()) {
553 if (e.getKey().startsWith(COLOR_PREFIX) && (e.getValue() instanceof StringSetting)) {
554 all.put(e.getKey().substring(6), ((StringSetting) e.getValue()).getValue());
555 }
556 }
557 return all;
558 }
559
560 /**
561 * Gets an boolean that may be specialized
562 * @param key The basic key
563 * @param specName The sub-key to append to the key
564 * @param def The default value
565 * @return The boolean value or the default value if it could not be parsed
566 */
567 public synchronized boolean getBoolean(final String key, final String specName, final boolean def) {
568 boolean generic = getBoolean(key, def);
569 String skey = key+'.'+specName;
570 Setting<?> prop = settingsMap.get(skey);
571 if (prop instanceof StringSetting)
572 return Boolean.parseBoolean(((StringSetting) prop).getValue());
573 else
574 return generic;
575 }
576
577 /**
578 * Set a boolean value for a certain setting.
579 * @param key the unique identifier for the setting
580 * @param value The new value
581 * @return {@code true}, if something has changed (i.e. value is different than before)
582 * @see BooleanProperty
583 * @deprecated use {@link IPreferences#putBoolean(String, boolean)}
584 */
585 @Deprecated
586 public boolean put(final String key, final boolean value) {
587 return put(key, Boolean.toString(value));
588 }
589
590 /**
591 * Set a boolean value for a certain setting.
592 * @param key the unique identifier for the setting
593 * @param value The new value
594 * @return {@code true}, if something has changed (i.e. value is different than before)
595 * @see IntegerProperty#put(Integer)
596 * @deprecated use {@link IPreferences#putInt(String, int)}
597 */
598 @Deprecated
599 public boolean putInteger(final String key, final Integer value) {
600 return put(key, Integer.toString(value));
601 }
602
603 /**
604 * Set a boolean value for a certain setting.
605 * @param key the unique identifier for the setting
606 * @param value The new value
607 * @return {@code true}, if something has changed (i.e. value is different than before)
608 * @see DoubleProperty#put(Double)
609 * @deprecated use {@link IPreferences#putDouble(java.lang.String, double)}
610 */
611 @Deprecated
612 public boolean putDouble(final String key, final Double value) {
613 return put(key, Double.toString(value));
614 }
615
616 /**
617 * Set a boolean value for a certain setting.
618 * @param key the unique identifier for the setting
619 * @param value The new value
620 * @return {@code true}, if something has changed (i.e. value is different than before)
621 * @see LongProperty#put(Long)
622 * @deprecated use {@link IPreferences#putLong(java.lang.String, long)}
623 */
624 @Deprecated
625 public boolean putLong(final String key, final Long value) {
626 return put(key, Long.toString(value));
627 }
628
629 /**
630 * Called after every put. In case of a problem, do nothing but output the error in log.
631 * @throws IOException if any I/O error occurs
632 */
633 public synchronized void save() throws IOException {
634 save(getPreferenceFile(), settingsMap.entrySet().stream().filter(NO_DEFAULT_SETTINGS_ENTRY), false);
635 }
636
637 /**
638 * Stores the defaults to the defaults file
639 * @throws IOException If the file could not be saved
640 */
641 public synchronized void saveDefaults() throws IOException {
642 save(getDefaultsCacheFile(), defaultsMap.entrySet().stream(), true);
643 }
644
645 protected void save(File prefFile, Stream<Entry<String, Setting<?>>> settings, boolean defaults) throws IOException {
646 if (!defaults) {
647 /* currently unused, but may help to fix configuration issues in future */
648 putInt("josm.version", Version.getInstance().getVersion());
649
650 updateSystemProperties();
651 }
652
653 File backupFile = new File(prefFile + "_backup");
654
655 // Backup old preferences if there are old preferences
656 if (initSuccessful && prefFile.exists() && prefFile.length() > 0) {
657 Utils.copyFile(prefFile, backupFile);
658 }
659
660 try (PreferencesWriter writer = new PreferencesWriter(
661 new PrintWriter(new File(prefFile + "_tmp"), StandardCharsets.UTF_8.name()), false, defaults)) {
662 writer.write(settings);
663 }
664
665 File tmpFile = new File(prefFile + "_tmp");
666 Utils.copyFile(tmpFile, prefFile);
667 Utils.deleteFile(tmpFile, marktr("Unable to delete temporary file {0}"));
668
669 setCorrectPermissions(prefFile);
670 setCorrectPermissions(backupFile);
671 }
672
673 private static void setCorrectPermissions(File file) {
674 if (!file.setReadable(false, false) && Logging.isTraceEnabled()) {
675 Logging.trace(tr("Unable to set file non-readable {0}", file.getAbsolutePath()));
676 }
677 if (!file.setWritable(false, false) && Logging.isTraceEnabled()) {
678 Logging.trace(tr("Unable to set file non-writable {0}", file.getAbsolutePath()));
679 }
680 if (!file.setExecutable(false, false) && Logging.isTraceEnabled()) {
681 Logging.trace(tr("Unable to set file non-executable {0}", file.getAbsolutePath()));
682 }
683 if (!file.setReadable(true, true) && Logging.isTraceEnabled()) {
684 Logging.trace(tr("Unable to set file readable {0}", file.getAbsolutePath()));
685 }
686 if (!file.setWritable(true, true) && Logging.isTraceEnabled()) {
687 Logging.trace(tr("Unable to set file writable {0}", file.getAbsolutePath()));
688 }
689 }
690
691 /**
692 * Loads preferences from settings file.
693 * @throws IOException if any I/O error occurs while reading the file
694 * @throws SAXException if the settings file does not contain valid XML
695 * @throws XMLStreamException if an XML error occurs while parsing the file (after validation)
696 */
697 protected void load() throws IOException, SAXException, XMLStreamException {
698 File pref = getPreferenceFile();
699 PreferencesReader.validateXML(pref);
700 PreferencesReader reader = new PreferencesReader(pref, false);
701 reader.parse();
702 settingsMap.clear();
703 settingsMap.putAll(reader.getSettings());
704 updateSystemProperties();
705 removeObsolete(reader.getVersion());
706 }
707
708 /**
709 * Loads default preferences from default settings cache file.
710 *
711 * Discards entries older than {@link #MAX_AGE_DEFAULT_PREFERENCES}.
712 *
713 * @throws IOException if any I/O error occurs while reading the file
714 * @throws SAXException if the settings file does not contain valid XML
715 * @throws XMLStreamException if an XML error occurs while parsing the file (after validation)
716 */
717 protected void loadDefaults() throws IOException, XMLStreamException, SAXException {
718 File def = getDefaultsCacheFile();
719 PreferencesReader.validateXML(def);
720 PreferencesReader reader = new PreferencesReader(def, true);
721 reader.parse();
722 defaultsMap.clear();
723 long minTime = System.currentTimeMillis() / 1000 - MAX_AGE_DEFAULT_PREFERENCES;
724 for (Entry<String, Setting<?>> e : reader.getSettings().entrySet()) {
725 if (e.getValue().getTime() >= minTime) {
726 defaultsMap.put(e.getKey(), e.getValue());
727 }
728 }
729 }
730
731 /**
732 * Loads preferences from XML reader.
733 * @param in XML reader
734 * @throws XMLStreamException if any XML stream error occurs
735 * @throws IOException if any I/O error occurs
736 */
737 public void fromXML(Reader in) throws XMLStreamException, IOException {
738 PreferencesReader reader = new PreferencesReader(in, false);
739 reader.parse();
740 settingsMap.clear();
741 settingsMap.putAll(reader.getSettings());
742 }
743
744 /**
745 * Initializes preferences.
746 * @param reset if {@code true}, current settings file is replaced by the default one
747 */
748 public void init(boolean reset) {
749 initSuccessful = false;
750 // get the preferences.
751 File prefDir = getPreferencesDirectory(false);
752 if (prefDir.exists()) {
753 if (!prefDir.isDirectory()) {
754 Logging.warn(tr("Failed to initialize preferences. Preference directory ''{0}'' is not a directory.",
755 prefDir.getAbsoluteFile()));
756 JOptionPane.showMessageDialog(
757 Main.parent,
758 tr("<html>Failed to initialize preferences.<br>Preference directory ''{0}'' is not a directory.</html>",
759 prefDir.getAbsoluteFile()),
760 tr("Error"),
761 JOptionPane.ERROR_MESSAGE
762 );
763 return;
764 }
765 } else {
766 if (!prefDir.mkdirs()) {
767 Logging.warn(tr("Failed to initialize preferences. Failed to create missing preference directory: {0}",
768 prefDir.getAbsoluteFile()));
769 JOptionPane.showMessageDialog(
770 Main.parent,
771 tr("<html>Failed to initialize preferences.<br>Failed to create missing preference directory: {0}</html>",
772 prefDir.getAbsoluteFile()),
773 tr("Error"),
774 JOptionPane.ERROR_MESSAGE
775 );
776 return;
777 }
778 }
779
780 File preferenceFile = getPreferenceFile();
781 try {
782 if (!preferenceFile.exists()) {
783 Logging.info(tr("Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile()));
784 resetToDefault();
785 save();
786 } else if (reset) {
787 File backupFile = new File(prefDir, "preferences.xml.bak");
788 Main.platform.rename(preferenceFile, backupFile);
789 Logging.warn(tr("Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile()));
790 resetToDefault();
791 save();
792 }
793 } catch (IOException e) {
794 Logging.error(e);
795 JOptionPane.showMessageDialog(
796 Main.parent,
797 tr("<html>Failed to initialize preferences.<br>Failed to reset preference file to default: {0}</html>",
798 getPreferenceFile().getAbsoluteFile()),
799 tr("Error"),
800 JOptionPane.ERROR_MESSAGE
801 );
802 return;
803 }
804 try {
805 load();
806 initSuccessful = true;
807 } catch (IOException | SAXException | XMLStreamException e) {
808 Logging.error(e);
809 File backupFile = new File(prefDir, "preferences.xml.bak");
810 JOptionPane.showMessageDialog(
811 Main.parent,
812 tr("<html>Preferences file had errors.<br> Making backup of old one to <br>{0}<br> " +
813 "and creating a new default preference file.</html>",
814 backupFile.getAbsoluteFile()),
815 tr("Error"),
816 JOptionPane.ERROR_MESSAGE
817 );
818 Main.platform.rename(preferenceFile, backupFile);
819 try {
820 resetToDefault();
821 save();
822 } catch (IOException e1) {
823 Logging.error(e1);
824 Logging.warn(tr("Failed to initialize preferences. Failed to reset preference file to default: {0}", getPreferenceFile()));
825 }
826 }
827 File def = getDefaultsCacheFile();
828 if (def.exists()) {
829 try {
830 loadDefaults();
831 } catch (IOException | XMLStreamException | SAXException e) {
832 Logging.error(e);
833 Logging.warn(tr("Failed to load defaults cache file: {0}", def));
834 defaultsMap.clear();
835 if (!def.delete()) {
836 Logging.warn(tr("Failed to delete faulty defaults cache file: {0}", def));
837 }
838 }
839 }
840 }
841
842 /**
843 * Resets the preferences to their initial state. This resets all values and file associations.
844 * The default values and listeners are not removed.
845 * <p>
846 * It is meant to be called before {@link #init(boolean)}
847 * @since 10876
848 */
849 public void resetToInitialState() {
850 resetToDefault();
851 preferencesDir = null;
852 cacheDir = null;
853 userdataDir = null;
854 saveOnPut = true;
855 initSuccessful = false;
856 }
857
858 /**
859 * Reset all values stored in this map to the default values. This clears the preferences.
860 */
861 public final void resetToDefault() {
862 settingsMap.clear();
863 }
864
865 /**
866 * Convenience method for accessing colour preferences.
867 * <p>
868 * To be removed: end of 2016
869 *
870 * @param colName name of the colour
871 * @param def default value
872 * @return a Color object for the configured colour, or the default value if none configured.
873 * @deprecated Use a {@link ColorProperty} instead.
874 */
875 @Deprecated
876 public synchronized Color getColor(String colName, Color def) {
877 return getColor(colName, null, def);
878 }
879
880 /* only for preferences */
881 public synchronized String getColorName(String o) {
882 Matcher m = Pattern.compile("mappaint\\.(.+?)\\.(.+)").matcher(o);
883 if (m.matches()) {
884 return tr("Paint style {0}: {1}", tr(I18n.escape(m.group(1))), tr(I18n.escape(m.group(2))));
885 }
886 m = Pattern.compile("layer (.+)").matcher(o);
887 if (m.matches()) {
888 return tr("Layer: {0}", tr(I18n.escape(m.group(1))));
889 }
890 return tr(I18n.escape(colornames.containsKey(o) ? colornames.get(o) : o));
891 }
892
893 /**
894 * Convenience method for accessing colour preferences.
895 * <p>
896 * To be removed: end of 2016
897 * @param colName name of the colour
898 * @param specName name of the special colour settings
899 * @param def default value
900 * @return a Color object for the configured colour, or the default value if none configured.
901 * @deprecated Use a {@link ColorProperty} instead.
902 * You can replace this by: <code>new ColorProperty(colName, def).getChildColor(specName)</code>
903 */
904 @Deprecated
905 public synchronized Color getColor(String colName, String specName, Color def) {
906 String colKey = ColorProperty.getColorKey(colName);
907 registerColor(colKey, colName);
908 String colStr = specName != null ? get(COLOR_PREFIX+specName) : "";
909 if (colStr.isEmpty()) {
910 colStr = get(colKey, ColorHelper.color2html(def, true));
911 }
912 if (colStr != null && !colStr.isEmpty()) {
913 return ColorHelper.html2color(colStr);
914 } else {
915 return def;
916 }
917 }
918
919 /**
920 * Registers a color name conversion for the global color registry.
921 * @param colKey The key
922 * @param colName The name of the color.
923 * @since 10824
924 */
925 public void registerColor(String colKey, String colName) {
926 if (!colKey.equals(colName)) {
927 colornames.put(colKey, colName);
928 }
929 }
930
931 /**
932 * Gets the default color that was registered with the preference
933 * @param colKey The color name
934 * @return The color
935 */
936 public synchronized Color getDefaultColor(String colKey) {
937 StringSetting col = Utils.cast(defaultsMap.get(COLOR_PREFIX+colKey), StringSetting.class);
938 String colStr = col == null ? null : col.getValue();
939 return colStr == null || colStr.isEmpty() ? null : ColorHelper.html2color(colStr);
940 }
941
942 /**
943 * Stores a color
944 * @param colKey The color name
945 * @param val The color
946 * @return true if the setting was modified
947 * @see ColorProperty#put(Color)
948 */
949 public synchronized boolean putColor(String colKey, Color val) {
950 return put(COLOR_PREFIX+colKey, val != null ? ColorHelper.color2html(val, true) : null);
951 }
952
953 /**
954 * Gets an integer preference
955 * @param key The preference key
956 * @param def The default value to use
957 * @return The integer
958 * @see IntegerProperty#get()
959 * @deprecated use {@link IPreferences#getInt(String, int)}
960 */
961 @Deprecated
962 public synchronized int getInteger(String key, int def) {
963 String v = get(key, Integer.toString(def));
964 if (v.isEmpty())
965 return def;
966
967 try {
968 return Integer.parseInt(v);
969 } catch (NumberFormatException e) {
970 // fall out
971 Logging.trace(e);
972 }
973 return def;
974 }
975
976 /**
977 * Gets an integer that may be specialized
978 * @param key The basic key
979 * @param specName The sub-key to append to the key
980 * @param def The default value
981 * @return The integer value or the default value if it could not be parsed
982 */
983 public synchronized int getInteger(String key, String specName, int def) {
984 String v = get(key+'.'+specName);
985 if (v.isEmpty())
986 v = get(key, Integer.toString(def));
987 if (v.isEmpty())
988 return def;
989
990 try {
991 return Integer.parseInt(v);
992 } catch (NumberFormatException e) {
993 // fall out
994 Logging.trace(e);
995 }
996 return def;
997 }
998
999 /**
1000 * Get a list of values for a certain key
1001 * @param key the identifier for the setting
1002 * @param def the default value.
1003 * @return the corresponding value if the property has been set before, {@code def} otherwise
1004 * @deprecated use {@link IPreferences#getList(java.lang.String, java.util.List)}
1005 */
1006 @Deprecated
1007 public Collection<String> getCollection(String key, Collection<String> def) {
1008 return getSetting(key, ListSetting.create(def), ListSetting.class).getValue();
1009 }
1010
1011 /**
1012 * Get a list of values for a certain key
1013 * @param key the identifier for the setting
1014 * @return the corresponding value if the property has been set before, an empty collection otherwise.
1015 * @deprecated use {@link IPreferences#getList(java.lang.String)}
1016 */
1017 @Deprecated
1018 public Collection<String> getCollection(String key) {
1019 Collection<String> val = getList(key, null);
1020 return val == null ? Collections.<String>emptyList() : val;
1021 }
1022
1023 /**
1024 * Removes a value from a given String collection
1025 * @param key The preference key the collection is stored with
1026 * @param value The value that should be removed in the collection
1027 * @see #getList(String)
1028 */
1029 public synchronized void removeFromCollection(String key, String value) {
1030 List<String> a = new ArrayList<>(getList(key, Collections.<String>emptyList()));
1031 a.remove(value);
1032 putList(key, a);
1033 }
1034
1035 /**
1036 * Set a value for a certain setting. The changed setting is saved to the preference file immediately.
1037 * Due to caching mechanisms on modern operating systems and hardware, this shouldn't be a performance problem.
1038 * @param key the unique identifier for the setting
1039 * @param setting the value of the setting. In case it is null, the key-value entry will be removed.
1040 * @return {@code true}, if something has changed (i.e. value is different than before)
1041 */
1042 @Override
1043 public boolean putSetting(final String key, Setting<?> setting) {
1044 CheckParameterUtil.ensureParameterNotNull(key);
1045 if (setting != null && setting.getValue() == null)
1046 throw new IllegalArgumentException("setting argument must not have null value");
1047 Setting<?> settingOld;
1048 Setting<?> settingCopy = null;
1049 synchronized (this) {
1050 if (setting == null) {
1051 settingOld = settingsMap.remove(key);
1052 if (settingOld == null)
1053 return false;
1054 } else {
1055 settingOld = settingsMap.get(key);
1056 if (setting.equals(settingOld))
1057 return false;
1058 if (settingOld == null && setting.equals(defaultsMap.get(key)))
1059 return false;
1060 settingCopy = setting.copy();
1061 settingsMap.put(key, settingCopy);
1062 }
1063 if (saveOnPut) {
1064 try {
1065 save();
1066 } catch (IOException e) {
1067 Logging.log(Logging.LEVEL_WARN, tr("Failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile()), e);
1068 }
1069 }
1070 }
1071 // Call outside of synchronized section in case some listener wait for other thread that wait for preference lock
1072 firePreferenceChanged(key, settingOld, settingCopy);
1073 return true;
1074 }
1075
1076 /**
1077 * Get a setting of any type
1078 * @param key The key for the setting
1079 * @param def The default value to use if it was not found
1080 * @return The setting
1081 */
1082 public synchronized Setting<?> getSetting(String key, Setting<?> def) {
1083 return getSetting(key, def, Setting.class);
1084 }
1085
1086 /**
1087 * Get settings value for a certain key and provide default a value.
1088 * @param <T> the setting type
1089 * @param key the identifier for the setting
1090 * @param def the default value. For each call of getSetting() with a given key, the default value must be the same.
1091 * <code>def</code> must not be null, but the value of <code>def</code> can be null.
1092 * @param klass the setting type (same as T)
1093 * @return the corresponding value if the property has been set before, {@code def} otherwise
1094 */
1095 @SuppressWarnings("unchecked")
1096 @Override
1097 public synchronized <T extends Setting<?>> T getSetting(String key, T def, Class<T> klass) {
1098 CheckParameterUtil.ensureParameterNotNull(key);
1099 CheckParameterUtil.ensureParameterNotNull(def);
1100 Setting<?> oldDef = defaultsMap.get(key);
1101 if (oldDef != null && oldDef.isNew() && oldDef.getValue() != null && def.getValue() != null && !def.equals(oldDef)) {
1102 Logging.info("Defaults for " + key + " differ: " + def + " != " + defaultsMap.get(key));
1103 }
1104 if (def.getValue() != null || oldDef == null) {
1105 Setting<?> defCopy = def.copy();
1106 defCopy.setTime(System.currentTimeMillis() / 1000);
1107 defCopy.setNew(true);
1108 defaultsMap.put(key, defCopy);
1109 }
1110 Setting<?> prop = settingsMap.get(key);
1111 if (klass.isInstance(prop)) {
1112 return (T) prop;
1113 } else {
1114 return def;
1115 }
1116 }
1117
1118 /**
1119 * Put a collection.
1120 * @param key key
1121 * @param value value
1122 * @return {@code true}, if something has changed (i.e. value is different than before)
1123 * @deprecated use {@link IPreferences#putList(java.lang.String, java.util.List)}
1124 */
1125 @Deprecated
1126 public boolean putCollection(String key, Collection<String> value) {
1127 return putSetting(key, value == null ? null : ListSetting.create(value));
1128 }
1129
1130 /**
1131 * Saves at most {@code maxsize} items of collection {@code val}.
1132 * @param key key
1133 * @param maxsize max number of items to save
1134 * @param val value
1135 * @return {@code true}, if something has changed (i.e. value is different than before)
1136 */
1137 public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) {
1138 List<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));
1139 for (String i : val) {
1140 if (newCollection.size() >= maxsize) {
1141 break;
1142 }
1143 newCollection.add(i);
1144 }
1145 return putList(key, newCollection);
1146 }
1147
1148 /**
1149 * Used to read a 2-dimensional array of strings from the preference file.
1150 * If not a single entry could be found, <code>def</code> is returned.
1151 * @param key preference key
1152 * @param def default array value
1153 * @return array value
1154 */
1155 @SuppressWarnings({ "unchecked", "rawtypes" })
1156 public synchronized Collection<Collection<String>> getArray(String key, Collection<Collection<String>> def) {
1157 ListListSetting val = getSetting(key, ListListSetting.create(def), ListListSetting.class);
1158 return (Collection) val.getValue();
1159 }
1160
1161 /**
1162 * Gets a collection of string collections for the given key
1163 * @param key The key
1164 * @return The collection of string collections or an empty collection as default
1165 * @deprecated use {@link IPreferences#getListOfLists(java.lang.String)}
1166 */
1167 @Deprecated
1168 public Collection<Collection<String>> getArray(String key) {
1169 Collection<Collection<String>> res = getArray(key, null);
1170 return res == null ? Collections.<Collection<String>>emptyList() : res;
1171 }
1172
1173 /**
1174 * Put an array.
1175 * @param key key
1176 * @param value value
1177 * @return {@code true}, if something has changed (i.e. value is different than before)
1178 * @deprecated use {@link IPreferences#putListOfLists(java.lang.String, java.util.List)}
1179 */
1180 @Deprecated
1181 public boolean putArray(String key, Collection<Collection<String>> value) {
1182 return putSetting(key, value == null ? null : ListListSetting.create(value));
1183 }
1184
1185 /**
1186 * Gets a collection of key/value maps.
1187 * @param key The key to search at
1188 * @param def The default value to use
1189 * @return The stored value or the default one if it could not be parsed
1190 * @deprecated use {@link IPreferences#getListOfMaps(java.lang.String, java.util.List)}
1191 */
1192 @Deprecated
1193 public Collection<Map<String, String>> getListOfStructs(String key, Collection<Map<String, String>> def) {
1194 return getSetting(key, new MapListSetting(def == null ? null : new ArrayList<>(def)), MapListSetting.class).getValue();
1195 }
1196
1197 /**
1198 * Stores a list of structs
1199 * @param key The key to store the list in
1200 * @param value A list of key/value maps
1201 * @return <code>true</code> if the value was changed
1202 * @see #getListOfMaps(java.lang.String, java.util.List)
1203 * @deprecated use {@link IPreferences#putListOfMaps(java.lang.String, java.util.List)}
1204 */
1205 @Deprecated
1206 public boolean putListOfStructs(String key, Collection<Map<String, String>> value) {
1207 return putSetting(key, value == null ? null : new MapListSetting(new ArrayList<>(value)));
1208 }
1209
1210 /**
1211 * Annotation used for converting objects to String Maps and vice versa.
1212 * Indicates that a certain field should be considered in the conversion process. Otherwise it is ignored.
1213 *
1214 * @see #serializeStruct(java.lang.Object, java.lang.Class)
1215 * @see #deserializeStruct(java.util.Map, java.lang.Class)
1216 * @deprecated use {@link StructUtils.StructEntry}
1217 */
1218 @Deprecated
1219 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime
1220 public @interface pref { }
1221
1222 /**
1223 * Annotation used for converting objects to String Maps.
1224 * Indicates that a certain field should be written to the map, even if the value is the same as the default value.
1225 *
1226 * @see #serializeStruct(java.lang.Object, java.lang.Class)
1227 * @deprecated use {@link StructUtils.WriteExplicitly}
1228 */
1229 @Deprecated
1230 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime
1231 public @interface writeExplicitly { }
1232
1233 /**
1234 * Get a list of hashes which are represented by a struct-like class.
1235 * Possible properties are given by fields of the class klass that have the @pref annotation.
1236 * Default constructor is used to initialize the struct objects, properties then override some of these default values.
1237 * @param <T> klass type
1238 * @param key main preference key
1239 * @param klass The struct class
1240 * @return a list of objects of type T or an empty list if nothing was found
1241 * @deprecated use {@link StructUtils#getListOfStructs(IPreferences, String, Class)}
1242 */
1243 @Deprecated
1244 public <T> List<T> getListOfStructs(String key, Class<T> klass) {
1245 return StructUtils.getListOfStructs(this, key, klass);
1246 }
1247
1248 /**
1249 * same as above, but returns def if nothing was found
1250 * @param <T> klass type
1251 * @param key main preference key
1252 * @param def default value
1253 * @param klass The struct class
1254 * @return a list of objects of type T or {@code def} if nothing was found
1255 * @deprecated use {@link StructUtils#getListOfStructs(IPreferences, String, Collection, Class)}
1256 */
1257 @Deprecated
1258 public <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass) {
1259 return StructUtils.getListOfStructs(this, key, def, klass);
1260 }
1261
1262 /**
1263 * Convenience method that saves a MapListSetting which is provided as a collection of objects.
1264 *
1265 * Each object is converted to a <code>Map&lt;String, String&gt;</code> using the fields with {@link pref} annotation.
1266 * The field name is the key and the value will be converted to a string.
1267 *
1268 * Considers only fields that have the @pref annotation.
1269 * In addition it does not write fields with null values. (Thus they are cleared)
1270 * Default values are given by the field values after default constructor has been called.
1271 * Fields equal to the default value are not written unless the field has the @writeExplicitly annotation.
1272 * @param <T> the class,
1273 * @param key main preference key
1274 * @param val the list that is supposed to be saved
1275 * @param klass The struct class
1276 * @return true if something has changed
1277 * @deprecated use {@link StructUtils#putListOfStructs(IPreferences, String, Collection, Class)}
1278 */
1279 @Deprecated
1280 public <T> boolean putListOfStructs(String key, Collection<T> val, Class<T> klass) {
1281 return StructUtils.putListOfStructs(this, key, val, klass);
1282 }
1283
1284 /**
1285 * Convert an object to a String Map, by using field names and values as map key and value.
1286 *
1287 * The field value is converted to a String.
1288 *
1289 * Only fields with annotation {@link pref} are taken into account.
1290 *
1291 * Fields will not be written to the map if the value is null or unchanged
1292 * (compared to an object created with the no-arg-constructor).
1293 * The {@link writeExplicitly} annotation overrides this behavior, i.e. the default value will also be written.
1294 *
1295 * @param <T> the class of the object <code>struct</code>
1296 * @param struct the object to be converted
1297 * @param klass the class T
1298 * @return the resulting map (same data content as <code>struct</code>)
1299 * @deprecated use {@link StructUtils#serializeStruct(java.lang.Object, java.lang.Class)}
1300 */
1301 @Deprecated
1302 public static <T> Map<String, String> serializeStruct(T struct, Class<T> klass) {
1303 return StructUtils.serializeStruct(struct, klass);
1304 }
1305
1306 /**
1307 * Converts a String-Map to an object of a certain class, by comparing map keys to field names of the class and assigning
1308 * map values to the corresponding fields.
1309 *
1310 * The map value (a String) is converted to the field type. Supported types are: boolean, Boolean, int, Integer, double,
1311 * Double, String, Map&lt;String, String&gt; and Map&lt;String, List&lt;String&gt;&gt;.
1312 *
1313 * Only fields with annotation {@link pref} are taken into account.
1314 * @param <T> the class
1315 * @param hash the string map with initial values
1316 * @param klass the class T
1317 * @return an object of class T, initialized as described above
1318 * @deprecated use {@link StructUtils#deserializeStruct(java.util.Map, java.lang.Class)}
1319 */
1320 @Deprecated
1321 public static <T> T deserializeStruct(Map<String, String> hash, Class<T> klass) {
1322 return StructUtils.deserializeStruct(hash, klass);
1323 }
1324
1325 /**
1326 * Gets a map of all settings that are currently stored
1327 * @return The settings
1328 */
1329 public Map<String, Setting<?>> getAllSettings() {
1330 return new TreeMap<>(settingsMap);
1331 }
1332
1333 /**
1334 * Gets a map of all currently known defaults
1335 * @return The map (key/setting)
1336 */
1337 public Map<String, Setting<?>> getAllDefaults() {
1338 return new TreeMap<>(defaultsMap);
1339 }
1340
1341 /**
1342 * Updates system properties with the current values in the preferences.
1343 */
1344 public void updateSystemProperties() {
1345 if ("true".equals(get("prefer.ipv6", "auto")) && !"true".equals(Utils.updateSystemProperty("java.net.preferIPv6Addresses", "true"))) {
1346 // never set this to false, only true!
1347 Logging.info(tr("Try enabling IPv6 network, prefering IPv6 over IPv4 (only works on early startup)."));
1348 }
1349 Utils.updateSystemProperty("http.agent", Version.getInstance().getAgentString());
1350 Utils.updateSystemProperty("user.language", get("language"));
1351 // Workaround to fix a Java bug. This ugly hack comes from Sun bug database: https://bugs.openjdk.java.net/browse/JDK-6292739
1352 // Force AWT toolkit to update its internal preferences (fix #6345).
1353 // Does not work anymore with Java 9, to remove with Java 9 migration
1354 if (Utils.getJavaVersion() < 9 && !GraphicsEnvironment.isHeadless()) {
1355 try {
1356 Field field = Toolkit.class.getDeclaredField("resources");
1357 Utils.setObjectsAccessible(field);
1358 field.set(null, ResourceBundle.getBundle("sun.awt.resources.awt"));
1359 } catch (ReflectiveOperationException | RuntimeException e) { // NOPMD
1360 // Catch RuntimeException in order to catch InaccessibleObjectException, new in Java 9
1361 Logging.warn(e);
1362 }
1363 }
1364 // Possibility to disable SNI (not by default) in case of misconfigured https servers
1365 // See #9875 + http://stackoverflow.com/a/14884941/2257172
1366 // then https://josm.openstreetmap.de/ticket/12152#comment:5 for details
1367 if (getBoolean("jdk.tls.disableSNIExtension", false)) {
1368 Utils.updateSystemProperty("jsse.enableSNIExtension", "false");
1369 }
1370 }
1371
1372 /**
1373 * Replies the collection of plugin site URLs from where plugin lists can be downloaded.
1374 * @return the collection of plugin site URLs
1375 * @see #getOnlinePluginSites
1376 */
1377 public Collection<String> getPluginSites() {
1378 return getList("pluginmanager.sites", Collections.singletonList(Main.getJOSMWebsite()+"/pluginicons%<?plugins=>"));
1379 }
1380
1381 /**
1382 * Returns the list of plugin sites available according to offline mode settings.
1383 * @return the list of available plugin sites
1384 * @since 8471
1385 */
1386 public Collection<String> getOnlinePluginSites() {
1387 Collection<String> pluginSites = new ArrayList<>(getPluginSites());
1388 for (Iterator<String> it = pluginSites.iterator(); it.hasNext();) {
1389 try {
1390 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(it.next(), Main.getJOSMWebsite());
1391 } catch (OfflineAccessException ex) {
1392 Logging.log(Logging.LEVEL_WARN, ex);
1393 it.remove();
1394 }
1395 }
1396 return pluginSites;
1397 }
1398
1399 /**
1400 * Sets the collection of plugin site URLs.
1401 *
1402 * @param sites the site URLs
1403 */
1404 public void setPluginSites(Collection<String> sites) {
1405 putList("pluginmanager.sites", new ArrayList<>(sites));
1406 }
1407
1408 /**
1409 * Returns XML describing these preferences.
1410 * @param nopass if password must be excluded
1411 * @return XML
1412 */
1413 public String toXML(boolean nopass) {
1414 return toXML(settingsMap.entrySet(), nopass, false);
1415 }
1416
1417 /**
1418 * Returns XML describing the given preferences.
1419 * @param settings preferences settings
1420 * @param nopass if password must be excluded
1421 * @param defaults true, if default values are converted to XML, false for
1422 * regular preferences
1423 * @return XML
1424 */
1425 public String toXML(Collection<Entry<String, Setting<?>>> settings, boolean nopass, boolean defaults) {
1426 try (
1427 StringWriter sw = new StringWriter();
1428 PreferencesWriter prefWriter = new PreferencesWriter(new PrintWriter(sw), nopass, defaults)
1429 ) {
1430 prefWriter.write(settings);
1431 sw.flush();
1432 return sw.toString();
1433 } catch (IOException e) {
1434 Logging.error(e);
1435 return null;
1436 }
1437 }
1438
1439 /**
1440 * Removes obsolete preference settings. If you throw out a once-used preference
1441 * setting, add it to the list here with an expiry date (written as comment). If you
1442 * see something with an expiry date in the past, remove it from the list.
1443 * @param loadedVersion JOSM version when the preferences file was written
1444 */
1445 private void removeObsolete(int loadedVersion) {
1446 // drop in March 2017
1447 removeUrlFromEntries(loadedVersion, 10063,
1448 "validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
1449 "resource://data/validator/power.mapcss");
1450 // drop in March 2017
1451 if (loadedVersion < 11058) {
1452 migrateOldColorKeys();
1453 }
1454 // drop in September 2017
1455 if (loadedVersion < 11424) {
1456 addNewerDefaultEntry(
1457 "validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
1458 "resource://data/validator/territories.mapcss");
1459 }
1460
1461 for (String key : OBSOLETE_PREF_KEYS) {
1462 if (settingsMap.containsKey(key)) {
1463 settingsMap.remove(key);
1464 Logging.info(tr("Preference setting {0} has been removed since it is no longer used.", key));
1465 }
1466 }
1467 }
1468
1469 private void migrateOldColorKeys() {
1470 settingsMap.keySet().stream()
1471 .filter(key -> key.startsWith(COLOR_PREFIX))
1472 .flatMap(this::searchOldColorKey)
1473 .collect(Collectors.toList()) // to avoid ConcurrentModificationException
1474 .forEach(entry -> {
1475 final String oldKey = entry.getKey();
1476 final String newKey = entry.getValue();
1477 Logging.info("Migrating old color key {0} => {1}", oldKey, newKey);
1478 put(newKey, get(oldKey));
1479 put(oldKey, null);
1480 });
1481 }
1482
1483 private Stream<AbstractMap.SimpleImmutableEntry<String, String>> searchOldColorKey(String key) {
1484 final String newKey = ColorProperty.getColorKey(key.substring(COLOR_PREFIX.length()));
1485 return key.equals(newKey) || settingsMap.containsKey(newKey)
1486 ? Stream.empty()
1487 : Stream.of(new AbstractMap.SimpleImmutableEntry<>(key, newKey));
1488 }
1489
1490 private void removeUrlFromEntries(int loadedVersion, int versionMax, String key, String urlPart) {
1491 if (loadedVersion < versionMax) {
1492 Setting<?> setting = settingsMap.get(key);
1493 if (setting instanceof MapListSetting) {
1494 List<Map<String, String>> l = new LinkedList<>();
1495 boolean modified = false;
1496 for (Map<String, String> map: ((MapListSetting) setting).getValue()) {
1497 String url = map.get("url");
1498 if (url != null && url.contains(urlPart)) {
1499 modified = true;
1500 } else {
1501 l.add(map);
1502 }
1503 }
1504 if (modified) {
1505 putListOfMaps(key, l);
1506 }
1507 }
1508 }
1509 }
1510
1511 private void addNewerDefaultEntry(String key, final String url) {
1512 Setting<?> setting = settingsMap.get(key);
1513 if (setting instanceof MapListSetting) {
1514 List<Map<String, String>> l = new ArrayList<>(((MapListSetting) setting).getValue());
1515 if (l.stream().noneMatch(x -> x.containsValue(url))) {
1516 ValidatorPrefHelper helper = ValidatorPrefHelper.INSTANCE;
1517 Optional<ExtendedSourceEntry> val = helper.getDefault().stream().filter(x -> url.equals(x.url)).findFirst();
1518 if (val.isPresent()) {
1519 l.add(helper.serialize(val.get()));
1520 }
1521 putListOfMaps(key, l);
1522 }
1523 }
1524 }
1525
1526 /**
1527 * Enables or not the preferences file auto-save mechanism (save each time a setting is changed).
1528 * This behaviour is enabled by default.
1529 * @param enable if {@code true}, makes JOSM save preferences file each time a setting is changed
1530 * @since 7085
1531 */
1532 public final void enableSaveOnPut(boolean enable) {
1533 synchronized (this) {
1534 saveOnPut = enable;
1535 }
1536 }
1537}
Note: See TracBrowser for help on using the repository browser.