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

Last change on this file since 20484 was 20390, checked in by pieren, 16 years ago

Many small fixes and improvements

  • Property svn:eol-style set to native
File size: 8.2 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 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 try {
141 FileInputStream fis = new FileInputStream(file);
142 ObjectInputStream ois = new ObjectInputStream(fis);
143 successfulRead = wmsLayer.read(ois, currentLambertZone);
144 ois.close();
145 fis.close();
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 }
151 if (successfulRead && wmsLayer.isRaster()) {
152 // serialized raster bufferedImage hangs-up on Java6. Recreate them here
153 wmsLayer.images.get(0).image = RasterImageModifier.fixRasterImage(wmsLayer.images.get(0).image);
154 }
155 return successfulRead;
156 }
157
158
159 public synchronized void saveCache(GeorefImage image) {
160 imagesLock.lock();
161 this.imagesToSave.add(image);
162 this.notify();
163 imagesLock.unlock();
164 }
165
166 /**
167 * Thread saving the grabbed images in background.
168 */
169 public synchronized void run() {
170 for (;;) {
171 imagesLock.lock();
172 //ArrayList<GeorefImage> images = new ArrayList<GeorefImage>(imagesToSave);
173 int size = imagesToSave.size();
174 imagesLock.unlock();
175 if (size > 0) {
176 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension());
177 try {
178 if (file.exists()) {
179 ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend(
180 new BufferedOutputStream(new FileOutputStream(file, true)));
181 for (int i=0; i < size; i++) {
182 oos.writeObject(imagesToSave.get(i));
183 }
184 oos.close();
185 } else {
186 ObjectOutputStream oos = new ObjectOutputStream(
187 new BufferedOutputStream(new FileOutputStream(file)));
188 wmsLayer.write(oos);
189 for (int i=0; i < size; i++) {
190 oos.writeObject(imagesToSave.get(i));
191 }
192 oos.close();
193 }
194 } catch (IOException e) {
195 e.printStackTrace(System.out);
196 }
197 imagesLock.lock();
198 for (int i=0; i < size; i++) {
199 imagesToSave.remove(0);
200 }
201 imagesLock.unlock();
202 }
203 try {wait();} catch (InterruptedException e) {
204 e.printStackTrace(System.out);
205 }
206 }
207 }
208
209 private String WMSFileExtension() {
210 String ext = String.valueOf((wmsLayer.getLambertZone() + 1));
211 if (Main.proj instanceof LambertCC9Zones)
212 ext = cLambertCC9Z + ext;
213 else if (Main.proj instanceof UTM_20N_France_DOM)
214 ext = cUTM20N + ext;
215 return ext;
216 }
217
218}
Note: See TracBrowser for help on using the repository browser.