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

Last change on this file since 10723 was 10723, checked in by simon04, 8 years ago

Fix javadoc

  • Property svn:eol-style set to native
File size: 5.4 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.TileJob;
13import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
14import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
15import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
16import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
17import org.openstreetmap.josm.data.cache.HostLimitQueue;
18import org.openstreetmap.josm.data.preferences.IntegerProperty;
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 /**
47 * separate from JCS thread pool for TMS loader, so we can have different thread pools for default JCS
48 * and for TMS imagery
49 */
50 private static ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER = getNewThreadPoolExecutor("TMS-downloader-%d");
51
52
53 private ThreadPoolExecutor downloadExecutor = DEFAULT_DOWNLOAD_JOB_DISPATCHER;
54
55 /**
56 * Constructor
57 * @param listener called when tile loading has finished
58 * @param cache of the cache
59 * @param connectTimeout to remote resource
60 * @param readTimeout to remote resource
61 * @param headers HTTP headers to be sent along with request
62 * @throws IOException when cache initialization fails
63 */
64 public TMSCachedTileLoader(TileLoaderListener listener, ICacheAccess<String, BufferedImageCacheEntry> cache,
65 int connectTimeout, int readTimeout, Map<String, String> headers) throws IOException {
66 this.cache = cache;
67 this.connectTimeout = connectTimeout;
68 this.readTimeout = readTimeout;
69 this.headers = headers;
70 this.listener = listener;
71 }
72
73 /**
74 * @param nameFormat see {@link Utils#newThreadFactory(String, int)}
75 * @param workers number of worker thread to keep
76 * @return new ThreadPoolExecutor that will use a @see HostLimitQueue based queue
77 */
78 public static ThreadPoolExecutor getNewThreadPoolExecutor(String nameFormat, int workers) {
79 return new ThreadPoolExecutor(
80 workers, // keep the thread number constant
81 workers, // do not this number of threads
82 30, // keepalive for thread
83 TimeUnit.SECONDS,
84 new HostLimitQueue(HOST_LIMIT.get().intValue()),
85 Utils.newThreadFactory(nameFormat, Thread.NORM_PRIORITY)
86 );
87 }
88
89 /**
90 * @param name name of threads
91 * @return new ThreadPoolExecutor that will use a @see HostLimitQueue based queue, with default number of threads
92 */
93 public static ThreadPoolExecutor getNewThreadPoolExecutor(String name) {
94 return getNewThreadPoolExecutor(name, THREAD_LIMIT.get().intValue());
95 }
96
97 @Override
98 public TileJob createTileLoaderJob(Tile tile) {
99 return new TMSCachedTileLoaderJob(listener, tile, cache,
100 connectTimeout, readTimeout, headers, getDownloadExecutor());
101 }
102
103 @Override
104 public void clearCache(TileSource source) {
105 this.cache.remove(source.getName() + ':');
106 }
107
108 /**
109 * @return cache statistics as string
110 */
111 public String getStats() {
112 return cache.getStats();
113 }
114
115 /**
116 * cancels all outstanding tasks in the queue. This rollbacks the state of the tiles in the queue
117 * to loading = false / loaded = false
118 */
119 @Override
120 public void cancelOutstandingTasks() {
121 for (Runnable r: downloadExecutor.getQueue()) {
122 if (downloadExecutor.remove(r) && r instanceof TMSCachedTileLoaderJob) {
123 ((TMSCachedTileLoaderJob) r).handleJobCancellation();
124 }
125 }
126 }
127
128 /**
129 * Sets the download executor that will be used to download tiles instead of default one.
130 * You can use {@link #getNewThreadPoolExecutor} to create a new download executor with separate
131 * queue from default.
132 *
133 * @param downloadExecutor download executor that will be used to download tiles
134 */
135 public void setDownloadExecutor(ThreadPoolExecutor downloadExecutor) {
136 this.downloadExecutor = downloadExecutor;
137 }
138
139 /**
140 * @return download executor that is used by this factory
141 */
142 public ThreadPoolExecutor getDownloadExecutor() {
143 return downloadExecutor;
144 }
145}
Note: See TracBrowser for help on using the repository browser.