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

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 3.8 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;
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 int.class,
47 int.class,
48 Map.class);
49 } catch (NoSuchMethodException | SecurityException e) {
50 Logging.warn(e);
51 throw new IllegalArgumentException(e);
52 }
53 }
54
55 private static StringProperty getTileCacheDir() {
56 String defPath = null;
57 try {
58 defPath = new File(Main.pref.getCacheDirectory(), "tiles").getAbsolutePath();
59 } catch (SecurityException e) {
60 Logging.warn(e);
61 }
62 return new StringProperty("imagery.generic.loader.cachedir", defPath);
63 }
64
65 @Override
66 public TileLoader makeTileLoader(TileLoaderListener listener, Map<String, String> inputHeaders) {
67 Map<String, String> headers = new ConcurrentHashMap<>();
68 headers.put("User-Agent", Version.getInstance().getFullAgentString());
69 headers.put("Accept", "text/html, image/png, image/jpeg, image/gif, */*");
70 if (inputHeaders != null)
71 headers.putAll(inputHeaders);
72
73 return getLoader(listener, cache,
74 (int) TimeUnit.SECONDS.toMillis(Main.pref.getInteger("socket.timeout.connect", 15)),
75 (int) TimeUnit.SECONDS.toMillis(Main.pref.getInteger("socket.timeout.read", 30)),
76 headers);
77 }
78
79 protected TileLoader getLoader(TileLoaderListener listener, ICacheAccess<String, BufferedImageCacheEntry> cache,
80 int connectTimeout, int readTimeout, Map<String, String> headers) {
81 try {
82 return tileLoaderConstructor.newInstance(
83 listener,
84 cache,
85 connectTimeout,
86 readTimeout,
87 headers);
88 } catch (IllegalArgumentException e) {
89 Logging.warn(e);
90 throw e;
91 } catch (ReflectiveOperationException e) {
92 Logging.warn(e);
93 throw new IllegalArgumentException(e);
94 }
95 }
96}
Note: See TracBrowser for help on using the repository browser.