View Javadoc
1   /*
2    * Copyright MojoHaus and Contributors
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   */
17  
18  package org.codehaus.plexus.archiver.manager;
19  
20  import javax.annotation.Nonnull;
21  
22  import java.io.File;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.Locale;
27  import java.util.Map;
28  import java.util.function.Supplier;
29  
30  import org.codehaus.plexus.archiver.Archiver;
31  import org.codehaus.plexus.archiver.UnArchiver;
32  import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
33  import org.codehaus.plexus.util.StringUtils;
34  
35  import static java.util.Objects.requireNonNull;
36  
37  /**
38   * Shared lookup logic of the archiver managers.
39   * <p>
40   * On the 4.x line the public methods stay non-final: up to 4.15.0 they were declared on
41   * {@link DefaultArchiverManager}, which consumers subclass and override. Being non-final also lets
42   * javac emit public bridge methods in the public subclasses, so reflective calls through
43   * {@code DefaultArchiverManager} do not reach this package-private class.
44   */
45  abstract class AbstractArchiverManager implements ArchiverManager {
46  
47      private final Map<String, Supplier<Archiver>> archivers;
48  
49      private final Map<String, Supplier<UnArchiver>> unArchivers;
50  
51      private final Map<String, Supplier<PlexusIoResourceCollection>> plexusIoResourceCollections;
52  
53      protected AbstractArchiverManager(
54              Map<String, Supplier<Archiver>> archivers,
55              Map<String, Supplier<UnArchiver>> unArchivers,
56              Map<String, Supplier<PlexusIoResourceCollection>> plexusIoResourceCollections) {
57          this.archivers = Collections.unmodifiableMap(new HashMap<>(archivers));
58          this.unArchivers = Collections.unmodifiableMap(new HashMap<>(unArchivers));
59          this.plexusIoResourceCollections = Collections.unmodifiableMap(new HashMap<>(plexusIoResourceCollections));
60      }
61  
62      @Override
63      @Nonnull
64      public Archiver getArchiver(@Nonnull String archiverName) throws NoSuchArchiverException {
65          requireNonNull(archiverName);
66          Supplier<Archiver> archiver = archivers.get(archiverName);
67          if (archiver == null) {
68              throw new NoSuchArchiverException(archiverName);
69          }
70          return archiver.get();
71      }
72  
73      @Override
74      @Nonnull
75      public UnArchiver getUnArchiver(@Nonnull String unArchiverName) throws NoSuchArchiverException {
76          requireNonNull(unArchiverName);
77          Supplier<UnArchiver> unArchiver = unArchivers.get(unArchiverName);
78          if (unArchiver == null) {
79              throw new NoSuchArchiverException(unArchiverName);
80          }
81          return unArchiver.get();
82      }
83  
84      @Override
85      @Nonnull
86      public PlexusIoResourceCollection getResourceCollection(String resourceCollectionName)
87              throws NoSuchArchiverException {
88          requireNonNull(resourceCollectionName);
89          Supplier<PlexusIoResourceCollection> resourceCollection =
90                  plexusIoResourceCollections.get(resourceCollectionName);
91          if (resourceCollection == null) {
92              throw new NoSuchArchiverException(resourceCollectionName);
93          }
94          return resourceCollection.get();
95      }
96  
97      @Override
98      @Nonnull
99      public Archiver getArchiver(@Nonnull File file) throws NoSuchArchiverException {
100         return getArchiver(getFileExtension(file));
101     }
102 
103     @Override
104     public Collection<String> getAvailableArchivers() {
105         return archivers.keySet();
106     }
107 
108     @Override
109     @Nonnull
110     public UnArchiver getUnArchiver(@Nonnull File file) throws NoSuchArchiverException {
111         return getUnArchiver(getFileExtension(file));
112     }
113 
114     @Nonnull
115     @Override
116     public Collection<String> getAvailableUnArchivers() {
117         return unArchivers.keySet();
118     }
119 
120     @Override
121     @Nonnull
122     public PlexusIoResourceCollection getResourceCollection(@Nonnull File file) throws NoSuchArchiverException {
123         return getResourceCollection(getFileExtension(file));
124     }
125 
126     @Nonnull
127     @Override
128     public Collection<String> getAvailableResourceCollections() {
129         return plexusIoResourceCollections.keySet();
130     }
131 
132     @Nonnull
133     private static String getFileExtension(@Nonnull File file) {
134 
135         String fileName = file.getName().toLowerCase(Locale.ROOT);
136         String[] tokens = StringUtils.split(fileName, ".");
137 
138         String archiveExt = "";
139 
140         if (tokens.length == 2) {
141             archiveExt = tokens[1];
142         } else if (tokens.length > 2 && "tar".equals(tokens[tokens.length - 2])) {
143             archiveExt = "tar." + tokens[tokens.length - 1];
144         } else if (tokens.length > 2) {
145             archiveExt = tokens[tokens.length - 1];
146         }
147 
148         return archiveExt;
149     }
150 }