1 package org.apache.maven.archiva.reporting.artifact;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.ConfigurationNames;
25 import org.apache.maven.archiva.configuration.FileTypes;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
28 import org.apache.maven.archiva.consumers.ArchivaArtifactConsumer;
29 import org.apache.maven.archiva.consumers.ConsumerException;
30 import org.apache.maven.archiva.database.ArchivaDAO;
31 import org.apache.maven.archiva.database.ArchivaDatabaseException;
32 import org.apache.maven.archiva.model.ArchivaArtifact;
33 import org.apache.maven.archiva.model.ArchivaProjectModel;
34 import org.apache.maven.archiva.model.RepositoryProblem;
35 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
36 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory;
37 import org.apache.maven.archiva.repository.layout.LayoutException;
38 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
39 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
40 import org.codehaus.plexus.registry.Registry;
41 import org.codehaus.plexus.registry.RegistryListener;
42 import org.codehaus.plexus.util.SelectorUtils;
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.Enumeration;
48 import java.util.HashMap;
49 import java.util.List;
51 import java.util.jar.JarEntry;
52 import java.util.jar.JarFile;
55 * Validate the location of the artifact based on the values indicated
56 * in its pom (both the pom packaged with the artifact & the pom in the
59 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
61 * @plexus.component role="org.apache.maven.archiva.consumers.ArchivaArtifactConsumer"
62 * role-hint="validate-artifacts-location"
64 public class LocationArtifactsConsumer
65 extends AbstractMonitoredConsumer
66 implements ArchivaArtifactConsumer, RegistryListener, Initializable
69 * @plexus.configuration default-value="duplicate-artifacts"
74 * @plexus.configuration default-value="Check for Duplicate Artifacts via SHA1 Checksums"
76 private String description;
81 private ArchivaConfiguration configuration;
86 private FileTypes filetypes;
89 * @plexus.requirement role-hint="jdo"
91 private ArchivaDAO dao;
96 private BidirectionalRepositoryLayoutFactory layoutFactory;
98 private Map repositoryMap = new HashMap();
100 private List includes = new ArrayList();
102 public String getId()
107 public String getDescription()
112 public boolean isPermanent()
117 public void beginScan()
122 public void completeScan()
127 public List getIncludedTypes()
133 * Check whether the artifact is in its proper location. The location of the artifact
134 * is validated first against the groupId, artifactId and versionId in the specified model
135 * object (pom in the file system). Then unpack the artifact (jar file) and get the model (pom)
136 * included in the package. If a model exists inside the package, then check if the artifact's
137 * location is valid based on the location specified in the pom. Check if the both the location
138 * specified in the file system pom and in the pom included in the package is the same.
140 public void processArchivaArtifact( ArchivaArtifact artifact )
141 throws ConsumerException
143 ManagedRepositoryConfiguration repository = findRepository( artifact );
145 File artifactFile = new File( repository.getLocation(), toPath( artifact ) );
146 ArchivaProjectModel fsModel = readFilesystemModel( artifactFile );
147 ArchivaProjectModel embeddedModel = readEmbeddedModel( artifact, artifactFile );
149 validateAppropriateModel( "Filesystem", artifact, fsModel );
150 validateAppropriateModel( "Embedded", artifact, embeddedModel );
153 private void validateAppropriateModel( String location, ArchivaArtifact artifact, ArchivaProjectModel model )
154 throws ConsumerException
158 if ( !StringUtils.equals( model.getGroupId(), artifact.getGroupId() ) )
160 addProblem( artifact, "The groupId of the " + location +
161 " project model doesn't match with the artifact, expected <" + artifact.getGroupId() +
162 ">, but was actually <" + model.getGroupId() + ">" );
165 if ( !StringUtils.equals( model.getArtifactId(), artifact.getArtifactId() ) )
167 addProblem( artifact, "The artifactId of the " + location +
168 " project model doesn't match with the artifact, expected <" + artifact.getArtifactId() +
169 ">, but was actually <" + model.getArtifactId() + ">" );
172 if ( !StringUtils.equals( model.getVersion(), artifact.getVersion() ) )
174 addProblem( artifact, "The version of the " + location +
175 " project model doesn't match with the artifact, expected <" + artifact.getVersion() +
176 ">, but was actually <" + model.getVersion() + ">" );
181 private ArchivaProjectModel readEmbeddedModel( ArchivaArtifact artifact, File artifactFile )
182 throws ConsumerException
186 JarFile jar = new JarFile( artifactFile );
188 // Get the entry and its input stream.
189 JarEntry expectedEntry = jar.getJarEntry(
190 "META-INF/maven/" + artifact.getGroupId() + "/" + artifact.getArtifactId() + "/pom.xml" );
192 if ( expectedEntry != null )
194 // TODO: read and resolve model here.
198 /* Expected Entry not found, look for alternate that might
199 * indicate that the artifact is, indeed located in the wrong place.
202 List actualPomXmls = findJarEntryPattern( jar, "META-INF/maven/**/pom.xml" );
203 if ( actualPomXmls.isEmpty() )
209 // TODO: test for invalid actual pom.xml
212 catch ( IOException e )
214 // Not able to read from the file.
215 String emsg = "Unable to read file contents: " + e.getMessage();
216 addProblem( artifact, emsg );
222 private List findJarEntryPattern( JarFile jar, String pattern )
224 List hits = new ArrayList();
226 Enumeration entries = jar.entries();
227 while ( entries.hasMoreElements() )
229 JarEntry entry = (JarEntry) entries.nextElement();
230 if ( SelectorUtils.match( pattern, entry.getName() ) )
239 private void addProblem( ArchivaArtifact artifact, String msg )
240 throws ConsumerException
242 RepositoryProblem problem = new RepositoryProblem();
243 problem.setRepositoryId( artifact.getModel().getRepositoryId() );
244 problem.setPath( toPath( artifact ) );
245 problem.setGroupId( artifact.getGroupId() );
246 problem.setArtifactId( artifact.getArtifactId() );
247 problem.setVersion( artifact.getVersion() );
248 problem.setType( LocationArtifactsReport.PROBLEM_TYPE_BAD_ARTIFACT_LOCATION );
249 problem.setOrigin( getId() );
250 problem.setMessage( msg );
254 dao.getRepositoryProblemDAO().saveRepositoryProblem( problem );
256 catch ( ArchivaDatabaseException e )
258 String emsg = "Unable to save problem with artifact location to DB: " + e.getMessage();
259 getLogger().warn( emsg, e );
260 throw new ConsumerException( emsg, e );
264 private ArchivaProjectModel readFilesystemModel( File artifactFile )
266 File pomFile = createPomFileReference( artifactFile );
268 // TODO: read and resolve model here.
273 private File createPomFileReference( File artifactFile )
275 String pomFilename = artifactFile.getAbsolutePath();
277 int pos = pomFilename.lastIndexOf( '.' );
284 pomFilename = pomFilename.substring( 0, pos ) + ".pom";
285 return new File( pomFilename );
288 private ManagedRepositoryConfiguration findRepository( ArchivaArtifact artifact )
290 return (ManagedRepositoryConfiguration) this.repositoryMap.get( artifact.getModel().getRepositoryId() );
293 private String toPath( ArchivaArtifact artifact )
297 BidirectionalRepositoryLayout layout = layoutFactory.getLayout( artifact );
298 return layout.toPath( artifact );
300 catch ( LayoutException e )
302 getLogger().warn( "Unable to calculate path for artifact: " + artifact );
307 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
309 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
314 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
320 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
325 private void initIncludes()
329 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
332 private void initRepositoryMap()
334 synchronized ( this.repositoryMap )
336 this.repositoryMap.clear();
338 Map<String, ManagedRepositoryConfiguration> map =
339 configuration.getConfiguration().getManagedRepositoriesAsMap();
341 for ( Map.Entry<String, ManagedRepositoryConfiguration> entry : map.entrySet() )
343 this.repositoryMap.put( entry.getKey(), entry.getValue() );
348 public void initialize()
349 throws InitializationException
353 configuration.addChangeListener( this );