source: josm/trunk/src/org/openstreetmap/josm/gui/layer/AbstractCachedTileSourceLayer.java@ 8624

Last change on this file since 8624 was 8624, checked in by bastiK, 9 years ago

add missing svn:eol-style=native

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import java.io.IOException;
5import java.util.Map;
6import java.util.concurrent.ConcurrentHashMap;
7
8import org.apache.commons.jcs.access.CacheAccess;
9import org.apache.commons.jcs.access.behavior.ICacheAccess;
10import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
13import org.openstreetmap.josm.data.cache.JCSCacheManager;
14import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;
15import org.openstreetmap.josm.data.imagery.ImageryInfo;
16import org.openstreetmap.josm.data.imagery.TileLoaderFactory;
17import org.openstreetmap.josm.data.preferences.IntegerProperty;
18
19/**
20 *
21 * Class providing cache to other layers
22 *
23 * @author Wiktor Niesiobędzki
24 *
25 */
26public abstract class AbstractCachedTileSourceLayer extends AbstractTileSourceLayer {
27 /** loader factory responsible for loading tiles for all layers */
28 private static Map<String, TileLoaderFactory> loaderFactories = new ConcurrentHashMap<>();
29
30 private static final String PREFERENCE_PREFIX = "imagery.cache.";
31
32 private static volatile TileLoaderFactory loaderFactoryOverride = null;
33
34 /**
35 * how many object on disk should be stored for TMS region in MB. 500 MB is default value
36 */
37 public static final IntegerProperty MAX_DISK_CACHE_SIZE = new IntegerProperty(PREFERENCE_PREFIX + "max_disk_size", 512);
38
39 /**
40 * use fairly small memory cache, as cached objects are quite big, as they contain BufferedImages
41 */
42 public static final IntegerProperty MEMORY_CACHE_SIZE = new IntegerProperty(PREFERENCE_PREFIX + "cache.max_objects_ram", 200);
43
44 private ICacheAccess<String, BufferedImageCacheEntry> cache;
45 private TileLoaderFactory loaderFactory;
46
47
48 /**
49 * Creates an instance of class based on InageryInfo
50 *
51 * @param info ImageryInfo describing the layer
52 */
53 public AbstractCachedTileSourceLayer(ImageryInfo info) {
54 super(info);
55
56 if (loaderFactoryOverride != null) {
57 loaderFactory = loaderFactoryOverride;
58 } else {
59 String key = this.getClass().getCanonicalName();
60 loaderFactory = loaderFactories.get(key);
61 if (loaderFactory == null) {
62 synchronized (AbstractCachedTileSourceLayer.class) {
63 // check again, maybe another thread initialized factory
64 loaderFactory = loaderFactories.get(key);
65 if (loaderFactory == null) {
66 loaderFactory = new CachedTileLoaderFactory(getCache(), getTileLoaderClass());
67 loaderFactories.put(key, loaderFactory);
68 }
69 }
70 }
71 }
72 }
73
74 @Override
75 protected synchronized TileLoaderFactory getTileLoaderFactory() {
76 if (loaderFactory == null) {
77 loaderFactory = new CachedTileLoaderFactory(getCache(), getTileLoaderClass());
78 }
79 return loaderFactory;
80 }
81
82 /**
83 * @return cache used by this layer
84 */
85 private synchronized ICacheAccess<String, BufferedImageCacheEntry> getCache() {
86 if (cache != null) {
87 return cache;
88 }
89 try {
90 cache = JCSCacheManager.getCache(getCacheName(),
91 getMemoryCacheSize(),
92 getDiskCacheSize(),
93 CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
94 return cache;
95 } catch (IOException e) {
96 Main.warn(e);
97 return null;
98 }
99 }
100
101
102 /**
103 * Plugins that wish to set custom tile loader should call this method
104 * @param newLoaderFactory that will be used to load tiles
105 */
106
107 public static synchronized void setTileLoaderFactory(TileLoaderFactory newLoaderFactory) {
108 loaderFactoryOverride = newLoaderFactory;
109 }
110
111 /**
112 * Returns tile loader factory for cache region and specified TileLoader class
113 * @param name of the cache region
114 * @param klazz type of the TileLoader
115 * @return factory returning cached tile loaders using specified cache and TileLoaders
116 */
117 public static TileLoaderFactory getTileLoaderFactory(String name, Class<? extends TileLoader> klazz) {
118 return new CachedTileLoaderFactory(getCache(name), klazz);
119 }
120
121 /**
122 * @param name of cache region
123 * @return cache configured object for specified cache region
124 */
125 public static CacheAccess<String, BufferedImageCacheEntry> getCache(String name) {
126 try {
127 return JCSCacheManager.getCache(name,
128 MEMORY_CACHE_SIZE.get(),
129 MAX_DISK_CACHE_SIZE.get() * 1024, // MAX_DISK_CACHE_SIZE is in MB
130 CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
131 } catch (IOException e) {
132 Main.warn(e);
133 return null;
134 }
135 }
136
137 protected abstract Class<? extends TileLoader> getTileLoaderClass();
138
139 protected int getMemoryCacheSize() {
140 return MEMORY_CACHE_SIZE.get();
141 }
142
143 protected int getDiskCacheSize() {
144 return MAX_DISK_CACHE_SIZE.get() * 1024;
145 }
146
147 protected abstract String getCacheName();
148}
Note: See TracBrowser for help on using the repository browser.