]> source.dussan.org Git - archiva.git/blob
e7b5a88915188f15cfd74b6be09c54d131a63e98
[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.configuration default-value="true"
100      */
101     private boolean mergeWithSourceMetadata;
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 ), artifact.getRepository(),
129                                     targetRepository, metadata, 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                     metadata.setVersioning( versioning );
145                     updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), artifact.getRepository(),
146                                     targetRepository, metadata, transaction );
147
148                     if ( !dryrun )
149                     {
150                         transaction.commit();
151                     }
152                     reporter.addSuccess( artifact );
153                 }
154             }
155         }
156     }
157
158     private static Metadata createBaseMetadata( Artifact artifact )
159     {
160         Metadata metadata = new Metadata();
161         metadata.setArtifactId( artifact.getArtifactId() );
162         metadata.setGroupId( artifact.getGroupId() );
163         return metadata;
164     }
165
166     private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository sourceRepository,
167                                  ArtifactRepository targetRepository, Metadata newMetadata, FileTransaction transaction )
168         throws RepositoryConversionException
169     {
170         Metadata metadata;
171         boolean changed = false;
172
173         //merge with target repository metadata
174         File file = new File( targetRepository.getBasedir(),
175                               targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
176         if ( file.exists() )
177         {
178             metadata = readMetadata( file );
179             changed = metadata.merge( newMetadata );
180         }
181         else
182         {
183             changed = true;
184             metadata = newMetadata;
185         }
186
187         //merge with source repository metadata
188         if ( mergeWithSourceMetadata )
189         {
190             File srcfile = new File( sourceRepository.getBasedir(),
191                              sourceRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
192
193             if ( srcfile.exists() )
194             {
195                 Metadata sourceMetadata = readMetadata( srcfile );
196                 changed = changed | metadata.merge( sourceMetadata );
197             }
198         }
199
200         if ( changed )
201         {
202             StringWriter writer = null;
203             try
204             {
205                 writer = new StringWriter();
206
207                 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
208
209                 mappingWriter.write( writer, metadata );
210
211                 transaction.createFile( writer.toString(), file );
212             }
213             catch ( IOException e )
214             {
215                 throw new RepositoryConversionException( "Error writing target metadata", e );
216             }
217             finally
218             {
219                 IOUtil.close( writer );
220             }
221         }
222     }
223
224     private Metadata readMetadata( File file )
225         throws RepositoryConversionException
226     {
227         Metadata metadata;
228         MetadataXpp3Reader reader = new MetadataXpp3Reader();
229         FileReader fileReader = null;
230         try
231         {
232             fileReader = new FileReader( file );
233             metadata = reader.read( fileReader );
234         }
235         catch ( FileNotFoundException e )
236         {
237             throw new RepositoryConversionException( "Error reading target metadata", e );
238         }
239         catch ( IOException e )
240         {
241             throw new RepositoryConversionException( "Error reading target metadata", e );
242         }
243         catch ( XmlPullParserException e )
244         {
245             throw new RepositoryConversionException( "Error reading target metadata", e );
246         }
247         finally
248         {
249             IOUtil.close( fileReader );
250         }
251         return metadata;
252     }
253
254     private boolean validateMetadata( Artifact artifact, ArtifactReporter reporter )
255         throws RepositoryConversionException
256     {
257         ArtifactRepository repository = artifact.getRepository();
258
259         boolean result = true;
260
261         RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
262         File file =
263             new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
264         if ( file.exists() )
265         {
266             Metadata metadata = readMetadata( file );
267             result = validateMetadata( metadata, repositoryMetadata, artifact, reporter );
268         }
269
270         repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
271         file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
272         if ( file.exists() )
273         {
274             Metadata metadata = readMetadata( file );
275             result = result && validateMetadata( metadata, repositoryMetadata, artifact, reporter );
276         }
277
278         return result;
279     }
280
281     private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact,
282                                       ArtifactReporter reporter )
283     {
284         String groupIdKey;
285         String artifactIdKey = null;
286         String snapshotKey = null;
287         String versionKey = null;
288         String versionsKey = null;
289         if ( repositoryMetadata.storedInGroupDirectory() )
290         {
291             groupIdKey = "failure.incorrect.groupMetadata.groupId";
292         }
293         else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
294         {
295             groupIdKey = "failure.incorrect.snapshotMetadata.groupId";
296             artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId";
297             versionKey = "failure.incorrect.snapshotMetadata.version";
298             snapshotKey = "failure.incorrect.snapshotMetadata.snapshot";
299         }
300         else
301         {
302             groupIdKey = "failure.incorrect.artifactMetadata.groupId";
303             artifactIdKey = "failure.incorrect.artifactMetadata.artifactId";
304             versionsKey = "failure.incorrect.artifactMetadata.versions";
305         }
306
307         boolean result = true;
308
309         if ( !metadata.getGroupId().equals( artifact.getGroupId() ) )
310         {
311             reporter.addFailure( artifact, getI18NString( groupIdKey ) );
312             result = false;
313         }
314         if ( !repositoryMetadata.storedInGroupDirectory() )
315         {
316             if ( !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
317             {
318                 reporter.addFailure( artifact, getI18NString( artifactIdKey ) );
319                 result = false;
320             }
321             if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
322             {
323                 // artifact metadata
324
325                 boolean foundVersion = false;
326                 if ( metadata.getVersioning() != null )
327                 {
328                     for ( Iterator i = metadata.getVersioning().getVersions().iterator();
329                           i.hasNext() && !foundVersion; )
330                     {
331                         String version = (String) i.next();
332                         if ( version.equals( artifact.getBaseVersion() ) )
333                         {
334                             foundVersion = true;
335                         }
336                     }
337                 }
338
339                 if ( !foundVersion )
340                 {
341                     reporter.addFailure( artifact, getI18NString( versionsKey ) );
342                     result = false;
343                 }
344             }
345             else
346             {
347                 // snapshot metadata
348                 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
349                 {
350                     reporter.addFailure( artifact, getI18NString( versionKey ) );
351                     result = false;
352                 }
353
354                 if ( artifact.isSnapshot() )
355                 {
356                     Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
357                     if ( matcher.matches() )
358                     {
359                         boolean correct = false;
360                         if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
361                         {
362                             Snapshot snapshot = metadata.getVersioning().getSnapshot();
363                             int build = Integer.valueOf( matcher.group( 3 ) ).intValue();
364                             String ts = matcher.group( 2 );
365                             if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
366                             {
367                                 correct = true;
368                             }
369                         }
370
371                         if ( !correct )
372                         {
373                             reporter.addFailure( artifact, getI18NString( snapshotKey ) );
374                             result = false;
375                         }
376                     }
377                 }
378             }
379         }
380         return result;
381     }
382
383     private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter,
384                              FileTransaction transaction )
385         throws RepositoryConversionException
386     {
387         Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
388                                                               artifact.getVersion() );
389         pom.setBaseVersion( artifact.getBaseVersion() );
390         ArtifactRepository repository = artifact.getRepository();
391         File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
392
393         boolean result = true;
394         if ( file.exists() )
395         {
396             File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
397
398             String contents = null;
399             boolean checksumsValid = false;
400             try
401             {
402                 if ( testChecksums( artifact, file, reporter ) )
403                 {
404                     checksumsValid = true;
405                     contents = FileUtils.fileRead( file );
406                 }
407             }
408             catch ( IOException e )
409             {
410                 throw new RepositoryConversionException( "Unable to read source POM: " + e.getMessage(), e );
411             }
412
413             if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 )
414             {
415                 // v4 POM
416                 try
417                 {
418                     boolean matching = false;
419                     if ( !force && targetFile.exists() )
420                     {
421                         String targetContents = FileUtils.fileRead( targetFile );
422                         matching = targetContents.equals( contents );
423                     }
424                     if ( force || !matching )
425                     {
426                         transaction.createFile( contents, targetFile );
427                     }
428                 }
429                 catch ( IOException e )
430                 {
431                     throw new RepositoryConversionException( "Unable to write target POM: " + e.getMessage(), e );
432                 }
433             }
434             else
435             {
436                 // v3 POM
437                 StringReader stringReader = new StringReader( contents );
438                 StringWriter writer = null;
439                 try
440                 {
441                     MavenXpp3Reader v3Reader = new MavenXpp3Reader();
442                     org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
443
444                     if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
445                     {
446                         Artifact relocatedPom = artifactFactory.createProjectArtifact( artifact.getGroupId(),
447                                                                                        artifact.getArtifactId(),
448                                                                                        artifact.getVersion() );
449                         targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
450                     }
451
452                     Model v4Model = translator.translate( v3Model );
453
454                     translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
455                                                  v3Model.getVersion(), v3Model.getPackage() );
456
457                     writer = new StringWriter();
458                     MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
459                     Xpp3Writer.write( writer, v4Model );
460
461                     transaction.createFile( writer.toString(), targetFile );
462
463                     List warnings = translator.getWarnings();
464
465                     for ( Iterator i = warnings.iterator(); i.hasNext(); )
466                     {
467                         String message = (String) i.next();
468                         reporter.addWarning( artifact, message );
469                     }
470                 }
471                 catch ( XmlPullParserException e )
472                 {
473                     reporter.addFailure( artifact, getI18NString( "failure.invalid.source.pom", e.getMessage() ) );
474                     result = false;
475                 }
476                 catch ( IOException e )
477                 {
478                     throw new RepositoryConversionException( "Unable to write converted POM", e );
479                 }
480                 catch ( PomTranslationException e )
481                 {
482                     reporter.addFailure( artifact, getI18NString( "failure.invalid.source.pom", e.getMessage() ) );
483                     result = false;
484                 }
485                 finally
486                 {
487                     IOUtil.close( writer );
488                 }
489             }
490         }
491         else
492         {
493             reporter.addWarning( artifact, getI18NString( "warning.missing.pom" ) );
494         }
495         return result;
496     }
497
498     private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
499                                   ArtifactRepository repository, FileTransaction transaction )
500         throws IOException
501     {
502         Properties properties = v3Model.getProperties();
503         if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" ) ||
504             properties.containsKey( "relocated.version" ) )
505         {
506             String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() );
507             properties.remove( "relocated.groupId" );
508
509             String newArtifactId = properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() );
510             properties.remove( "relocated.artifactId" );
511
512             String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() );
513             properties.remove( "relocated.version" );
514
515             String message = properties.getProperty( "relocated.message", "" );
516             properties.remove( "relocated.message" );
517
518             if ( properties.isEmpty() )
519             {
520                 v3Model.setProperties( null );
521             }
522
523             writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
524                                 newArtifactId, newVersion, message, repository, transaction );
525
526             v3Model.setGroupId( newGroupId );
527             v3Model.setArtifactId( newArtifactId );
528             v3Model.setVersion( newVersion );
529
530             artifact.setGroupId( newGroupId );
531             artifact.setArtifactId( newArtifactId );
532             artifact.setVersion( newVersion );
533
534             return true;
535         }
536         else
537         {
538             return false;
539         }
540     }
541
542     private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
543                                      String newArtifactId, String newVersion, String message,
544                                      ArtifactRepository repository, FileTransaction transaction )
545         throws IOException
546     {
547         Model pom = new Model();
548         pom.setGroupId( groupId );
549         pom.setArtifactId( artifactId );
550         pom.setVersion( version );
551
552         DistributionManagement dMngt = new DistributionManagement();
553
554         Relocation relocation = new Relocation();
555         relocation.setGroupId( newGroupId );
556         relocation.setArtifactId( newArtifactId );
557         relocation.setVersion( newVersion );
558         if ( message != null && message.length() > 0 )
559         {
560             relocation.setMessage( message );
561         }
562
563         dMngt.setRelocation( relocation );
564
565         pom.setDistributionManagement( dMngt );
566
567         Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
568         File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
569
570         StringWriter strWriter = new StringWriter();
571         MavenXpp3Writer pomWriter = new MavenXpp3Writer();
572         pomWriter.write( strWriter, pom );
573
574         transaction.createFile( strWriter.toString(), pomFile );
575     }
576
577     private String getI18NString( String key, String arg0 )
578     {
579         return i18n.format( getClass().getName(), Locale.getDefault(), key, arg0 );
580     }
581
582     private String getI18NString( String key )
583     {
584         return i18n.getString( getClass().getName(), Locale.getDefault(), key );
585     }
586
587     private boolean testChecksums( Artifact artifact, File file, ArtifactReporter reporter )
588         throws IOException, RepositoryConversionException
589     {
590         boolean result;
591
592         try
593         {
594             result = verifyChecksum( file, file.getName() + ".md5", Digester.MD5, reporter, artifact,
595                                      "failure.incorrect.md5" );
596             result = result && verifyChecksum( file, file.getName() + ".sha1", Digester.SHA1, reporter, artifact,
597                                                "failure.incorrect.sha1" );
598         }
599         catch ( NoSuchAlgorithmException e )
600         {
601             throw new RepositoryConversionException( "Error copying artifact: " + e.getMessage(), e );
602         }
603         return result;
604     }
605
606     private boolean verifyChecksum( File file, String fileName, String algorithm, ArtifactReporter reporter,
607                                     Artifact artifact, String key )
608         throws IOException, NoSuchAlgorithmException
609     {
610         boolean result = true;
611
612         File md5 = new File( file.getParentFile(), fileName );
613         if ( md5.exists() )
614         {
615             String checksum = FileUtils.fileRead( md5 );
616             if ( !digester.verifyChecksum( file, checksum, algorithm ) )
617             {
618                 reporter.addFailure( artifact, getI18NString( key ) );
619                 result = false;
620             }
621         }
622         return result;
623     }
624
625     private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter,
626                                   FileTransaction transaction )
627         throws RepositoryConversionException
628     {
629         File sourceFile = artifact.getFile();
630
631         File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
632
633         boolean result = true;
634         try
635         {
636             boolean matching = false;
637             if ( !force && targetFile.exists() )
638             {
639                 matching = FileUtils.contentEquals( sourceFile, targetFile );
640                 if ( !matching )
641                 {
642                     reporter.addFailure( artifact, getI18NString( "failure.target.already.exists" ) );
643                     result = false;
644                 }
645             }
646             if ( result )
647             {
648                 if ( force || !matching )
649                 {
650                     if ( testChecksums( artifact, sourceFile, reporter ) )
651                     {
652                         transaction.copyFile( sourceFile, targetFile );
653                     }
654                     else
655                     {
656                         result = false;
657                     }
658                 }
659             }
660         }
661         catch ( IOException e )
662         {
663             throw new RepositoryConversionException( "Error copying artifact", e );
664         }
665         return result;
666     }
667
668     public void convert( List artifacts, ArtifactRepository targetRepository, ArtifactReporter reporter )
669         throws RepositoryConversionException
670     {
671         for ( Iterator i = artifacts.iterator(); i.hasNext(); )
672         {
673             Artifact artifact = (Artifact) i.next();
674             convert( artifact, targetRepository, reporter );
675         }
676     }
677 }