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.factory.ArtifactFactory;
21 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
22 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
23 import org.apache.maven.artifact.repository.metadata.Metadata;
24 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
25 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
26 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
27 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.io.Reader;
35 import java.net.MalformedURLException;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.StringTokenizer;
44 * This class gets all the paths that contain the metadata files.
46 * @plexus.component role="org.apache.maven.repository.discovery.MetadataDiscoverer" role-hint="org.apache.maven.repository.discovery.DefaultMetadataDiscoverer"
48 public class DefaultMetadataDiscoverer
49 extends AbstractArtifactDiscoverer
50 implements MetadataDiscoverer
55 private ArtifactFactory artifactFactory;
58 * Standard patterns to include in discovery of metadata files.
60 private static final String[] STANDARD_DISCOVERY_INCLUDES = {"**/*-metadata.xml", "**/*/*-metadata.xml",
61 "**/*/*/*-metadata.xml", "**/*-metadata-*.xml", "**/*/*-metadata-*.xml", "**/*/*/*-metadata-*.xml"};
64 * Search the repository for metadata files.
66 * @param repositoryBase
67 * @param blacklistedPatterns
69 public List discoverMetadata( File repositoryBase, String blacklistedPatterns )
71 List metadataFiles = new ArrayList();
72 String[] metadataPaths =
73 scanForArtifactPaths( repositoryBase, blacklistedPatterns, STANDARD_DISCOVERY_INCLUDES, null );
75 for ( int i = 0; i < metadataPaths.length; i++ )
77 RepositoryMetadata metadata = buildMetadata( repositoryBase.getPath(), metadataPaths[i] );
79 if ( metadata != null )
81 metadataFiles.add( metadata );
85 addKickedOutPath( metadataPaths[i] );
93 * Create RepositoryMetadata object.
95 * @param repo The path to the repository.
96 * @param metadataPath The path to the metadata file.
97 * @return the metadata
99 private RepositoryMetadata buildMetadata( String repo, String metadataPath )
101 RepositoryMetadata metadata = null;
105 URL url = new File( repo + "/" + metadataPath ).toURL();
106 InputStream is = url.openStream();
107 Reader reader = new InputStreamReader( is );
108 MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
110 Metadata m = metadataReader.read( reader );
111 String metaGroupId = m.getGroupId();
112 String metaArtifactId = m.getArtifactId();
113 String metaVersion = m.getVersion();
115 // check if the groupId, artifactId and version is in the
117 // parse the path, in reverse order
118 List pathParts = new ArrayList();
119 StringTokenizer st = new StringTokenizer( metadataPath, "/\\" );
120 while ( st.hasMoreTokens() )
122 pathParts.add( st.nextToken() );
125 Collections.reverse( pathParts );
126 // remove the metadata file
127 pathParts.remove( 0 );
128 Iterator it = pathParts.iterator();
129 String tmpDir = (String) it.next();
131 //ArtifactHandler handler = new DefaultArtifactHandler( "jar" );
132 //if( metaVersion != null && !metaVersion.equals( "" ) )
134 // VersionRange version = VersionRange.createFromVersion( metaVersion );
137 Artifact artifact = null;
138 if ( metaVersion != null && !metaVersion.equals( "" ) )
140 artifact = artifactFactory.createBuildArtifact( metaGroupId, metaArtifactId, metaVersion, "jar" );
144 if ( tmpDir != null && tmpDir.equals( metaVersion ) )
146 if ( artifact != null )
148 metadata = new SnapshotArtifactRepositoryMetadata( artifact );
151 else if ( tmpDir != null && tmpDir.equals( metaArtifactId ) )
154 if ( artifact != null )
156 metadata = new ArtifactRepositoryMetadata( artifact );
162 String groupDir = "";
164 for ( it = pathParts.iterator(); it.hasNext(); )
166 String path = (String) it.next();
173 groupDir = path + "." + groupDir;
179 if ( metaGroupId != null && metaGroupId.equals( groupDir ) )
181 metadata = new GroupRepositoryMetadata( metaGroupId );
186 catch ( FileNotFoundException fe )
188 // TODO: log ignored metadata
190 catch ( XmlPullParserException xe )
192 // TODO: log ignored metadata
194 catch ( MalformedURLException e )
196 // TODO: log ignored metadata
198 catch ( IOException ie )
200 // TODO: log ignored metadata