Lines 43-48
Link Here
|
43 |
|
43 |
|
44 |
import java.io.BufferedOutputStream; |
44 |
import java.io.BufferedOutputStream; |
45 |
import java.io.File; |
45 |
import java.io.File; |
|
|
46 |
import java.io.FileInputStream; |
46 |
import java.io.FileOutputStream; |
47 |
import java.io.FileOutputStream; |
47 |
import java.io.IOException; |
48 |
import java.io.IOException; |
48 |
import java.io.InputStream; |
49 |
import java.io.InputStream; |
Lines 60-66
Link Here
|
60 |
*/ |
61 |
*/ |
61 |
public class AutoupdateCatalogCache { |
62 |
public class AutoupdateCatalogCache { |
62 |
private File cacheDir; |
63 |
private File cacheDir; |
63 |
private Exception storedException; |
64 |
|
64 |
|
65 |
|
65 |
private static AutoupdateCatalogCache INSTANCE; |
66 |
private static AutoupdateCatalogCache INSTANCE; |
66 |
|
67 |
|
Lines 100-106
Link Here
|
100 |
assert dir != null && dir.exists () : "Cache directory must exist."; |
101 |
assert dir != null && dir.exists () : "Cache directory must exist."; |
101 |
File cache = new File (dir, codeName); |
102 |
File cache = new File (dir, codeName); |
102 |
|
103 |
|
103 |
copy (original, cache); |
104 |
copyCatalog(original, cache); |
104 |
|
105 |
|
105 |
try { |
106 |
try { |
106 |
url = cache.toURI ().toURL (); |
107 |
url = cache.toURI ().toURL (); |
Lines 137-144
Link Here
|
137 |
return null; |
138 |
return null; |
138 |
} |
139 |
} |
139 |
} |
140 |
} |
|
|
141 |
private File getLicenseDir() { |
142 |
return new File(getCatalogCache(), "licenses"); |
143 |
} |
144 |
|
145 |
private File getLicenseFile(String name) { |
146 |
return new File(getLicenseDir(), name); |
147 |
} |
148 |
|
149 |
public String getLicense(String name) { |
150 |
return getLicense(name, null); |
151 |
} |
152 |
|
153 |
public String getLicense(String name, URL url) { |
154 |
synchronized (name.intern()) { |
155 |
File file = getLicenseFile(name); |
156 |
if (!file.exists()) { |
157 |
if(url==null) { |
158 |
return null; |
159 |
} |
160 |
try { |
161 |
return copyLicense(url, file); |
162 |
} catch (IOException e) { |
163 |
err.log(Level.INFO, "Can`t load license from " + url, e); |
164 |
return null; |
165 |
} |
166 |
} |
167 |
try { |
168 |
FileInputStream fr = new FileInputStream(file); |
169 |
byte[] buffer = new byte[8192]; |
170 |
int n = 0; |
171 |
StringBuilder sb = new StringBuilder(); |
172 |
while ((n = fr.read(buffer)) != -1) { |
173 |
sb.append(new String(buffer, 0, n, "utf-8"));//NOI18N |
174 |
} |
175 |
return sb.toString(); |
176 |
} catch (IOException e) { |
177 |
err.log(Level.INFO, "Can`t read license from file " + file, e); |
178 |
return null; |
179 |
} |
180 |
} |
181 |
} |
182 |
|
183 |
public void storeLicense(String name, String content) { |
184 |
synchronized (name.intern()) { |
185 |
File file = getLicenseFile(name); |
186 |
if (file.exists() || content==null) { |
187 |
return; |
188 |
} |
189 |
|
190 |
FileOutputStream fw = null; |
191 |
try { |
192 |
fw = new FileOutputStream(file); |
193 |
fw.write(content.getBytes("utf-8")); //NOI18N |
194 |
} catch (IOException e) { |
195 |
err.log(Level.INFO, "Can`t write license " + name + " to " + file, e); |
196 |
} finally { |
197 |
if (fw != null) { |
198 |
try { |
199 |
fw.flush(); |
200 |
fw.close(); |
201 |
} catch (IOException e) { |
202 |
err.log(Level.INFO, "Can`t output stream for " + file, e); |
203 |
} |
204 |
} |
205 |
} |
206 |
} |
207 |
} |
140 |
|
208 |
|
141 |
private void copy (final URL sourceUrl, final File cache) throws IOException { |
209 |
private void copyCatalog (final URL sourceUrl, final File cache) throws IOException { |
142 |
// -- create NetworkListener |
210 |
// -- create NetworkListener |
143 |
// -- request stream |
211 |
// -- request stream |
144 |
// -- report success or IOException |
212 |
// -- report success or IOException |
Lines 151-274
Link Here
|
151 |
prefix += cache.getName(); |
219 |
prefix += cache.getName(); |
152 |
} |
220 |
} |
153 |
final File temp = File.createTempFile (prefix, null, cache.getParentFile ()); //NOI18N |
221 |
final File temp = File.createTempFile (prefix, null, cache.getParentFile ()); //NOI18N |
154 |
temp.deleteOnExit(); |
222 |
temp.deleteOnExit(); |
155 |
storeException (null); |
|
|
156 |
|
223 |
|
157 |
NetworkAccess.NetworkListener nwl = new NetworkAccess.NetworkListener () { |
224 |
DownloadCatalogListener nwl = new DownloadCatalogListener(sourceUrl, cache, temp); |
158 |
|
|
|
159 |
public void streamOpened (InputStream stream, int contentLength) { |
160 |
err.log (Level.FINE, "Successfully started reading URI " + sourceUrl); |
161 |
try { |
162 |
doCopy (sourceUrl, stream, cache, temp, contentLength); |
163 |
} catch (IOException ex) { |
164 |
storeException (ex); |
165 |
} |
166 |
} |
167 |
|
168 |
public void accessCanceled () { |
169 |
err.log (Level.FINE, "Processing " + sourceUrl + " was cancelled."); |
170 |
storeException (new IOException ("Processing " + sourceUrl + " was cancelled.")); |
171 |
} |
172 |
|
173 |
public void accessTimeOut () { |
174 |
err.log (Level.FINE, "Timeout when processing " + sourceUrl); |
175 |
storeException (new IOException ("Timeout when processing " + sourceUrl)); |
176 |
} |
177 |
|
178 |
public void notifyException (Exception x){ |
179 |
err.log (Level.INFO, |
180 |
"Reading URL " + sourceUrl + " failed (" + x + |
181 |
")"); |
182 |
storeException (x); |
183 |
} |
184 |
|
185 |
}; |
186 |
|
225 |
|
187 |
NetworkAccess.Task task = NetworkAccess.createNetworkAcessTask (sourceUrl, AutoupdateSettings.getOpenConnectionTimeout (), nwl); |
226 |
NetworkAccess.Task task = NetworkAccess.createNetworkAcessTask (sourceUrl, AutoupdateSettings.getOpenConnectionTimeout (), nwl); |
188 |
task.waitFinished (); |
227 |
task.waitFinished (); |
189 |
notifyException (); |
228 |
nwl.notifyException (); |
190 |
} |
229 |
} |
191 |
|
230 |
|
192 |
private void notifyException () throws IOException { |
231 |
private String copyLicense (final URL sourceUrl, final File cache) throws IOException { |
193 |
if (isExceptionStored ()) { |
232 |
// -- create NetworkListener |
194 |
throw new IOException (getStoredException ().getLocalizedMessage ()); |
233 |
// -- request stream |
|
|
234 |
// -- report success or IOException |
235 |
// -- if success then do copy |
236 |
|
237 |
err.log(Level.INFO, "Processing URL: " + sourceUrl); // NOI18N |
238 |
|
239 |
String prefix = ""; |
240 |
while (prefix.length () < 3) { |
241 |
prefix += cache.getName(); |
195 |
} |
242 |
} |
|
|
243 |
final File temp = File.createTempFile (prefix, null, cache.getParentFile ()); //NOI18N |
244 |
temp.deleteOnExit(); |
245 |
|
246 |
DownloadLicenseListener nwl = new DownloadLicenseListener(sourceUrl, cache, temp); |
247 |
|
248 |
NetworkAccess.Task task = NetworkAccess.createNetworkAcessTask (sourceUrl, AutoupdateSettings.getOpenConnectionTimeout (), nwl); |
249 |
task.waitFinished (); |
250 |
nwl.notifyException (); |
251 |
return nwl.getLicense(); |
196 |
} |
252 |
} |
197 |
|
253 |
|
198 |
private boolean isExceptionStored () { |
|
|
199 |
return storedException != null; |
200 |
} |
201 |
|
202 |
private void storeException (Exception x) { |
203 |
storedException = x; |
204 |
} |
205 |
|
206 |
private Exception getStoredException () { |
207 |
return storedException; |
208 |
} |
209 |
|
210 |
private void doCopy (URL sourceUrl, InputStream is, File cache, File temp, int contentLength) throws IOException { |
211 |
|
212 |
OutputStream os = null; |
213 |
int read = 0; |
214 |
int totalRead = 0; |
215 |
|
216 |
try { |
217 |
os = new BufferedOutputStream(new FileOutputStream (temp)); |
218 |
byte [] bytes = new byte [1024]; |
219 |
while ((read = is.read (bytes)) != -1) { |
220 |
os.write (bytes, 0, read); |
221 |
totalRead+=read; |
222 |
} |
223 |
is.close (); |
224 |
os.flush (); |
225 |
os.close (); |
226 |
os = null; |
227 |
if(contentLength!=-1 && contentLength!=totalRead) { |
228 |
err.log(Level.INFO, "Content length was reported as " + contentLength + " bytes, but read " + totalRead + " bytes from " + sourceUrl); |
229 |
throw new IOException("unexpected closed connection to " + sourceUrl); |
230 |
} |
231 |
if(totalRead==0) { |
232 |
err.log(Level.INFO, "Connection content length was " + contentLength + " bytes (read " + totalRead + "bytes), catalog size can`t be that size - likely server with catalog at " + sourceUrl + " is temporary down"); |
233 |
throw new IOException("zero sized catalog reported at " + sourceUrl); |
234 |
} |
235 |
synchronized (this) { |
236 |
if (cache.exists () && ! cache.delete ()) { |
237 |
err.log (Level.INFO, "Cannot delete cache " + cache); |
238 |
try { |
239 |
Thread.sleep(200); |
240 |
} catch (InterruptedException ie) { |
241 |
assert false : ie; |
242 |
} |
243 |
cache.delete(); |
244 |
} |
245 |
} |
246 |
err.log (Level.INFO, "Read " + totalRead + " bytes from catalog at " + sourceUrl); |
247 |
|
248 |
if(temp.length()==0) { |
249 |
err.log (Level.INFO, "Temp cache size is zero bytes"); |
250 |
} |
251 |
if (! temp.renameTo (cache)) { |
252 |
err.log (Level.INFO, "Cannot rename temp " + temp + " to cache " + cache); |
253 |
} |
254 |
if(cache.exists() && cache.length()==0) { |
255 |
err.log (Level.INFO, "Final cache size is zero bytes"); |
256 |
} |
257 |
} catch (IOException ioe) { |
258 |
err.log (Level.INFO, "Writing content of URL " + sourceUrl + " failed.", ioe); |
259 |
throw ioe; |
260 |
} finally { |
261 |
try { |
262 |
if (is != null) is.close (); |
263 |
if (os != null) { |
264 |
os.flush (); |
265 |
os.close (); |
266 |
} |
267 |
} catch (IOException ioe) { |
268 |
err.log (Level.INFO, "Closing streams failed.", ioe); |
269 |
} |
270 |
} |
271 |
|
272 |
} |
273 |
|
254 |
|
274 |
} |
255 |
} |