1 | |
package org.apache.maven.archetype.common; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | 42 | public class DefaultArchetypeRegistryManager |
48 | |
extends AbstractLogEnabled |
49 | |
implements ArchetypeRegistryManager |
50 | |
{ |
51 | |
|
52 | |
|
53 | |
|
54 | |
@Requirement |
55 | |
private ArtifactRepositoryFactory artifactRepositoryFactory; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
@Requirement |
61 | |
private ArtifactRepositoryLayout defaultArtifactRepositoryLayout; |
62 | |
|
63 | |
public List<String> getFilteredExtensions( String archetypeFilteredExtentions, File archetypeRegistryFile ) |
64 | |
throws IOException |
65 | |
{ |
66 | 0 | List<String> filteredExtensions = new ArrayList<String>(); |
67 | |
|
68 | 0 | if ( StringUtils.isNotEmpty( archetypeFilteredExtentions ) ) |
69 | |
{ |
70 | 0 | filteredExtensions.addAll( Arrays.asList( StringUtils.split( archetypeFilteredExtentions, "," ) ) ); |
71 | |
} |
72 | |
|
73 | |
try |
74 | |
{ |
75 | 0 | ArchetypeRegistry registry = readArchetypeRegistry( archetypeRegistryFile ); |
76 | |
|
77 | 0 | filteredExtensions.addAll( registry.getFilteredExtensions() ); |
78 | |
} |
79 | 0 | catch ( IOException e ) |
80 | |
{ |
81 | 0 | getLogger().warn( "Cannot read ~/.m2/archetype.xml" ); |
82 | |
} |
83 | 0 | catch ( XmlPullParserException e ) |
84 | |
{ |
85 | 0 | getLogger().warn( "Cannot read ~/.m2/archetype.xml" ); |
86 | 0 | } |
87 | |
|
88 | 0 | if ( filteredExtensions.isEmpty() ) |
89 | |
{ |
90 | 0 | filteredExtensions.addAll( Constants.DEFAULT_FILTERED_EXTENSIONS ); |
91 | |
} |
92 | |
|
93 | 0 | return filteredExtensions; |
94 | |
} |
95 | |
|
96 | |
public List<String> getLanguages( String archetypeLanguages, File archetypeRegistryFile ) |
97 | |
throws IOException |
98 | |
{ |
99 | 0 | List<String> languages = new ArrayList<String>(); |
100 | |
|
101 | 0 | if ( StringUtils.isNotEmpty( archetypeLanguages ) ) |
102 | |
{ |
103 | 0 | languages.addAll( Arrays.asList( StringUtils.split( archetypeLanguages, "," ) ) ); |
104 | |
} |
105 | |
|
106 | |
try |
107 | |
{ |
108 | 0 | ArchetypeRegistry registry = readArchetypeRegistry( archetypeRegistryFile ); |
109 | |
|
110 | 0 | languages.addAll( registry.getLanguages() ); |
111 | |
} |
112 | 0 | catch ( IOException e ) |
113 | |
{ |
114 | 0 | getLogger().warn( "Can not read ~/.m2/archetype.xml" ); |
115 | |
} |
116 | 0 | catch ( XmlPullParserException e ) |
117 | |
{ |
118 | 0 | getLogger().warn( "Can not read ~/.m2/archetype.xml" ); |
119 | 0 | } |
120 | |
|
121 | 0 | if ( languages.isEmpty() ) |
122 | |
{ |
123 | 0 | languages.addAll( Constants.DEFAULT_LANGUAGES ); |
124 | |
} |
125 | |
|
126 | 0 | return languages; |
127 | |
} |
128 | |
|
129 | |
public ArchetypeRegistry readArchetypeRegistry( File archetypeRegistryFile ) |
130 | |
throws IOException, XmlPullParserException |
131 | |
{ |
132 | 0 | if ( !archetypeRegistryFile.exists() ) |
133 | |
{ |
134 | 0 | return getDefaultArchetypeRegistry(); |
135 | |
} |
136 | |
else |
137 | |
{ |
138 | 0 | return readArchetypeRegistry( ReaderFactory.newXmlReader( archetypeRegistryFile ) ); |
139 | |
} |
140 | |
} |
141 | |
|
142 | |
public ArchetypeRegistry readArchetypeRegistry( Reader reader ) |
143 | |
throws IOException, XmlPullParserException |
144 | |
{ |
145 | 0 | ArchetypeRegistryXpp3Reader xpp3Reader = new ArchetypeRegistryXpp3Reader(); |
146 | |
|
147 | |
try |
148 | |
{ |
149 | 0 | return xpp3Reader.read( reader ); |
150 | |
} |
151 | |
finally |
152 | |
{ |
153 | 0 | IOUtil.close( reader ); |
154 | |
} |
155 | |
} |
156 | |
|
157 | |
public void writeArchetypeRegistry( File archetypeRegistryFile, ArchetypeRegistry archetypeRegistry ) |
158 | |
throws IOException |
159 | |
{ |
160 | 0 | ArchetypeRegistryXpp3Writer writer = new ArchetypeRegistryXpp3Writer(); |
161 | 0 | Writer out = WriterFactory.newXmlWriter( archetypeRegistryFile ); |
162 | |
|
163 | |
try |
164 | |
{ |
165 | 0 | writer.write( out, archetypeRegistry ); |
166 | |
} |
167 | |
finally |
168 | |
{ |
169 | 0 | IOUtil.close( out ); |
170 | 0 | } |
171 | 0 | } |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public ArtifactRepository createRepository( String url, String repositoryId ) |
178 | |
{ |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | 42 | String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS; |
185 | |
|
186 | 42 | String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN; |
187 | |
|
188 | 42 | ArtifactRepositoryPolicy snapshotsPolicy = |
189 | |
new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag ); |
190 | |
|
191 | 42 | ArtifactRepositoryPolicy releasesPolicy = |
192 | |
new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag ); |
193 | |
|
194 | 42 | return artifactRepositoryFactory.createArtifactRepository( repositoryId, url, defaultArtifactRepositoryLayout, |
195 | |
snapshotsPolicy, releasesPolicy ); |
196 | |
} |
197 | |
|
198 | |
public ArchetypeRegistry getDefaultArchetypeRegistry() |
199 | |
{ |
200 | 0 | ArchetypeRegistry registry = new ArchetypeRegistry(); |
201 | |
|
202 | 0 | registry.getLanguages().addAll( Constants.DEFAULT_LANGUAGES ); |
203 | |
|
204 | 0 | registry.getFilteredExtensions().addAll( Constants.DEFAULT_FILTERED_EXTENSIONS ); |
205 | |
|
206 | 0 | return registry; |
207 | |
} |
208 | |
} |