]> source.dussan.org Git - archiva.git/blob
86078ef6ba1701b428a8eabcb1538cf80826f844
[archiva.git] /
1 package org.apache.maven.repository.converter;
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.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
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.Snapshot;
26 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
27 import org.apache.maven.artifact.repository.metadata.Versioning;
28 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
29 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
30 import org.apache.maven.model.DistributionManagement;
31 import org.apache.maven.model.Model;
32 import org.apache.maven.model.Relocation;
33 import org.apache.maven.model.converter.ArtifactPomRewriter;
34 import org.apache.maven.model.converter.ModelConverter;
35 import org.apache.maven.model.converter.PomTranslationException;
36 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
37 import org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader;
38 import org.apache.maven.repository.converter.transaction.FileTransaction;
39 import org.apache.maven.repository.digest.Digester;
40 import org.apache.maven.repository.digest.DigesterException;
41 import org.apache.maven.repository.reporting.ArtifactReporter;
42 import org.codehaus.plexus.i18n.I18N;
43 import org.codehaus.plexus.util.FileUtils;
44 import org.codehaus.plexus.util.IOUtil;
45 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
46
47 import java.io.File;
48 import java.io.FileNotFoundException;
49 import java.io.FileReader;
50 import java.io.IOException;
51 import java.io.StringReader;
52 import java.io.StringWriter;
53 import java.util.Iterator;
54 import java.util.List;
55 import java.util.Locale;
56 import java.util.Properties;
57 import java.util.regex.Matcher;
58
59 /**
60  * Implementation of repository conversion class.
61  *
62  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
63  * @plexus.component role="org.apache.maven.repository.converter.RepositoryConverter" role-hint="default"
64  */
65 public class DefaultRepositoryConverter
66     implements RepositoryConverter
67 {
68     /**
69      * @plexus.requirement role-hint="sha1"
70      */
71     private Digester sha1Digester;
72
73     /**
74      * @plexus.requirement role-hint="md5"
75      */
76     private Digester md5Digester;
77
78     /**
79      * @plexus.requirement
80      */
81     private ArtifactFactory artifactFactory;
82
83     /**
84      * @plexus.requirement
85      */
86     private ArtifactPomRewriter rewriter;
87
88     /**
89      * @plexus.requirement
90      */
91     private ModelConverter translator;
92
93     /**
94      * @plexus.configuration default-value="false"
95      */
96     private boolean force;
97
98     /**
99      * @plexus.configuration default-value="false"
100      */
101     private boolean dryrun;
102
103     /**
104      * @plexus.requirement
105      */
106     private I18N i18n;
107
108     public void convert( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter )
109         throws RepositoryConversionException
110     {
111         if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
112         {
113             throw new RepositoryConversionException( getI18NString( "exception.repositories.match" ) );
114         }
115
116         if ( validateMetadata( artifact, reporter ) )
117         {
118             FileTransaction transaction = new FileTransaction();
119
120             if ( copyPom( artifact, targetRepository, reporter, transaction ) )
121             {
122                 if ( copyArtifact( artifact, targetRepository, reporter, transaction ) )
123                 {
124                     Metadata metadata = createBaseMetadata( artifact );
125                     Versioning versioning = new Versioning();
126                     versioning.addVersion( artifact.getBaseVersion() );
127                     metadata.setVersioning( versioning );
128                     updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata,
129                                     transaction );
130
131                     metadata = createBaseMetadata( artifact );
132                     metadata.setVersion( artifact.getBaseVersion() );
133                     versioning = new Versioning();
134
135                     Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
136                     if ( matcher.matches() )
137                     {
138                         Snapshot snapshot = new Snapshot();
139                         snapshot.setBuildNumber( Integer.valueOf( matcher.group( 3 ) ).intValue() );
140                         snapshot.setTimestamp( matcher.group( 2 ) );
141                         versioning.setSnapshot( snapshot );
142                     }
143
144                     // TODO: merge latest/release/snapshot from source instead
145                     metadata.setVersioning( versioning );
146                     updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata,
147                                     transaction );
148
149                     if ( !dryrun )
150                     {
151                         transaction.commit();
152                     }
153                     reporter.addSuccess( artifact );
154                 }
155             }
156         }
157     }
158
159     private static Metadata createBaseMetadata( Artifact artifact )
160     {
161         Metadata metadata = new Metadata();
162         metadata.setArtifactId( artifact.getArtifactId() );
163         metadata.setGroupId( artifact.getGroupId() );
164         return metadata;
165     }
166
167     private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
168                                  Metadata newMetadata, FileTransaction transaction )
169         throws RepositoryConversionException
170     {
171         File file = new File( targetRepository.getBasedir(),
172                               targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
173
174         Metadata metadata;
175         boolean changed;
176
177         if ( file.exists() )
178         {
179             metadata = readMetadata( file );
180             changed = metadata.merge( newMetadata );
181         }
182         else
183         {
184             changed = true;
185             metadata = newMetadata;
186         }
187
188         if ( changed )
189         {
190             StringWriter writer = null;
191             try
192             {
193                 writer = new StringWriter();
194
195                 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
196
197                 mappingWriter.write( writer, metadata );
198
199                 transaction.createFile( writer.toString(), file );
200             }
201             catch ( IOException e )
202             {
203                 throw new RepositoryConversionException( "Error writing target metadata", e );
204             }
205             finally
206             {
207                 IOUtil.close( writer );
208             }
209         }
210     }
211
212     private Metadata readMetadata( File file )
213         throws RepositoryConversionException
214     {
215         Metadata metadata;
216         MetadataXpp3Reader reader = new MetadataXpp3Reader();
217         FileReader fileReader = null;
218         try
219         {
220             fileReader = new FileReader( file );
221             metadata = reader.read( fileReader );
222         }
223         catch ( FileNotFoundException e )
224         {
225             throw new RepositoryConversionException( "Error reading target metadata", e );
226         }
227         catch ( IOException e )
228         {
229             throw new RepositoryConversionException( "Error reading target metadata", e );
230         }
231         catch ( XmlPullParserException e )
232         {
233             throw new RepositoryConversionException( "Error reading target metadata", e );
234         }
235         finally
236         {
237             IOUtil.close( fileReader );
238         }
239         return metadata;
240     }
241
242     private boolean validateMetadata( Artifact artifact, ArtifactReporter reporter )
243         throws RepositoryConversionException
244     {
245         ArtifactRepository repository = artifact.getRepository();
246
247         boolean result = true;
248
249         RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
250         File file =
251             new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
252         if ( file.exists() )
253         {
254             Metadata metadata = readMetadata( file );
255             result = validateMetadata( metadata, repositoryMetadata, artifact, reporter );
256         }
257
258         repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
259         file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
260         if ( file.exists() )
261         {
262             Metadata metadata = readMetadata( file );
263             result = result && validateMetadata( metadata, repositoryMetadata, artifact, reporter );
264         }
265
266         return result;
267     }
268
269     private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact,
270                                       ArtifactReporter reporter )
271     {
272         String groupIdKey;
273         String artifactIdKey = null;
274         String snapshotKey = null;
275         String versionKey = null;
276         String versionsKey = null;
277         if ( repositoryMetadata.storedInGroupDirectory() )
278         {
279             groupIdKey = "failure.incorrect.groupMetadata.groupId";
280         }
281         else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
282         {
283             groupIdKey = "failure.incorrect.snapshotMetadata.groupId";
284             artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId";
285             versionKey = "failure.incorrect.snapshotMetadata.version";
286             snapshotKey = "failure.incorrect.snapshotMetadata.snapshot";
287         }
288         else
289         {
290             groupIdKey = "failure.incorrect.artifactMetadata.groupId";
291             artifactIdKey = "failure.incorrect.artifactMetadata.artifactId";
292             versionsKey = "failure.incorrect.artifactMetadata.versions";
293         }
294
295         boolean result = true;
296
297         if ( !metadata.getGroupId().equals( artifact.getGroupId() ) )
298         {
299             reporter.addFailure( artifact, getI18NString( groupIdKey ) );
300             result = false;
301         }
302         if ( !repositoryMetadata.storedInGroupDirectory() )
303         {
304             if ( !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
305             {
306                 reporter.addFailure( artifact, getI18NString( artifactIdKey ) );
307                 result = false;
308             }
309             if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
310             {
311                 // artifact metadata
312
313                 boolean foundVersion = false;
314                 if ( metadata.getVersioning() != null )
315                 {
316                     for ( Iterator i = metadata.getVersioning().getVersions().iterator();
317                           i.hasNext() && !foundVersion; )
318                     {
319                         String version = (String) i.next();
320                         if ( version.equals( artifact.getBaseVersion() ) )
321                         {
322                             foundVersion = true;
323                         }
324                     }
325                 }
326
327                 if ( !foundVersion )
328                 {
329                     reporter.addFailure( artifact, getI18NString( versionsKey ) );
330                     result = false;
331                 }
332             }
333             else
334             {
335                 // snapshot metadata
336                 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
337                 {
338                     reporter.addFailure( artifact, getI18NString( versionKey ) );
339                     result = false;
340                 }
341
342                 if ( artifact.isSnapshot() )
343                 {
344                     Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
345                     if ( matcher.matches() )
346                     {
347                         boolean correct = false;
348                         if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
349                         {
350                             Snapshot snapshot = metadata.getVersioning().getSnapshot();
351                             int build = Integer.valueOf( matcher.group( 3 ) ).intValue();
352                             String ts = matcher.group( 2 );
353                             if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
354                             {
355                                 correct = true;
356                             }
357                         }
358
359                         if ( !correct )
360                         {
361                             reporter.addFailure( artifact, getI18NString( snapshotKey ) );
362                             result = false;
363                         }
364                     }
365                 }
366             }
367         }
368         return result;
369     }
370
371     private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter,
372                              FileTransaction transaction )
373         throws RepositoryConversionException
374     {
375         Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
376                                                               artifact.getVersion() );
377         pom.setBaseVersion( artifact.getBaseVersion() );
378         ArtifactRepository repository = artifact.getRepository();
379         File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
380
381         boolean result = true;
382         if ( file.exists() )
383         {
384             File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
385
386             String contents = null;
387             boolean checksumsValid = false;
388             try
389             {
390                 if ( testChecksums( artifact, file, reporter ) )
391                 {
392                     checksumsValid = true;
393                     contents = FileUtils.fileRead( file );
394                 }
395             }
396             catch ( IOException e )
397             {
398                 throw new RepositoryConversionException( "Unable to read source POM: " + e.getMessage(), e );
399             }
400
401             if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 )
402             {
403                 // v4 POM
404                 try
405                 {
406                     boolean matching = false;
407                     if ( !force && targetFile.exists() )
408                     {
409                         String targetContents = FileUtils.fileRead( targetFile );
410                         matching = targetContents.equals( contents );
411                     }
412                     if ( force || !matching )
413                     {
414                         transaction.createFile( contents, targetFile );
415                     }
416                 }
417                 catch ( IOException e )
418                 {
419                     throw new RepositoryConversionException( "Unable to write target POM: " + e.getMessage(), e );
420                 }
421             }
422             else
423             {
424                 // v3 POM
425                 StringReader stringReader = new StringReader( contents );
426                 StringWriter writer = null;
427                 try
428                 {
429                     MavenXpp3Reader v3Reader = new MavenXpp3Reader();
430                     org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
431
432                     if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
433                     {
434                         Artifact relocatedPom = artifactFactory.createProjectArtifact( artifact.getGroupId(),
435                                                                                        artifact.getArtifactId(),
436                                                                                        artifact.getVersion() );
437                         targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
438                     }
439
440                     Model v4Model = translator.translate( v3Model );
441
442                     translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
443                                                  v3Model.getVersion(), v3Model.getPackage() );
444
445                     writer = new StringWriter();
446                     MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
447                     Xpp3Writer.write( writer, v4Model );
448
449                     transaction.createFile( writer.toString(), targetFile );
450
451                     List warnings = translator.getWarnings();
452
453                     for ( Iterator i = warnings.iterator(); i.hasNext(); )
454                     {
455                         String message = (String) i.next();
456                         reporter.addWarning( artifact, message );
457                     }
458                 }
459                 catch ( XmlPullParserException e )
460                 {
461                     reporter.addFailure( artifact, getI18NString( "failure.invalid.source.pom", e.getMessage() ) );
462                     result = false;
463                 }
464                 catch ( IOException e )
465                 {
466                     throw new RepositoryConversionException( "Unable to write converted POM", e );
467                 }
468                 catch ( PomTranslationException e )
469                 {
470                     reporter.addFailure( artifact, getI18NString( "failure.invalid.source.pom", e.getMessage() ) );
471                     result = false;
472                 }
473                 finally
474                 {
475                     IOUtil.close( writer );
476                 }
477             }
478         }
479         else
480         {
481             reporter.addWarning( artifact, getI18NString( "warning.missing.pom" ) );
482         }
483         return result;
484     }
485
486     private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
487                                   ArtifactRepository repository, FileTransaction transaction )
488         throws IOException
489     {
490         Properties properties = v3Model.getProperties();
491         if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" ) ||
492             properties.containsKey( "relocated.version" ) )
493         {
494             String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() );
495             properties.remove( "relocated.groupId" );
496
497             String newArtifactId = properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() );
498             properties.remove( "relocated.artifactId" );
499
500             String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() );
501             properties.remove( "relocated.version" );
502
503             String message = properties.getProperty( "relocated.message", "" );
504             properties.remove( "relocated.message" );
505
506             if ( properties.isEmpty() )
507             {
508                 v3Model.setProperties( null );
509             }
510
511             writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
512                                 newArtifactId, newVersion, message, repository, transaction );
513
514             v3Model.setGroupId( newGroupId );
515             v3Model.setArtifactId( newArtifactId );
516             v3Model.setVersion( newVersion );
517
518             artifact.setGroupId( newGroupId );
519             artifact.setArtifactId( newArtifactId );
520             artifact.setVersion( newVersion );
521
522             return true;
523         }
524         else
525         {
526             return false;
527         }
528     }
529
530     private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
531                                      String newArtifactId, String newVersion, String message,
532                                      ArtifactRepository repository, FileTransaction transaction )
533         throws IOException
534     {
535         Model pom = new Model();
536         pom.setGroupId( groupId );
537         pom.setArtifactId( artifactId );
538         pom.setVersion( version );
539
540         DistributionManagement dMngt = new DistributionManagement();
541
542         Relocation relocation = new Relocation();
543         relocation.setGroupId( newGroupId );
544         relocation.setArtifactId( newArtifactId );
545         relocation.setVersion( newVersion );
546         if ( message != null && message.length() > 0 )
547         {
548             relocation.setMessage( message );
549         }
550
551         dMngt.setRelocation( relocation );
552
553         pom.setDistributionManagement( dMngt );
554
555         Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
556         File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
557
558         StringWriter strWriter = new StringWriter();
559         MavenXpp3Writer pomWriter = new MavenXpp3Writer();
560         pomWriter.write( strWriter, pom );
561
562         transaction.createFile( strWriter.toString(), pomFile );
563     }
564
565     private String getI18NString( String key, String arg0 )
566     {
567         return i18n.format( getClass().getName(), Locale.getDefault(), key, arg0 );
568     }
569
570     private String getI18NString( String key )
571     {
572         return i18n.getString( getClass().getName(), Locale.getDefault(), key );
573     }
574
575     private boolean testChecksums( Artifact artifact, File file, ArtifactReporter reporter )
576         throws IOException
577     {
578
579         boolean result =
580             verifyChecksum( file, file.getName() + ".md5", md5Digester, reporter, artifact, "failure.incorrect.md5" );
581         result = result && verifyChecksum( file, file.getName() + ".sha1", sha1Digester, reporter, artifact,
582                                            "failure.incorrect.sha1" );
583         return result;
584     }
585
586     private boolean verifyChecksum( File file, String fileName, Digester digester, ArtifactReporter reporter,
587                                     Artifact artifact, String key )
588         throws IOException
589     {
590         boolean result = true;
591
592         File checksumFile = new File( file.getParentFile(), fileName );
593         if ( checksumFile.exists() )
594         {
595             String checksum = FileUtils.fileRead( checksumFile );
596             try
597             {
598                 digester.verify( file, checksum );
599             }
600             catch ( DigesterException e )
601             {
602                 reporter.addFailure( artifact, getI18NString( key ) );
603                 result = false;
604             }
605         }
606         return result;
607     }
608
609     private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter,
610                                   FileTransaction transaction )
611         throws RepositoryConversionException
612     {
613         File sourceFile = artifact.getFile();
614
615         File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
616
617         boolean result = true;
618         try
619         {
620             boolean matching = false;
621             if ( !force && targetFile.exists() )
622             {
623                 matching = FileUtils.contentEquals( sourceFile, targetFile );
624                 if ( !matching )
625                 {
626                     reporter.addFailure( artifact, getI18NString( "failure.target.already.exists" ) );
627                     result = false;
628                 }
629             }
630             if ( result )
631             {
632                 if ( force || !matching )
633                 {
634                     if ( testChecksums( artifact, sourceFile, reporter ) )
635                     {
636                         transaction.copyFile( sourceFile, targetFile );
637                     }
638                     else
639                     {
640                         result = false;
641                     }
642                 }
643             }
644         }
645         catch ( IOException e )
646         {
647             throw new RepositoryConversionException( "Error copying artifact", e );
648         }
649         return result;
650     }
651
652     public void convert( List artifacts, ArtifactRepository targetRepository, ArtifactReporter reporter )
653         throws RepositoryConversionException
654     {
655         for ( Iterator i = artifacts.iterator(); i.hasNext(); )
656         {
657             Artifact artifact = (Artifact) i.next();
658             convert( artifact, targetRepository, reporter );
659         }
660     }
661 }