1 package org.apache.maven.archiva.reporting.metadata;
3 import org.apache.commons.lang.StringUtils;
4 import org.codehaus.plexus.util.FileUtils;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.Iterator;
13 import java.util.List;
17 * MetadataValidateConsumer
19 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
22 * TODO: whoops, how do we consumer metadata?
24 public class MetadataValidateConsumer
28 // * Process the metadata encountered in the repository and report all errors found, if any.
30 // * @param metadata the metadata to be processed.
31 // * @param repository the repository where the metadata was encountered
32 // * @param reporter the ReportingDatabase to receive processing results
34 // public void processMetadata( RepositoryMetadata metadata, ArtifactRepository repository )
36 // if ( metadata.storedInGroupDirectory() )
40 // checkPluginMetadata( metadata, repository );
42 // catch ( IOException e )
44 // addWarning( metadata, null, "Error getting plugin artifact directories versions: " + e );
49 // Versioning versioning = metadata.getMetadata().getVersioning();
50 // boolean found = false;
51 // if ( versioning != null )
53 // String lastUpdated = versioning.getLastUpdated();
54 // if ( lastUpdated != null && lastUpdated.length() != 0 )
61 // addFailure( metadata, "missing-last-updated", "Missing lastUpdated element inside the metadata." );
64 // if ( metadata.storedInArtifactVersionDirectory() )
66 // checkSnapshotMetadata( metadata, repository );
70 // checkMetadataVersions( metadata, repository );
74 // checkRepositoryVersions( metadata, repository );
76 // catch ( IOException e )
78 // String reason = "Error getting plugin artifact directories versions: " + e;
79 // addWarning( metadata, null, reason );
85 // private void addWarning( RepositoryMetadata metadata, String problem, String reason )
87 // // TODO: reason could be an i18n key derived from the processor and the problem ID and the
88 // database.addWarning( metadata, ROLE_HINT, problem, reason );
92 // * Method for processing a GroupRepositoryMetadata
94 // * @param metadata the metadata to be processed.
95 // * @param repository the repository where the metadata was encountered
96 // * @param reporter the ReportingDatabase to receive processing results
98 // private void checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository )
101 // File metadataDir = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( metadata ) )
103 // List pluginDirs = getArtifactIdFiles( metadataDir );
105 // Map prefixes = new HashMap();
106 // for ( Iterator plugins = metadata.getMetadata().getPlugins().iterator(); plugins.hasNext(); )
108 // Plugin plugin = (Plugin) plugins.next();
110 // String artifactId = plugin.getArtifactId();
111 // if ( artifactId == null || artifactId.length() == 0 )
113 // addFailure( metadata, "missing-artifact-id:" + plugin.getPrefix(),
114 // "Missing or empty artifactId in group metadata for plugin " + plugin.getPrefix() );
117 // String prefix = plugin.getPrefix();
118 // if ( prefix == null || prefix.length() == 0 )
120 // addFailure( metadata, "missing-plugin-prefix:" + artifactId,
121 // "Missing or empty plugin prefix for artifactId " + artifactId + "." );
125 // if ( prefixes.containsKey( prefix ) )
127 // addFailure( metadata, "duplicate-plugin-prefix:" + prefix, "Duplicate plugin prefix found: "
132 // prefixes.put( prefix, plugin );
136 // if ( artifactId != null && artifactId.length() > 0 )
138 // File pluginDir = new File( metadataDir, artifactId );
139 // if ( !pluginDirs.contains( pluginDir ) )
141 // addFailure( metadata, "missing-plugin-from-repository:" + artifactId, "Metadata plugin "
142 // + artifactId + " not found in the repository" );
146 // pluginDirs.remove( pluginDir );
151 // if ( pluginDirs.size() > 0 )
153 // for ( Iterator plugins = pluginDirs.iterator(); plugins.hasNext(); )
155 // File plugin = (File) plugins.next();
156 // addFailure( metadata, "missing-plugin-from-metadata:" + plugin.getName(), "Plugin " + plugin.getName()
157 // + " is present in the repository but " + "missing in the metadata." );
163 // * Method for processing a SnapshotArtifactRepository
165 // * @param metadata the metadata to be processed.
166 // * @param repository the repository where the metadata was encountered
167 // * @param reporter the ReportingDatabase to receive processing results
169 // private void checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository )
171 // RepositoryQueryLayer repositoryQueryLayer = repositoryQueryLayerFactory.createRepositoryQueryLayer( repository );
173 // Versioning versioning = metadata.getMetadata().getVersioning();
174 // if ( versioning != null )
176 // Snapshot snapshot = versioning.getSnapshot();
178 // String version = StringUtils.replace( metadata.getBaseVersion(), Artifact.SNAPSHOT_VERSION, snapshot
180 // + "-" + snapshot.getBuildNumber() );
181 // Artifact artifact = artifactFactory.createProjectArtifact( metadata.getGroupId(), metadata.getArtifactId(),
183 // artifact.isSnapshot(); // trigger baseVersion correction
185 // if ( !repositoryQueryLayer.containsArtifact( artifact ) )
187 // addFailure( metadata, "missing-snapshot-artifact-from-repository:" + version, "Snapshot artifact "
188 // + version + " does not exist." );
194 // * Method for validating the versions declared inside an ArtifactRepositoryMetadata
196 // * @param metadata the metadata to be processed.
197 // * @param repository the repository where the metadata was encountered
198 // * @param reporter the ReportingDatabase to receive processing results
200 // private void checkMetadataVersions( RepositoryMetadata metadata, ArtifactRepository repository )
202 // RepositoryQueryLayer repositoryQueryLayer = repositoryQueryLayerFactory.createRepositoryQueryLayer( repository );
204 // Versioning versioning = metadata.getMetadata().getVersioning();
205 // if ( versioning != null )
207 // for ( Iterator versions = versioning.getVersions().iterator(); versions.hasNext(); )
209 // String version = (String) versions.next();
211 // Artifact artifact = artifactFactory.createProjectArtifact( metadata.getGroupId(), metadata
212 // .getArtifactId(), version );
214 // if ( !repositoryQueryLayer.containsArtifact( artifact ) )
216 // addFailure( metadata, "missing-artifact-from-repository:" + version, "Artifact version " + version
217 // + " is present in metadata but " + "missing in the repository." );
224 // * Searches the artifact repository directory for all versions and verifies that all of them are listed in the
225 // * ArtifactRepositoryMetadata
227 // * @param metadata the metadata to be processed.
228 // * @param repository the repository where the metadata was encountered
229 // * @param reporter the ReportingDatabase to receive processing results
230 // * @throws java.io.IOException if there is a problem reading from the file system
232 // private void checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository )
233 // throws IOException
235 // Versioning versioning = metadata.getMetadata().getVersioning();
236 // List metadataVersions = versioning != null ? versioning.getVersions() : Collections.EMPTY_LIST;
237 // File versionsDir = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( metadata ) )
240 // // TODO: I don't know how this condition can happen, but it was seen on the main repository.
241 // // Avoid hard failure
242 // if ( versionsDir.exists() )
244 // List versions = FileUtils.getFileNames( versionsDir, "*/*.pom", null, false );
245 // for ( Iterator i = versions.iterator(); i.hasNext(); )
247 // File path = new File( (String) i.next() );
248 // String version = path.getParentFile().getName();
249 // if ( !metadataVersions.contains( version ) )
251 // addFailure( metadata, "missing-artifact-from-metadata:" + version, "Artifact version " + version
252 // + " found in the repository but " + "missing in the metadata." );
258 // addFailure( metadata, null, "Metadata's directory did not exist: " + versionsDir );
263 // * Used to gather artifactIds from a groupId directory.
265 // * @param groupIdDir the directory of the group
266 // * @return the list of artifact ID File objects for each directory
267 // * @throws IOException if there was a failure to read the directories
269 // private List getArtifactIdFiles( File groupIdDir )
270 // throws IOException
272 // List artifactIdFiles = new ArrayList();
274 // File[] files = groupIdDir.listFiles();
275 // if ( files != null )
277 // for ( Iterator i = Arrays.asList( files ).iterator(); i.hasNext(); )
279 // File artifactDir = (File) i.next();
281 // if ( artifactDir.isDirectory() )
283 // List versions = FileUtils.getFileNames( artifactDir, "*/*.pom", null, false );
284 // if ( versions.size() > 0 )
286 // artifactIdFiles.add( artifactDir );
292 // return artifactIdFiles;
295 // private void addFailure( RepositoryMetadata metadata, String problem, String reason )
297 // // TODO: reason could be an i18n key derived from the processor and the problem ID and the
298 // database.addFailure( metadata, ROLE_HINT, problem, reason );