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.IOException;
24 import java.io.InputStream;
25 import java.nio.file.Files;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.zip.ZipEntry;
29 import java.util.zip.ZipFile;
30
31 import org.apache.lucene.document.Document;
32 import org.apache.lucene.document.Field;
33 import org.apache.lucene.document.StoredField;
34 import org.apache.maven.index.artifact.Gav;
35 import org.apache.maven.index.context.IndexCreator;
36 import org.apache.maven.index.context.IndexingContext;
37 import org.apache.maven.model.Model;
38 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
39 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44
45
46
47
48
49
50
51 public class ArtifactContext
52 {
53
54 private static final Logger LOGGER = LoggerFactory.getLogger( ArtifactContext.class );
55
56 private final File pom;
57
58 private final File artifact;
59
60 private final File metadata;
61
62 private final ArtifactInfo artifactInfo;
63
64 private final Gav gav;
65
66 private final List<Exception> errors = new ArrayList<>();
67
68 public ArtifactContext( File pom, File artifact, File metadata, ArtifactInfo artifactInfo, Gav gav )
69 throws IllegalArgumentException
70 {
71 if ( artifactInfo == null )
72 {
73 throw new IllegalArgumentException( "Parameter artifactInfo must not be null." );
74 }
75
76 this.pom = pom;
77 this.artifact = artifact;
78 this.metadata = metadata;
79 this.artifactInfo = artifactInfo;
80 this.gav = gav == null ? artifactInfo.calculateGav() : gav;
81 }
82
83 public File getPom()
84 {
85 return pom;
86 }
87
88 public Model getPomModel()
89 {
90
91 File pom = getPom();
92 if ( pom != null && pom.isFile() )
93 {
94 try ( InputStream inputStream = Files.newInputStream( pom.toPath() ) )
95 {
96 return new MavenXpp3Reader().read( inputStream, false );
97 }
98 catch ( IOException | XmlPullParserException e )
99 {
100 LOGGER.warn( "skip error reading pom: " + pom, e );
101 }
102 }
103
104 else if ( getArtifact() != null && getArtifact().isFile() )
105 {
106 try ( ZipFile zipFile = new ZipFile( artifact ) )
107 {
108 final String embeddedPomPath =
109 "META-INF/maven/" + getGav().getGroupId() + "/" + getGav().getArtifactId() + "/pom.xml";
110
111 ZipEntry zipEntry = zipFile.getEntry( embeddedPomPath );
112
113 if ( zipEntry != null )
114 {
115 try ( InputStream inputStream = zipFile.getInputStream( zipEntry ) )
116 {
117 return new MavenXpp3Reader().read( inputStream, false );
118 }
119 }
120 }
121 catch ( IOException | XmlPullParserException e )
122 {
123 LOGGER.warn( "skip error reading pom withing artifact:" + artifact, e );
124 }
125 }
126
127 return null;
128 }
129
130 public File getArtifact()
131 {
132 return artifact;
133 }
134
135 public File getMetadata()
136 {
137 return metadata;
138 }
139
140 public ArtifactInfo getArtifactInfo()
141 {
142 return artifactInfo;
143 }
144
145 public Gav getGav()
146 {
147 return gav;
148 }
149
150 public List<Exception> getErrors()
151 {
152 return errors;
153 }
154
155 public void addError( Exception e )
156 {
157 errors.add( e );
158 }
159
160
161
162
163 public Document createDocument( IndexingContext context )
164 {
165 Document doc = new Document();
166
167
168 doc.add( new Field( ArtifactInfo.UINFO, getArtifactInfo().getUinfo(), IndexerField.KEYWORD_STORED ) );
169
170 doc.add( new StoredField( ArtifactInfo.LAST_MODIFIED,
171 Long.toString( System.currentTimeMillis() ) ) );
172
173 for ( IndexCreator indexCreator : context.getIndexCreators() )
174 {
175 try
176 {
177 indexCreator.populateArtifactInfo( this );
178 }
179 catch ( IOException ex )
180 {
181 addError( ex );
182 }
183 }
184
185
186 for ( IndexCreator indexCreator : context.getIndexCreators() )
187 {
188 indexCreator.updateDocument( getArtifactInfo(), doc );
189 }
190
191 return doc;
192 }
193 }