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

Last change on this file since 18404 was 18278, checked in by pieren, 15 years ago

fix some warnings and easier selection of WMSlayer in WMSAdjustAction

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