1 package org.apache.maven.repository.discovery;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.DefaultArtifact;
21 import org.apache.maven.artifact.handler.ArtifactHandler;
22 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
23 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
24 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
25 import org.apache.maven.artifact.repository.metadata.Metadata;
26 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
27 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
28 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
29 import org.apache.maven.artifact.versioning.VersionRange;
30 import org.codehaus.plexus.util.DirectoryScanner;
31 import org.codehaus.plexus.util.FileUtils;
32 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35 import java.io.FileNotFoundException;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.io.Reader;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.Iterator;
45 import java.util.List;
46 import java.util.StringTokenizer;
49 * This class gets all the paths that contain the metadata files.
51 public class DefaultMetadataDiscoverer
52 implements MetadataDiscoverer
56 * Standard patterns to exclude from discovery as they are not artifacts.
58 private static final String[] STANDARD_DISCOVERY_EXCLUDES = {"bin/**", "reports/**", ".maven/**", "**/*.md5",
59 "**/*.MD5", "**/*.sha1", "**/*.SHA1", "**/*snapshot-version", "*/website/**", "*/licenses/**", "*/licences/**",
60 "**/.htaccess", "**/*.html", "**/*.asc", "**/*.txt", "**/README*", "**/CHANGELOG*", "**/KEYS*"};
63 * Standard patterns to include in discovery of metadata files.
65 private static final String[] STANDARD_DISCOVERY_INCLUDES = {"**/*-metadata.xml", "**/*/*-metadata.xml",
66 "**/*/*/*-metadata.xml", "**/*-metadata-*.xml", "**/*/*-metadata-*.xml", "**/*/*/*-metadata-*.xml"};
68 private static final String[] EMPTY_STRING_ARRAY = new String[0];
70 private List excludedPaths = new ArrayList();
72 private List kickedOutPaths = new ArrayList();
75 * Search the repository for metadata files.
77 * @param repositoryBase
78 * @param blacklistedPatterns
80 public List discoverMetadata( File repositoryBase, String blacklistedPatterns )
82 List metadataFiles = new ArrayList();
83 String[] metadataPaths = scanForMetadataPaths( repositoryBase, blacklistedPatterns );
85 for ( int i = 0; i < metadataPaths.length; i++ )
87 RepositoryMetadata metadata = buildMetadata( repositoryBase
88 .getPath(), metadataPaths[i] );
90 if ( metadata != null )
92 metadataFiles.add( metadata );
96 kickedOutPaths.add( metadataPaths[i] );
100 return metadataFiles;
104 * Create RepositoryMetadata object.
106 * @param repo The path to the repository.
107 * @param metadataPath The path to the metadata file.
110 private RepositoryMetadata buildMetadata( String repo, String metadataPath )
113 RepositoryMetadata metadata = null;
117 URL url = new File( repo + "/" + metadataPath ).toURL();
118 InputStream is = url.openStream();
119 Reader reader = new InputStreamReader( is );
120 MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
122 Metadata m = metadataReader.read( reader );
123 String metaGroupId = m.getGroupId();
124 String metaArtifactId = m.getArtifactId();
125 String metaVersion = m.getVersion();
127 // check if the groupId, artifactId and version is in the
129 // parse the path, in reverse order
130 List pathParts = new ArrayList();
131 StringTokenizer st = new StringTokenizer( metadataPath, "/\\" );
132 while ( st.hasMoreTokens() )
134 pathParts.add( st.nextToken() );
137 Collections.reverse( pathParts );
138 // remove the metadata file
139 pathParts.remove( 0 );
140 Iterator it = pathParts.iterator();
141 String tmpDir = (String) it.next();
143 ArtifactHandler handler = new DefaultArtifactHandler( "jar" );
144 VersionRange version = VersionRange.createFromVersion( metaVersion );
146 new DefaultArtifact( metaGroupId, metaArtifactId, version, "compile", "jar", "", handler );
149 if ( tmpDir.equals( metaVersion ) )
151 metadata = new SnapshotArtifactRepositoryMetadata( artifact );
153 else if ( tmpDir.equals( metaArtifactId ) )
156 metadata = new ArtifactRepositoryMetadata( artifact );
161 String groupDir = "";
163 for ( it = pathParts.iterator(); it.hasNext(); )
167 groupDir = (String) it.next();
171 groupDir = (String) it.next() + "." + groupDir;
177 if ( metaGroupId.equals( groupDir ) )
179 metadata = new GroupRepositoryMetadata( metaGroupId );
184 catch ( FileNotFoundException fe )
188 catch ( XmlPullParserException xe )
192 catch ( IOException ie )
201 * Scan or search for metadata files.
203 * @param repositoryBase The repository directory.
204 * @param blacklistedPatterns The patterns to be exluded from the search.
207 private String[] scanForMetadataPaths( File repositoryBase, String blacklistedPatterns )
210 List allExcludes = new ArrayList();
211 allExcludes.addAll( FileUtils.getDefaultExcludesAsList() );
212 allExcludes.addAll( Arrays.asList( STANDARD_DISCOVERY_EXCLUDES ) );
214 if ( blacklistedPatterns != null && blacklistedPatterns.length() > 0 )
216 allExcludes.addAll( Arrays.asList( blacklistedPatterns.split( "," ) ) );
219 DirectoryScanner scanner = new DirectoryScanner();
220 scanner.setBasedir( repositoryBase );
221 scanner.setIncludes( STANDARD_DISCOVERY_INCLUDES );
222 scanner.setExcludes( (String[]) allExcludes.toArray( EMPTY_STRING_ARRAY ) );
225 excludedPaths.addAll( Arrays.asList( scanner.getExcludedFiles() ) );
227 return scanner.getIncludedFiles();
231 public Iterator getExcludedPathsIterator()
233 return excludedPaths.iterator();
236 public Iterator getKickedOutPathsIterator()
238 return kickedOutPaths.iterator();