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

Last change on this file since 28887 was 28887, checked in by bastik, 14 years ago

update to latest josm (projection)

  • Property svn:eol-style set to native
File size: 8.3 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 try {
96 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension());
97 if (file.exists()) {
98 JOptionPane pane = new JOptionPane(
99 tr("Location \"{0}\" found in cache.\n"+
100 "Load cache first ?\n"+
101 "(No = new cache)", wmsLayer.getName()),
102 JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null);
103 // this below is a temporary workaround to fix the "always on top" issue
104 JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
105 CadastrePlugin.prepareDialog(dialog);
106 dialog.setVisible(true);
107 int reply = (Integer)pane.getValue();
108 // till here
109
110 if (reply == JOptionPane.OK_OPTION && loadCache(file, wmsLayer.getLambertZone())) {
111 return true;
112 } else {
113 delete(file);
114 }
115 }
116 } catch (Exception e) {
117 e.printStackTrace(System.out);
118 }
119 return false;
120 }
121
122 public void deleteCacheFile() {
123 try {
124 delete(new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension()));
125 } catch (Exception e) {
126 e.printStackTrace(System.out);
127 }
128 }
129
130 private void delete(File file) {
131 System.out.println("Delete file "+file);
132 if (file.exists())
133 file.delete();
134 while (file.exists()) // wait until file is really gone (otherwise appends to existing one)
135 CadastrePlugin.safeSleep(500);
136 }
137
138 public boolean loadCache(File file, int currentLambertZone) {
139 boolean successfulRead = false;
140 FileInputStream fis = null;
141 ObjectInputStream ois = null;
142 try {
143 fis = new FileInputStream(file);
144 ois = new ObjectInputStream(fis);
145 successfulRead = wmsLayer.read(ois, currentLambertZone);
146 } catch (Exception ex) {
147 ex.printStackTrace(System.out);
148 JOptionPane.showMessageDialog(Main.parent, tr("Error loading file.\nProbably an old version of the cache file."), tr("Error"), JOptionPane.ERROR_MESSAGE);
149 return false;
150 } finally {
151 try {
152 ois.close();
153 fis.close();
154 } catch (Exception e) {
155 e.printStackTrace();
156 }
157 }
158 if (successfulRead && wmsLayer.isRaster()) {
159 // serialized raster bufferedImage hangs-up on Java6. Recreate them here
160 wmsLayer.getImage(0).image = RasterImageModifier.fixRasterImage(wmsLayer.getImage(0).image);
161 }
162 return successfulRead;
163 }
164
165
166 public synchronized void saveCache(GeorefImage image) {
167 imagesLock.lock();
168 this.imagesToSave.add(image);
169 this.notify();
170 imagesLock.unlock();
171 }
172
173 /**
174 * Thread saving the grabbed images in background.
175 */
176 public synchronized void run() {
177 for (;;) {
178 imagesLock.lock();
179 int size = imagesToSave.size();
180 imagesLock.unlock();
181 if (size > 0) {
182 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension());
183 try {
184 if (file.exists()) {
185 ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend(
186 new BufferedOutputStream(new FileOutputStream(file, true)));
187 for (int i=0; i < size; i++) {
188 oos.writeObject(imagesToSave.get(i));
189 }
190 oos.close();
191 } else {
192 ObjectOutputStream oos = new ObjectOutputStream(
193 new BufferedOutputStream(new FileOutputStream(file)));
194 wmsLayer.write(oos);
195 for (int i=0; i < size; i++) {
196 oos.writeObject(imagesToSave.get(i));
197 }
198 oos.close();
199 }
200 } catch (IOException e) {
201 e.printStackTrace(System.out);
202 }
203 imagesLock.lock();
204 for (int i=0; i < size; i++) {
205 imagesToSave.remove(0);
206 }
207 imagesLock.unlock();
208 }
209 try {wait();} catch (InterruptedException e) {
210 e.printStackTrace(System.out);
211 }
212 }
213 }
214
215 private String WMSFileExtension() {
216 String ext = String.valueOf((wmsLayer.getLambertZone() + 1));
217 if (CadastrePlugin.isLambert_cc9())
218 ext = cLambertCC9Z + ext;
219 else if (CadastrePlugin.isUtm_france_dom())
220 ext = cUTM20N + ext;
221 return ext;
222 }
223
224}
Note: See TracBrowser for help on using the repository browser.