Changeset 36292 in osm for applications/viewer/jmapviewer


Ignore:
Timestamp:
2024-07-16T17:33:01+02:00 (3 months ago)
Author:
taylor.smock
Message:

See #23721: Application hangs indefinitely if Bing API unavailable

BingAerialTileSource.getAttribution is supposed to exit quickly and return null
if the attributions are not yet loaded. Instead attributions.get() was doing
the following:

  1. Creating a new attributions loader on a different thread (if it didn't exist)
  2. Waiting for the attributions loader to finish
  3. If the loader finished properly, we returned the actual data. Otherwise, null was returned.

This additionally fixes some lint issues, of specific note URL constructors are
deprecated (for details, see https://inside.java/2023/02/15/quality-heads-up/ ).

Location:
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/FeatureAdapter.java

    r36223 r36292  
    217217
    218218    /**
     219     * Reads an image using the current {@link ImageAdapter}.
     220     * @param url image URI to read
     221     * @return a <code>BufferedImage</code> containing the decoded contents of the input, or <code>null</code>.
     222     * @throws java.net.MalformedURLException if the URI could not be converted to a URL
     223     * @throws IOException if an error occurs during reading.
     224     */
     225    public static BufferedImage readImage(URI url) throws IOException {
     226        return imageAdapter.read(url.toURL(), false, false);
     227    }
     228
     229    /**
    219230     * Translates a text using the current {@link TranslationAdapter}.
    220231     * @param text the text to translate.
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java

    r36223 r36292  
    55import java.io.IOException;
    66import java.net.MalformedURLException;
     7import java.net.URI;
     8import java.net.URISyntaxException;
    79import java.net.URL;
    810import java.util.ArrayList;
     
    128130
    129131    protected URL getAttributionUrl() throws MalformedURLException {
    130         return new URL(FeatureAdapter.getSetting(METADATA_API_SETTING, METADATA_API_URL)
    131                 .replace(API_KEY_PLACEHOLDER, FeatureAdapter.getSetting(API_KEY_SETTING, API_KEY))
    132                 .replace(API_KEY_LAYER, this.layer));
     132        try {
     133            return new URI(FeatureAdapter.getSetting(METADATA_API_SETTING, METADATA_API_URL)
     134                    .replace(API_KEY_PLACEHOLDER, FeatureAdapter.getSetting(API_KEY_SETTING, API_KEY))
     135                    .replace(API_KEY_LAYER, this.layer)).toURL();
     136        } catch (URISyntaxException e) {
     137            MalformedURLException malformedURLException = new MalformedURLException(e.getMessage());
     138            malformedURLException.initCause(e);
     139            throw malformedURLException;
     140        }
    133141    }
    134142
     
    233241                if (brandLogoUri != null && !brandLogoUri.isEmpty()) {
    234242                    LOG.log(Level.FINE, "Reading Bing logo from {0}", brandLogoUri);
    235                     return FeatureAdapter.readImage(new URL(brandLogoUri));
    236                 }
    237             }
    238         } catch (IOException e) {
    239             LOG.log(Level.SEVERE, "Error while retrieving Bing logo: "+e.getMessage());
     243                    return FeatureAdapter.readImage(new URI(brandLogoUri));
     244                }
     245            }
     246        } catch (URISyntaxException | IOException e) {
     247            LOG.log(Level.SEVERE, String.format("Error while retrieving Bing logo: %s", e.getMessage()));
    240248        }
    241249        return null;
     
    267275                    return r;
    268276                } catch (IOException ex) {
    269                     LOG.log(Level.SEVERE, "Could not connect to Bing API. Will retry in " + waitTimeSec + " seconds.");
     277                    LOG.log(Level.SEVERE, String.format("Could not connect to Bing API. Will retry in %d seconds.", waitTimeSec));
    270278                    LOG.log(Level.FINE, ex.getMessage(), ex);
    271279                    Thread.sleep(TimeUnit.SECONDS.toMillis(waitTimeSec));
     
    276284    }
    277285
     286    /**
     287     * Get the attribution data that is currently loaded
     288     * @return The list of {@link Attribution} data or {@code null}, if no attribution data has been loaded yet.
     289     */
    278290    protected List<Attribution> getAttribution() {
    279291        if (attributions == null) {
     
    287299            }
    288300        }
    289         try {
    290             return attributions.get();
    291         } catch (ExecutionException ex) {
    292             throw new JMapViewerRuntimeException(ex);
    293         } catch (InterruptedException ign) {
    294             LOG.log(Level.SEVERE, "InterruptedException: {0}", ign.getMessage());
    295             LOG.log(Level.FINE, ign.getMessage(), ign);
    296             Thread.currentThread().interrupt();
     301        if (attributions.isDone()) {
     302            try {
     303                return attributions.get();
     304            } catch (ExecutionException ex) {
     305                throw new JMapViewerRuntimeException(ex);
     306            } catch (InterruptedException ign) {
     307                LOG.log(Level.SEVERE, "InterruptedException: {0}", ign.getMessage());
     308                LOG.log(Level.FINE, ign.getMessage(), ign);
     309                Thread.currentThread().interrupt();
     310            }
    297311        }
    298312        return null;
Note: See TracChangeset for help on using the changeset viewer.