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

Last change on this file since 29784 was 29732, checked in by pieren, 11 years ago

restore loadCache() fixing a deserialization issue with bufferedImage (perf pbl)

  • Property svn:eol-style set to native
File size: 8.4 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;
23
24public class CacheControl implements Runnable {
25
26 public static final String cLambertCC9Z = "CC";
27
28 public static final String cUTM20N = "UTM";
29
30 public class ObjectOutputStreamAppend extends ObjectOutputStream {
31 public ObjectOutputStreamAppend(OutputStream out) throws IOException {
32 super(out);
33 }
34 protected void writeStreamHeader() throws IOException {
35 reset();
36 }
37 }
38
39 public static boolean cacheEnabled = true;
40
41 public static int cacheSize = 500;
42
43
44 public WMSLayer wmsLayer = null;
45
46 private ArrayList<GeorefImage> imagesToSave = new ArrayList<GeorefImage>();
47 private Lock imagesLock = new ReentrantLock();
48
49 public boolean isCachePipeEmpty() {
50 imagesLock.lock();
51 boolean ret = imagesToSave.isEmpty();
52 imagesLock.unlock();
53 return ret;
54 }
55
56 public CacheControl(WMSLayer wmsLayer) {
57 cacheEnabled = Main.pref.getBoolean("cadastrewms.enableCaching", true);
58 this.wmsLayer = wmsLayer;
59 try {
60 cacheSize = Integer.parseInt(Main.pref.get("cadastrewms.cacheSize", String.valueOf(CadastrePreferenceSetting.DEFAULT_CACHE_SIZE)));
61 } catch (NumberFormatException e) {
62 cacheSize = CadastrePreferenceSetting.DEFAULT_CACHE_SIZE;
63 }
64 File path = new File(CadastrePlugin.cacheDir);
65 if (!path.exists())
66 path.mkdirs();
67 else // check directory capacity
68 checkDirSize(path);
69 new Thread(this).start();
70 }
71
72 private void checkDirSize(File path) {
73 if (cacheSize != 0) {
74 long size = 0;
75 long oldestFileDate = Long.MAX_VALUE;
76 int oldestFile = 0;
77 File[] files = path.listFiles();
78 for (int i = 0; i < files.length; i++) {
79 size += files[i].length();
80 if (files[i].lastModified() < oldestFileDate) {
81 oldestFile = i;
82 oldestFileDate = files[i].lastModified();
83 }
84 }
85 if (size > (long)cacheSize*1024*1024) {
86 System.out.println("Delete oldest file \""+ files[oldestFile].getName()
87 + "\" in cache dir to stay under the limit of " + cacheSize + " MB.");
88 files[oldestFile].delete();
89 checkDirSize(path);
90 }
91 }
92 }
93
94 public boolean loadCacheIfExist() {
95 if (!CadastrePlugin.isCadastreProjection()) {
96 CadastrePlugin.askToChangeProjection();
97 }
98 try {
99 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension());
100 if (file.exists()) {
101 JOptionPane pane = new JOptionPane(
102 tr("Location \"{0}\" found in cache.\n"+
103 "Load cache first ?\n"+
104 "(No = new cache)", wmsLayer.getName()),
105 JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null);
106 // this below is a temporary workaround to fix the "always on top" issue
107 JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
108 CadastrePlugin.prepareDialog(dialog);
109 dialog.setVisible(true);
110 int reply = (Integer)pane.getValue();
111 // till here
112
113 if (reply == JOptionPane.OK_OPTION && loadCache(file, wmsLayer.getLambertZone())) {
114 return true;
115 } else {
116 delete(file);
117 }
118 }
119 } catch (Exception e) {
120 e.printStackTrace(System.out);
121 }
122 return false;
123 }
124
125 public void deleteCacheFile() {
126 try {
127 delete(new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension()));
128 } catch (Exception e) {
129 e.printStackTrace(System.out);
130 }
131 }
132
133 private void delete(File file) {
134 System.out.println("Delete file "+file);
135 if (file.exists())
136 file.delete();
137 while (file.exists()) // wait until file is really gone (otherwise appends to existing one)
138 CadastrePlugin.safeSleep(500);
139 }
140
141 public boolean loadCache(File file, int currentLambertZone) {
142 boolean successfulRead = false;
143 FileInputStream fis = null;
144 ObjectInputStream ois = null;
145 try {
146 fis = new FileInputStream(file);
147 ois = new ObjectInputStream(fis);
148 successfulRead = wmsLayer.read(ois, currentLambertZone);
149 } catch (Exception ex) {
150 ex.printStackTrace(System.out);
151 JOptionPane.showMessageDialog(Main.parent, tr("Error loading file.\nProbably an old version of the cache file."), tr("Error"), JOptionPane.ERROR_MESSAGE);
152 return false;
153 } finally {
154 try {
155 ois.close();
156 fis.close();
157 } catch (Exception e) {
158 e.printStackTrace();
159 }
160 }
161 if (successfulRead && wmsLayer.isRaster()) {
162 // serialized raster bufferedImage hangs-up on Java6. Recreate them here
163 wmsLayer.getImage(0).image = RasterImageModifier.fixRasterImage(wmsLayer.getImage(0).image);
164 }
165 return successfulRead;
166 }
167
168
169 public synchronized void saveCache(GeorefImage image) {
170 imagesLock.lock();
171 this.imagesToSave.add(image);
172 this.notify();
173 imagesLock.unlock();
174 }
175
176 /**
177 * Thread saving the grabbed images in background.
178 */
179 public synchronized void run() {
180 for (;;) {
181 imagesLock.lock();
182 int size = imagesToSave.size();
183 imagesLock.unlock();
184 if (size > 0) {
185 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension());
186 try {
187 if (file.exists()) {
188 ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend(
189 new BufferedOutputStream(new FileOutputStream(file, true)));
190 for (int i=0; i < size; i++) {
191 oos.writeObject(imagesToSave.get(i));
192 }
193 oos.close();
194 } else {
195 ObjectOutputStream oos = new ObjectOutputStream(
196 new BufferedOutputStream(new FileOutputStream(file)));
197 wmsLayer.write(oos);
198 for (int i=0; i < size; i++) {
199 oos.writeObject(imagesToSave.get(i));
200 }
201 oos.close();
202 }
203 } catch (IOException e) {
204 e.printStackTrace(System.out);
205 }
206 imagesLock.lock();
207 for (int i=0; i < size; i++) {
208 imagesToSave.remove(0);
209 }
210 imagesLock.unlock();
211 }
212 try {wait();} catch (InterruptedException e) {
213 e.printStackTrace(System.out);
214 }
215 }
216 }
217
218 private String WMSFileExtension() {
219 String ext = String.valueOf((wmsLayer.getLambertZone() + 1));
220 if (CadastrePlugin.isLambert_cc9())
221 ext = cLambertCC9Z + ext;
222 else if (CadastrePlugin.isUtm_france_dom())
223 ext = cUTM20N + ext;
224 return ext;
225 }
226
227}
Note: See TracBrowser for help on using the repository browser.