1 package org.apache.maven.index.artifact;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import org.codehaus.plexus.component.annotations.Component;
30 import org.codehaus.plexus.logging.AbstractLogEnabled;
31 import org.codehaus.plexus.util.IOUtil;
32
33
34
35
36
37
38
39
40 @Component( role = ArtifactPackagingMapper.class )
41 public class DefaultArtifactPackagingMapper
42 extends AbstractLogEnabled
43 implements ArtifactPackagingMapper
44 {
45
46 public static final String MAPPING_PROPERTIES_FILE = "packaging2extension-mapping.properties";
47
48 private File propertiesFile;
49
50 private volatile Map<String, String> packaging2extensionMapping;
51
52 private final static Map<String, String> defaults;
53
54 static
55 {
56 defaults = new HashMap<String, String>();
57 defaults.put( "ejb-client", "jar" );
58 defaults.put( "ejb", "jar" );
59 defaults.put( "rar", "jar" );
60 defaults.put( "par", "jar" );
61 defaults.put( "maven-plugin", "jar" );
62 defaults.put( "maven-archetype", "jar" );
63 defaults.put( "plexus-application", "jar" );
64 defaults.put( "eclipse-plugin", "jar" );
65 defaults.put( "eclipse-feature", "jar" );
66 defaults.put( "eclipse-application", "zip" );
67 defaults.put( "nexus-plugin", "jar" );
68 defaults.put( "java-source", "jar" );
69 defaults.put( "javadoc", "jar" );
70 defaults.put( "test-jar", "jar" );
71 }
72
73 public void setPropertiesFile( File propertiesFile )
74 {
75 this.propertiesFile = propertiesFile;
76 this.packaging2extensionMapping = null;
77 }
78
79 public Map<String, String> getPackaging2extensionMapping()
80 {
81 if ( packaging2extensionMapping == null )
82 {
83 synchronized ( this )
84 {
85 if ( packaging2extensionMapping == null )
86 {
87 packaging2extensionMapping = new HashMap<String, String>();
88
89
90 packaging2extensionMapping.putAll( defaults );
91
92 if ( propertiesFile != null && propertiesFile.exists() )
93 {
94 getLogger().info( "Found user artifact packaging mapping file, applying it..." );
95
96 Properties userMappings = new Properties();
97
98 FileInputStream fis = null;
99
100 try
101 {
102 fis = new FileInputStream( propertiesFile );
103
104 userMappings.load( fis );
105
106 if ( userMappings.keySet().size() > 0 )
107 {
108 for ( Object key : userMappings.keySet() )
109 {
110 packaging2extensionMapping.put( key.toString(),
111 userMappings.getProperty( key.toString() ) );
112 }
113
114 getLogger().info(
115 propertiesFile.getAbsolutePath()
116 + " user artifact packaging mapping file contained "
117 + userMappings.keySet().size() + " mappings, applied them all succesfully." );
118 }
119 }
120 catch ( IOException e )
121 {
122 getLogger().warn(
123 "Got IO exception during read of file: " + propertiesFile.getAbsolutePath() );
124 }
125 finally
126 {
127 IOUtil.close( fis );
128 }
129
130 }
131 else
132 {
133
134 getLogger().debug(
135 "User artifact packaging mappings file not found, will work with defaults..." );
136 }
137 }
138 }
139 }
140
141 return packaging2extensionMapping;
142 }
143
144 public void setPackaging2extensionMapping( Map<String, String> packaging2extensionMapping )
145 {
146 this.packaging2extensionMapping = packaging2extensionMapping;
147 }
148
149 public Map<String, String> getDefaults()
150 {
151 return defaults;
152 }
153
154 public String getExtensionForPackaging( String packaging )
155 {
156 if ( packaging == null )
157 {
158 return "jar";
159 }
160
161 if ( getPackaging2extensionMapping().containsKey( packaging ) )
162 {
163 return getPackaging2extensionMapping().get( packaging );
164 }
165 else
166 {
167
168 return packaging;
169 }
170 }
171 }