1 package org.apache.maven.archiva.consumers;
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.common.utils.BaseFile;
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
26 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
27 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
28 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
29 import org.apache.maven.artifact.repository.metadata.Metadata;
30 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
31 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
32 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
33 import org.codehaus.plexus.util.IOUtil;
34 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
36 import java.io.FileReader;
37 import java.io.IOException;
38 import java.io.Reader;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.StringTokenizer;
46 * GenericRepositoryMetadataConsumer - Consume any maven-metadata.xml files as {@link RepositoryMetadata} objects.
48 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
51 public abstract class GenericRepositoryMetadataConsumer
52 extends AbstractConsumer
55 public abstract void processRepositoryMetadata( RepositoryMetadata metadata, BaseFile file );
57 private static final List includePatterns;
61 includePatterns = new ArrayList();
62 includePatterns.add( "**/maven-metadata.xml" );
65 public List getIncludePatterns()
67 return includePatterns;
70 public boolean isEnabled()
72 // the RepositoryMetadata objects only exist in 'default' layout repositories.
73 ArtifactRepositoryLayout layout = repository.getLayout();
74 return ( layout instanceof DefaultRepositoryLayout );
77 public void processFile( BaseFile file )
78 throws ConsumerException
80 if ( file.length() <= 0 )
82 throw new ConsumerException( file, "File is empty." );
85 if ( !file.canRead() )
87 throw new ConsumerException( file, "Not allowed to read file due to permission settings on file." );
90 RepositoryMetadata metadata = buildMetadata( file );
91 processRepositoryMetadata( metadata, file );
94 private RepositoryMetadata buildMetadata( BaseFile metadataFile )
95 throws ConsumerException
101 reader = new FileReader( metadataFile );
102 MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
104 m = metadataReader.read( reader );
106 catch ( XmlPullParserException e )
108 throw new ConsumerException( metadataFile, "Error parsing metadata file: " + e.getMessage(), e );
110 catch ( IOException e )
112 throw new ConsumerException( metadataFile, "Error reading metadata file: " + e.getMessage(), e );
116 IOUtil.close( reader );
119 RepositoryMetadata repositoryMetadata = buildMetadata( m, metadataFile );
121 if ( repositoryMetadata == null )
123 throw new ConsumerException( metadataFile, "Unable to build a repository metadata from path." );
126 return repositoryMetadata;
130 * Builds a RepositoryMetadata object from a Metadata object and its path.
133 * @param metadataFile file information
134 * @return RepositoryMetadata if the parameters represent one; null if not
135 * @throws ConsumerException
137 private RepositoryMetadata buildMetadata( Metadata m, BaseFile metadataFile )
138 throws ConsumerException
140 if ( artifactFactory == null )
142 throw new IllegalStateException( "Unable to build metadata with a null artifactFactory." );
145 String metaGroupId = m.getGroupId();
146 String metaArtifactId = m.getArtifactId();
147 String metaVersion = m.getVersion();
149 // check if the groupId, artifactId and version is in the
151 // parse the path, in reverse order
152 List pathParts = new ArrayList();
153 StringTokenizer st = new StringTokenizer( metadataFile.getRelativePath(), "/\\" );
154 while ( st.hasMoreTokens() )
156 pathParts.add( st.nextToken() );
159 Collections.reverse( pathParts );
160 // remove the metadata file
161 pathParts.remove( 0 );
162 Iterator it = pathParts.iterator();
163 String tmpDir = (String) it.next();
165 Artifact artifact = null;
166 if ( StringUtils.isNotEmpty( metaVersion ) )
168 artifact = artifactFactory.createProjectArtifact( metaGroupId, metaArtifactId, metaVersion );
172 RepositoryMetadata metadata = null;
173 if ( tmpDir != null && tmpDir.equals( metaVersion ) )
175 if ( artifact != null )
177 metadata = new SnapshotArtifactRepositoryMetadata( artifact );
180 else if ( tmpDir != null && tmpDir.equals( metaArtifactId ) )
183 if ( artifact != null )
185 metadata = new ArtifactRepositoryMetadata( artifact );
189 artifact = artifactFactory.createProjectArtifact( metaGroupId, metaArtifactId, "1.0" );
190 metadata = new ArtifactRepositoryMetadata( artifact );
195 String groupDir = "";
197 for ( it = pathParts.iterator(); it.hasNext(); )
199 String path = (String) it.next();
206 groupDir = path + "." + groupDir;
212 if ( metaGroupId != null && metaGroupId.equals( groupDir ) )
214 metadata = new GroupRepositoryMetadata( metaGroupId );
218 /* If we reached this point, we have some bad metadata.
219 * We have a metadata file, with values for groupId / artifactId / version.
220 * But the information it is providing does not exist relative to the file location.
222 * See ${basedir}/src/test/repository/javax/maven-metadata.xml for example
224 throw new ConsumerException( metadataFile,
225 "Contents of metadata are not appropriate for its location on disk." );