Ignore:
Timestamp:
2014-08-20T03:07:15+02:00 (10 years ago)
Author:
Don-vip
Message:

fix #8885 (see #4614) - add offline mode with new command line argument --offline which can take one of several of these values (comma separated):

  • josm_website: to disable all accesses to JOSM website (when not cached, disables Getting Started page, help, plugin list, styles, imagery, presets, rules)
  • osm_api: to disable all accesses to OSM API (disables download, upload, changeset queries, history, user message notification)
  • all: alias to disable all values. Currently equivalent to "josm_website,osm_api"

Plus improved javadoc, fixed EDT violations, and fixed a bug with HTTP redirection sent without "Location" header

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/Main.java

    r7420 r7434  
    2525import java.util.Collections;
    2626import java.util.HashMap;
     27import java.util.HashSet;
    2728import java.util.Iterator;
    2829import java.util.List;
    2930import java.util.Map;
    3031import java.util.Objects;
     32import java.util.Set;
    3133import java.util.StringTokenizer;
    3234import java.util.concurrent.Callable;
     
    9193import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
    9294import org.openstreetmap.josm.io.FileWatcher;
     95import org.openstreetmap.josm.io.OnlineResource;
    9396import org.openstreetmap.josm.io.OsmApi;
    9497import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    219222    private static final List<String> ERRORS_AND_WARNINGS = Collections.<String>synchronizedList(new ArrayList<String>());
    220223
     224    private static final Set<OnlineResource> OFFLINE_RESOURCES = new HashSet<>();
     225
    221226    /**
    222227     * Logging level (5 = trace, 4 = debug, 3 = info, 2 = warn, 1 = error, 0 = none).
     
    543548        List<Callable<Void>> tasks = new ArrayList<>();
    544549
    545         tasks.add(new InitializationTask(tr("Initializing OSM API")) {
    546 
    547             @Override
    548             public void initialize() throws Exception {
    549                 // We try to establish an API connection early, so that any API
    550                 // capabilities are already known to the editor instance. However
    551                 // if it goes wrong that's not critical at this stage.
    552                 try {
    553                     OsmApi.getOsmApi().initialize(null, true);
    554                 } catch (Exception e) {
    555                     Main.warn(getErrorMessage(Utils.getRootCause(e)));
     550        if (isOffline(OnlineResource.OSM_API)) {
     551            Main.warn(tr("{0} not available (offline mode)", tr("OSM API")));
     552        } else {
     553            tasks.add(new InitializationTask(tr("Initializing OSM API")) {
     554
     555                @Override
     556                public void initialize() throws Exception {
     557                    // We try to establish an API connection early, so that any API
     558                    // capabilities are already known to the editor instance. However
     559                    // if it goes wrong that's not critical at this stage.
     560                    try {
     561                        OsmApi.getOsmApi().initialize(null, true);
     562                    } catch (Exception e) {
     563                        Main.warn(getErrorMessage(Utils.getRootCause(e)));
     564                    }
    556565                }
    557             }
    558         });
     566            });
     567        }
    559568
    560569        tasks.add(new InitializationTask(tr("Initializing validator")) {
     
    15531562        return Main.platform instanceof PlatformHookWindows;
    15541563    }
     1564
     1565    /**
     1566     * Determines if the given online resource is currently offline.
     1567     * @param r the online resource
     1568     * @return {@code true} if {@code r} is offline and should not be accessed
     1569     * @since 7434
     1570     */
     1571    public static boolean isOffline(OnlineResource r) {
     1572        return OFFLINE_RESOURCES.contains(r) || OFFLINE_RESOURCES.contains(OnlineResource.ALL);
     1573    }
     1574
     1575    /**
     1576     * Sets the given online resource to offline state.
     1577     * @param r the online resource
     1578     * @return {@code true} if {@code r} was not already offline
     1579     * @since 7434
     1580     */
     1581    public static boolean setOffline(OnlineResource r) {
     1582        return OFFLINE_RESOURCES.add(r);
     1583    }
     1584
     1585    /**
     1586     * Replies the set of online resources currently offline.
     1587     * @return the set of online resources currently offline
     1588     * @since 7434
     1589     */
     1590    public static Set<OnlineResource> getOfflineResources() {
     1591        return new HashSet<>(OFFLINE_RESOURCES);
     1592    }
    15551593}
Note: See TracChangeset for help on using the changeset viewer.