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 javax.inject.Named;
19  
20  import java.io.Closeable;
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Enumeration;
24  import java.util.Iterator;
25  
26  import org.apache.commons.compress.archivers.ArchiveEntry;
27  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
28  import org.codehaus.plexus.components.io.resources.AbstractPlexusIoArchiveResourceCollection;
29  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
30  
31  @Named("tar")
32  public class PlexusIoTarFileResourceCollection extends AbstractPlexusIoArchiveResourceCollection implements Closeable {
33  
34      protected TarFile newTarFile(File file) {
35          return new TarFile(file);
36      }
37  
38      TarFile tarFile = null;
39  
40      @Override
41      public void close() throws IOException {
42          if (tarFile != null) {
43              tarFile.close();
44          }
45      }
46  
47      @Override
48      public boolean isConcurrentAccessSupported() {
49          return false;
50      }
51  
52      @Override
53      protected Iterator<PlexusIoResource> getEntries() throws IOException {
54          final File f = getFile();
55          if (f == null) {
56              throw new IOException("The tar archive file has not been set.");
57          }
58          if (tarFile == null) {
59              tarFile = newTarFile(f);
60          }
61          final Enumeration<ArchiveEntry> en = tarFile.getEntries();
62          return new Iterator<PlexusIoResource>() {
63  
64              @Override
65              public boolean hasNext() {
66                  return en.hasMoreElements();
67              }
68  
69              @Override
70              public PlexusIoResource next() {
71                  final TarArchiveEntry entry = (TarArchiveEntry) en.nextElement();
72                  return entry.isSymbolicLink()
73                          ? new TarSymlinkResource(tarFile, entry)
74                          : new TarResource(tarFile, entry);
75              }
76  
77              @Override
78              public void remove() {
79                  throw new UnsupportedOperationException("Removing isn't implemented.");
80              }
81          };
82      }
83  }