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

Last change on this file since 17707 was 17215, checked in by pieren, 15 years ago

improve georeferencing action cancellation; fixed bug of raster image loaded from cache not working on Java1.6; improve mouse click bounce detection during georeferencing process

  • Property svn:eol-style set to native
File size: 7.0 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;
13
14import java.awt.image.BufferedImage;
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 class ObjectOutputStreamAppend extends ObjectOutputStream {
27 public ObjectOutputStreamAppend(OutputStream out) throws IOException {
28 super(out);
29 }
30 protected void writeStreamHeader() throws IOException {
31 reset();
32 }
33 }
34
35 public static boolean cacheEnabled = true;
36
37 public static int cacheSize = 500;
38
39
40 public WMSLayer wmsLayer = null;
41
42 private ArrayList<GeorefImage> imagesToSave = new ArrayList<GeorefImage>();
43 private Lock imagesLock = new ReentrantLock();
44
45 public CacheControl(WMSLayer wmsLayer) {
46 cacheEnabled = Main.pref.getBoolean("cadastrewms.enableCaching", true);
47 this.wmsLayer = wmsLayer;
48 try {
49 cacheSize = Integer.parseInt(Main.pref.get("cadastrewms.cacheSize", String.valueOf(CadastrePreferenceSetting.DEFAULT_CACHE_SIZE)));
50 } catch (NumberFormatException e) {
51 cacheSize = CadastrePreferenceSetting.DEFAULT_CACHE_SIZE;
52 }
53 File path = new File(CadastrePlugin.cacheDir);
54 if (!path.exists())
55 path.mkdirs();
56 else // check directory capacity
57 checkDirSize(path);
58 new Thread(this).start();
59 }
60
61 private void checkDirSize(File path) {
62 long size = 0;
63 long oldestFileDate = Long.MAX_VALUE;
64 int oldestFile = 0;
65 File[] files = path.listFiles();
66 for (int i = 0; i < files.length; i++) {
67 size += files[i].length();
68 if (files[i].lastModified() < oldestFileDate) {
69 oldestFile = i;
70 oldestFileDate = files[i].lastModified();
71 }
72 }
73 if (size > cacheSize*1024*1024) {
74 System.out.println("Delete oldest file \""+ files[oldestFile].getName()
75 + "\" in cache dir to stay under the limit of " + cacheSize + " MB.");
76 files[oldestFile].delete();
77 checkDirSize(path);
78 }
79 }
80
81 public boolean loadCacheIfExist() {
82 try {
83 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + String.valueOf(wmsLayer.lambertZone+1));
84 if (file.exists()) {
85 JOptionPane pane = new JOptionPane(
86 tr("Location \"{0}\" found in cache.\n"+
87 "Load cache first ?\n"+
88 "(No = new cache)", wmsLayer.getName()),
89 JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null);
90 // this below is a temporary workaround to fix the "always on top" issue
91 JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
92 CadastrePlugin.prepareDialog(dialog);
93 dialog.setVisible(true);
94 int reply = (Integer)pane.getValue();
95 // till here
96
97 if (reply == JOptionPane.OK_OPTION) {
98 return loadCache(file, wmsLayer.lambertZone);
99 } else
100 file.delete();
101 }
102 } catch (Exception e) {
103 e.printStackTrace(System.out);
104 }
105 return false;
106 }
107
108 public void deleteCacheFile() {
109 try {
110 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + String.valueOf(wmsLayer.lambertZone+1));
111 if (file.exists())
112 file.delete();
113 } catch (Exception e) {
114 e.printStackTrace(System.out);
115 }
116 }
117
118 public boolean loadCache(File file, int currentLambertZone) {
119 try {
120 FileInputStream fis = new FileInputStream(file);
121 ObjectInputStream ois = new ObjectInputStream(fis);
122 if (wmsLayer.read(ois, currentLambertZone) == false)
123 return false;
124 ois.close();
125 fis.close();
126 } catch (Exception ex) {
127 ex.printStackTrace(System.out);
128 JOptionPane.showMessageDialog(Main.parent, tr("Error loading file"), tr("Error"), JOptionPane.ERROR_MESSAGE);
129 return false;
130 }
131 if (wmsLayer.isRaster()) {
132 // serialized raster bufferedImage hangs-up on Java6. Recreate them here
133 wmsLayer.images.get(0).image = RasterImageModifier.fixRasterImage(wmsLayer.images.get(0).image);
134 }
135 return true;
136 }
137
138
139 public synchronized void saveCache(GeorefImage image) {
140 imagesLock.lock();
141 this.imagesToSave.add(image);
142 this.notify();
143 imagesLock.unlock();
144 }
145
146 /**
147 * Thread saving the grabbed images in background.
148 */
149 public synchronized void run() {
150 for (;;) {
151 imagesLock.lock();
152 // copy locally the images to save for better performance
153 ArrayList<GeorefImage> images = new ArrayList<GeorefImage>(imagesToSave);
154 imagesToSave.clear();
155 imagesLock.unlock();
156 if (images != null && !images.isEmpty()) {
157 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + String.valueOf((wmsLayer.lambertZone + 1)));
158 try {
159 if (file.exists()) {
160 ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend(
161 new BufferedOutputStream(new FileOutputStream(file, true)));
162 for (GeorefImage img : images) {
163 oos.writeObject(img);
164 }
165 oos.close();
166 } else {
167 ObjectOutputStream oos = new ObjectOutputStream(
168 new BufferedOutputStream(new FileOutputStream(file)));
169 wmsLayer.write(oos, images);
170 oos.close();
171 }
172 } catch (IOException e) {
173 e.printStackTrace(System.out);
174 }
175 }
176 try {wait();} catch (InterruptedException e) {
177 e.printStackTrace(System.out);
178 }
179 }
180 }
181}
Note: See TracBrowser for help on using the repository browser.