1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
|
---|
2 | package 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 |
|
---|
13 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
14 |
|
---|
15 | import java.io.*;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.concurrent.locks.Lock;
|
---|
18 | import java.util.concurrent.locks.ReentrantLock;
|
---|
19 |
|
---|
20 | import javax.swing.JDialog;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.data.projection.LambertCC9Zones;
|
---|
24 | import org.openstreetmap.josm.data.projection.UTM_France_DOM;
|
---|
25 |
|
---|
26 | public 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 | if (cacheSize != 0) {
|
---|
76 | long size = 0;
|
---|
77 | long oldestFileDate = Long.MAX_VALUE;
|
---|
78 | int oldestFile = 0;
|
---|
79 | File[] files = path.listFiles();
|
---|
80 | for (int i = 0; i < files.length; i++) {
|
---|
81 | size += files[i].length();
|
---|
82 | if (files[i].lastModified() < oldestFileDate) {
|
---|
83 | oldestFile = i;
|
---|
84 | oldestFileDate = files[i].lastModified();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | if (size > (long)cacheSize*1024*1024) {
|
---|
88 | System.out.println("Delete oldest file \""+ files[oldestFile].getName()
|
---|
89 | + "\" in cache dir to stay under the limit of " + cacheSize + " MB.");
|
---|
90 | files[oldestFile].delete();
|
---|
91 | checkDirSize(path);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | public boolean loadCacheIfExist() {
|
---|
97 | try {
|
---|
98 | File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension());
|
---|
99 | if (file.exists()) {
|
---|
100 | JOptionPane pane = new JOptionPane(
|
---|
101 | tr("Location \"{0}\" found in cache.\n"+
|
---|
102 | "Load cache first ?\n"+
|
---|
103 | "(No = new cache)", wmsLayer.getName()),
|
---|
104 | JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null);
|
---|
105 | // this below is a temporary workaround to fix the "always on top" issue
|
---|
106 | JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
|
---|
107 | CadastrePlugin.prepareDialog(dialog);
|
---|
108 | dialog.setVisible(true);
|
---|
109 | int reply = (Integer)pane.getValue();
|
---|
110 | // till here
|
---|
111 |
|
---|
112 | if (reply == JOptionPane.OK_OPTION && loadCache(file, wmsLayer.getLambertZone())) {
|
---|
113 | return true;
|
---|
114 | } else {
|
---|
115 | delete(file);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | } catch (Exception e) {
|
---|
119 | e.printStackTrace(System.out);
|
---|
120 | }
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void deleteCacheFile() {
|
---|
125 | try {
|
---|
126 | delete(new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension()));
|
---|
127 | } catch (Exception e) {
|
---|
128 | e.printStackTrace(System.out);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | private void delete(File file) {
|
---|
133 | System.out.println("Delete file "+file);
|
---|
134 | if (file.exists())
|
---|
135 | file.delete();
|
---|
136 | while (file.exists()) // wait until file is really gone (otherwise appends to existing one)
|
---|
137 | CadastrePlugin.safeSleep(500);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public boolean loadCache(File file, int currentLambertZone) {
|
---|
141 | boolean successfulRead = false;
|
---|
142 | try {
|
---|
143 | FileInputStream fis = new FileInputStream(file);
|
---|
144 | ObjectInputStream ois = new ObjectInputStream(fis);
|
---|
145 | successfulRead = wmsLayer.read(ois, currentLambertZone);
|
---|
146 | ois.close();
|
---|
147 | fis.close();
|
---|
148 | } catch (Exception ex) {
|
---|
149 | ex.printStackTrace(System.out);
|
---|
150 | JOptionPane.showMessageDialog(Main.parent, tr("Error loading file.\nProbably an old version of the cache file."), tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
151 | return false;
|
---|
152 | }
|
---|
153 | if (successfulRead && wmsLayer.isRaster()) {
|
---|
154 | // serialized raster bufferedImage hangs-up on Java6. Recreate them here
|
---|
155 | wmsLayer.getImage(0).image = RasterImageModifier.fixRasterImage(wmsLayer.getImage(0).image);
|
---|
156 | }
|
---|
157 | return successfulRead;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | public synchronized void saveCache(GeorefImage image) {
|
---|
162 | imagesLock.lock();
|
---|
163 | this.imagesToSave.add(image);
|
---|
164 | this.notify();
|
---|
165 | imagesLock.unlock();
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Thread saving the grabbed images in background.
|
---|
170 | */
|
---|
171 | public synchronized void run() {
|
---|
172 | for (;;) {
|
---|
173 | imagesLock.lock();
|
---|
174 | int size = imagesToSave.size();
|
---|
175 | imagesLock.unlock();
|
---|
176 | if (size > 0) {
|
---|
177 | File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension());
|
---|
178 | try {
|
---|
179 | if (file.exists()) {
|
---|
180 | ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend(
|
---|
181 | new BufferedOutputStream(new FileOutputStream(file, true)));
|
---|
182 | for (int i=0; i < size; i++) {
|
---|
183 | oos.writeObject(imagesToSave.get(i));
|
---|
184 | }
|
---|
185 | oos.close();
|
---|
186 | } else {
|
---|
187 | ObjectOutputStream oos = new ObjectOutputStream(
|
---|
188 | new BufferedOutputStream(new FileOutputStream(file)));
|
---|
189 | wmsLayer.write(oos);
|
---|
190 | for (int i=0; i < size; i++) {
|
---|
191 | oos.writeObject(imagesToSave.get(i));
|
---|
192 | }
|
---|
193 | oos.close();
|
---|
194 | }
|
---|
195 | } catch (IOException e) {
|
---|
196 | e.printStackTrace(System.out);
|
---|
197 | }
|
---|
198 | imagesLock.lock();
|
---|
199 | for (int i=0; i < size; i++) {
|
---|
200 | imagesToSave.remove(0);
|
---|
201 | }
|
---|
202 | imagesLock.unlock();
|
---|
203 | }
|
---|
204 | try {wait();} catch (InterruptedException e) {
|
---|
205 | e.printStackTrace(System.out);
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | private String WMSFileExtension() {
|
---|
211 | String ext = String.valueOf((wmsLayer.getLambertZone() + 1));
|
---|
212 | if (Main.proj instanceof LambertCC9Zones)
|
---|
213 | ext = cLambertCC9Z + ext;
|
---|
214 | else if (Main.proj instanceof UTM_France_DOM)
|
---|
215 | ext = cUTM20N + ext;
|
---|
216 | return ext;
|
---|
217 | }
|
---|
218 |
|
---|
219 | }
|
---|