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 | import java.util.concurrent.TimeUnit;
|
---|
9 |
|
---|
10 | import org.apache.commons.jcs.access.behavior.ICacheAccess;
|
---|
11 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
12 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
|
---|
13 | import org.openstreetmap.josm.data.Version;
|
---|
14 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
15 | import org.openstreetmap.josm.data.preferences.StringProperty;
|
---|
16 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
17 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
18 | import org.openstreetmap.josm.tools.Logging;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * TileLoaderFactory creating JCS cached TileLoaders
|
---|
22 | *
|
---|
23 | * @author Wiktor Niesiobędzki
|
---|
24 | * @since 8526
|
---|
25 | */
|
---|
26 | public 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 | }
|
---|