]> source.dussan.org Git - archiva.git/blob
71095806969893dd0c2578c34851bee6ea02eb30
[archiva.git] /
1 package org.apache.maven.repository.discovery;
2
3 /*
4  * Copyright 2005-2006 The Apache Software Foundation.
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.codehaus.plexus.util.StringUtils;
22
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.StringTokenizer;
28
29 /**
30  * Artifact discoverer for the new repository layout (Maven 2.0+).
31  *
32  * @author John Casey
33  * @author Brett Porter
34  */
35 public class DefaultArtifactDiscoverer
36     extends AbstractArtifactDiscoverer
37     implements ArtifactDiscoverer
38 {
39     private ArtifactFactory artifactFactory;
40
41     public List discoverArtifacts( File repositoryBase, String blacklistedPatterns, boolean includeSnapshots )
42     {
43         List artifacts = new ArrayList();
44
45         String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
46
47         for ( int i = 0; i < artifactPaths.length; i++ )
48         {
49             String path = artifactPaths[i];
50
51             Artifact artifact = buildArtifact( path );
52
53             if ( artifact != null )
54             {
55                 if ( includeSnapshots || !artifact.isSnapshot() )
56                 {
57                     artifacts.add( artifact );
58                 }
59             }
60         }
61
62         return artifacts;
63     }
64
65     private Artifact buildArtifact( String path )
66     {
67         List pathParts = new ArrayList();
68         StringTokenizer st = new StringTokenizer( path, "/\\" );
69         while ( st.hasMoreTokens() )
70         {
71             pathParts.add( st.nextToken() );
72         }
73
74         Collections.reverse( pathParts );
75
76         Artifact finalResult = null;
77         if ( pathParts.size() < 4 )
78         {
79             addKickedOutPath( path );
80         }
81         else
82         {
83             // the actual artifact filename.
84             String filename = (String) pathParts.remove( 0 );
85
86             // the next one is the version.
87             String version = (String) pathParts.remove( 0 );
88
89             // the next one is the artifactId.
90             String artifactId = (String) pathParts.remove( 0 );
91
92             // the remaining are the groupId.
93             Collections.reverse( pathParts );
94             String groupId = StringUtils.join( pathParts.iterator(), "." );
95
96             String remainingFilename = filename;
97             if ( !remainingFilename.startsWith( artifactId + "-" ) )
98             {
99                 addKickedOutPath( path );
100             }
101             else
102             {
103                 remainingFilename = remainingFilename.substring( artifactId.length() + 1 );
104
105                 String classifier = null;
106
107                 // TODO: use artifact handler, share with legacy discoverer
108                 String type = null;
109                 if ( remainingFilename.endsWith( ".tar.gz" ) )
110                 {
111                     type = "distribution-tgz";
112                     remainingFilename =
113                         remainingFilename.substring( 0, remainingFilename.length() - ".tar.gz".length() );
114                 }
115                 else if ( remainingFilename.endsWith( ".zip" ) )
116                 {
117                     type = "distribution-zip";
118                     remainingFilename = remainingFilename.substring( 0, remainingFilename.length() - ".zip".length() );
119                 }
120                 else if ( remainingFilename.endsWith( "-sources.jar" ) )
121                 {
122                     type = "java-source";
123                     classifier = "sources";
124                     remainingFilename =
125                         remainingFilename.substring( 0, remainingFilename.length() - "-sources.jar".length() );
126                 }
127                 else
128                 {
129                     int index = remainingFilename.lastIndexOf( "." );
130                     if ( index < 0 )
131                     {
132                         addKickedOutPath( path );
133                     }
134                     else
135                     {
136                         type = remainingFilename.substring( index + 1 );
137                         remainingFilename = remainingFilename.substring( 0, index );
138                     }
139                 }
140
141                 if ( type != null )
142                 {
143                     Artifact result;
144
145                     if ( classifier == null )
146                     {
147                         result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME,
148                                                                  type );
149                     }
150                     else
151                     {
152                         result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
153                                                                                classifier );
154                     }
155
156                     if ( result.isSnapshot() )
157                     {
158                         // version is XXX-SNAPSHOT, filename is XXX-yyyyMMdd.hhmmss-b
159                         int classifierIndex = remainingFilename.indexOf( '-', version.length() + 8 );
160                         if ( classifierIndex >= 0 )
161                         {
162                             classifier = remainingFilename.substring( classifierIndex + 1 );
163                             remainingFilename = remainingFilename.substring( 0, classifierIndex );
164                             result = artifactFactory.createArtifactWithClassifier( groupId, artifactId,
165                                                                                    remainingFilename, type,
166                                                                                    classifier );
167                         }
168                         else
169                         {
170                             result = artifactFactory.createArtifact( groupId, artifactId, remainingFilename,
171                                                                      Artifact.SCOPE_RUNTIME, type );
172                         }
173
174                         // poor encapsulation requires we do this to populate base version
175                         if ( !result.isSnapshot() )
176                         {
177                             addKickedOutPath( path );
178                         }
179                         else if ( !result.getBaseVersion().equals( version ) )
180                         {
181                             addKickedOutPath( path );
182                         }
183                         else
184                         {
185                             finalResult = result;
186                         }
187                     }
188                     else if ( !remainingFilename.startsWith( version ) )
189                     {
190                         addKickedOutPath( path );
191                     }
192                     else if ( !remainingFilename.equals( version ) )
193                     {
194                         if ( remainingFilename.charAt( version.length() ) != '-' )
195                         {
196                             addKickedOutPath( path );
197                         }
198                         else
199                         {
200                             classifier = remainingFilename.substring( version.length() + 1 );
201                             finalResult = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version,
202                                                                                         type, classifier );
203                         }
204                     }
205                 }
206             }
207         }
208
209         if ( finalResult != null )
210         {
211             finalResult.setFile( new File( path ) );
212         }
213
214         return finalResult;
215     }
216 }