1 | package 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 |
|
---|
12 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
13 | import java.io.*;
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.concurrent.locks.Lock;
|
---|
16 | import java.util.concurrent.locks.ReentrantLock;
|
---|
17 |
|
---|
18 | import javax.swing.JOptionPane;
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 |
|
---|
21 | public 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.showMessageDialog(Main.parent, tr("Error loading file"), tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
120 | return false;
|
---|
121 | }
|
---|
122 | return true;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | public synchronized void saveCache(GeorefImage image) {
|
---|
127 | imagesLock.lock();
|
---|
128 | this.imagesToSave.add(image);
|
---|
129 | this.notify();
|
---|
130 | imagesLock.unlock();
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Thread saving the grabbed images in background.
|
---|
135 | */
|
---|
136 | public synchronized void run() {
|
---|
137 | for (;;) {
|
---|
138 | imagesLock.lock();
|
---|
139 | // copy locally the images to save for better performance
|
---|
140 | ArrayList<GeorefImage> images = new ArrayList<GeorefImage>(imagesToSave);
|
---|
141 | imagesToSave.clear();
|
---|
142 | imagesLock.unlock();
|
---|
143 | if (images != null && !images.isEmpty()) {
|
---|
144 | File file = new File(CadastrePlugin.cacheDir + wmsLayer.name + "." + String.valueOf((wmsLayer.lambertZone + 1)));
|
---|
145 | try {
|
---|
146 | if (file.exists()) {
|
---|
147 | ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend(
|
---|
148 | new BufferedOutputStream(new FileOutputStream(file, true)));
|
---|
149 | for (GeorefImage img : images) {
|
---|
150 | oos.writeObject(img);
|
---|
151 | }
|
---|
152 | oos.close();
|
---|
153 | } else {
|
---|
154 | ObjectOutputStream oos = new ObjectOutputStream(
|
---|
155 | new BufferedOutputStream(new FileOutputStream(file)));
|
---|
156 | wmsLayer.write(oos, images);
|
---|
157 | oos.close();
|
---|
158 | }
|
---|
159 | } catch (IOException e) {
|
---|
160 | e.printStackTrace(System.out);
|
---|
161 | }
|
---|
162 | }
|
---|
163 | try {wait();} catch (InterruptedException e) {
|
---|
164 | e.printStackTrace(System.out);
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | }
|
---|