View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.index.artifact;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  import java.util.HashMap;
28  import java.util.Map;
29  import java.util.Properties;
30  
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * A very simple artifact packaging mapper, that has everything for quick-start wired in this class. Also, it takes into
36   * account the "${nexus-work}/conf/packaging2extension-mapping.properties" file into account if found. To override the
37   * "defaults" in this class, simply add lines to properties file with same keys.
38   *
39   * @author cstamas
40   */
41  @Singleton
42  @Named
43  public class DefaultArtifactPackagingMapper implements ArtifactPackagingMapper {
44  
45      private final Logger logger = LoggerFactory.getLogger(getClass());
46  
47      protected Logger getLogger() {
48          return logger;
49      }
50  
51      public static final String MAPPING_PROPERTIES_FILE = "packaging2extension-mapping.properties";
52  
53      private File propertiesFile;
54  
55      private volatile Map<String, String> packaging2extensionMapping;
56  
57      private static final Map<String, String> DEFAULTS;
58  
59      static {
60          DEFAULTS = new HashMap<>();
61          DEFAULTS.put("ejb-client", "jar");
62          DEFAULTS.put("ejb", "jar");
63          DEFAULTS.put("rar", "jar");
64          DEFAULTS.put("par", "jar");
65          DEFAULTS.put("maven-plugin", "jar");
66          DEFAULTS.put("maven-archetype", "jar");
67          DEFAULTS.put("plexus-application", "jar");
68          DEFAULTS.put("eclipse-plugin", "jar");
69          DEFAULTS.put("eclipse-feature", "jar");
70          DEFAULTS.put("eclipse-application", "zip");
71          DEFAULTS.put("nexus-plugin", "jar");
72          DEFAULTS.put("java-source", "jar");
73          DEFAULTS.put("javadoc", "jar");
74          DEFAULTS.put("test-jar", "jar");
75      }
76  
77      public void setPropertiesFile(File propertiesFile) {
78          this.propertiesFile = propertiesFile;
79          this.packaging2extensionMapping = null;
80      }
81  
82      public Map<String, String> getPackaging2extensionMapping() {
83          if (packaging2extensionMapping == null) {
84              synchronized (this) {
85                  if (packaging2extensionMapping == null) {
86                      packaging2extensionMapping = new HashMap<>();
87  
88                      // merge defaults
89                      packaging2extensionMapping.putAll(DEFAULTS);
90  
91                      if (propertiesFile != null && propertiesFile.exists()) {
92                          getLogger().info("Found user artifact packaging mapping file, applying it...");
93  
94                          Properties userMappings = new Properties();
95  
96                          try (FileInputStream fis = new FileInputStream(propertiesFile)) {
97                              userMappings.load(fis);
98  
99                              if (userMappings.keySet().size() > 0) {
100                                 for (Object key : userMappings.keySet()) {
101                                     packaging2extensionMapping.put(
102                                             key.toString(), userMappings.getProperty(key.toString()));
103                                 }
104 
105                                 getLogger()
106                                         .info(propertiesFile.getAbsolutePath()
107                                                 + " user artifact packaging mapping file contained "
108                                                 + userMappings.keySet().size()
109                                                 + " mappings, applied them all succesfully.");
110                             }
111                         } catch (IOException e) {
112                             getLogger()
113                                     .warn("Got IO exception during read of file: " + propertiesFile.getAbsolutePath());
114                         }
115 
116                     } else {
117                         // make it silent if using defaults
118                         getLogger()
119                                 .debug("User artifact packaging mappings file not found, will work with defaults...");
120                     }
121                 }
122             }
123         }
124 
125         return packaging2extensionMapping;
126     }
127 
128     public void setPackaging2extensionMapping(Map<String, String> packaging2extensionMapping) {
129         this.packaging2extensionMapping = packaging2extensionMapping;
130     }
131 
132     public Map<String, String> getDefaults() {
133         return DEFAULTS;
134     }
135 
136     public String getExtensionForPackaging(String packaging) {
137         if (packaging == null) {
138             return "jar";
139         }
140 
141         // default's to packaging name, ie. "jar", "war", "pom", etc.
142         return getPackaging2extensionMapping().getOrDefault(packaging, packaging);
143     }
144 }