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.repository.metadata.ArtifactRepositoryMetadata;
21 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
22 import org.apache.maven.artifact.repository.metadata.Metadata;
23 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
24 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
25 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
26 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.Reader;
34 import java.net.MalformedURLException;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.StringTokenizer;
43 * This class gets all the paths that contain the metadata files.
45 * @plexus.component role="org.apache.maven.repository.discovery.MetadataDiscoverer" role-hint="org.apache.maven.repository.discovery.DefaultMetadataDiscoverer"
47 public class DefaultMetadataDiscoverer
48 extends AbstractDiscoverer
49 implements MetadataDiscoverer
52 * Standard patterns to include in discovery of metadata files.
54 private static final String[] STANDARD_DISCOVERY_INCLUDES = {"**/*-metadata.xml", "**/*/*-metadata.xml",
55 "**/*/*/*-metadata.xml", "**/*-metadata-*.xml", "**/*/*-metadata-*.xml", "**/*/*/*-metadata-*.xml"};
58 * Search the repository for metadata files.
60 * @param repositoryBase
61 * @param blacklistedPatterns
63 public List discoverMetadata( File repositoryBase, String blacklistedPatterns )
65 List metadataFiles = new ArrayList();
66 String[] metadataPaths =
67 scanForArtifactPaths( repositoryBase, blacklistedPatterns, STANDARD_DISCOVERY_INCLUDES, null );
69 for ( int i = 0; i < metadataPaths.length; i++ )
71 RepositoryMetadata metadata = buildMetadata( repositoryBase.getPath(), metadataPaths[i] );
73 if ( metadata != null )
75 metadataFiles.add( metadata );
79 addKickedOutPath( metadataPaths[i] );
87 * Create RepositoryMetadata object.
89 * @param repo The path to the repository.
90 * @param metadataPath The path to the metadata file.
91 * @return the metadata
93 private RepositoryMetadata buildMetadata( String repo, String metadataPath )
95 RepositoryMetadata metadata = null;
99 URL url = new File( repo + "/" + metadataPath ).toURL();
100 InputStream is = url.openStream();
101 Reader reader = new InputStreamReader( is );
102 MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
104 Metadata m = metadataReader.read( reader );
105 String metaGroupId = m.getGroupId();
106 String metaArtifactId = m.getArtifactId();
107 String metaVersion = m.getVersion();
109 // check if the groupId, artifactId and version is in the
111 // parse the path, in reverse order
112 List pathParts = new ArrayList();
113 StringTokenizer st = new StringTokenizer( metadataPath, "/\\" );
114 while ( st.hasMoreTokens() )
116 pathParts.add( st.nextToken() );
119 Collections.reverse( pathParts );
120 // remove the metadata file
121 pathParts.remove( 0 );
122 Iterator it = pathParts.iterator();
123 String tmpDir = (String) it.next();
125 //ArtifactHandler handler = new DefaultArtifactHandler( "jar" );
126 //if( metaVersion != null && !metaVersion.equals( "" ) )
128 // VersionRange version = VersionRange.createFromVersion( metaVersion );
131 Artifact artifact = null;
132 if ( metaVersion != null && !"".equals( metaVersion ) )
134 artifact = artifactFactory.createBuildArtifact( metaGroupId, metaArtifactId, metaVersion, "jar" );
138 if ( tmpDir != null && tmpDir.equals( metaVersion ) )
140 if ( artifact != null )
142 metadata = new SnapshotArtifactRepositoryMetadata( artifact );
145 else if ( tmpDir != null && tmpDir.equals( metaArtifactId ) )
148 if ( artifact != null )
150 metadata = new ArtifactRepositoryMetadata( artifact );
156 String groupDir = "";
158 for ( it = pathParts.iterator(); it.hasNext(); )
160 String path = (String) it.next();
167 groupDir = path + "." + groupDir;
173 if ( metaGroupId != null && metaGroupId.equals( groupDir ) )
175 metadata = new GroupRepositoryMetadata( metaGroupId );
180 catch ( FileNotFoundException fe )
182 // TODO: log ignored metadata
184 catch ( XmlPullParserException xe )
186 // TODO: log ignored metadata
188 catch ( MalformedURLException e )
190 // TODO: log ignored metadata
192 catch ( IOException ie )
194 // TODO: log ignored metadata