Changeset 9414 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2016-01-11T19:07:11+01:00 (9 years ago)
Author:
simon04
Message:

see #8824 - Fail fast when initializing imagery entries while starting

Use a short HTTP connect timeout of 1s for fetching the entries

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java

    r9412 r9414  
    6363    }
    6464
    65     public void load() {
     65    /**
     66     * Loads the custom as well as default imagery entries.
     67     * @param fastFail whether opening HTTP connections should fail fast, see {@link ImageryReader#setFastFail(boolean)}
     68     */
     69     public void load(boolean fastFail) {
    6670        clear();
    6771        List<ImageryPreferenceEntry> entries = Main.pref.getListOfStructs("imagery.entries", null, ImageryPreferenceEntry.class);
     
    7781            Collections.sort(layers);
    7882        }
    79         loadDefaults(false, true);
     83        loadDefaults(false, true, fastFail);
    8084    }
    8185
     
    8993     * @param clearCache if true, clear the cache and start a fresh download.
    9094     * @param quiet whether not the loading should be performed using a {@link PleaseWaitRunnable} in the background
    91      */
    92     public void loadDefaults(boolean clearCache, boolean quiet) {
    93         final DefaultEntryLoader loader = new DefaultEntryLoader(clearCache);
     95     * @param fastFail whether opening HTTP connections should fail fast, see {@link ImageryReader#setFastFail(boolean)}
     96     */
     97    public void loadDefaults(boolean clearCache, boolean quiet, boolean fastFail) {
     98        final DefaultEntryLoader loader = new DefaultEntryLoader(clearCache, fastFail);
    9499        if (quiet) {
    95100            loader.realRun();
    96101            loader.finish();
    97102        } else {
    98             Main.worker.execute(new DefaultEntryLoader(clearCache));
     103            Main.worker.execute(new DefaultEntryLoader(clearCache, fastFail));
    99104        }
    100105    }
     
    106111
    107112        private final boolean clearCache;
     113        private final boolean fastFail;
    108114        private final List<ImageryInfo> newLayers = new ArrayList<>();
    109115        private transient ImageryReader reader;
    110116        private transient boolean canceled;
    111117
    112         DefaultEntryLoader(boolean clearCache) {
     118        DefaultEntryLoader(boolean clearCache, boolean fastFail) {
    113119            super(tr("Update default entries"));
    114120            this.clearCache = clearCache;
     121            this.fastFail = fastFail;
    115122        }
    116123
     
    144151            try {
    145152                reader = new ImageryReader(source);
     153                reader.setFastFail(fastFail);
    146154                Collection<ImageryInfo> result = reader.parse();
    147155                newLayers.addAll(result);
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreference.java

    r9412 r9414  
    156156        layerInfo.save();
    157157        ImageryLayerInfo.instance.clear();
    158         ImageryLayerInfo.instance.load();
     158        ImageryLayerInfo.instance.load(false);
    159159        Main.main.menu.imageryMenu.refreshOffsetMenu();
    160160        OffsetBookmark.saveBookmarks();
     
    642642            @Override
    643643            public void actionPerformed(ActionEvent evt) {
    644                 layerInfo.loadDefaults(true, false);
     644                layerInfo.loadDefaults(true, false, false);
    645645                defaultModel.fireTableDataChanged();
    646646                defaultTable.getSelectionModel().clearSelection();
     
    946946     */
    947947    public static void initialize() {
    948         ImageryLayerInfo.instance.load();
     948        ImageryLayerInfo.instance.load(true);
    949949        OffsetBookmark.loadBookmarks();
    950950        Main.main.menu.imageryMenu.refreshImageryMenu();
  • trunk/src/org/openstreetmap/josm/io/CachedFile.java

    r9411 r9414  
    6969    protected CachingStrategy cachingStrategy;
    7070
     71    private transient boolean fastFail;
    7172    private transient HttpClient activeConnection;
    7273    protected File cacheFile;
     
    159160        this.httpHeaders.putAll(headers);
    160161        return this;
     162    }
     163
     164    /**
     165     * Sets whether opening HTTP connections should fail fast, i.e., whether a
     166     * {@link HttpClient#setConnectTimeout(int) low connect timeout} should be used.
     167     * @param fastFail whether opening HTTP connections should fail fast
     168     */
     169    public void setFastFail(boolean fastFail) {
     170        this.fastFail = fastFail;
    161171    }
    162172
     
    433443                    .setIfModifiedSince(ifModifiedSince == null ? 0L : ifModifiedSince)
    434444                    .setHeaders(httpHeaders);
     445            if (fastFail) {
     446                activeConnection.setReadTimeout(1000);
     447            }
    435448            final HttpClient.Response con = activeConnection.connect();
    436449            if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
  • trunk/src/org/openstreetmap/josm/io/imagery/ImageryReader.java

    r9412 r9414  
    2121import org.openstreetmap.josm.data.imagery.Shape;
    2222import org.openstreetmap.josm.io.CachedFile;
    23 import org.openstreetmap.josm.io.UTFInputStreamReader;
     23import org.openstreetmap.josm.tools.HttpClient;
    2424import org.openstreetmap.josm.tools.LanguageInfo;
    2525import org.openstreetmap.josm.tools.Utils;
     
    3333    private final String source;
    3434    private transient CachedFile cachedFile;
     35    private transient boolean fastFail;
    3536
    3637    private enum State {
     
    5657        try {
    5758            cachedFile = new CachedFile(source);
     59            cachedFile.setFastFail(fastFail);
    5860            try (BufferedReader in = cachedFile
    5961                    .setMaxAge(CachedFile.DAYS)
     
    359361    }
    360362
     363    /**
     364     * Sets whether opening HTTP connections should fail fast, i.e., whether a
     365     * {@link HttpClient#setConnectTimeout(int) low connect timeout} should be used.
     366     * @param fastFail whether opening HTTP connections should fail fast
     367     * @see CachedFile#setFastFail(boolean)
     368     */
     369    public void setFastFail(boolean fastFail) {
     370        this.fastFail = fastFail;
     371    }
     372
    361373    @Override
    362374    public void close() throws IOException {
Note: See TracChangeset for help on using the changeset viewer.