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

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

add municipality bbox in grab and cache

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