1 | |
package org.apache.maven.index; |
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.FileNotFoundException; |
25 | |
import java.io.IOException; |
26 | |
import java.io.InputStream; |
27 | |
import java.io.InputStreamReader; |
28 | |
import java.io.Reader; |
29 | |
import java.util.ArrayList; |
30 | |
import java.util.List; |
31 | |
|
32 | |
import org.apache.lucene.document.Document; |
33 | |
import org.apache.lucene.document.Field; |
34 | |
import org.apache.lucene.document.Field.Index; |
35 | |
import org.apache.lucene.document.Field.Store; |
36 | |
import org.apache.maven.index.artifact.Gav; |
37 | |
import org.apache.maven.index.context.IndexCreator; |
38 | |
import org.apache.maven.index.context.IndexingContext; |
39 | |
import org.apache.maven.index.util.zip.ZipFacade; |
40 | |
import org.apache.maven.index.util.zip.ZipHandle; |
41 | |
import org.apache.maven.model.Model; |
42 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
43 | |
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; |
44 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public class ArtifactContext |
56 | |
{ |
57 | |
private final File pom; |
58 | |
|
59 | |
private final File artifact; |
60 | |
|
61 | |
private final File metadata; |
62 | |
|
63 | |
private final ArtifactInfo artifactInfo; |
64 | |
|
65 | |
private final Gav gav; |
66 | |
|
67 | 14698 | private final List<Exception> errors = new ArrayList<Exception>(); |
68 | |
|
69 | |
public ArtifactContext( File pom, File artifact, File metadata, ArtifactInfo artifactInfo, Gav gav ) |
70 | |
throws IllegalArgumentException |
71 | 14698 | { |
72 | 14698 | if ( artifactInfo == null ) |
73 | |
{ |
74 | 0 | throw new IllegalArgumentException( "Parameter artifactInfo must not be null." ); |
75 | |
} |
76 | |
|
77 | 14698 | this.pom = pom; |
78 | 14698 | this.artifact = artifact; |
79 | 14698 | this.metadata = metadata; |
80 | 14698 | this.artifactInfo = artifactInfo; |
81 | 14698 | this.gav = gav == null ? artifactInfo.calculateGav() : gav; |
82 | 14698 | } |
83 | |
|
84 | |
public File getPom() |
85 | |
{ |
86 | 34985 | return pom; |
87 | |
} |
88 | |
|
89 | |
public Model getPomModel() |
90 | |
{ |
91 | |
|
92 | 11575 | if ( getPom() != null && getPom().exists() ) |
93 | |
{ |
94 | |
try |
95 | |
{ |
96 | 5761 | return new ModelReader().readModel( new FileInputStream( getPom() ) ); |
97 | |
} |
98 | 0 | catch ( FileNotFoundException e ) |
99 | |
{ |
100 | 0 | } |
101 | |
} |
102 | |
|
103 | 5814 | else if ( getArtifact() != null ) |
104 | |
{ |
105 | 258 | ZipHandle handle = null; |
106 | |
|
107 | |
try |
108 | |
{ |
109 | 258 | handle = ZipFacade.getZipHandle( getArtifact() ); |
110 | |
|
111 | 232 | final String embeddedPomPath = |
112 | |
"META-INF/maven/" + getGav().getGroupId() + "/" + getGav().getArtifactId() + "/pom.xml"; |
113 | |
|
114 | 232 | if ( handle.hasEntry( embeddedPomPath ) ) |
115 | |
{ |
116 | 2 | return new ModelReader().readModel( handle.getEntryContent( embeddedPomPath ) ); |
117 | |
} |
118 | |
} |
119 | 26 | catch ( IOException e ) |
120 | |
{ |
121 | |
} |
122 | |
finally |
123 | |
{ |
124 | 0 | try |
125 | |
{ |
126 | 258 | ZipFacade.close( handle ); |
127 | |
} |
128 | 0 | catch ( Exception e ) |
129 | |
{ |
130 | 260 | } |
131 | 0 | } |
132 | |
} |
133 | |
|
134 | 5812 | return null; |
135 | |
} |
136 | |
|
137 | |
public File getArtifact() |
138 | |
{ |
139 | 30741 | return artifact; |
140 | |
} |
141 | |
|
142 | |
public File getMetadata() |
143 | |
{ |
144 | 0 | return metadata; |
145 | |
} |
146 | |
|
147 | |
public ArtifactInfo getArtifactInfo() |
148 | |
{ |
149 | 104692 | return artifactInfo; |
150 | |
} |
151 | |
|
152 | |
public Gav getGav() |
153 | |
{ |
154 | 18056 | return gav; |
155 | |
} |
156 | |
|
157 | |
public List<Exception> getErrors() |
158 | |
{ |
159 | 5989 | return errors; |
160 | |
} |
161 | |
|
162 | |
public void addError( Exception e ) |
163 | |
{ |
164 | 45 | errors.add( e ); |
165 | 45 | } |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public Document createDocument( IndexingContext context ) |
171 | |
{ |
172 | 11575 | Document doc = new Document(); |
173 | |
|
174 | |
|
175 | 11575 | doc.add( new Field( ArtifactInfo.UINFO, getArtifactInfo().getUinfo(), Store.YES, Index.NOT_ANALYZED ) ); |
176 | |
|
177 | 11575 | doc.add( new Field( ArtifactInfo.LAST_MODIFIED, |
178 | |
Long.toString( System.currentTimeMillis() ), Store.YES, Index.NO ) ); |
179 | |
|
180 | 11575 | for ( IndexCreator indexCreator : context.getIndexCreators() ) |
181 | |
{ |
182 | |
try |
183 | |
{ |
184 | 24656 | indexCreator.populateArtifactInfo( this ); |
185 | |
} |
186 | 45 | catch ( IOException ex ) |
187 | |
{ |
188 | 45 | addError( ex ); |
189 | 49267 | } |
190 | |
} |
191 | |
|
192 | |
|
193 | 11575 | for ( IndexCreator indexCreator : context.getIndexCreators() ) |
194 | |
{ |
195 | 24656 | indexCreator.updateDocument( getArtifactInfo(), doc ); |
196 | |
} |
197 | |
|
198 | 11575 | return doc; |
199 | |
} |
200 | |
|
201 | 9075 | public static class ModelReader |
202 | |
{ |
203 | |
public Model readModel( InputStream pom ) |
204 | |
{ |
205 | 9075 | if ( pom == null ) |
206 | |
{ |
207 | 0 | return null; |
208 | |
} |
209 | |
|
210 | 9075 | Model model = new Model(); |
211 | |
|
212 | 9075 | Xpp3Dom dom = readPomInputStream( pom ); |
213 | |
|
214 | 9075 | if ( dom == null ) |
215 | |
{ |
216 | 240 | return null; |
217 | |
} |
218 | |
|
219 | 8835 | if ( dom.getChild( "packaging" ) != null ) |
220 | |
{ |
221 | 2107 | model.setPackaging( dom.getChild( "packaging" ).getValue() ); |
222 | |
} |
223 | |
|
224 | |
else |
225 | |
{ |
226 | 6728 | model.setPackaging( null ); |
227 | |
} |
228 | |
|
229 | 8835 | if ( dom.getChild( "name" ) != null ) |
230 | |
{ |
231 | 7301 | model.setName( dom.getChild( "name" ).getValue() ); |
232 | |
} |
233 | |
|
234 | 8835 | if ( dom.getChild( "description" ) != null ) |
235 | |
{ |
236 | 6188 | model.setDescription( dom.getChild( "description" ).getValue() ); |
237 | |
} |
238 | |
|
239 | 8835 | return model; |
240 | |
} |
241 | |
|
242 | |
private Xpp3Dom readPomInputStream( InputStream is ) |
243 | |
{ |
244 | 9075 | Reader r = new InputStreamReader( is ); |
245 | |
try |
246 | |
{ |
247 | 9075 | return Xpp3DomBuilder.build( r ); |
248 | |
} |
249 | 240 | catch ( XmlPullParserException e ) |
250 | |
{ |
251 | |
} |
252 | 0 | catch ( IOException e ) |
253 | |
{ |
254 | |
} |
255 | |
finally |
256 | |
{ |
257 | 0 | try |
258 | |
{ |
259 | 9075 | r.close(); |
260 | |
} |
261 | 0 | catch ( IOException e ) |
262 | |
{ |
263 | 17910 | } |
264 | 0 | } |
265 | |
|
266 | 240 | return null; |
267 | |
} |
268 | |
} |
269 | |
} |