root / tags / v1_0_2_Build_895 / frameworks / _fwAndami / src / com / iver / andami / Utilities.java @ 10328
History | View | Annotate | Download (11.7 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.andami; |
42 |
|
43 |
import java.awt.Component; |
44 |
import java.awt.Container; |
45 |
import java.io.BufferedInputStream; |
46 |
import java.io.BufferedOutputStream; |
47 |
import java.io.DataOutputStream; |
48 |
import java.io.File; |
49 |
import java.io.FileOutputStream; |
50 |
import java.io.IOException; |
51 |
import java.io.InputStream; |
52 |
import java.io.OutputStream; |
53 |
import java.net.ConnectException; |
54 |
import java.net.MalformedURLException; |
55 |
import java.net.URL; |
56 |
import java.net.UnknownHostException; |
57 |
import java.util.Enumeration; |
58 |
import java.util.Hashtable; |
59 |
import java.util.zip.ZipEntry; |
60 |
import java.util.zip.ZipException; |
61 |
import java.util.zip.ZipFile; |
62 |
|
63 |
import javax.swing.ImageIcon; |
64 |
import javax.swing.RootPaneContainer; |
65 |
|
66 |
import org.apache.log4j.Logger; |
67 |
|
68 |
import com.iver.andami.ui.mdiManager.IWindow; |
69 |
import com.iver.andami.ui.splash.MultiSplashWindow; |
70 |
|
71 |
|
72 |
|
73 |
/**
|
74 |
* Clase de utilidad
|
75 |
*
|
76 |
* @version $Revision: 10328 $
|
77 |
*/
|
78 |
public class Utilities { |
79 |
/**
|
80 |
* <b>key</b>: URL, <b>value</b>: path to the downloaded file.
|
81 |
*/
|
82 |
private static Hashtable downloadedFiles; |
83 |
/** DOCUMENT ME! */
|
84 |
private static Logger logger = Logger.getLogger(Utilities.class.getName()); |
85 |
private static final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"/tmp-andami"; |
86 |
|
87 |
|
88 |
/**
|
89 |
* Crea un icono a partir del path de una imagen
|
90 |
*
|
91 |
* @param path Path de la imagen dentro del jar de la aplicaci?n
|
92 |
*
|
93 |
* @return ImageIcon si encuentra la imagen y null si no la encuentra
|
94 |
*/
|
95 |
public static ImageIcon createImageIcon(String path) { |
96 |
URL imgURL = null; |
97 |
|
98 |
try {
|
99 |
imgURL = new URL("file:" + path); |
100 |
} catch (MalformedURLException e) { |
101 |
e.printStackTrace(); |
102 |
} |
103 |
|
104 |
if (imgURL != null) { |
105 |
return new ImageIcon(imgURL); |
106 |
} else {
|
107 |
return null; |
108 |
} |
109 |
} |
110 |
|
111 |
/**
|
112 |
* M?todo que libera la memoria de los jInternalFrame
|
113 |
*
|
114 |
* @param baseComponent JInternalFrame cuya memoria se quiere eliminar
|
115 |
*/
|
116 |
public static void cleanComponent(Component baseComponent) { |
117 |
try {
|
118 |
cleanComponent(baseComponent, 0);
|
119 |
} catch (Exception ignore) { // give some exception handling... |
120 |
} |
121 |
} |
122 |
|
123 |
/* * The "depth" parameter was being used for text output debugging. * But isn't essential now. I'll keep it anyways, as it avoids * calling the garbage collector every recursion. */
|
124 |
protected static void cleanComponent(Component baseComponent, int depth) { |
125 |
if (baseComponent == null) // recursion terminating clause |
126 |
{ |
127 |
return;
|
128 |
} |
129 |
|
130 |
if (baseComponent instanceof IWindow){ |
131 |
return;
|
132 |
} |
133 |
|
134 |
Container cont;
|
135 |
Component[] childComponents; |
136 |
int numChildren; // clean up component containers |
137 |
|
138 |
if (baseComponent instanceof Container) { // now clean up container instance variables |
139 |
|
140 |
if (baseComponent instanceof RootPaneContainer) { // Swing specialised container |
141 |
cont = (Container) baseComponent;
|
142 |
numChildren = cont.getComponentCount(); |
143 |
childComponents = cont.getComponents(); |
144 |
|
145 |
for (int i = 0; i < numChildren; i++) { // remove each component from the current container |
146 |
|
147 |
// each child component may be a container itself
|
148 |
cleanComponent(childComponents[i], depth + 1);
|
149 |
((RootPaneContainer) cont).getContentPane().remove(childComponents[i]);
|
150 |
} |
151 |
|
152 |
((RootPaneContainer) cont).getContentPane().setLayout(null); |
153 |
} else { // General Swing, and AWT, Containers |
154 |
cont = (Container) baseComponent;
|
155 |
numChildren = cont.getComponentCount(); |
156 |
childComponents = cont.getComponents(); |
157 |
|
158 |
for (int i = 0; i < numChildren; i++) //for(int i = 0;i < numChildren;i++) |
159 |
{ |
160 |
// remove each component from the current container // each child component may be a container itself
|
161 |
cleanComponent(childComponents[i], depth + 1);
|
162 |
cont.remove(childComponents[i]); |
163 |
} |
164 |
|
165 |
cont.setLayout(null);
|
166 |
} |
167 |
} |
168 |
|
169 |
// if component is also a container
|
170 |
} |
171 |
|
172 |
|
173 |
|
174 |
/**
|
175 |
* Extrae un fichero zip en un directorio
|
176 |
*
|
177 |
* @param file fichero comprimido
|
178 |
* @param dir Directorio donde se extraera el fichero
|
179 |
*
|
180 |
* @throws ZipException Si hay un error con el formato del fichero
|
181 |
* @throws IOException Si se produce un error de entrada salida gen?rico
|
182 |
*/
|
183 |
public static void extractTo(File file, File dir, MultiSplashWindow splash) |
184 |
throws ZipException, IOException { |
185 |
ZipFile zip = new ZipFile(file); |
186 |
Enumeration e = zip.entries();
|
187 |
|
188 |
// Pasada para crear las carpetas
|
189 |
while (e.hasMoreElements()) {
|
190 |
ZipEntry entry = (ZipEntry) e.nextElement(); |
191 |
|
192 |
if (entry.isDirectory()) {
|
193 |
File directorio = new File(dir.getAbsolutePath() |
194 |
+ File.separator + entry.getName());
|
195 |
|
196 |
directorio.mkdirs(); |
197 |
} |
198 |
|
199 |
} |
200 |
|
201 |
// Pasada para crear los ficheros
|
202 |
e = zip.entries(); |
203 |
while (e.hasMoreElements()) {
|
204 |
ZipEntry entry = (ZipEntry) e.nextElement(); |
205 |
splash.process(30, "Procesando " + entry.getName() + "..."); |
206 |
if (!entry.isDirectory()) {
|
207 |
InputStream in = zip.getInputStream(entry);
|
208 |
OutputStream out = new FileOutputStream(dir + File.separator |
209 |
+ entry.getName()); |
210 |
BufferedInputStream bin = new BufferedInputStream(in); |
211 |
BufferedOutputStream bout = new BufferedOutputStream(out); |
212 |
|
213 |
int i;
|
214 |
|
215 |
while ((i = bin.read()) != -1) { |
216 |
bout.write(i); |
217 |
} |
218 |
|
219 |
bout.flush(); |
220 |
bout.close(); |
221 |
bin.close(); |
222 |
|
223 |
} |
224 |
|
225 |
} |
226 |
|
227 |
zip.close(); |
228 |
zip = null;
|
229 |
System.gc();
|
230 |
|
231 |
} |
232 |
/**
|
233 |
* Returns the content of this URL as a file from the file system.<br>
|
234 |
* <p>
|
235 |
* If the URL has been already downloaded in this session and notified
|
236 |
* to the system using the static <b>Utilities.addDownloadedURL(URL)</b>
|
237 |
* method, it can be restored faster from the file system avoiding to
|
238 |
* download it again.
|
239 |
* </p>
|
240 |
* @param url
|
241 |
* @return File containing this URL's content or null if no file was found.
|
242 |
*/
|
243 |
private static File getPreviousDownloadedURL(URL url){ |
244 |
File f = null; |
245 |
if (downloadedFiles!=null && downloadedFiles.containsKey(url)){ |
246 |
String filePath = (String) downloadedFiles.get(url); |
247 |
f = new File(filePath); |
248 |
} |
249 |
return f;
|
250 |
} |
251 |
|
252 |
/**
|
253 |
* Adds an URL to the table of downloaded files for further uses. If the URL
|
254 |
* already exists in the table its filePath value is updated to the new one and
|
255 |
* the old file itself is removed from the file system.
|
256 |
*
|
257 |
* @param url
|
258 |
* @param filePath
|
259 |
*/
|
260 |
private static void addDownloadedURL(URL url, String filePath){ |
261 |
if (downloadedFiles==null) |
262 |
downloadedFiles = new Hashtable(); |
263 |
String fileName = (String) downloadedFiles.put(url, filePath); |
264 |
//JMV: No se puede eliminar el anterior porque puede que alguien lo
|
265 |
// este usando
|
266 |
/*
|
267 |
if (fileName!=null){
|
268 |
File f = new File(fileName);
|
269 |
if (f.exists())
|
270 |
f.delete();
|
271 |
}
|
272 |
*/
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* Downloads an URL into a temporary file that is removed the next time the
|
277 |
* tempFileManager class is called, which means the next time gvSIG is launched.
|
278 |
*
|
279 |
* @param url
|
280 |
* @param name
|
281 |
* @return
|
282 |
* @throws IOException
|
283 |
* @throws ServerErrorResponseException
|
284 |
* @throws ConnectException
|
285 |
* @throws UnknownHostException
|
286 |
*/
|
287 |
public static File downloadFile(URL url, String name) throws IOException,ConnectException, UnknownHostException{ |
288 |
File f = null; |
289 |
|
290 |
try{
|
291 |
if ((f=getPreviousDownloadedURL(url))==null){ |
292 |
createTempDirectory(); |
293 |
|
294 |
f = new File(tempDirectoryPath+"/"+name+System.currentTimeMillis()); |
295 |
|
296 |
System.out.println("downloading '"+url.toString()+"' to: "+f.getAbsolutePath()); |
297 |
|
298 |
f.deleteOnExit(); |
299 |
DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f))); |
300 |
byte[] buffer = new byte[1024*256]; |
301 |
InputStream is = url.openStream();
|
302 |
long readed = 0; |
303 |
for (int i = is.read(buffer); i>0; i = is.read(buffer)){ |
304 |
dos.write(buffer, 0, i);
|
305 |
readed += i; |
306 |
} |
307 |
dos.close(); |
308 |
addDownloadedURL(url, f.getAbsolutePath()); |
309 |
} |
310 |
} catch (IOException io) { |
311 |
io.printStackTrace(); |
312 |
} |
313 |
|
314 |
return f;
|
315 |
} |
316 |
|
317 |
/**
|
318 |
* Cleans every temporal file previously downloaded.
|
319 |
*/
|
320 |
public static void cleanUpTempFiles() { |
321 |
try{
|
322 |
File tempDirectory = new File(tempDirectoryPath); |
323 |
|
324 |
File[] files = tempDirectory.listFiles(); |
325 |
if (files!=null) { |
326 |
for (int i = 0; i < files.length; i++) { |
327 |
// s?lo por si en un futuro se necesitan crear directorios temporales
|
328 |
if (files[i].isDirectory()) deleteDirectory(files[i]);
|
329 |
files[i].delete(); |
330 |
} |
331 |
} |
332 |
tempDirectory.delete(); |
333 |
} catch (Exception e) { } |
334 |
|
335 |
} |
336 |
/**
|
337 |
* Recursive directory delete.
|
338 |
* @param f
|
339 |
*/
|
340 |
private static void deleteDirectory(File f) { |
341 |
File[] files = f.listFiles(); |
342 |
for (int i = 0; i < files.length; i++) { |
343 |
if (files[i].isDirectory()) deleteDirectory(files[i]);
|
344 |
files[i].delete(); |
345 |
} |
346 |
|
347 |
} |
348 |
|
349 |
/**
|
350 |
* Crea un fichero temporal con un nombre concreto y unos datos pasados por
|
351 |
* par?metro.
|
352 |
* @param fileName Nombre de fichero
|
353 |
* @param data datos a guardar en el fichero
|
354 |
*/
|
355 |
public static void createTemp(String fileName, String data)throws IOException{ |
356 |
File f = new File(fileName); |
357 |
DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)) ); |
358 |
dos.writeBytes(data); |
359 |
dos.close(); |
360 |
f.deleteOnExit(); |
361 |
} |
362 |
|
363 |
/**
|
364 |
* Remove an URL from the system cache. The file will remain in the file
|
365 |
* system for further eventual uses.
|
366 |
* @param request
|
367 |
*/
|
368 |
public static void removeURL(URL url) { |
369 |
if (downloadedFiles != null && downloadedFiles.containsKey(url)) |
370 |
downloadedFiles.remove(url); |
371 |
} |
372 |
|
373 |
/**
|
374 |
* Esta funci?n crea el directorio para temporales y devuelve el nombre de este para que cualquier
|
375 |
* extensi?n en gvSIG pueda utilizar este directorio para guardar ficheros que se desee eliminar
|
376 |
* al salir
|
377 |
* @return
|
378 |
*/
|
379 |
public static String createTempDirectory(){ |
380 |
File tempDirectory = new File(tempDirectoryPath); |
381 |
if (!tempDirectory.exists())
|
382 |
tempDirectory.mkdir(); |
383 |
return tempDirectoryPath;
|
384 |
} |
385 |
} |