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 java.util.HashMap;
21  import java.util.Map;
22  import java.util.ServiceLoader;
23  import java.util.function.Supplier;
24  
25  import org.codehaus.plexus.archiver.Archiver;
26  import org.codehaus.plexus.archiver.UnArchiver;
27  import org.codehaus.plexus.archivers.spi.ArchiverProvider;
28  import org.codehaus.plexus.archivers.spi.PlexusIoResourceCollectionProvider;
29  import org.codehaus.plexus.archivers.spi.UnArchiverProvider;
30  import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
31  
32  public class ServiceLoaderArchiverManager extends AbstractArchiverManager {
33  
34      public ServiceLoaderArchiverManager() {
35          super(archivers(), unarchivers(), plexusIoResourceCollections());
36      }
37  
38      private static Map<String, Supplier<Archiver>> archivers() {
39          Map<String, Supplier<Archiver>> result = new HashMap<>();
40          for (ArchiverProvider provider : ServiceLoader.load(ArchiverProvider.class)) {
41              result.put(provider.getName(), provider::newArchiver);
42          }
43          return result;
44      }
45  
46      private static Map<String, Supplier<UnArchiver>> unarchivers() {
47          Map<String, Supplier<UnArchiver>> result = new HashMap<>();
48          for (UnArchiverProvider provider : ServiceLoader.load(UnArchiverProvider.class)) {
49              result.put(provider.getName(), provider::newUnarchiver);
50          }
51          return result;
52      }
53  
54      private static Map<String, Supplier<PlexusIoResourceCollection>> plexusIoResourceCollections() {
55          Map<String, Supplier<PlexusIoResourceCollection>> result = new HashMap<>();
56          for (PlexusIoResourceCollectionProvider provider :
57                  ServiceLoader.load(PlexusIoResourceCollectionProvider.class)) {
58              result.put(provider.getName(), provider::newPlexusIoResourceCollection);
59          }
60          return result;
61      }
62  }