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

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

sonar - fb-contrib:BED_BOGUS_EXCEPTION_DECLARATION - Correctness - Non derivable method declares throwing an exception that isn't thrown

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import java.util.Map;
5import java.util.concurrent.ThreadPoolExecutor;
6import java.util.concurrent.TimeUnit;
7
8import org.apache.commons.jcs.access.behavior.ICacheAccess;
9import org.openstreetmap.gui.jmapviewer.Tile;
10import org.openstreetmap.gui.jmapviewer.interfaces.CachedTileLoader;
11import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
12import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
13import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
14import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
15import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
16import org.openstreetmap.josm.data.cache.HostLimitQueue;
17import org.openstreetmap.josm.data.preferences.IntegerProperty;
18import org.openstreetmap.josm.tools.CheckParameterUtil;
19import org.openstreetmap.josm.tools.Utils;
20
21/**
22 * Wrapper class that bridges between JCS cache and Tile Loaders
23 *
24 * @author Wiktor Niesiobędzki
25 */
26public class TMSCachedTileLoader implements TileLoader, CachedTileLoader {
27
28 protected final ICacheAccess<String, BufferedImageCacheEntry> cache;
29 protected final int connectTimeout;
30 protected final int readTimeout;
31 protected final Map<String, String> headers;
32 protected final TileLoaderListener listener;
33
34 /**
35 * overrides the THREAD_LIMIT in superclass, as we want to have separate limit and pool for TMS
36 */
37
38 public static final IntegerProperty THREAD_LIMIT = new IntegerProperty("imagery.tms.tmsloader.maxjobs", 25);
39
40 /**
41 * Limit definition for per host concurrent connections
42 */
43 public static final IntegerProperty HOST_LIMIT = new IntegerProperty("imagery.tms.tmsloader.maxjobsperhost", 6);
44
45 /**
46 * separate from JCS thread pool for TMS loader, so we can have different thread pools for default JCS
47 * and for TMS imagery
48 */
49 private static ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER = getNewThreadPoolExecutor("TMS-downloader-%d");
50
51
52 private ThreadPoolExecutor downloadExecutor = DEFAULT_DOWNLOAD_JOB_DISPATCHER;
53
54 /**
55 * Constructor
56 * @param listener called when tile loading has finished
57 * @param cache of the cache
58 * @param connectTimeout to remote resource
59 * @param readTimeout to remote resource
60 * @param headers HTTP headers to be sent along with request
61 */
62 public TMSCachedTileLoader(TileLoaderListener listener, ICacheAccess<String, BufferedImageCacheEntry> cache,
63 int connectTimeout, int readTimeout, Map<String, String> headers) {
64 CheckParameterUtil.ensureParameterNotNull(cache, "cache");
65 this.cache = cache;
66 this.connectTimeout = connectTimeout;
67 this.readTimeout = readTimeout;
68 this.headers = headers;
69 this.listener = listener;
70 }
71
72 /**
73 * @param nameFormat see {@link Utils#newThreadFactory(String, int)}
74 * @param workers number of worker thread to keep
75 * @return new ThreadPoolExecutor that will use a @see HostLimitQueue based queue
76 */
77 public static ThreadPoolExecutor getNewThreadPoolExecutor(String nameFormat, int workers) {
78 HostLimitQueue workQueue = new HostLimitQueue(HOST_LIMIT.get().intValue());
79 ThreadPoolExecutor executor = new ThreadPoolExecutor(
80 0, // 0 so for unused thread pools threads will eventually die, freeing also the threadpool
81 workers, // do not this number of threads
82 300, // keepalive for thread
83 TimeUnit.SECONDS,
84 workQueue,
85 Utils.newThreadFactory(nameFormat, Thread.NORM_PRIORITY)
86 );
87 workQueue.setExecutor(executor);
88 return executor;
89 }
90
91 /**
92 * @param name name of threads
93 * @return new ThreadPoolExecutor that will use a @see HostLimitQueue based queue, with default number of threads
94 */
95 public static ThreadPoolExecutor getNewThreadPoolExecutor(String name) {
96 return getNewThreadPoolExecutor(name, THREAD_LIMIT.get().intValue());
97 }
98
99 @Override
100 public TileJob createTileLoaderJob(Tile tile) {
101 return new TMSCachedTileLoaderJob(listener, tile, cache,
102 connectTimeout, readTimeout, headers, getDownloadExecutor());
103 }
104
105 @Override
106 public void clearCache(TileSource source) {
107 this.cache.remove(source.getName() + ':');
108 }
109
110 /**
111 * @return cache statistics as string
112 */
113 public String getStats() {
114 return cache.getStats();
115 }
116
117 /**
118 * cancels all outstanding tasks in the queue. This rollbacks the state of the tiles in the queue
119 * to loading = false / loaded = false
120 */
121 @Override
122 public void cancelOutstandingTasks() {
123 for (Runnable r: downloadExecutor.getQueue()) {
124 if (downloadExecutor.remove(r) && r instanceof TMSCachedTileLoaderJob) {
125 ((TMSCachedTileLoaderJob) r).handleJobCancellation();
126 }
127 }
128 }
129
130 /**
131 * Sets the download executor that will be used to download tiles instead of default one.
132 * You can use {@link #getNewThreadPoolExecutor} to create a new download executor with separate
133 * queue from default.
134 *
135 * @param downloadExecutor download executor that will be used to download tiles
136 */
137 public void setDownloadExecutor(ThreadPoolExecutor downloadExecutor) {
138 this.downloadExecutor = downloadExecutor;
139 }
140
141 /**
142 * @return download executor that is used by this factory
143 */
144 public ThreadPoolExecutor getDownloadExecutor() {
145 return downloadExecutor;
146 }
147}
Note: See TracBrowser for help on using the repository browser.