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

Last change on this file since 11296 was 11288, checked in by simon04, 7 years ago

see #13376 - Use TimeUnit instead of combinations of 1000/60/60/24

  • Property svn:eol-style set to native
File size: 3.7 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.Main;
14import org.openstreetmap.josm.data.Version;
15import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
16import org.openstreetmap.josm.data.preferences.StringProperty;
17import org.openstreetmap.josm.tools.CheckParameterUtil;
18
19/**
20 * TileLoaderFactory creating JCS cached TileLoaders
21 *
22 * @author Wiktor Niesiobędzki
23 * @since 8526
24 */
25public class CachedTileLoaderFactory implements TileLoaderFactory {
26 /**
27 * Keeps the cache directory where
28 */
29 public static final StringProperty PROP_TILECACHE_DIR = getTileCacheDir();
30 private final ICacheAccess<String, BufferedImageCacheEntry> cache;
31 private Constructor<? extends TileLoader> tileLoaderConstructor;
32
33 /**
34 * @param cache cache instance which will be used by tile loaders created by this tile loader
35 * @param tileLoaderClass tile loader class that will be created
36 * @throws IllegalArgumentException if a suitable constructor cannot be found for {@code tileLoaderClass}
37 */
38 public CachedTileLoaderFactory(ICacheAccess<String, BufferedImageCacheEntry> cache, Class<? extends TileLoader> tileLoaderClass) {
39 CheckParameterUtil.ensureParameterNotNull(cache, "cache");
40 this.cache = cache;
41 try {
42 tileLoaderConstructor = tileLoaderClass.getConstructor(
43 TileLoaderListener.class,
44 ICacheAccess.class,
45 int.class,
46 int.class,
47 Map.class);
48 } catch (NoSuchMethodException | SecurityException e) {
49 Main.warn(e);
50 throw new IllegalArgumentException(e);
51 }
52 }
53
54 private static StringProperty getTileCacheDir() {
55 String defPath = null;
56 try {
57 defPath = new File(Main.pref.getCacheDirectory(), "tiles").getAbsolutePath();
58 } catch (SecurityException e) {
59 Main.warn(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) {
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 (int) TimeUnit.SECONDS.toMillis(Main.pref.getInteger("socket.timeout.connect", 15)),
74 (int) TimeUnit.SECONDS.toMillis(Main.pref.getInteger("socket.timeout.read", 30)),
75 headers);
76 }
77
78 protected TileLoader getLoader(TileLoaderListener listener, ICacheAccess<String, BufferedImageCacheEntry> cache,
79 int connectTimeout, int readTimeout, Map<String, String> headers) {
80 try {
81 return tileLoaderConstructor.newInstance(
82 listener,
83 cache,
84 connectTimeout,
85 readTimeout,
86 headers);
87 } catch (IllegalArgumentException e) {
88 Main.warn(e);
89 throw e;
90 } catch (ReflectiveOperationException e) {
91 Main.warn(e);
92 throw new IllegalArgumentException(e);
93 }
94 }
95}
Note: See TracBrowser for help on using the repository browser.