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

Last change on this file since 18920 was 18838, checked in by pieren, 15 years ago

Fix minor issues if Grab is called without layer (possible since projection rework)

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