source: josm/trunk/src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoader.java@ 8510

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

checkstyle: enable relevant whitespace checks and fix them

File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import java.io.IOException;
5import java.util.Map;
6import java.util.concurrent.ThreadPoolExecutor;
7import java.util.concurrent.TimeUnit;
8
9import org.apache.commons.jcs.access.behavior.ICacheAccess;
10import org.openstreetmap.gui.jmapviewer.Tile;
11import org.openstreetmap.gui.jmapviewer.interfaces.CachedTileLoader;
12import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
13import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
14import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
15import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
16import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
17import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
18import org.openstreetmap.josm.data.cache.HostLimitQueue;
19import org.openstreetmap.josm.data.cache.JCSCacheManager;
20import org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob;
21import org.openstreetmap.josm.data.preferences.IntegerProperty;
22
23/**
24 * @author Wiktor Niesiobędzki
25 *
26 * Wrapper class that bridges between JCS cache and Tile Loaders
27 *
28 */
29public class TMSCachedTileLoader implements TileLoader, CachedTileLoader, TileCache {
30
31 private final ICacheAccess<String, BufferedImageCacheEntry> cache;
32 private final int connectTimeout;
33 private final int readTimeout;
34 private final Map<String, String> headers;
35 private final TileLoaderListener listener;
36 private static final String PREFERENCE_PREFIX = "imagery.tms.cache.";
37
38 /**
39 * how many object on disk should be stored for TMS region. Average tile size is about 20kb. 25000 is around 500MB under this assumption
40 */
41 public static final IntegerProperty MAX_OBJECTS_ON_DISK = new IntegerProperty(PREFERENCE_PREFIX + "max_objects_disk", 25000);
42
43 /**
44 * overrides the THREAD_LIMIT in superclass, as we want to have separate limit and pool for TMS
45 */
46 public static final IntegerProperty THREAD_LIMIT = new IntegerProperty("imagery.tms.tmsloader.maxjobs", 25);
47
48 /**
49 * Limit definition for per host concurrent connections
50 */
51 public static final IntegerProperty HOST_LIMIT = new IntegerProperty("imagery.tms.tmsloader.maxjobsperhost", 6);
52
53 /**
54 * separate from JCS thread pool for TMS loader, so we can have different thread pools for default JCS
55 * and for TMS imagery
56 */
57 private static ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER = getThreadPoolExecutor();
58
59 private final ThreadPoolExecutor downloadExecutor = DEFAULT_DOWNLOAD_JOB_DISPATCHER;
60
61 private static ThreadPoolExecutor getThreadPoolExecutor() {
62 return new ThreadPoolExecutor(
63 THREAD_LIMIT.get().intValue(), // keep the thread number constant
64 THREAD_LIMIT.get().intValue(), // do not this number of threads
65 30, // keepalive for thread
66 TimeUnit.SECONDS,
67 new HostLimitQueue(HOST_LIMIT.get().intValue()),
68 JCSCachedTileLoaderJob.getNamedThreadFactory("TMS downloader")
69 );
70 }
71
72 /**
73 * Constructor
74 * @param listener called when tile loading has finished
75 * @param name of the cache
76 * @param connectTimeout to remote resource
77 * @param readTimeout to remote resource
78 * @param headers HTTP headers to be sent along with request
79 * @param cacheDir where cache file shall reside
80 * @throws IOException when cache initialization fails
81 */
82 public TMSCachedTileLoader(TileLoaderListener listener, String name, int connectTimeout, int readTimeout,
83 Map<String, String> headers, String cacheDir) throws IOException {
84 this.cache = JCSCacheManager.getCache(name,
85 200, // use fairly small memory cache, as cached objects are quite big, as they contain BufferedImages
86 MAX_OBJECTS_ON_DISK.get(),
87 cacheDir);
88 this.connectTimeout = connectTimeout;
89 this.readTimeout = readTimeout;
90 this.headers = headers;
91 this.listener = listener;
92 }
93
94 @Override
95 public TileJob createTileLoaderJob(Tile tile) {
96 return new TMSCachedTileLoaderJob(listener, tile, cache,
97 connectTimeout, readTimeout, headers, downloadExecutor);
98 }
99
100 @Override
101 public void clearCache(TileSource source) {
102 this.cache.clear();
103 }
104
105 @Override
106 public Tile getTile(TileSource source, int x, int y, int z) {
107 return createTileLoaderJob(new Tile(source, x, y, z)).getTile();
108 }
109
110 @Override
111 public void addTile(Tile tile) {
112 createTileLoaderJob(tile).getTile();
113 }
114
115 @Override
116 public int getTileCount() {
117 return 0;
118 }
119
120 @Override
121 public void clear() {
122 cache.clear();
123 }
124
125 /**
126 * @return cache statistics as string
127 */
128 public String getStats() {
129 return cache.getStats();
130 }
131
132 /**
133 * cancels all outstanding tasks in the queue. This rollbacks the state of the tiles in the queue
134 * to loading = false / loaded = false
135 */
136 public void cancelOutstandingTasks() {
137 for (Runnable r: downloadExecutor.getQueue()) {
138 if (downloadExecutor.remove(r) && r instanceof TMSCachedTileLoaderJob) {
139 ((TMSCachedTileLoaderJob) r).handleJobCancellation();
140 }
141 }
142 }
143}
Note: See TracBrowser for help on using the repository browser.