Changeset 27519 in osm for applications


Ignore:
Timestamp:
2012-01-21T10:57:57+01:00 (13 years ago)
Author:
simon04
Message:

jmapviewer, Bing: fix "Error: null" (NPE)

File:
1 edited

Legend:

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

    r27507 r27519  
    1111import java.util.Locale;
    1212import java.util.concurrent.Callable;
     13import java.util.concurrent.ExecutionException;
    1314import java.util.concurrent.Executors;
    1415import java.util.concurrent.Future;
     
    3637public class BingAerialTileSource extends AbstractTMSTileSource {
    3738    private static String API_KEY = "Arzdiw4nlOJzRwOz__qailc8NiR31Tt51dN2D7cm57NrnceZnCpgOkmJhNpGoppU";
    38     private static Future<List<Attribution>> attributions;
     39    private static volatile Future<List<Attribution>> attributions; // volatile is required for getAttribution(), see below.
    3940    private static String imageUrlTemplate;
    4041    private static Integer imageryZoomMax;
     
    5960    @Override
    6061    public String getTileUrl(int zoom, int tilex, int tiley) throws IOException {
     62        // make sure that attribution is loaded. otherwise subdomains is null.
     63        getAttribution();
     64
    6165        int t = (zoom + tilex + tiley) % subdomains.length;
    6266        String subdomain = subdomains[t];
     
    212216    }
    213217
     218    protected List<Attribution> getAttribution() {
     219        if (attributions == null) {
     220            // see http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
     221            synchronized (BingAerialTileSource.class) {
     222                if (attributions == null) {
     223                    attributions = Executors.newSingleThreadExecutor().submit(getAttributionLoaderCallable());
     224                }
     225            }
     226        }
     227        try {
     228            return attributions.get(1000, TimeUnit.MILLISECONDS);
     229        } catch (TimeoutException ex) {
     230            System.err.println("Bing: attribution data is not yet loaded.");
     231        } catch (ExecutionException ex) {
     232            throw new RuntimeException(ex.getCause());
     233        } catch (InterruptedException ign) {
     234        }
     235        return null;
     236    }
     237
    214238    @Override
    215239    public String getAttributionText(int zoom, Coordinate topLeft, Coordinate botRight) {
    216         if (attributions == null) {
    217             attributions = Executors.newSingleThreadExecutor().submit(getAttributionLoaderCallable());
    218         }
    219         try {
    220             final List<Attribution> data;
    221             try {
    222                 data = attributions.get(1000, TimeUnit.MILLISECONDS);
    223             } catch (TimeoutException ex) {
    224                 return "Loading Bing attribution data...";
    225             }
     240        try {
     241            final List<Attribution> data = getAttribution();
    226242            if (data == null) {
    227243                return "Error loading Bing attribution data";
Note: See TracChangeset for help on using the changeset viewer.