View Javadoc
1   package org.apache.maven.index;
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 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   * An artifact context used to provide information about artifact during scanning. It is passed to the
46   * {@link IndexCreator}, which can populate {@link ArtifactInfo} for the given artifact.
47   * 
48   * @see IndexCreator#populateArtifactInfo(ArtifactContext)
49   * @author Jason van Zyl
50   * @author Tamas Cservenak
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          // First check for local pom file
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         // Otherwise, check for pom contained in maven generated artifact
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      * Creates Lucene Document using {@link IndexCreator}s from the given {@link IndexingContext}.
163      */
164     public Document createDocument( IndexingContext context )
165     {
166         Document doc = new Document();
167 
168         // unique key
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         // need a second pass in case index creators updated document attributes
187         for ( IndexCreator indexCreator : context.getIndexCreators() )
188         {
189             indexCreator.updateDocument( getArtifactInfo(), doc );
190         }
191 
192         return doc;
193     }
194 }