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