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