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

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

raster image feature implementation

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