Revision 43345 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/java/org/gvsig/installer/lib/impl/execution/DefaultInstallPackageService.java
DefaultInstallPackageService.java | ||
---|---|---|
28 | 28 |
|
29 | 29 |
import java.io.BufferedInputStream; |
30 | 30 |
import java.io.File; |
31 |
import java.io.FileFilter; |
|
32 | 31 |
import java.io.FileInputStream; |
33 | 32 |
import java.io.FileNotFoundException; |
34 | 33 |
import java.io.IOException; |
... | ... | |
36 | 35 |
import java.net.MalformedURLException; |
37 | 36 |
import java.net.URL; |
38 | 37 |
import java.util.ArrayList; |
38 |
import java.util.Collection; |
|
39 | 39 |
import java.util.Collections; |
40 | 40 |
import java.util.HashMap; |
41 | 41 |
import java.util.HashSet; |
... | ... | |
43 | 43 |
import java.util.Map; |
44 | 44 |
import java.util.Properties; |
45 | 45 |
import java.util.Set; |
46 |
import org.apache.commons.io.FileUtils; |
|
47 |
import org.apache.commons.io.IOUtils; |
|
46 | 48 |
import org.apache.commons.lang3.StringUtils; |
47 | 49 |
|
48 | 50 |
import org.slf4j.Logger; |
... | ... | |
60 | 62 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
61 | 63 |
import org.gvsig.installer.lib.spi.InstallerProviderManager; |
62 | 64 |
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider; |
63 |
import org.gvsig.tools.exception.BaseException; |
|
64 | 65 |
import org.gvsig.tools.service.Manager; |
65 | 66 |
import org.gvsig.tools.service.ServiceException; |
66 | 67 |
import org.gvsig.tools.task.SimpleTaskStatus; |
... | ... | |
76 | 77 |
|
77 | 78 |
private static final String PACKAGE_FILE_NAME = "packages.gvspki"; |
78 | 79 |
|
79 |
private Map<PackageInfo, File> packageInfoFileMap = null;
|
|
80 |
private Map<PackageInfo, String> zipEntriesMap = null; |
|
81 |
private List<PackageInfo> packageInfos = null; |
|
80 |
private PackagesCache packagesCache = null;
|
|
81 |
// private Map<PackageInfo, String> zipEntriesMap = null;
|
|
82 |
// private List<PackageInfo> packageInfos = null;
|
|
82 | 83 |
private InstallerManager manager; |
83 | 84 |
private InstallPackageProviderServices installerProviderServices = null; |
84 | 85 |
|
... | ... | |
89 | 90 |
} |
90 | 91 |
|
91 | 92 |
public void reset() { |
92 |
packageInfoFileMap = new HashMap<PackageInfo, File>(); |
|
93 |
packageInfos = new ArrayList<PackageInfo>(); |
|
94 |
zipEntriesMap = new HashMap<PackageInfo, String>(); |
|
93 |
// packageInfos = new ArrayList<PackageInfo>(); |
|
94 |
// zipEntriesMap = new HashMap<PackageInfo, String>(); |
|
95 | 95 |
installerProviderServices = InstallerProviderLocator |
96 | 96 |
.getProviderManager().createInstallerProviderServices(); |
97 |
packagesCache = new PackagesCache(installerProviderServices); |
|
97 | 98 |
} |
98 | 99 |
|
99 | 100 |
public class InstallerApplicationDirectoryNotFoundException extends |
... | ... | |
237 | 238 |
|
238 | 239 |
} |
239 | 240 |
|
241 |
@Override |
|
240 | 242 |
public void installPackage(File applicationDirectory, |
241 | 243 |
PackageInfo packageInfo) throws InstallPackageServiceException { |
242 | 244 |
if (!applicationDirectory.exists()) { |
... | ... | |
244 | 246 |
throw new InstallerApplicationDirectoryNotFoundException( |
245 | 247 |
applicationDirectory); |
246 | 248 |
} |
247 |
if (!packageInfoFileMap.containsKey(packageInfo)) {
|
|
249 |
if (!packagesCache.contains(packageInfo)) {
|
|
248 | 250 |
LOG.warn("Can install package '" + packageInfo.getCode() + "', package not found."); |
249 | 251 |
throw new InstallerPackageNotFoundException(); |
250 | 252 |
} |
251 | 253 |
|
252 | 254 |
InstallPackageProvider installerExecutionProvider = createProvider(packageInfo); |
253 | 255 |
|
254 |
// Get the package or package set file |
|
255 |
File file = packageInfoFileMap.get(packageInfo); |
|
256 |
if (file == null) { |
|
257 |
if (packageInfo.getDownloadURL() == null) { |
|
258 |
throw new InstallerPackageNotFoundException(); |
|
259 |
} |
|
260 |
this.downloadPackage(packageInfo); |
|
261 |
file = packageInfoFileMap.get(packageInfo); |
|
262 |
} |
|
263 |
|
|
264 |
// Open and install the package or package set file |
|
256 |
File file = packagesCache.getFile(packageInfo); |
|
265 | 257 |
try { |
266 |
|
|
267 |
InputStream packageStream; |
|
268 |
InputStream fis = new FileInputStream(file); |
|
269 |
InputStream bis = new BufferedInputStream(fis); |
|
270 |
if (isPackage(file)) { |
|
271 |
packageStream = bis; |
|
272 |
} else { |
|
273 |
if (!isPackageSet(file)) { |
|
274 |
LOG.info("Trying to install a package file ({0}) " |
|
275 |
+ "without a known file extension. Will try " |
|
276 |
+ "to install it as a package set", file); |
|
277 |
} |
|
278 |
packageStream = installerProviderServices.searchPackage(bis, |
|
279 |
zipEntriesMap.get(packageInfo)); |
|
280 |
} |
|
281 |
|
|
258 |
InputStream packageStream = this.packagesCache.getInputStream(packageInfo); |
|
282 | 259 |
try { |
283 | 260 |
installerExecutionProvider.install(applicationDirectory, |
284 | 261 |
packageStream, packageInfo); |
285 | 262 |
} catch (InstallPackageServiceException e) { |
286 |
|
|
287 |
packageStream.close(); |
|
288 |
if (bis != packageStream) { |
|
289 |
bis.close(); |
|
290 |
} |
|
291 |
fis.close(); |
|
292 |
|
|
293 |
// if fails the installation, zip files need to be reopen |
|
294 |
// the package will be installed into the update folder for |
|
295 |
// installation after a gvSIG restart |
|
296 |
fis = new FileInputStream(file); |
|
297 |
bis = new BufferedInputStream(fis); |
|
298 |
if (isPackage(file)) { |
|
299 |
packageStream = bis; |
|
300 |
} else { |
|
301 |
if (!isPackageSet(file)) { |
|
302 |
LOG.info("Trying to install a package file ({0}) " |
|
303 |
+ "without a known file extension. Will try " |
|
304 |
+ "to install it as a package set", file); |
|
305 |
} |
|
306 |
packageStream = installerProviderServices.searchPackage( |
|
307 |
bis, zipEntriesMap.get(packageInfo)); |
|
308 |
} |
|
263 |
IOUtils.closeQuietly(packageStream); |
|
264 |
packageStream = this.packagesCache.getInputStream(packageInfo); |
|
309 | 265 |
installerExecutionProvider.installLater(applicationDirectory, |
310 | 266 |
packageStream, packageInfo); |
267 |
} finally { |
|
268 |
IOUtils.closeQuietly(packageStream); |
|
311 | 269 |
} |
312 | 270 |
|
313 |
packageStream.close(); |
|
314 |
if (bis != packageStream) { |
|
315 |
bis.close(); |
|
316 |
} |
|
317 |
fis.close(); |
|
318 |
|
|
319 | 271 |
} catch (FileNotFoundException e) { |
320 | 272 |
throw new InstallerFileNotFoundException(file); |
321 | 273 |
} catch (IOException e) { |
... | ... | |
324 | 276 |
|
325 | 277 |
} |
326 | 278 |
|
279 |
@Override |
|
327 | 280 |
public void installPackage(File applicationDirectory, String packageCode) |
328 | 281 |
throws InstallPackageServiceException { |
329 | 282 |
PackageInfo packageInfo = getPackageInfo(packageCode); |
... | ... | |
346 | 299 |
} |
347 | 300 |
} |
348 | 301 |
|
302 |
@Override |
|
349 | 303 |
public PackageInfo getPackageInfo(int index) { |
350 |
if (index >= packageInfos.size()) {
|
|
304 |
if (index >= packagesCache.size()) {
|
|
351 | 305 |
return null; |
352 | 306 |
} |
353 |
return packageInfos.get(index);
|
|
307 |
return packagesCache.get(index);
|
|
354 | 308 |
} |
355 | 309 |
|
310 |
@Override |
|
356 | 311 |
public PackageInfo getPackageInfo(String packageCode) { |
357 |
for (int i = 0; i < getPackageCount(); i++) {
|
|
358 |
if (packageInfos.get(i).getCode().equals(packageCode)) {
|
|
359 |
return packageInfos.get(i);
|
|
312 |
for( PackageInfo packageInfo : packagesCache ) {
|
|
313 |
if (packageInfo.getCode().equals(packageCode)) { |
|
314 |
return packageInfo; |
|
360 | 315 |
} |
361 | 316 |
} |
362 | 317 |
return null; |
... | ... | |
364 | 319 |
|
365 | 320 |
@Override |
366 | 321 |
public void addBundle(File bundle, SimpleTaskStatus taskStatus) throws InstallPackageServiceException { |
367 |
|
|
322 |
Map<PackageInfo, String> zipEntriesNames = new HashMap<>(); |
|
323 |
List<PackageInfo> packages = new ArrayList<>(); |
|
324 |
|
|
368 | 325 |
if (!bundle.exists()) { |
369 | 326 |
throw new InstallPackageServiceException(); |
370 | 327 |
} |
371 | 328 |
|
372 |
int packageInfoCount = packageInfos.size(); |
|
373 |
|
|
374 |
FileInputStream fis; |
|
329 |
BufferedInputStream bis = null; |
|
330 |
FileInputStream fis = null; |
|
375 | 331 |
try { |
376 | 332 |
fis = new FileInputStream(bundle); |
333 |
bis = new BufferedInputStream(fis); |
|
334 |
installerProviderServices.readPackageInfo( |
|
335 |
bis, packages, zipEntriesNames, bundle.getName(), taskStatus |
|
336 |
); |
|
377 | 337 |
} catch (FileNotFoundException e) { |
378 | 338 |
throw new InstallerBundleNotFoundException(bundle, e); |
339 |
} finally { |
|
340 |
IOUtils.closeQuietly(bis); |
|
341 |
IOUtils.closeQuietly(fis); |
|
342 |
bis = null; |
|
343 |
fis = null; |
|
379 | 344 |
} |
380 |
BufferedInputStream bis = new BufferedInputStream(fis); |
|
381 |
if (isPackage(bundle)) { |
|
382 |
installerProviderServices.readPackageInfo(bis, packageInfos, |
|
383 |
zipEntriesMap, bundle.getName(), taskStatus); |
|
384 |
} else { |
|
385 |
if (!isPackageSet(bundle)) { |
|
386 |
LOG |
|
387 |
.info( |
|
388 |
"Trying to add a package file ({0}) without a known " |
|
389 |
+ "file extension. Will try to add it as a package set", |
|
390 |
bundle); |
|
345 |
|
|
346 |
if( packages.size()==1 ) { |
|
347 |
PackageInfo pkg = packages.get(0); |
|
348 |
String entryName = zipEntriesNames.get(pkg); |
|
349 |
if( entryName.endsWith("."+manager.getDefaultPackageFileExtension()) ) { |
|
350 |
// Es un xxx.gvspkg |
|
351 |
packagesCache.addPackage(pkg, bundle); |
|
352 |
return; |
|
353 |
} else if( entryName.endsWith("."+manager.getDefaultIndexSetFileExtension()) ) { |
|
354 |
// Es un xxx.gvspki |
|
355 |
packagesCache.addPackageIndex(pkg); |
|
356 |
} else { |
|
357 |
throw new IllegalArgumentException("Can't recognize the file '"+bundle.getAbsolutePath()+"'."); |
|
391 | 358 |
} |
392 |
installerProviderServices.readPackageSetInfo(fis, packageInfos, |
|
393 |
zipEntriesMap, taskStatus); |
|
394 | 359 |
} |
360 |
|
|
361 |
packages.clear(); |
|
362 |
zipEntriesNames.clear(); |
|
395 | 363 |
try { |
396 |
bis.close(); |
|
397 |
fis.close(); |
|
398 |
} catch (IOException e) { |
|
399 |
LOG.info("Error closing the input streams of the package file: " |
|
400 |
+ bundle, e); |
|
364 |
fis = new FileInputStream(bundle); |
|
365 |
bis = new BufferedInputStream(fis); |
|
366 |
installerProviderServices.readPackageSetInfo( |
|
367 |
bis, packages, zipEntriesNames, taskStatus |
|
368 |
); |
|
369 |
} catch (FileNotFoundException e) { |
|
370 |
throw new InstallerBundleNotFoundException(bundle, e); |
|
371 |
} finally { |
|
372 |
IOUtils.closeQuietly(bis); |
|
373 |
IOUtils.closeQuietly(fis); |
|
374 |
bis = null; |
|
375 |
fis = null; |
|
401 | 376 |
} |
402 |
|
|
403 |
for (int i = packageInfoCount; i < packageInfos.size(); i++) { |
|
404 |
packageInfoFileMap.put(packageInfos.get(i), bundle); |
|
377 |
for( PackageInfo pkg : packages ) { |
|
378 |
String entryName = zipEntriesNames.get(pkg); |
|
379 |
if( entryName.endsWith("."+manager.getDefaultPackageFileExtension()) ) { |
|
380 |
// Es un xxx.gvspkg |
|
381 |
packagesCache.addBundledPackage(pkg, bundle, entryName); |
|
382 |
} else if( entryName.endsWith("."+manager.getDefaultIndexSetFileExtension()) ) { |
|
383 |
// Es un xxx.gvspki |
|
384 |
packagesCache.addBundledPackageIndex(pkg, bundle, entryName); |
|
385 |
} else { |
|
386 |
throw new IllegalArgumentException("Can't recognize the file '"+bundle.getAbsolutePath()+"'."); |
|
387 |
} |
|
405 | 388 |
} |
406 | 389 |
} |
407 | 390 |
|
408 |
private boolean isPackageSet(File file) { |
|
409 |
return file.getName().endsWith( |
|
410 |
manager.getDefaultPackageSetFileExtension()); |
|
411 |
} |
|
412 |
|
|
413 |
private boolean isPackage(File file) { |
|
414 |
return file.getName() |
|
415 |
.endsWith(manager.getDefaultPackageFileExtension()); |
|
416 |
} |
|
417 |
|
|
418 | 391 |
@Override |
419 | 392 |
public void addBundle(URL bundleURL, SimpleTaskStatus taskStatus) throws InstallPackageServiceException { |
420 | 393 |
File bundle; |
... | ... | |
465 | 438 |
if (!directory.isDirectory()) { |
466 | 439 |
throw new InstallerNoDirectoryException(directory); |
467 | 440 |
} |
468 |
List<File> files = new ArrayList<File>(); |
|
469 |
|
|
470 |
listRecursively(directory, new FileFilter() { |
|
471 |
|
|
472 |
private String packageExt = manager |
|
473 |
.getDefaultPackageFileExtension(); |
|
474 |
private String packageSetExt = manager |
|
475 |
.getDefaultPackageSetFileExtension(); |
|
476 |
|
|
477 |
public boolean accept(File file) { |
|
478 |
String name = file.getName().toLowerCase(); |
|
479 |
return file.isDirectory() || name.endsWith(packageExt) |
|
480 |
|| name.endsWith(packageSetExt); |
|
481 |
} |
|
482 |
}, files); |
|
483 |
for (int i = 0; i < files.size(); i++) { |
|
484 |
if (files.get(i).isFile()) { |
|
485 |
addBundle(files.get(i),taskStatus); |
|
486 |
} |
|
441 |
Collection<File> files = FileUtils.listFiles( |
|
442 |
directory, |
|
443 |
new String[] { |
|
444 |
manager.getDefaultPackageFileExtension(), |
|
445 |
manager.getDefaultPackageSetFileExtension(), |
|
446 |
manager.getDefaultIndexSetFileExtension() |
|
447 |
}, |
|
448 |
true |
|
449 |
); |
|
450 |
for( File file : files ) { |
|
451 |
addBundle(file,taskStatus); |
|
487 | 452 |
} |
488 | 453 |
} |
489 | 454 |
|
490 |
private void listRecursively(File fileOrDir, FileFilter filter, |
|
491 |
List<File> files) { |
|
492 |
files.add(fileOrDir); |
|
493 |
|
|
494 |
if (fileOrDir.isDirectory()) { |
|
495 |
|
|
496 |
File[] dirContents = fileOrDir.listFiles(filter); |
|
497 |
|
|
498 |
for (File f : dirContents) { |
|
499 |
listRecursively(f, filter, files); // Recursively list. |
|
500 |
} |
|
501 |
} else { |
|
502 |
files.add(fileOrDir); |
|
503 |
} |
|
504 |
} |
|
505 |
|
|
455 |
@Override |
|
506 | 456 |
public int getPackageCount() { |
507 |
if (packageInfos == null) {
|
|
457 |
if (packagesCache == null) {
|
|
508 | 458 |
return 0; |
509 | 459 |
} |
510 |
return packageInfos.size();
|
|
460 |
return packagesCache.size();
|
|
511 | 461 |
} |
512 | 462 |
|
463 |
@Override |
|
513 | 464 |
public Manager getManager() { |
514 | 465 |
return this.manager; |
515 | 466 |
} |
... | ... | |
519 | 470 |
this.downloadPackage(packageInfo, null); |
520 | 471 |
} |
521 | 472 |
|
473 |
@Override |
|
522 | 474 |
public void downloadPackage(PackageInfo packageInfo, |
523 | 475 |
SimpleTaskStatus taskStatus) throws InstallPackageServiceException { |
524 |
|
|
525 |
File file = packageInfoFileMap.get(packageInfo); |
|
526 |
if (file == null) { |
|
527 |
try { |
|
528 |
file = packageInfo.downloadFile(taskStatus); |
|
529 |
} catch (BaseException e) { |
|
530 |
throw new InstallPackageServiceException(e); |
|
531 |
} |
|
532 |
this.packageInfoFileMap.put(packageInfo, file); |
|
533 |
} |
|
476 |
packagesCache.download(packageInfo, taskStatus); |
|
534 | 477 |
} |
535 | 478 |
|
479 |
@Override |
|
536 | 480 |
public List<String> getDefaultSelectedPackagesIDs() { |
537 | 481 |
return installerProviderServices.getDefaultSelectedPackagesIDs(); |
538 | 482 |
} |
... | ... | |
542 | 486 |
return installerProviderServices.getProperties(); |
543 | 487 |
} |
544 | 488 |
|
489 |
@Override |
|
545 | 490 |
public List<String> getCategories() { |
546 |
Set<String> categories = new HashSet<String>(); |
|
547 |
|
|
548 |
for (int i = 0; i < packageInfos.size(); i++) { |
|
549 |
PackageInfo pkginfo = packageInfos.get(i); |
|
491 |
Set<String> categories = new HashSet<>(); |
|
492 |
for( PackageInfo pkginfo : packagesCache ) { |
|
550 | 493 |
List<String> pkgcategories = pkginfo.getCategories(); |
551 | 494 |
categories.addAll(pkgcategories); |
552 | 495 |
} |
553 | 496 |
try { |
554 | 497 |
PackageInfo[] pkgs = manager.getInstalledPackages(); |
555 |
for (int i = 0; i < pkgs.length; i++) { |
|
556 |
PackageInfo pkginfo = pkgs[i]; |
|
498 |
for( PackageInfo pkginfo : pkgs ) { |
|
557 | 499 |
List<String> pkgcategories = pkginfo.getCategories(); |
558 | 500 |
categories.addAll(pkgcategories); |
559 | 501 |
} |
... | ... | |
561 | 503 |
} catch (MakePluginPackageServiceException e) { |
562 | 504 |
// Ignore exceptions |
563 | 505 |
} |
564 |
ArrayList<String> l = new ArrayList<String>(categories);
|
|
506 |
ArrayList<String> l = new ArrayList<>(categories); |
|
565 | 507 |
Collections.sort(l); |
566 | 508 |
return l; |
567 | 509 |
} |
568 | 510 |
|
511 |
@Override |
|
569 | 512 |
public List<String> getTypes() { |
570 |
Set<String> types = new HashSet<String>();
|
|
513 |
Set<String> types = new HashSet<>(); |
|
571 | 514 |
|
572 |
for (int i = 0; i < packageInfos.size(); i++) { |
|
573 |
PackageInfo pkginfo = packageInfos.get(i); |
|
515 |
for( PackageInfo pkginfo : packagesCache ) { |
|
574 | 516 |
types.add(pkginfo.getType()); |
575 | 517 |
} |
576 | 518 |
try { |
577 | 519 |
PackageInfo[] pkgs = manager.getInstalledPackages(); |
578 |
for (int i = 0; i < pkgs.length; i++) { |
|
579 |
PackageInfo pkginfo = pkgs[i]; |
|
520 |
for( PackageInfo pkginfo : pkgs ) { |
|
580 | 521 |
types.add(pkginfo.getType()); |
581 | 522 |
} |
582 | 523 |
|
Also available in: Unified diff