View Javadoc
1   package org.apache.maven.archetype.common;
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 org.apache.maven.archetype.registry.ArchetypeRegistry;
23  import org.apache.maven.archetype.registry.io.xpp3.ArchetypeRegistryXpp3Reader;
24  import org.apache.maven.archetype.registry.io.xpp3.ArchetypeRegistryXpp3Writer;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
27  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
28  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
29  import org.codehaus.plexus.component.annotations.Component;
30  import org.codehaus.plexus.component.annotations.Requirement;
31  import org.codehaus.plexus.logging.AbstractLogEnabled;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.codehaus.plexus.util.ReaderFactory;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.util.WriterFactory;
36  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
37  
38  import java.io.File;
39  import java.io.IOException;
40  import java.io.Reader;
41  import java.io.Writer;
42  import java.util.ArrayList;
43  import java.util.Arrays;
44  import java.util.List;
45  
46  @Component( role = ArchetypeRegistryManager.class )
47  public class DefaultArchetypeRegistryManager
48      extends AbstractLogEnabled
49      implements ArchetypeRegistryManager
50  {
51      /**
52       * Used to create ArtifactRepository objects given the urls of the remote repositories.
53       */
54      @Requirement
55      private ArtifactRepositoryFactory artifactRepositoryFactory;
56  
57      /**
58       * Determines whether the layout is legacy or not.
59       */
60      @Requirement
61      private ArtifactRepositoryLayout defaultArtifactRepositoryLayout;
62  
63      public List<String> getFilteredExtensions( String archetypeFilteredExtentions, File archetypeRegistryFile )
64          throws IOException
65      {
66          List<String> filteredExtensions = new ArrayList<String>();
67  
68          if ( StringUtils.isNotEmpty( archetypeFilteredExtentions ) )
69          {
70              filteredExtensions.addAll( Arrays.asList( StringUtils.split( archetypeFilteredExtentions, "," ) ) );
71          }
72  
73          try
74          {
75              ArchetypeRegistry registry = readArchetypeRegistry( archetypeRegistryFile );
76  
77              filteredExtensions.addAll( registry.getFilteredExtensions() );
78          }
79          catch ( IOException e )
80          {
81              getLogger().warn( "Cannot read ~/.m2/archetype.xml" );
82          }
83          catch ( XmlPullParserException e )
84          {
85              getLogger().warn( "Cannot read ~/.m2/archetype.xml" );
86          }
87  
88          if ( filteredExtensions.isEmpty() )
89          {
90              filteredExtensions.addAll( Constants.DEFAULT_FILTERED_EXTENSIONS );
91          }
92  
93          return filteredExtensions;
94      }
95  
96      public List<String> getLanguages( String archetypeLanguages, File archetypeRegistryFile )
97          throws IOException
98      {
99          List<String> languages = new ArrayList<String>();
100 
101         if ( StringUtils.isNotEmpty( archetypeLanguages ) )
102         {
103             languages.addAll( Arrays.asList( StringUtils.split( archetypeLanguages, "," ) ) );
104         }
105 
106         try
107         {
108             ArchetypeRegistry registry = readArchetypeRegistry( archetypeRegistryFile );
109 
110             languages.addAll( registry.getLanguages() );
111         }
112         catch ( IOException e )
113         {
114             getLogger().warn( "Can not read ~/.m2/archetype.xml" );
115         }
116         catch ( XmlPullParserException e )
117         {
118             getLogger().warn( "Can not read ~/.m2/archetype.xml" );
119         }
120 
121         if ( languages.isEmpty() )
122         {
123             languages.addAll( Constants.DEFAULT_LANGUAGES );
124         }
125 
126         return languages;
127     }
128 
129     public ArchetypeRegistry readArchetypeRegistry( File archetypeRegistryFile )
130         throws IOException, XmlPullParserException
131     {
132         if ( !archetypeRegistryFile.exists() )
133         {
134             return getDefaultArchetypeRegistry();
135         }
136         else
137         {
138             return readArchetypeRegistry( ReaderFactory.newXmlReader( archetypeRegistryFile ) );
139         }
140     }
141 
142     public ArchetypeRegistry readArchetypeRegistry( Reader reader )
143         throws IOException, XmlPullParserException
144     {
145         ArchetypeRegistryXpp3Reader xpp3Reader = new ArchetypeRegistryXpp3Reader();
146 
147         try
148         {
149             return xpp3Reader.read( reader );
150         }
151         finally
152         {
153             IOUtil.close( reader );
154         }
155     }
156 
157     public void writeArchetypeRegistry( File archetypeRegistryFile, ArchetypeRegistry archetypeRegistry )
158         throws IOException
159     {
160         ArchetypeRegistryXpp3Writer writer = new ArchetypeRegistryXpp3Writer();
161         Writer out = WriterFactory.newXmlWriter( archetypeRegistryFile );
162 
163         try
164         {
165             writer.write( out, archetypeRegistry );
166         }
167         finally
168         {
169             IOUtil.close( out );
170         }
171     }
172 
173     /**
174      * Code stealed from MavenArchetypeMojo
175      * (org.apache.maven.plugins:maven-archetype-plugin:1.0-alpha4).
176      */
177     public ArtifactRepository createRepository( String url, String repositoryId )
178     {
179         // snapshots vs releases
180         // offline = to turning the update policy off
181 
182         // TODO: we'll need to allow finer grained creation of repositories but this will do for now
183 
184         String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;
185 
186         String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;
187 
188         ArtifactRepositoryPolicy snapshotsPolicy =
189             new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
190 
191         ArtifactRepositoryPolicy releasesPolicy =
192             new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
193 
194         return artifactRepositoryFactory.createArtifactRepository( repositoryId, url, defaultArtifactRepositoryLayout,
195                                                                    snapshotsPolicy, releasesPolicy );
196     }
197 
198     public ArchetypeRegistry getDefaultArchetypeRegistry()
199     {
200         ArchetypeRegistry registry = new ArchetypeRegistry();
201 
202         registry.getLanguages().addAll( Constants.DEFAULT_LANGUAGES );
203 
204         registry.getFilteredExtensions().addAll( Constants.DEFAULT_FILTERED_EXTENSIONS );
205 
206         return registry;
207     }
208 }