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