42914debf78412d6cb8a8d4ef811450e7a944d9b
[fw/altos] / altosuilib / AltosSiteMapCache.java
1 /*
2  * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_2;
19
20 import javax.swing.*;
21 import javax.imageio.ImageIO;
22 import java.awt.image.*;
23 import java.awt.*;
24 import java.io.*;
25 import java.net.*;
26
27 class AltosCacheImage {
28         Component       component;
29         File            file;
30         VolatileImage   image;
31         int             width;
32         int             height;
33         long            used;
34
35         public void load_image() throws IOException {
36                 BufferedImage   bimg = ImageIO.read(file);
37                 if (bimg == null)
38                         throw new IOException("Can't load image file");
39                 Graphics2D      g = image.createGraphics();
40                 g.drawImage(bimg, 0, 0, null);
41                 bimg.flush();
42                 bimg = null;
43         }
44
45         public Image validate() {
46                 int     returnCode;
47
48                 if (image != null)
49                         returnCode = image.validate(component.getGraphicsConfiguration());
50                 else
51                         returnCode = VolatileImage.IMAGE_INCOMPATIBLE;
52                 if (returnCode == VolatileImage.IMAGE_RESTORED) {
53                         try {
54                                 load_image();
55                         } catch (IOException e) {
56                                 return null;
57                         }
58                 } else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
59                         image = component.createVolatileImage(width, height);
60                         try {
61                                 load_image();
62                         } catch (IOException e) {
63                                 return null;
64                         }
65                 }
66                 return image;
67         }
68
69         public void flush() {
70                 image.flush();
71         }
72
73         public AltosCacheImage(Component component, File file, int w, int h) throws IOException {
74                 this.component = component;
75                 this.file = file;
76                 width = w;
77                 height = h;
78                 image = component.createVolatileImage(w, h);
79                 used = 0;
80         }
81 }
82
83 public class AltosSiteMapCache {
84         static final long google_maps_ratelimit_ms = 1200;
85         // Google limits static map queries to 50 per minute per IP, so
86         // each query should take at least 1.2 seconds.
87
88         static final int        success = 0;
89         static final int        loading = 1;
90         static final int        failed = 2;
91         static final int        bad_request = 3;
92         static final int        forbidden = 4;
93
94         public static boolean has_map(File file, String url) {
95                 return file.exists();
96         }
97
98         static long     forbidden_time;
99         static boolean  forbidden_set = false;
100         static final long       forbidden_interval = 60l * 1000l * 1000l * 1000l;
101
102         public static synchronized int fetch_map(File file, String url) {
103                 if (file.exists())
104                         return success;
105
106                 if (forbidden_set && (System.nanoTime() - forbidden_time) < forbidden_interval)
107                         return forbidden;
108
109                 URL u;
110                 long startTime = System.nanoTime();
111
112                 try {
113                         u = new URL(url);
114                 } catch (java.net.MalformedURLException e) {
115                         return bad_request;
116                 }
117
118                 byte[] data;
119                 URLConnection uc = null;
120                 try {
121                         uc = u.openConnection();
122                         String type = uc.getContentType();
123                         int contentLength = uc.getContentLength();
124                         if (uc instanceof HttpURLConnection) {
125                                 int response = ((HttpURLConnection) uc).getResponseCode();
126                                 switch (response) {
127                                 case HttpURLConnection.HTTP_FORBIDDEN:
128                                 case HttpURLConnection.HTTP_PAYMENT_REQUIRED:
129                                 case HttpURLConnection.HTTP_UNAUTHORIZED:
130                                         forbidden_time = System.nanoTime();
131                                         forbidden_set = true;
132                                         return forbidden;
133                                 }
134                         }
135                         InputStream in = new BufferedInputStream(uc.getInputStream());
136                         int bytesRead = 0;
137                         int offset = 0;
138                         data = new byte[contentLength];
139                         while (offset < contentLength) {
140                                 bytesRead = in.read(data, offset, data.length - offset);
141                                 if (bytesRead == -1)
142                                         break;
143                                 offset += bytesRead;
144                         }
145                         in.close();
146
147                         if (offset != contentLength)
148                                 return failed;
149
150                 } catch (IOException e) {
151                         return failed;
152                 }
153
154                 try {
155                         FileOutputStream out = new FileOutputStream(file);
156                         out.write(data);
157                         out.flush();
158                         out.close();
159                 } catch (FileNotFoundException e) {
160                         return bad_request;
161                 } catch (IOException e) {
162                         if (file.exists())
163                                 file.delete();
164                         return bad_request;
165                 }
166
167                 long duration_ms = (System.nanoTime() - startTime) / 1000000;
168                 if (duration_ms < google_maps_ratelimit_ms) {
169                         try {
170                                 Thread.sleep(google_maps_ratelimit_ms - duration_ms);
171                         } catch (InterruptedException e) {
172                                 Thread.currentThread().interrupt();
173                         }
174                 }
175
176                 return success;
177         }
178
179         static final int                cache_size = 12;
180
181         static AltosCacheImage[]        images;
182
183         static long                     used;
184
185         public static Image get_image(Component component, File file, int width, int height) {
186                 int             oldest = -1;
187                 long            age = used;
188                 AltosCacheImage image;
189                 if (images == null)
190                         images = new AltosCacheImage[cache_size];
191                 for (int i = 0; i < cache_size; i++) {
192                         image = images[i];
193
194                         if (image == null) {
195                                 oldest = i;
196                                 break;
197                         }
198                         if (image.component == component && file.equals(image.file)) {
199                                 image.used = used++;
200                                 return image.validate();
201                         }
202                         if (image.used < age) {
203                                 oldest = i;
204                                 age = image.used;
205                         }
206                 }
207
208                 try {
209                         image = new AltosCacheImage(component, file, width, height);
210                         image.used = used++;
211                         if (images[oldest] != null)
212                                 images[oldest].flush();
213                         images[oldest] = image;
214                         return image.validate();
215                 } catch (IOException e) {
216                         return null;
217                 }
218         }
219 }