source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CacheControl.java@ 19424

Last change on this file since 19424 was 19371, checked in by pieren, 15 years ago

Various minor improvements. More bboxes data in the cache. New cache format v3.

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
1// License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
2package cadastre_fr;
3/**
4 * This class handles the WMS layer cache mechanism. The design is oriented for a good performance (no
5 * wait status on GUI, fast saving even in big file). A separate thread is created for each WMS
6 * layer to not suspend the GUI until disk I/O is terminated (a file for the cache can take
7 * several MB's). If the cache file already exists, new images are just appended to the file
8 * (performance). Since we use the ObjectStream methods, it is required to modify the standard
9 * ObjectOutputStream in order to have objects appended readable (otherwise a stream header
10 * is inserted before each append and an exception is raised at objects read).
11 */
12
13import static org.openstreetmap.josm.tools.I18n.tr;
14
15import java.io.*;
16import java.util.ArrayList;
17import java.util.concurrent.locks.Lock;
18import java.util.concurrent.locks.ReentrantLock;
19
20import javax.swing.JDialog;
21import javax.swing.JOptionPane;
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.data.projection.LambertCC9Zones;
24import org.openstreetmap.josm.data.projection.UTM_20N_France_DOM;
25
26public class CacheControl implements Runnable {
27
28 public static final String cLambertCC9Z = "CC";
29
30 public static final String cUTM20N = "UTM";
31
32 public class ObjectOutputStreamAppend extends ObjectOutputStream {
33 public ObjectOutputStreamAppend(OutputStream out) throws IOException {
34 super(out);
35 }
36 protected void writeStreamHeader() throws IOException {
37 reset();
38 }
39 }
40
41 public static boolean cacheEnabled = true;
42
43 public static int cacheSize = 500;
44
45
46 public WMSLayer wmsLayer = null;
47
48 private ArrayList<GeorefImage> imagesToSave = new ArrayList<GeorefImage>();
49 private Lock imagesLock = new ReentrantLock();
50
51 public boolean isCachePipeEmpty() {
52 imagesLock.lock();
53 boolean ret = imagesToSave.isEmpty();
54 imagesLock.unlock();
55 return ret;
56 }
57
58 public CacheControl(WMSLayer wmsLayer) {
59 cacheEnabled = Main.pref.getBoolean("cadastrewms.enableCaching", true);
60 this.wmsLayer = wmsLayer;
61 try {
62 cacheSize = Integer.parseInt(Main.pref.get("cadastrewms.cacheSize", String.valueOf(CadastrePreferenceSetting.DEFAULT_CACHE_SIZE)));
63 } catch (NumberFormatException e) {
64 cacheSize = CadastrePreferenceSetting.DEFAULT_CACHE_SIZE;
65 }
66 File path = new File(CadastrePlugin.cacheDir);
67 if (!path.exists())
68 path.mkdirs();
69 else // check directory capacity
70 checkDirSize(path);
71 new Thread(this).start();
72 }
73
74 private void checkDirSize(File path) {
75 long size = 0;
76 long oldestFileDate = Long.MAX_VALUE;
77 int oldestFile = 0;
78 File[] files = path.listFiles();
79 for (int i = 0; i < files.length; i++) {
80 size += files[i].length();
81 if (files[i].lastModified() < oldestFileDate) {
82 oldestFile = i;
83 oldestFileDate = files[i].lastModified();
84 }
85 }
86 if (size > cacheSize*1024*1024) {
87 System.out.println("Delete oldest file \""+ files[oldestFile].getName()
88 + "\" in cache dir to stay under the limit of " + cacheSize + " MB.");
89 files[oldestFile].delete();
90 checkDirSize(path);
91 }
92 }
93
94 public boolean loadCacheIfExist() {
95 try {
96 String extension = String.valueOf((wmsLayer.getLambertZone() + 1));
97 if (Main.proj instanceof LambertCC9Zones)
98 extension = cLambertCC9Z + extension;
99 else if (Main.proj instanceof UTM_20N_France_DOM)
100 extension = cUTM20N + extension;
101 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + extension);
102 if (file.exists()) {
103 JOptionPane pane = new JOptionPane(
104 tr("Location \"{0}\" found in cache.\n"+
105 "Load cache first ?\n"+
106 "(No = new cache)", wmsLayer.getName()),
107 JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null);
108 // this below is a temporary workaround to fix the "always on top" issue
109 JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
110 CadastrePlugin.prepareDialog(dialog);
111 dialog.setVisible(true);
112 int reply = (Integer)pane.getValue();
113 // till here
114
115 if (reply == JOptionPane.OK_OPTION && loadCache(file, wmsLayer.getLambertZone())) {
116 return true;
117 } else {
118 delete(file);
119 }
120 }
121 } catch (Exception e) {
122 e.printStackTrace(System.out);
123 }
124 return false;
125 }
126
127 public void deleteCacheFile() {
128 try {
129 String extension = String.valueOf((wmsLayer.getLambertZone() + 1));
130 if (Main.proj instanceof LambertCC9Zones)
131 extension = cLambertCC9Z + extension;
132 delete(new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + extension));
133 } catch (Exception e) {
134 e.printStackTrace(System.out);
135 }
136 }
137
138 private void delete(File file) {
139 System.out.println("Delete file "+file);
140 if (file.exists())
141 file.delete();
142 while (file.exists()) // wait until file is really gone (otherwise appends to existing one)
143 CadastrePlugin.safeSleep(500);
144 }
145
146 public boolean loadCache(File file, int currentLambertZone) {
147 boolean successfulRead = false;
148 try {
149 FileInputStream fis = new FileInputStream(file);
150 ObjectInputStream ois = new ObjectInputStream(fis);
151 successfulRead = wmsLayer.read(ois, currentLambertZone);
152 ois.close();
153 fis.close();
154 } catch (Exception ex) {
155 ex.printStackTrace(System.out);
156 JOptionPane.showMessageDialog(Main.parent, tr("Error loading file.\nProbably an old version of the cache file."), tr("Error"), JOptionPane.ERROR_MESSAGE);
157 return false;
158 }
159 if (successfulRead && wmsLayer.isRaster()) {
160 // serialized raster bufferedImage hangs-up on Java6. Recreate them here
161 wmsLayer.images.get(0).image = RasterImageModifier.fixRasterImage(wmsLayer.images.get(0).image);
162 }
163 return successfulRead;
164 }
165
166
167 public synchronized void saveCache(GeorefImage image) {
168 imagesLock.lock();
169 this.imagesToSave.add(image);
170 this.notify();
171 imagesLock.unlock();
172 }
173
174 /**
175 * Thread saving the grabbed images in background.
176 */
177 public synchronized void run() {
178 for (;;) {
179 imagesLock.lock();
180 //ArrayList<GeorefImage> images = new ArrayList<GeorefImage>(imagesToSave);
181 int size = imagesToSave.size();
182 imagesLock.unlock();
183 if (size > 0) {
184 String extension = String.valueOf((wmsLayer.getLambertZone() + 1));
185 if (Main.proj instanceof LambertCC9Zones)
186 extension = cLambertCC9Z + extension;
187 else if (Main.proj instanceof UTM_20N_France_DOM)
188 extension = cUTM20N + extension;
189 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + extension);
190 try {
191 if (file.exists()) {
192 ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend(
193 new BufferedOutputStream(new FileOutputStream(file, true)));
194 for (int i=0; i < size; i++) {
195 oos.writeObject(imagesToSave.get(i));
196 }
197 oos.close();
198 } else {
199 ObjectOutputStream oos = new ObjectOutputStream(
200 new BufferedOutputStream(new FileOutputStream(file)));
201 wmsLayer.write(oos);
202 for (int i=0; i < size; i++) {
203 oos.writeObject(imagesToSave.get(i));
204 }
205 oos.close();
206 }
207 } catch (IOException e) {
208 e.printStackTrace(System.out);
209 }
210 imagesLock.lock();
211 for (int i=0; i < size; i++) {
212 imagesToSave.remove(0);
213 }
214 imagesLock.unlock();
215 }
216 try {wait();} catch (InterruptedException e) {
217 e.printStackTrace(System.out);
218 }
219 }
220 }
221}
Note: See TracBrowser for help on using the repository browser.