1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.net.URL;
|
---|
5 | import java.util.EnumSet;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.Set;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.tools.Logging;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Handles global network features (errors and online/offline resources).
|
---|
14 | * @since 14121
|
---|
15 | */
|
---|
16 | public 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 | }
|
---|