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

Last change on this file since 9375 was 9067, checked in by Don-vip, 8 years ago

sonar - Immutable Field

  • Property svn:eol-style set to native
File size: 3.6 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.lang.reflect.InvocationTargetException;
7import java.util.Map;
8import java.util.concurrent.ConcurrentHashMap;
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;
17
18/**
19 * TileLoaderFactory creating JCS cached TileLoaders
20 *
21 * @author Wiktor Niesiobędzki
22 * @since 8526
23 */
24public class CachedTileLoaderFactory implements TileLoaderFactory {
25 /**
26 * Keeps the cache directory where
27 */
28 public static final StringProperty PROP_TILECACHE_DIR = getTileCacheDir();
29 private final ICacheAccess<String, BufferedImageCacheEntry> cache;
30 private Constructor<? extends TileLoader> tileLoaderConstructor;
31
32 /**
33 * @param cache cache instance which will be used by tile loaders created by this tile loader
34 * @param tileLoaderClass tile loader class that will be created
35 *
36 */
37 public CachedTileLoaderFactory(ICacheAccess<String, BufferedImageCacheEntry> cache, Class<? extends TileLoader> tileLoaderClass) {
38 this.cache = cache;
39 try {
40 tileLoaderConstructor = tileLoaderClass.getConstructor(
41 TileLoaderListener.class,
42 ICacheAccess.class,
43 int.class,
44 int.class,
45 Map.class);
46 } catch (NoSuchMethodException | SecurityException e) {
47 Main.warn(e);
48 throw new RuntimeException(e);
49 }
50 }
51
52 private static StringProperty getTileCacheDir() {
53 String defPath = null;
54 try {
55 defPath = new File(Main.pref.getCacheDirectory(), "tiles").getAbsolutePath();
56 } catch (SecurityException e) {
57 Main.warn(e);
58 }
59 return new StringProperty("imagery.generic.loader.cachedir", defPath);
60 }
61
62 @Override
63 public TileLoader makeTileLoader(TileLoaderListener listener) {
64 return makeTileLoader(listener, null);
65 }
66
67 @Override
68 public TileLoader makeTileLoader(TileLoaderListener listener, Map<String, String> inputHeaders) {
69 Map<String, String> headers = new ConcurrentHashMap<>();
70 headers.put("User-Agent", Version.getInstance().getFullAgentString());
71 headers.put("Accept", "text/html, image/png, image/jpeg, image/gif, */*");
72 if (inputHeaders != null)
73 headers.putAll(inputHeaders);
74
75 return getLoader(listener, cache,
76 Main.pref.getInteger("socket.timeout.connect", 15) * 1000,
77 Main.pref.getInteger("socket.timeout.read", 30) * 1000,
78 headers);
79 }
80
81 protected TileLoader getLoader(TileLoaderListener listener, ICacheAccess<String, BufferedImageCacheEntry> cache,
82 int connectTimeout, int readTimeout, Map<String, String> headers) {
83 try {
84 return tileLoaderConstructor.newInstance(
85 listener,
86 cache,
87 connectTimeout,
88 readTimeout,
89 headers);
90 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
91 Main.warn(e);
92 throw new RuntimeException(e);
93 }
94 }
95}
Note: See TracBrowser for help on using the repository browser.