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 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   * A very simple artifact packaging mapper, that has everything for quick-start wired in this class. Also, it takes into
35   * account the "${nexus-work}/conf/packaging2extension-mapping.properties" file into account if found. To override the
36   * "defaults" in this class, simply add lines to properties file with same keys.
37   *
38   * @author cstamas
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                      // merge defaults
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                         // 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         if ( getPackaging2extensionMapping().containsKey( packaging ) )
162         {
163             return getPackaging2extensionMapping().get( packaging );
164         }
165         else
166         {
167             // default's to packaging name, ie. "jar", "war", "pom", etc.
168             return packaging;
169         }
170     }
171 }