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.consumers.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 repositoryMap = new HashMap();
103 // TODO: why is this not used? If it should be, what about excludes?
104 private List<String> includes = new ArrayList<String>();
106 public String getId()
111 public String getDescription()
116 public boolean isPermanent()
121 public void beginScan()
126 public void completeScan()
131 public List getIncludedTypes()
137 * Check whether the artifact is in its proper location. The location of the artifact
138 * is validated first against the groupId, artifactId and versionId in the specified model
139 * object (pom in the file system). Then unpack the artifact (jar file) and get the model (pom)
140 * included in the package. If a model exists inside the package, then check if the artifact's
141 * location is valid based on the location specified in the pom. Check if the both the location
142 * specified in the file system pom and in the pom included in the package is the same.
144 public void processArchivaArtifact( ArchivaArtifact artifact )
145 throws ConsumerException
147 ManagedRepositoryConfiguration repository = findRepository( artifact );
149 File artifactFile = new File( repository.getLocation(), toPath( artifact ) );
150 ArchivaProjectModel fsModel = readFilesystemModel( artifactFile );
151 ArchivaProjectModel embeddedModel = readEmbeddedModel( artifact, artifactFile );
153 validateAppropriateModel( "Filesystem", artifact, fsModel );
154 validateAppropriateModel( "Embedded", artifact, embeddedModel );
157 private void validateAppropriateModel( String location, ArchivaArtifact artifact, ArchivaProjectModel model )
158 throws ConsumerException
162 if ( !StringUtils.equals( model.getGroupId(), artifact.getGroupId() ) )
164 addProblem( artifact, "The groupId of the " + location +
165 " project model doesn't match with the artifact, expected <" + artifact.getGroupId() +
166 ">, but was actually <" + model.getGroupId() + ">" );
169 if ( !StringUtils.equals( model.getArtifactId(), artifact.getArtifactId() ) )
171 addProblem( artifact, "The artifactId of the " + location +
172 " project model doesn't match with the artifact, expected <" + artifact.getArtifactId() +
173 ">, but was actually <" + model.getArtifactId() + ">" );
176 if ( !StringUtils.equals( model.getVersion(), artifact.getVersion() ) )
178 addProblem( artifact, "The version of the " + location +
179 " project model doesn't match with the artifact, expected <" + artifact.getVersion() +
180 ">, but was actually <" + model.getVersion() + ">" );
185 private ArchivaProjectModel readEmbeddedModel( ArchivaArtifact artifact, File artifactFile )
186 throws ConsumerException
190 JarFile jar = new JarFile( artifactFile );
192 // Get the entry and its input stream.
193 JarEntry expectedEntry = jar.getJarEntry(
194 "META-INF/maven/" + artifact.getGroupId() + "/" + artifact.getArtifactId() + "/pom.xml" );
196 if ( expectedEntry != null )
198 // TODO: read and resolve model here.
202 /* Expected Entry not found, look for alternate that might
203 * indicate that the artifact is, indeed located in the wrong place.
206 List actualPomXmls = findJarEntryPattern( jar, "META-INF/maven/**/pom.xml" );
207 if ( actualPomXmls.isEmpty() )
213 // TODO: test for invalid actual pom.xml
216 catch ( IOException e )
218 // Not able to read from the file.
219 String emsg = "Unable to read file contents: " + e.getMessage();
220 addProblem( artifact, emsg );
226 private List<JarEntry> findJarEntryPattern( JarFile jar, String pattern )
228 List<JarEntry> hits = new ArrayList<JarEntry>();
230 Enumeration<JarEntry> entries = jar.entries();
231 while ( entries.hasMoreElements() )
233 JarEntry entry = entries.nextElement();
234 if ( SelectorUtils.match( pattern, entry.getName() ) )
243 private void addProblem( ArchivaArtifact artifact, String msg )
244 throws ConsumerException
246 RepositoryProblem problem = new RepositoryProblem();
247 problem.setRepositoryId( artifact.getModel().getRepositoryId() );
248 problem.setPath( toPath( artifact ) );
249 problem.setGroupId( artifact.getGroupId() );
250 problem.setArtifactId( artifact.getArtifactId() );
251 problem.setVersion( artifact.getVersion() );
252 problem.setType( LocationArtifactsReport.PROBLEM_TYPE_BAD_ARTIFACT_LOCATION );
253 problem.setOrigin( getId() );
254 problem.setMessage( msg );
258 dao.getRepositoryProblemDAO().saveRepositoryProblem( problem );
260 catch ( ArchivaDatabaseException e )
262 String emsg = "Unable to save problem with artifact location to DB: " + e.getMessage();
264 throw new ConsumerException( emsg, e );
268 private ArchivaProjectModel readFilesystemModel( File artifactFile )
270 File pomFile = createPomFileReference( artifactFile );
272 // TODO: read and resolve model here.
277 private File createPomFileReference( File artifactFile )
279 String pomFilename = artifactFile.getAbsolutePath();
281 int pos = pomFilename.lastIndexOf( '.' );
288 pomFilename = pomFilename.substring( 0, pos ) + ".pom";
289 return new File( pomFilename );
292 private ManagedRepositoryConfiguration findRepository( ArchivaArtifact artifact )
294 return (ManagedRepositoryConfiguration) this.repositoryMap.get( artifact.getModel().getRepositoryId() );
297 private String toPath( ArchivaArtifact artifact )
301 String repoId = artifact.getModel().getRepositoryId();
302 ManagedRepositoryContent repo = repositoryFactory.getManagedRepositoryContent( repoId );
303 return repo.toPath( artifact );
305 catch ( RepositoryException e )
307 log.warn( "Unable to calculate path for artifact: " + artifact );
312 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
314 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
319 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
325 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
330 private void initIncludes()
334 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
337 private void initRepositoryMap()
339 synchronized ( this.repositoryMap )
341 this.repositoryMap.clear();
343 Map<String, ManagedRepositoryConfiguration> map =
344 configuration.getConfiguration().getManagedRepositoriesAsMap();
346 for ( Map.Entry<String, ManagedRepositoryConfiguration> entry : map.entrySet() )
348 this.repositoryMap.put( entry.getKey(), entry.getValue() );
353 public void initialize()
354 throws InitializationException
358 configuration.addChangeListener( this );