source: josm/trunk/src/org/openstreetmap/josm/data/imagery/CachedTileLoaderFactory.java@ 13792

Last change on this file since 13792 was 13733, checked in by wiktorn, 6 years ago

Imagery definition refactor

Extend imagery definitions by:

  • allowing setting default layers for WMS_ENDPOINT and WMTS
  • allowing setting minimum expires time for tile for this imagery
  • allowing setting custom headers that will be sent for all requests

(get map, get capabilities) for this imagery

Additional changes in code:

  • use TileJobOptions to pass miscellaneous options to loaders
  • refactor WMSImagery to use SAX parser

See: #15981, #7953, #16224, #15940, #16249

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import java.io.File;
5import java.lang.reflect.Constructor;
6import java.util.Map;
7import java.util.concurrent.ConcurrentHashMap;
8import java.util.concurrent.TimeUnit;
9
10import org.apache.commons.jcs.access.behavior.ICacheAccess;
11import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
12import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
13import org.openstreetmap.josm.data.Version;
14import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
15import org.openstreetmap.josm.data.preferences.StringProperty;
16import org.openstreetmap.josm.spi.preferences.Config;
17import org.openstreetmap.josm.tools.CheckParameterUtil;
18import org.openstreetmap.josm.tools.Logging;
19
20/**
21 * TileLoaderFactory creating JCS cached TileLoaders
22 *
23 * @author Wiktor Niesiobędzki
24 * @since 8526
25 */
26public class CachedTileLoaderFactory implements TileLoaderFactory {
27 /**
28 * Keeps the cache directory where
29 */
30 public static final StringProperty PROP_TILECACHE_DIR = getTileCacheDir();
31 private final ICacheAccess<String, BufferedImageCacheEntry> cache;
32 private Constructor<? extends TileLoader> tileLoaderConstructor;
33
34 /**
35 * @param cache cache instance which will be used by tile loaders created by this tile loader
36 * @param tileLoaderClass tile loader class that will be created
37 * @throws IllegalArgumentException if a suitable constructor cannot be found for {@code tileLoaderClass}
38 */
39 public CachedTileLoaderFactory(ICacheAccess<String, BufferedImageCacheEntry> cache, Class<? extends TileLoader> tileLoaderClass) {
40 CheckParameterUtil.ensureParameterNotNull(cache, "cache");
41 this.cache = cache;
42 try {
43 tileLoaderConstructor = tileLoaderClass.getConstructor(
44 TileLoaderListener.class,
45 ICacheAccess.class,
46 TileJobOptions.class
47 );
48 } catch (NoSuchMethodException | SecurityException e) {
49 Logging.log(Logging.LEVEL_WARN, "Unable to initialize cache tile loader factory", e);
50 throw new IllegalArgumentException(e);
51 }
52 }
53
54 private static StringProperty getTileCacheDir() {
55 String defPath = null;
56 try {
57 defPath = new File(Config.getDirs().getCacheDirectory(true), "tiles").getAbsolutePath();
58 } catch (SecurityException e) {
59 Logging.log(Logging.LEVEL_WARN, "Unable to get tile cache directory", e);
60 }
61 return new StringProperty("imagery.generic.loader.cachedir", defPath);
62 }
63
64 @Override
65 public TileLoader makeTileLoader(TileLoaderListener listener, Map<String, String> inputHeaders, long minimumExpiryTime) {
66 Map<String, String> headers = new ConcurrentHashMap<>();
67 headers.put("User-Agent", Version.getInstance().getFullAgentString());
68 headers.put("Accept", "text/html, image/png, image/jpeg, image/gif, */*");
69 if (inputHeaders != null)
70 headers.putAll(inputHeaders);
71
72 return getLoader(listener, cache,
73 new TileJobOptions(
74 (int) TimeUnit.SECONDS.toMillis(Config.getPref().getInt("socket.timeout.connect", 15)),
75 (int) TimeUnit.SECONDS.toMillis(Config.getPref().getInt("socket.timeout.read", 30)),
76 headers,
77 minimumExpiryTime
78 )
79 );
80 }
81
82 protected TileLoader getLoader(TileLoaderListener listener, ICacheAccess<String, BufferedImageCacheEntry> cache,
83 TileJobOptions options) {
84 try {
85 return tileLoaderConstructor.newInstance(
86 listener,
87 cache,
88 options
89 );
90 } catch (IllegalArgumentException e) {
91 Logging.warn(e);
92 throw e;
93 } catch (ReflectiveOperationException e) {
94 Logging.warn(e);
95 throw new IllegalArgumentException(e);
96 }
97 }
98}
Note: See TracBrowser for help on using the repository browser.