source: josm/trunk/src/org/openstreetmap/josm/io/NetworkManager.java@ 14628

Last change on this file since 14628 was 14273, checked in by stoecker, 6 years ago

fix typos - patch by naoliv - fix #16781 - Thanks a lot

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import java.net.URL;
5import java.util.EnumSet;
6import java.util.HashMap;
7import java.util.Map;
8import java.util.Set;
9
10import org.openstreetmap.josm.tools.Logging;
11
12/**
13 * Handles global network features (errors and online/offline resources).
14 * @since 14121
15 */
16public final class NetworkManager {
17
18 private static final Map<String, Throwable> NETWORK_ERRORS = new HashMap<>();
19
20 private static final Set<OnlineResource> OFFLINE_RESOURCES = EnumSet.noneOf(OnlineResource.class);
21
22 private NetworkManager() {
23 // Hide constructor
24 }
25
26 /**
27 * Adds a new network error that occur to give a hint about broken Internet connection.
28 * Do not use this method for errors known for sure thrown because of a bad proxy configuration.
29 *
30 * @param url The accessed URL that caused the error
31 * @param t The network error
32 * @return The previous error associated to the given resource, if any. Can be {@code null}
33 */
34 public static Throwable addNetworkError(String url, Throwable t) {
35 if (url != null && t != null) {
36 return NETWORK_ERRORS.put(url, t);
37 }
38 return null;
39 }
40
41 /**
42 * Adds a new network error that occur to give a hint about broken Internet connection.
43 * Do not use this method for errors known for sure thrown because of a bad proxy configuration.
44 *
45 * @param url The accessed URL that caused the error
46 * @param t The network error
47 * @return The previous error associated to the given resource, if any. Can be {@code null}
48 */
49 public static Throwable addNetworkError(URL url, Throwable t) {
50 if (url != null && t != null) {
51 Throwable old = addNetworkError(url.toExternalForm(), t);
52 if (old != null) {
53 Logging.warn("Already here "+old);
54 }
55 return old;
56 }
57 return null;
58 }
59
60 /**
61 * Returns the network errors that occurred until now.
62 * @return the network errors that occurred until now, indexed by URL
63 */
64 public static Map<String, Throwable> getNetworkErrors() {
65 return new HashMap<>(NETWORK_ERRORS);
66 }
67
68 /**
69 * Clears the network errors cache.
70 */
71 public static void clearNetworkErrors() {
72 NETWORK_ERRORS.clear();
73 }
74
75 /**
76 * Determines if the given online resource is currently offline.
77 * @param r the online resource
78 * @return {@code true} if {@code r} is offline and should not be accessed
79 */
80 public static boolean isOffline(OnlineResource r) {
81 return OFFLINE_RESOURCES.contains(r) || OFFLINE_RESOURCES.contains(OnlineResource.ALL);
82 }
83
84 /**
85 * Sets the given online resource to offline state.
86 * @param r the online resource
87 * @return {@code true} if {@code r} was not already offline
88 */
89 public static boolean setOffline(OnlineResource r) {
90 return OFFLINE_RESOURCES.add(r);
91 }
92
93 /**
94 * Sets the given online resource to online state.
95 * @param r the online resource
96 * @return {@code true} if {@code r} was offline
97 */
98 public static boolean setOnline(OnlineResource r) {
99 return OFFLINE_RESOURCES.remove(r);
100 }
101
102 /**
103 * Replies the set of online resources currently offline.
104 * @return the set of online resources currently offline
105 */
106 public static Set<OnlineResource> getOfflineResources() {
107 return EnumSet.copyOf(OFFLINE_RESOURCES);
108 }
109}
Note: See TracBrowser for help on using the repository browser.