1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
|
---|
2 | package cadastre_fr;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.BufferedOutputStream;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileInputStream;
|
---|
9 | import java.io.FileOutputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.ObjectInputStream;
|
---|
12 | import java.io.ObjectOutputStream;
|
---|
13 | import java.io.OutputStream;
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.concurrent.locks.Lock;
|
---|
16 | import java.util.concurrent.locks.ReentrantLock;
|
---|
17 |
|
---|
18 | import javax.swing.JDialog;
|
---|
19 | import javax.swing.JOptionPane;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * This class handles the WMS layer cache mechanism. The design is oriented for a good performance (no
|
---|
25 | * wait status on GUI, fast saving even in big file). A separate thread is created for each WMS
|
---|
26 | * layer to not suspend the GUI until disk I/O is terminated (a file for the cache can take
|
---|
27 | * several MB's). If the cache file already exists, new images are just appended to the file
|
---|
28 | * (performance). Since we use the ObjectStream methods, it is required to modify the standard
|
---|
29 | * ObjectOutputStream in order to have objects appended readable (otherwise a stream header
|
---|
30 | * is inserted before each append and an exception is raised at objects read).
|
---|
31 | */
|
---|
32 | public class CacheControl implements Runnable {
|
---|
33 |
|
---|
34 | public static final String cLambertCC9Z = "CC";
|
---|
35 |
|
---|
36 | public static final String cUTM20N = "UTM";
|
---|
37 |
|
---|
38 | public class ObjectOutputStreamAppend extends ObjectOutputStream {
|
---|
39 | public ObjectOutputStreamAppend(OutputStream out) throws IOException {
|
---|
40 | super(out);
|
---|
41 | }
|
---|
42 | @Override
|
---|
43 | protected void writeStreamHeader() throws IOException {
|
---|
44 | reset();
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public static boolean cacheEnabled = true;
|
---|
49 |
|
---|
50 | public static int cacheSize = 500;
|
---|
51 |
|
---|
52 | public WMSLayer wmsLayer = null;
|
---|
53 |
|
---|
54 | private ArrayList<GeorefImage> imagesToSave = new ArrayList<>();
|
---|
55 | private Lock imagesLock = new ReentrantLock();
|
---|
56 |
|
---|
57 | public boolean isCachePipeEmpty() {
|
---|
58 | imagesLock.lock();
|
---|
59 | boolean ret = imagesToSave.isEmpty();
|
---|
60 | imagesLock.unlock();
|
---|
61 | return ret;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public CacheControl(WMSLayer wmsLayer) {
|
---|
65 | cacheEnabled = Main.pref.getBoolean("cadastrewms.enableCaching", true);
|
---|
66 | this.wmsLayer = wmsLayer;
|
---|
67 | try {
|
---|
68 | cacheSize = Integer.parseInt(Main.pref.get("cadastrewms.cacheSize", String.valueOf(CadastrePreferenceSetting.DEFAULT_CACHE_SIZE)));
|
---|
69 | } catch (NumberFormatException e) {
|
---|
70 | cacheSize = CadastrePreferenceSetting.DEFAULT_CACHE_SIZE;
|
---|
71 | }
|
---|
72 | File path = new File(CadastrePlugin.cacheDir);
|
---|
73 | if (!path.exists())
|
---|
74 | path.mkdirs();
|
---|
75 | else // check directory capacity
|
---|
76 | checkDirSize(path);
|
---|
77 | new Thread(this).start();
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void checkDirSize(File path) {
|
---|
81 | if (cacheSize != 0) {
|
---|
82 | long size = 0;
|
---|
83 | long oldestFileDate = Long.MAX_VALUE;
|
---|
84 | int oldestFile = 0;
|
---|
85 | File[] files = path.listFiles();
|
---|
86 | for (int i = 0; i < files.length; i++) {
|
---|
87 | size += files[i].length();
|
---|
88 | if (files[i].lastModified() < oldestFileDate) {
|
---|
89 | oldestFile = i;
|
---|
90 | oldestFileDate = files[i].lastModified();
|
---|
91 | }
|
---|
92 | }
|
---|
93 | if (size > (long)cacheSize*1024*1024) {
|
---|
94 | Main.info("Delete oldest file \""+ files[oldestFile].getName()
|
---|
95 | + "\" in cache dir to stay under the limit of " + cacheSize + " MB.");
|
---|
96 | files[oldestFile].delete();
|
---|
97 | checkDirSize(path);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | public boolean loadCacheIfExist() {
|
---|
103 | if (!CadastrePlugin.isCadastreProjection()) {
|
---|
104 | CadastrePlugin.askToChangeProjection();
|
---|
105 | }
|
---|
106 | try {
|
---|
107 | File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension());
|
---|
108 | if (file.exists()) {
|
---|
109 | JOptionPane pane = new JOptionPane(
|
---|
110 | tr("Location \"{0}\" found in cache.\n"+
|
---|
111 | "Load cache first ?\n"+
|
---|
112 | "(No = new cache)", wmsLayer.getName()),
|
---|
113 | JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null);
|
---|
114 | // this below is a temporary workaround to fix the "always on top" issue
|
---|
115 | JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
|
---|
116 | CadastrePlugin.prepareDialog(dialog);
|
---|
117 | dialog.setVisible(true);
|
---|
118 | int reply = (Integer)pane.getValue();
|
---|
119 | // till here
|
---|
120 |
|
---|
121 | if (reply == JOptionPane.OK_OPTION && loadCache(file, wmsLayer.getLambertZone())) {
|
---|
122 | return true;
|
---|
123 | } else {
|
---|
124 | delete(file);
|
---|
125 | }
|
---|
126 | }
|
---|
127 | } catch (Exception e) {
|
---|
128 | Main.error(e);
|
---|
129 | }
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 |
|
---|
133 | public void deleteCacheFile() {
|
---|
134 | try {
|
---|
135 | delete(new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension()));
|
---|
136 | } catch (Exception e) {
|
---|
137 | Main.error(e);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | private void delete(File file) {
|
---|
142 | Main.info("Delete file "+file);
|
---|
143 | if (file.exists())
|
---|
144 | file.delete();
|
---|
145 | while (file.exists()) // wait until file is really gone (otherwise appends to existing one)
|
---|
146 | CadastrePlugin.safeSleep(500);
|
---|
147 | }
|
---|
148 |
|
---|
149 | public boolean loadCache(File file, int currentLambertZone) {
|
---|
150 | boolean successfulRead = false;
|
---|
151 | try (
|
---|
152 | FileInputStream fis = new FileInputStream(file);
|
---|
153 | ObjectInputStream ois = new ObjectInputStream(fis);
|
---|
154 | ) {
|
---|
155 | successfulRead = wmsLayer.read(file, ois, currentLambertZone);
|
---|
156 | } catch (Exception ex) {
|
---|
157 | Main.error(ex);
|
---|
158 | JOptionPane.showMessageDialog(Main.parent, tr("Error loading file.\nProbably an old version of the cache file."), tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
159 | return false;
|
---|
160 | }
|
---|
161 | if (successfulRead && wmsLayer.isRaster()) {
|
---|
162 | // serialized raster bufferedImage hangs-up on Java6. Recreate them here
|
---|
163 | wmsLayer.getImage(0).image = RasterImageModifier.fixRasterImage(wmsLayer.getImage(0).image);
|
---|
164 | }
|
---|
165 | return successfulRead;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public synchronized void saveCache(GeorefImage image) {
|
---|
169 | imagesLock.lock();
|
---|
170 | this.imagesToSave.add(image);
|
---|
171 | this.notify();
|
---|
172 | imagesLock.unlock();
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Thread saving the grabbed images in background.
|
---|
177 | */
|
---|
178 | @Override
|
---|
179 | public synchronized void run() {
|
---|
180 | for (;;) {
|
---|
181 | imagesLock.lock();
|
---|
182 | int size = imagesToSave.size();
|
---|
183 | imagesLock.unlock();
|
---|
184 | if (size > 0) {
|
---|
185 | File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + WMSFileExtension());
|
---|
186 | try {
|
---|
187 | if (file.exists()) {
|
---|
188 | try (ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend(
|
---|
189 | new BufferedOutputStream(new FileOutputStream(file, true)))) {
|
---|
190 | for (int i=0; i < size; i++) {
|
---|
191 | oos.writeObject(imagesToSave.get(i));
|
---|
192 | }
|
---|
193 | }
|
---|
194 | } else {
|
---|
195 | try (ObjectOutputStream oos = new ObjectOutputStream(
|
---|
196 | new BufferedOutputStream(new FileOutputStream(file)))) {
|
---|
197 | wmsLayer.write(file, oos);
|
---|
198 | for (int i=0; i < size; i++) {
|
---|
199 | oos.writeObject(imagesToSave.get(i));
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 | } catch (IOException e) {
|
---|
204 | Main.error(e);
|
---|
205 | }
|
---|
206 | imagesLock.lock();
|
---|
207 | for (int i=0; i < size; i++) {
|
---|
208 | imagesToSave.remove(0);
|
---|
209 | }
|
---|
210 | imagesLock.unlock();
|
---|
211 | }
|
---|
212 | try {wait();} catch (InterruptedException e) {
|
---|
213 | Main.error(e);
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | private String WMSFileExtension() {
|
---|
219 | String ext = String.valueOf((wmsLayer.getLambertZone() + 1));
|
---|
220 | if (CadastrePlugin.isLambert_cc9())
|
---|
221 | ext = cLambertCC9Z + ext;
|
---|
222 | else if (CadastrePlugin.isUtm_france_dom())
|
---|
223 | ext = cUTM20N + ext;
|
---|
224 | return ext;
|
---|
225 | }
|
---|
226 |
|
---|
227 | }
|
---|