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
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Enumeration;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.jar.JarEntry;
30 import java.util.jar.JarFile;
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
34 import org.apache.maven.archiva.configuration.ConfigurationNames;
35 import org.apache.maven.archiva.configuration.FileTypes;
36 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
37 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
38 import org.apache.maven.archiva.consumers.ConsumerException;
39 import org.apache.maven.archiva.database.ArchivaDAO;
40 import org.apache.maven.archiva.database.ArchivaDatabaseException;
41 import org.apache.maven.archiva.database.updater.ArchivaArtifactConsumer;
42 import org.apache.maven.archiva.model.ArchivaArtifact;
43 import org.apache.maven.archiva.model.ArchivaProjectModel;
44 import org.apache.maven.archiva.model.RepositoryProblem;
45 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
46 import org.apache.maven.archiva.repository.RepositoryContentFactory;
47 import org.apache.maven.archiva.repository.RepositoryException;
48 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
49 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
50 import org.codehaus.plexus.registry.Registry;
51 import org.codehaus.plexus.registry.RegistryListener;
52 import org.codehaus.plexus.util.SelectorUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * Validate the location of the artifact based on the values indicated
58 * in its pom (both the pom packaged with the artifact & the pom in the
62 * @plexus.component role="org.apache.maven.archiva.database.updater.ArchivaArtifactConsumer"
63 * role-hint="validate-artifacts-location"
65 public class LocationArtifactsConsumer
66 extends AbstractMonitoredConsumer
67 implements ArchivaArtifactConsumer, RegistryListener, Initializable
69 private Logger log = LoggerFactory.getLogger( LocationArtifactsConsumer.class );
72 * @plexus.configuration default-value="duplicate-artifacts"
77 * @plexus.configuration default-value="Check for Duplicate Artifacts via SHA1 Checksums"
79 private String description;
84 private ArchivaConfiguration configuration;
89 private FileTypes filetypes;
92 * @plexus.requirement role-hint="jdo"
94 private ArchivaDAO dao;
99 private RepositoryContentFactory repositoryFactory;
101 private Map<String, ManagedRepositoryConfiguration> repositoryMap =
102 new HashMap<String, ManagedRepositoryConfiguration>();
104 // TODO: why is this not used? If it should be, what about excludes?
105 private List<String> includes = new ArrayList<String>();
107 public String getId()
112 public String getDescription()
117 public boolean isPermanent()
122 public void beginScan()
127 public void completeScan()
132 public List<String> getIncludedTypes()
138 * Check whether the artifact is in its proper location. The location of the artifact
139 * is validated first against the groupId, artifactId and versionId in the specified model
140 * object (pom in the file system). Then unpack the artifact (jar file) and get the model (pom)
141 * included in the package. If a model exists inside the package, then check if the artifact's
142 * location is valid based on the location specified in the pom. Check if the both the location
143 * specified in the file system pom and in the pom included in the package is the same.
145 public void processArchivaArtifact( ArchivaArtifact artifact )
146 throws ConsumerException
148 ManagedRepositoryConfiguration repository = findRepository( artifact );
150 File artifactFile = new File( repository.getLocation(), toPath( artifact ) );
151 ArchivaProjectModel fsModel = readFilesystemModel( artifactFile );
152 ArchivaProjectModel embeddedModel = readEmbeddedModel( artifact, artifactFile );
154 validateAppropriateModel( "Filesystem", artifact, fsModel );
155 validateAppropriateModel( "Embedded", artifact, embeddedModel );
158 private void validateAppropriateModel( String location, ArchivaArtifact artifact, ArchivaProjectModel model )
159 throws ConsumerException
163 if ( !StringUtils.equals( model.getGroupId(), artifact.getGroupId() ) )
165 addProblem( artifact, "The groupId of the " + location +
166 " project model doesn't match with the artifact, expected <" + artifact.getGroupId() +
167 ">, but was actually <" + model.getGroupId() + ">" );
170 if ( !StringUtils.equals( model.getArtifactId(), artifact.getArtifactId() ) )
172 addProblem( artifact, "The artifactId of the " + location +
173 " project model doesn't match with the artifact, expected <" + artifact.getArtifactId() +
174 ">, but was actually <" + model.getArtifactId() + ">" );
177 if ( !StringUtils.equals( model.getVersion(), artifact.getVersion() ) )
179 addProblem( artifact, "The version of the " + location +
180 " project model doesn't match with the artifact, expected <" + artifact.getVersion() +
181 ">, but was actually <" + model.getVersion() + ">" );
186 private ArchivaProjectModel readEmbeddedModel( ArchivaArtifact artifact, File artifactFile )
187 throws ConsumerException
191 JarFile jar = new JarFile( artifactFile );
193 // Get the entry and its input stream.
194 JarEntry expectedEntry = jar.getJarEntry(
195 "META-INF/maven/" + artifact.getGroupId() + "/" + artifact.getArtifactId() + "/pom.xml" );
197 if ( expectedEntry != null )
199 // TODO: read and resolve model here.
203 /* Expected Entry not found, look for alternate that might
204 * indicate that the artifact is, indeed located in the wrong place.
207 List<JarEntry> actualPomXmls = findJarEntryPattern( jar, "META-INF/maven/**/pom.xml" );
208 if ( actualPomXmls.isEmpty() )
214 // TODO: test for invalid actual pom.xml
217 catch ( IOException e )
219 // Not able to read from the file.
220 String emsg = "Unable to read file contents: " + e.getMessage();
221 addProblem( artifact, emsg );
227 private List<JarEntry> findJarEntryPattern( JarFile jar, String pattern )
229 List<JarEntry> hits = new ArrayList<JarEntry>();
231 Enumeration<JarEntry> entries = jar.entries();
232 while ( entries.hasMoreElements() )
234 JarEntry entry = entries.nextElement();
235 if ( SelectorUtils.match( pattern, entry.getName() ) )
244 private void addProblem( ArchivaArtifact artifact, String msg )
245 throws ConsumerException
247 RepositoryProblem problem = new RepositoryProblem();
248 problem.setRepositoryId( artifact.getModel().getRepositoryId() );
249 problem.setPath( toPath( artifact ) );
250 problem.setGroupId( artifact.getGroupId() );
251 problem.setArtifactId( artifact.getArtifactId() );
252 problem.setVersion( artifact.getVersion() );
253 problem.setType( LocationArtifactsReport.PROBLEM_TYPE_BAD_ARTIFACT_LOCATION );
254 problem.setOrigin( getId() );
255 problem.setMessage( msg );
259 dao.getRepositoryProblemDAO().saveRepositoryProblem( problem );
261 catch ( ArchivaDatabaseException e )
263 String emsg = "Unable to save problem with artifact location to DB: " + e.getMessage();
265 throw new ConsumerException( emsg, e );
269 private ArchivaProjectModel readFilesystemModel( File artifactFile )
271 // File pomFile = createPomFileReference( artifactFile );
273 // TODO: read and resolve model here.
278 // private File createPomFileReference( File artifactFile )
280 // String pomFilename = artifactFile.getAbsolutePath();
282 // int pos = pomFilename.lastIndexOf( '.' );
285 // // Invalid filename.
289 // pomFilename = pomFilename.substring( 0, pos ) + ".pom";
290 // return new File( pomFilename );
293 private ManagedRepositoryConfiguration findRepository( ArchivaArtifact artifact )
295 return (ManagedRepositoryConfiguration) this.repositoryMap.get( artifact.getModel().getRepositoryId() );
298 private String toPath( ArchivaArtifact artifact )
302 String repoId = artifact.getModel().getRepositoryId();
303 ManagedRepositoryContent repo = repositoryFactory.getManagedRepositoryContent( repoId );
304 return repo.toPath( artifact );
306 catch ( RepositoryException e )
308 log.warn( "Unable to calculate path for artifact: " + artifact );
313 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
315 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
320 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
326 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
331 private void initIncludes()
335 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
338 private void initRepositoryMap()
340 synchronized ( this.repositoryMap )
342 this.repositoryMap.clear();
344 Map<String, ManagedRepositoryConfiguration> map =
345 configuration.getConfiguration().getManagedRepositoriesAsMap();
347 for ( Map.Entry<String, ManagedRepositoryConfiguration> entry : map.entrySet() )
349 this.repositoryMap.put( entry.getKey(), entry.getValue() );
354 public void initialize()
355 throws InitializationException
359 configuration.addChangeListener( this );