View Javadoc
1   /*
2    * Copyright 2010-2015 The plexus developers.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.plexus.archiver.tar;
17  
18  import java.io.Closeable;
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.Enumeration;
22  import java.util.Iterator;
23  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
24  import org.codehaus.plexus.components.io.resources.AbstractPlexusIoArchiveResourceCollection;
25  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
26  
27  public class PlexusIoTarFileResourceCollection
28      extends AbstractPlexusIoArchiveResourceCollection implements Closeable
29  {
30  
31      /**
32       * The zip file resource collections role hint.
33       */
34      public static final String ROLE_HINT = "tar";
35  
36      protected TarFile newTarFile( File file )
37      {
38          return new TarFile( file );
39      }
40  
41      TarFile tarFile = null;
42  
43      @Override
44      public void close()
45          throws IOException
46      {
47          if ( tarFile != null )
48          {
49              tarFile.close();
50          }
51      }
52  
53      @Override
54      public boolean isConcurrentAccessSupported()
55      {
56          return false;
57      }
58  
59      @Override
60      protected Iterator<PlexusIoResource> getEntries()
61          throws IOException
62      {
63          final File f = getFile();
64          if ( f == null )
65          {
66              throw new IOException( "The tar archive file has not been set." );
67          }
68          if ( tarFile == null )
69          {
70              tarFile = newTarFile( f );
71          }
72          final Enumeration en = tarFile.getEntries();
73          return new Iterator<PlexusIoResource>()
74          {
75  
76              @Override
77              public boolean hasNext()
78              {
79                  return en.hasMoreElements();
80              }
81  
82              @Override
83              public PlexusIoResource next()
84              {
85                  final TarArchiveEntry entry = (TarArchiveEntry) en.nextElement();
86                  return entry.isSymbolicLink()
87                             ? new TarSymlinkResource( tarFile, entry )
88                             : new TarResource( tarFile, entry );
89              }
90  
91              @Override
92              public void remove()
93              {
94                  throw new UnsupportedOperationException( "Removing isn't implemented." );
95              }
96  
97          };
98      }
99  
100 }