1717 * limitations under the License.
1818 */
1919
20- import java .io .Closeable ;
21- import java .io .File ;
22- import java .io .IOException ;
23- import java .lang .reflect .UndeclaredThrowableException ;
24- import java .util .ArrayList ;
25- import java .util .HashMap ;
26- import java .util .HashSet ;
27- import java .util .Iterator ;
28- import java .util .List ;
29- import java .util .Map ;
30- import java .util .NoSuchElementException ;
31- import java .util .Set ;
32-
33- import javax .annotation .Nonnull ;
34-
3520import org .codehaus .plexus .PlexusConstants ;
3621import org .codehaus .plexus .PlexusContainer ;
3722import org .codehaus .plexus .archiver .manager .ArchiverManager ;
4126import org .codehaus .plexus .components .io .attributes .PlexusIoResourceAttributes ;
4227import org .codehaus .plexus .components .io .functions .ResourceAttributeSupplier ;
4328import org .codehaus .plexus .components .io .resources .AbstractPlexusIoResourceCollection ;
29+ import org .codehaus .plexus .components .io .resources .EncodingSupported ;
4430import org .codehaus .plexus .components .io .resources .PlexusIoArchivedResourceCollection ;
4531import org .codehaus .plexus .components .io .resources .PlexusIoFileResourceCollection ;
4632import org .codehaus .plexus .components .io .resources .PlexusIoResource ;
5440import org .codehaus .plexus .personality .plexus .lifecycle .phase .Contextualizable ;
5541import org .codehaus .plexus .util .Os ;
5642
43+ import javax .annotation .Nonnull ;
44+ import java .io .Closeable ;
45+ import java .io .File ;
46+ import java .io .IOException ;
47+ import java .lang .reflect .UndeclaredThrowableException ;
48+ import java .nio .charset .Charset ;
49+ import java .util .ArrayList ;
50+ import java .util .HashMap ;
51+ import java .util .HashSet ;
52+ import java .util .Iterator ;
53+ import java .util .List ;
54+ import java .util .Map ;
55+ import java .util .NoSuchElementException ;
56+ import java .util .Set ;
57+
5758import static org .codehaus .plexus .archiver .util .DefaultArchivedFileSet .archivedFileSet ;
5859import static org .codehaus .plexus .archiver .util .DefaultFileSet .fileSet ;
5960
@@ -100,6 +101,7 @@ public abstract class AbstractArchiver
100101 // On lunix-like systems, we replace windows backslashes with forward slashes
101102 private final boolean replacePathSlashesToJavaPaths = File .separatorChar == '/' ;
102103
104+ private final List <Closeable > closeables = new ArrayList <Closeable >( );
103105 /**
104106 * since 2.2 is on by default
105107 *
@@ -479,6 +481,12 @@ else if ( o instanceof PlexusIoResourceCollection )
479481 }
480482 else
481483 {
484+ // this will leak handles in the IO iterator if the iterator is not fully consumed.
485+ // alternately we'd have to make this method return a Closeable iterator back
486+ // to the client and ditch the whole issue onto the client.
487+ // this does not really make any sense either, might equally well change the
488+ // api into something that is not broken by design.
489+ addCloseable ( ioResourceIter );
482490 ioResourceIter = null ;
483491 }
484492 }
@@ -550,6 +558,33 @@ private String normalizedForDuplicateCheck(ArchiveEntry entry){
550558
551559 }
552560
561+ private static void closeIfCloseable ( Object resource )
562+ throws IOException
563+ {
564+ if ( resource == null )
565+ {
566+ return ;
567+ }
568+ if ( resource instanceof Closeable )
569+ {
570+ ( (Closeable ) resource ).close ();
571+ }
572+
573+ }
574+
575+ private static void closeQuietlyIfCloseable ( Object resource )
576+ {
577+ try
578+ {
579+ closeIfCloseable ( resource );
580+ }
581+ catch ( IOException e )
582+ {
583+ throw new RuntimeException ( e );
584+ }
585+ }
586+
587+
553588 public Map <String , ArchiveEntry > getFiles ()
554589 {
555590 try
@@ -603,7 +638,7 @@ protected Logger getLogger()
603638 return logger ;
604639 }
605640
606- protected PlexusIoResourceCollection asResourceCollection ( final ArchivedFileSet fileSet )
641+ protected PlexusIoResourceCollection asResourceCollection ( final ArchivedFileSet fileSet , Charset charset )
607642 throws ArchiverException
608643 {
609644 final File archiveFile = fileSet .getArchive ();
@@ -619,6 +654,10 @@ protected PlexusIoResourceCollection asResourceCollection( final ArchivedFileSet
619654 "Error adding archived file-set. PlexusIoResourceCollection not found for: " + archiveFile , e );
620655 }
621656
657+ if (resources instanceof EncodingSupported ) {
658+ ((EncodingSupported )resources ).setEncoding ( charset );
659+ }
660+
622661 if ( resources instanceof PlexusIoArchivedResourceCollection )
623662 {
624663 ( (PlexusIoArchivedResourceCollection ) resources ).setFile ( fileSet .getArchive () );
@@ -673,7 +712,14 @@ private void doAddResource(Object item){
673712 public void addArchivedFileSet ( final ArchivedFileSet fileSet )
674713 throws ArchiverException
675714 {
676- final PlexusIoResourceCollection resourceCollection = asResourceCollection ( fileSet );
715+ final PlexusIoResourceCollection resourceCollection = asResourceCollection ( fileSet , null );
716+ addResources ( resourceCollection );
717+ }
718+
719+ public void addArchivedFileSet ( final ArchivedFileSet fileSet , Charset charset )
720+ throws ArchiverException
721+ {
722+ final PlexusIoResourceCollection resourceCollection = asResourceCollection ( fileSet , charset );
677723 addResources ( resourceCollection );
678724 }
679725
@@ -921,26 +967,40 @@ protected void validate()
921967
922968 protected abstract String getArchiveType ();
923969
970+ private void addCloseable (Object maybeCloseable ){
971+ if (maybeCloseable instanceof Closeable )
972+ closeables .add ( (Closeable ) maybeCloseable );
973+
974+ }
975+ private void closeIterators ()
976+ {
977+ for ( Closeable closeable : closeables )
978+ {
979+ closeQuietlyIfCloseable ( closeable );
980+ }
981+
982+ }
924983 protected abstract void close ()
925984 throws IOException ;
926985
927986 protected void cleanUp ()
928987 throws IOException
929988 {
989+ closeIterators ();
990+
930991 for ( Object resource : resources )
931992 {
932993 if ( resource instanceof PlexusIoProxyResourceCollection )
933994 {
934995 resource = ( (PlexusIoProxyResourceCollection ) resource ).getSrc ();
935996 }
936- if ( resource instanceof Closeable )
937- {
938- ( (Closeable ) resource ).close ();
939- }
997+
998+ closeIfCloseable ( resource );
940999 }
9411000 resources .clear ();
9421001 }
9431002
1003+
9441004 protected abstract void execute ()
9451005 throws ArchiverException , IOException ;
9461006
0 commit comments