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