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