1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
37
38
39
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
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
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
142 return getPackaging2extensionMapping().getOrDefault(packaging, packaging);
143 }
144 }