Lines 41-52
Link Here
|
41 |
|
41 |
|
42 |
package org.netbeans.modules.autoupdate.updateprovider; |
42 |
package org.netbeans.modules.autoupdate.updateprovider; |
43 |
|
43 |
|
44 |
import java.io.BufferedOutputStream; |
|
|
45 |
import java.io.File; |
44 |
import java.io.File; |
|
|
45 |
import java.io.FileInputStream; |
46 |
import java.io.FileOutputStream; |
46 |
import java.io.FileOutputStream; |
47 |
import java.io.IOException; |
47 |
import java.io.IOException; |
48 |
import java.io.InputStream; |
|
|
49 |
import java.io.OutputStream; |
50 |
import java.net.MalformedURLException; |
48 |
import java.net.MalformedURLException; |
51 |
import java.net.URL; |
49 |
import java.net.URL; |
52 |
import java.util.logging.Level; |
50 |
import java.util.logging.Level; |
Lines 57-66
Link Here
|
57 |
/** |
55 |
/** |
58 |
* |
56 |
* |
59 |
* @author Jiri Rechtacek |
57 |
* @author Jiri Rechtacek |
|
|
58 |
* @author Dmitry Lipin |
60 |
*/ |
59 |
*/ |
61 |
public class AutoupdateCatalogCache { |
60 |
public class AutoupdateCatalogCache { |
62 |
private File cacheDir; |
61 |
private File cacheDir; |
63 |
private Exception storedException; |
62 |
|
64 |
|
63 |
|
65 |
private static AutoupdateCatalogCache INSTANCE; |
64 |
private static AutoupdateCatalogCache INSTANCE; |
66 |
|
65 |
|
Lines 89-94
Link Here
|
89 |
cacheDir = new File(dir, "catalogcache"); // NOI18N |
88 |
cacheDir = new File(dir, "catalogcache"); // NOI18N |
90 |
} |
89 |
} |
91 |
cacheDir.mkdirs(); |
90 |
cacheDir.mkdirs(); |
|
|
91 |
getLicenseDir().mkdirs(); |
92 |
err.log (Level.FINE, "getCacheDirectory: " + cacheDir.getPath ()); |
92 |
err.log (Level.FINE, "getCacheDirectory: " + cacheDir.getPath ()); |
93 |
return; |
93 |
return; |
94 |
} |
94 |
} |
Lines 100-106
Link Here
|
100 |
assert dir != null && dir.exists () : "Cache directory must exist."; |
100 |
assert dir != null && dir.exists () : "Cache directory must exist."; |
101 |
File cache = new File (dir, codeName); |
101 |
File cache = new File (dir, codeName); |
102 |
|
102 |
|
103 |
copy (original, cache); |
103 |
copy(original, cache, false); |
104 |
|
104 |
|
105 |
try { |
105 |
try { |
106 |
url = cache.toURI ().toURL (); |
106 |
url = cache.toURI ().toURL (); |
Lines 137-144
Link Here
|
137 |
return null; |
137 |
return null; |
138 |
} |
138 |
} |
139 |
} |
139 |
} |
|
|
140 |
private File getLicenseDir() { |
141 |
return new File(getCatalogCache(), "licenses"); |
142 |
} |
143 |
|
144 |
private File getLicenseFile(String name) { |
145 |
return new File(getLicenseDir(), name); |
146 |
} |
147 |
|
148 |
public String getLicense(String name) { |
149 |
return getLicense(name, null); |
150 |
} |
151 |
|
152 |
public String getLicense(String name, URL url) { |
153 |
synchronized (name.intern()) { |
154 |
File file = getLicenseFile(name); |
155 |
if (!file.exists()) { |
156 |
if (url == null) { |
157 |
return null; |
158 |
} |
159 |
try { |
160 |
copy(url, file, true); |
161 |
} catch (IOException e) { |
162 |
// if can`t get the license, treat it as empty but delete it on exit |
163 |
err.log(Level.INFO, "Can`t store license from " + url + " to " + file, e); |
164 |
try { |
165 |
if (file.exists()) { |
166 |
file.delete(); |
167 |
} |
168 |
file.createNewFile(); |
169 |
//in case of error remove the license file and try to download it on the next start |
170 |
file.deleteOnExit(); |
171 |
|
172 |
} catch (IOException ex) { |
173 |
err.log(Level.INFO, "Can`t create empty license file", ex); |
174 |
} |
175 |
} |
176 |
} |
177 |
return readFile(file); |
178 |
} |
179 |
} |
180 |
|
181 |
public void storeLicense(String name, String content) { |
182 |
synchronized (name.intern()) { |
183 |
File file = getLicenseFile(name); |
184 |
if (file.exists() || content == null) { |
185 |
return; |
186 |
} |
187 |
writeToFile(content, file); |
188 |
} |
189 |
} |
140 |
|
190 |
|
141 |
private void copy (final URL sourceUrl, final File cache) throws IOException { |
191 |
private String readFile(File file) { |
|
|
192 |
try { |
193 |
FileInputStream fr = new FileInputStream(file); |
194 |
byte[] buffer = new byte[8192]; |
195 |
int n = 0; |
196 |
StringBuilder sb = new StringBuilder(); |
197 |
while ((n = fr.read(buffer)) != -1) { |
198 |
sb.append(new String(buffer, 0, n, "utf-8"));//NOI18N |
199 |
} |
200 |
return sb.toString(); |
201 |
} catch (IOException e) { |
202 |
err.log(Level.INFO, "Can`t read license from file " + file, e); |
203 |
return null; |
204 |
} |
205 |
} |
206 |
private void writeToFile(String content, File file) { |
207 |
FileOutputStream fw = null; |
208 |
try { |
209 |
fw = new FileOutputStream(file); |
210 |
fw.write(content.getBytes("utf-8")); //NOI18N |
211 |
} catch (IOException e) { |
212 |
err.log(Level.INFO, "Can`t write to " + file, e); |
213 |
} finally { |
214 |
if (fw != null) { |
215 |
try { |
216 |
fw.flush(); |
217 |
fw.close(); |
218 |
} catch (IOException e) { |
219 |
err.log(Level.INFO, "Can`t output stream for " + file, e); |
220 |
} |
221 |
} |
222 |
} |
223 |
} |
224 |
|
225 |
private void copy (final URL sourceUrl, final File cache, final boolean allowZeroSize) throws IOException { |
142 |
// -- create NetworkListener |
226 |
// -- create NetworkListener |
143 |
// -- request stream |
227 |
// -- request stream |
144 |
// -- report success or IOException |
228 |
// -- report success or IOException |
Lines 151-274
Link Here
|
151 |
prefix += cache.getName(); |
235 |
prefix += cache.getName(); |
152 |
} |
236 |
} |
153 |
final File temp = File.createTempFile (prefix, null, cache.getParentFile ()); //NOI18N |
237 |
final File temp = File.createTempFile (prefix, null, cache.getParentFile ()); //NOI18N |
154 |
temp.deleteOnExit(); |
238 |
temp.deleteOnExit(); |
155 |
storeException (null); |
|
|
156 |
|
239 |
|
157 |
NetworkAccess.NetworkListener nwl = new NetworkAccess.NetworkListener () { |
240 |
DownloadListener nwl = new DownloadListener(sourceUrl, cache, temp,allowZeroSize); |
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 |
|
241 |
|
187 |
NetworkAccess.Task task = NetworkAccess.createNetworkAcessTask (sourceUrl, AutoupdateSettings.getOpenConnectionTimeout (), nwl); |
242 |
NetworkAccess.Task task = NetworkAccess.createNetworkAcessTask (sourceUrl, AutoupdateSettings.getOpenConnectionTimeout (), nwl); |
188 |
task.waitFinished (); |
243 |
task.waitFinished (); |
189 |
notifyException (); |
244 |
nwl.notifyException (); |
190 |
} |
245 |
} |
191 |
|
|
|
192 |
private void notifyException () throws IOException { |
193 |
if (isExceptionStored ()) { |
194 |
throw new IOException (getStoredException ().getLocalizedMessage ()); |
195 |
} |
196 |
} |
197 |
|
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 |
|
274 |
} |
246 |
} |