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