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

Last change on this file since 13382 was 13382, checked in by pieren, 17 years ago

First commit of the french land registry WMS plugin for JOSM.

File size: 9.6 KB
Line 
1package cadastre_fr;
2/**
3 * This class handles the WMS layer cache mechanism. The design is oriented for a good performance (no
4 * wait status on GUI, fast saving even in big file). A separate thread is created for each WMS
5 * layer to not suspend the GUI until disk I/O is terminated (a file for the cache can take
6 * several MB's). If the cache file already exists, new images are just appended to the file
7 * (performance). Since we use the ObjectStream methods, it is required to modify the standard
8 * ObjectOutputStream in order to have objects appended readable (otherwise a stream header
9 * is inserted before each append and an exception is raised at objects read).
10 */
11
12import static org.openstreetmap.josm.tools.I18n.tr;
13import java.io.*;
14import java.util.ArrayList;
15import java.util.concurrent.locks.Lock;
16import java.util.concurrent.locks.ReentrantLock;
17
18import javax.swing.JOptionPane;
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.coor.EastNorth;
21
22public class CacheControl implements Runnable {
23
24 public class ObjectOutputStreamAppend extends ObjectOutputStream {
25 public ObjectOutputStreamAppend(OutputStream out) throws IOException {
26 super(out);
27 }
28 protected void writeStreamHeader() throws IOException {
29 reset();
30 }
31 }
32
33 public static boolean cacheEnabled = true;
34
35 public static int cacheSize = 500;
36
37
38 public WMSLayer wmsLayer = null;
39
40 private ArrayList<GeorefImage> imagesToSave = new ArrayList<GeorefImage>();
41 private Lock imagesLock = new ReentrantLock();
42
43 public CacheControl(WMSLayer wmsLayer) {
44 cacheEnabled = Main.pref.getBoolean("cadastrewms.enableCaching", true);
45 this.wmsLayer = wmsLayer;
46 try {
47 cacheSize = Integer.parseInt(Main.pref.get("cadastrewms.cacheSize", String.valueOf(CadastrePreferenceSetting.DEFAULT_CACHE_SIZE)));
48 } catch (NumberFormatException e) {
49 cacheSize = CadastrePreferenceSetting.DEFAULT_CACHE_SIZE;
50 }
51 File path = new File(CadastrePlugin.cacheDir);
52 if (!path.exists())
53 path.mkdirs();
54 else // check directory capacity
55 checkDirSize(path);
56 new Thread(this).start();
57 }
58
59 private void checkDirSize(File path) {
60 long size = 0;
61 long oldestFileDate = Long.MAX_VALUE;
62 int oldestFile = 0;
63 File[] files = path.listFiles();
64 for (int i = 0; i < files.length; i++) {
65 size += files[i].length();
66 if (files[i].lastModified() < oldestFileDate) {
67 oldestFile = i;
68 oldestFileDate = files[i].lastModified();
69 }
70 }
71 if (size > cacheSize*1024*1024) {
72 System.out.println("Delete oldest file \""+ files[oldestFile].getName()
73 + "\" in cache dir to stay under the limit of " + cacheSize + " MB.");
74 files[oldestFile].delete();
75 checkDirSize(path);
76 }
77 }
78
79 public boolean loadCacheIfExist() {
80 try {
81 File file = new File(CadastrePlugin.cacheDir + wmsLayer.name + "." + String.valueOf(wmsLayer.lambertZone+1));
82 if (file.exists()) {
83 int reply = JOptionPane.showConfirmDialog(null,
84 "Location \""+wmsLayer.name+"\" found in cache.\n"+
85 "Load cache first ?\n"+
86 "(No = new cache)",
87 "Location in cache",
88 JOptionPane.YES_NO_OPTION);
89 if (reply == JOptionPane.OK_OPTION) {
90 return loadCache(file, wmsLayer.lambertZone);
91 } else
92 file.delete();
93 }
94 } catch (Exception e) {
95 e.printStackTrace(System.out);
96 }
97 return false;
98 }
99
100 public void deleteCacheFile() {
101 try {
102 File file = new File(CadastrePlugin.cacheDir + wmsLayer.name + "." + String.valueOf(wmsLayer.lambertZone+1));
103 if (file.exists())
104 file.delete();
105 } catch (Exception e) {
106 e.printStackTrace(System.out);
107 }
108 }
109
110 public boolean loadCache(File file, int currentLambertZone) {
111 try {
112 FileInputStream fis = new FileInputStream(file);
113 ObjectInputStream ois = new ObjectInputStream(fis);
114 int sfv = ois.readInt();
115 if (sfv != wmsLayer.serializeFormatVersion) {
116 JOptionPane.showMessageDialog(Main.parent, tr("Unsupported WMS file version; found {0}, expected {1}",
117 sfv, wmsLayer.serializeFormatVersion), tr("Cache Format Error"), JOptionPane.ERROR_MESSAGE);
118 return false;
119 }
120 wmsLayer.setLocation((String) ois.readObject());
121 wmsLayer.setCodeCommune((String) ois.readObject());
122 wmsLayer.lambertZone = ois.readInt();
123 wmsLayer.setRaster(ois.readBoolean());
124 wmsLayer.setRasterMin((EastNorth) ois.readObject());
125 wmsLayer.setRasterCenter((EastNorth) ois.readObject());
126 wmsLayer.setRasterRatio(ois.readDouble());
127 if (wmsLayer.lambertZone != currentLambertZone) {
128 JOptionPane.showMessageDialog(Main.parent, tr("Lambert zone {0} in cache "+
129 " incompatible with current Lambert zone {1}",
130 wmsLayer.lambertZone+1, currentLambertZone), tr("Cache Lambert Zone Error"), JOptionPane.ERROR_MESSAGE);
131 return false;
132 }
133 boolean EOF = false;
134 try {
135 while (!EOF) {
136 GeorefImage newImage = (GeorefImage) ois.readObject();
137 for (GeorefImage img : wmsLayer.images) {
138 if (CadastrePlugin.backgroundTransparent) {
139 if (img.overlap(newImage))
140 // mask overlapping zone in already grabbed image
141 img.withdraw(newImage);
142 else
143 // mask overlapping zone in new image only when
144 // new image covers completely the existing image
145 newImage.withdraw(img);
146 }
147 }
148 wmsLayer.images.add(newImage);
149 }
150 } catch (EOFException e) {}
151 ois.close();
152 fis.close();
153 } catch (Exception ex) {
154 ex.printStackTrace(System.out);
155 JOptionPane
156 .showMessageDialog(Main.parent, tr("Error loading file"), tr("Error"), JOptionPane.ERROR_MESSAGE);
157 }
158 return true;
159 }
160
161
162 public boolean existCache() {
163 try {
164 /*
165 File pathname = new File(CadastrePlugin.cacheDir);
166 String[] fileNames = pathname.list();
167 for (String fileName : fileNames) {
168 System.out.println("check file:"+fileName);
169 //WMSLayer cached = new WMSLayer(new File(cacheDir+fileName));
170 }*/
171 } catch (Exception e) {
172 e.printStackTrace(System.out);
173 }
174 return false;
175 }
176
177 public synchronized void saveCache(GeorefImage image) {
178 imagesLock.lock();
179 this.imagesToSave.add(image);
180 this.notify();
181 imagesLock.unlock();
182 }
183
184 /**
185 * Thread saving the grabbed images in background.
186 */
187 public synchronized void run() {
188 for (;;) {
189 imagesLock.lock();
190 // copy locally the images to save for better performance
191 ArrayList<GeorefImage> images = new ArrayList<GeorefImage>(imagesToSave);
192 imagesToSave.clear();
193 imagesLock.unlock();
194 if (images != null && !images.isEmpty()) {
195 File file = new File(CadastrePlugin.cacheDir + wmsLayer.name + "." + String.valueOf((wmsLayer.lambertZone + 1)));
196 try {
197 if (file.exists()) {
198 ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend(
199 new BufferedOutputStream(new FileOutputStream(file, true)));
200 for (GeorefImage img : images) {
201 oos.writeObject(img);
202 }
203 oos.close();
204 } else {
205 ObjectOutputStream oos = new ObjectOutputStream(
206 new BufferedOutputStream(new FileOutputStream(file)));
207 oos.writeInt(wmsLayer.serializeFormatVersion);
208 oos.writeObject(wmsLayer.getLocation());
209 oos.writeObject(wmsLayer.getCodeCommune());
210 oos.writeInt(wmsLayer.lambertZone);
211 oos.writeBoolean(wmsLayer.isRaster());
212 oos.writeObject(wmsLayer.getRasterMin());
213 oos.writeObject(wmsLayer.getRasterCenter());
214 oos.writeDouble(wmsLayer.getRasterRatio());
215 for (GeorefImage img : images) {
216 oos.writeObject(img);
217 }
218 oos.close();
219 }
220 } catch (IOException e) {
221 e.printStackTrace(System.out);
222 }
223 }
224 try {wait();} catch (InterruptedException e) {
225 e.printStackTrace(System.out);
226 }
227 }
228 }
229
230}
Note: See TracBrowser for help on using the repository browser.