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

Last change on this file since 18271 was 18207, checked in by pieren, 16 years ago

Use the new cadastre projection LambertCC9Zones

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