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