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

Last change on this file since 18743 was 18720, checked in by pieren, 15 years ago

Add subprojection handling

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1// License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
2package 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
13import static org.openstreetmap.josm.tools.I18n.tr;
14
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;
23import org.openstreetmap.josm.data.projection.LambertCC9Zones;
24import org.openstreetmap.josm.data.projection.UTM_20N_France_DOM;
25
26public 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 CacheControl(WMSLayer wmsLayer) {
52 cacheEnabled = Main.pref.getBoolean("cadastrewms.enableCaching", true);
53 this.wmsLayer = wmsLayer;
54 try {
55 cacheSize = Integer.parseInt(Main.pref.get("cadastrewms.cacheSize", String.valueOf(CadastrePreferenceSetting.DEFAULT_CACHE_SIZE)));
56 } catch (NumberFormatException e) {
57 cacheSize = CadastrePreferenceSetting.DEFAULT_CACHE_SIZE;
58 }
59 File path = new File(CadastrePlugin.cacheDir);
60 if (!path.exists())
61 path.mkdirs();
62 else // check directory capacity
63 checkDirSize(path);
64 new Thread(this).start();
65 }
66
67 private void checkDirSize(File path) {
68 long size = 0;
69 long oldestFileDate = Long.MAX_VALUE;
70 int oldestFile = 0;
71 File[] files = path.listFiles();
72 for (int i = 0; i < files.length; i++) {
73 size += files[i].length();
74 if (files[i].lastModified() < oldestFileDate) {
75 oldestFile = i;
76 oldestFileDate = files[i].lastModified();
77 }
78 }
79 if (size > cacheSize*1024*1024) {
80 System.out.println("Delete oldest file \""+ files[oldestFile].getName()
81 + "\" in cache dir to stay under the limit of " + cacheSize + " MB.");
82 files[oldestFile].delete();
83 checkDirSize(path);
84 }
85 }
86
87 public boolean loadCacheIfExist() {
88 try {
89 String extension = String.valueOf((wmsLayer.getLambertZone() + 1));
90 if (Main.proj instanceof LambertCC9Zones)
91 extension = cLambertCC9Z + extension;
92 else if (Main.proj instanceof UTM_20N_France_DOM)
93 extension = cUTM20N + extension;
94 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + extension);
95 if (file.exists()) {
96 JOptionPane pane = new JOptionPane(
97 tr("Location \"{0}\" found in cache.\n"+
98 "Load cache first ?\n"+
99 "(No = new cache)", wmsLayer.getName()),
100 JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null);
101 // this below is a temporary workaround to fix the "always on top" issue
102 JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
103 CadastrePlugin.prepareDialog(dialog);
104 dialog.setVisible(true);
105 int reply = (Integer)pane.getValue();
106 // till here
107
108 if (reply == JOptionPane.OK_OPTION) {
109 return loadCache(file, wmsLayer.getLambertZone());
110 } else
111 file.delete();
112 }
113 } catch (Exception e) {
114 e.printStackTrace(System.out);
115 }
116 return false;
117 }
118
119 public void deleteCacheFile() {
120 try {
121 String extension = String.valueOf((wmsLayer.getLambertZone() + 1));
122 if (Main.proj instanceof LambertCC9Zones)
123 extension = cLambertCC9Z + extension;
124 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + extension);
125 if (file.exists())
126 file.delete();
127 } catch (Exception e) {
128 e.printStackTrace(System.out);
129 }
130 }
131
132 public boolean loadCache(File file, int currentLambertZone) {
133 try {
134 FileInputStream fis = new FileInputStream(file);
135 ObjectInputStream ois = new ObjectInputStream(fis);
136 if (wmsLayer.read(ois, currentLambertZone) == false)
137 return false;
138 ois.close();
139 fis.close();
140 } catch (Exception ex) {
141 ex.printStackTrace(System.out);
142 JOptionPane.showMessageDialog(Main.parent, tr("Error loading file"), tr("Error"), JOptionPane.ERROR_MESSAGE);
143 return false;
144 }
145 if (wmsLayer.isRaster()) {
146 // serialized raster bufferedImage hangs-up on Java6. Recreate them here
147 wmsLayer.images.get(0).image = RasterImageModifier.fixRasterImage(wmsLayer.images.get(0).image);
148 }
149 return true;
150 }
151
152
153 public synchronized void saveCache(GeorefImage image) {
154 imagesLock.lock();
155 this.imagesToSave.add(image);
156 this.notify();
157 imagesLock.unlock();
158 }
159
160 /**
161 * Thread saving the grabbed images in background.
162 */
163 public synchronized void run() {
164 for (;;) {
165 imagesLock.lock();
166 // copy locally the images to save for better performance
167 ArrayList<GeorefImage> images = new ArrayList<GeorefImage>(imagesToSave);
168 imagesToSave.clear();
169 imagesLock.unlock();
170 if (images != null && !images.isEmpty()) {
171 String extension = String.valueOf((wmsLayer.getLambertZone() + 1));
172 if (Main.proj instanceof LambertCC9Zones)
173 extension = cLambertCC9Z + extension;
174 else if (Main.proj instanceof UTM_20N_France_DOM)
175 extension = cUTM20N + extension;
176 File file = new File(CadastrePlugin.cacheDir + wmsLayer.getName() + "." + extension);
177 try {
178 if (file.exists()) {
179 ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend(
180 new BufferedOutputStream(new FileOutputStream(file, true)));
181 for (GeorefImage img : images) {
182 oos.writeObject(img);
183 }
184 oos.close();
185 } else {
186 ObjectOutputStream oos = new ObjectOutputStream(
187 new BufferedOutputStream(new FileOutputStream(file)));
188 wmsLayer.write(oos);
189 for (GeorefImage img : images) {
190 oos.writeObject(img);
191 }
192 oos.close();
193 }
194 } catch (IOException e) {
195 e.printStackTrace(System.out);
196 }
197 }
198 try {wait();} catch (InterruptedException e) {
199 e.printStackTrace(System.out);
200 }
201 }
202 }
203}
Note: See TracBrowser for help on using the repository browser.