View Javadoc

1   package org.apache.maven.repository.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.Reader;
25  import java.io.Writer;
26  
27  import org.apache.maven.artifact.repository.metadata.Metadata;
28  import org.apache.maven.artifact.repository.metadata.Versioning;
29  import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
30  import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
31  import org.codehaus.plexus.util.IOUtil;
32  import org.codehaus.plexus.util.ReaderFactory;
33  import org.codehaus.plexus.util.WriterFactory;
34  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35  import org.sonatype.aether.RepositoryException;
36  import org.sonatype.aether.metadata.MergeableMetadata;
37  
38  /**
39   * @author Benjamin Bentmann
40   */
41  abstract class MavenMetadata
42      implements MergeableMetadata
43  {
44  
45      private final File file;
46  
47      protected Metadata metadata;
48  
49      private boolean merged;
50  
51      protected MavenMetadata( Metadata metadata, File file )
52      {
53          this.metadata = metadata;
54          this.file = file;
55      }
56  
57      public String getType()
58      {
59          return "maven-metadata.xml";
60      }
61  
62      public File getFile()
63      {
64          return file;
65      }
66  
67      public void merge( File existing, File result )
68          throws RepositoryException
69      {
70          Metadata recessive = read( existing );
71  
72          merge( recessive );
73  
74          write( result, metadata );
75  
76          merged = true;
77      }
78  
79      public boolean isMerged()
80      {
81          return merged;
82      }
83  
84      protected void merge( Metadata recessive )
85      {
86          Versioning versioning = recessive.getVersioning();
87          if ( versioning != null )
88          {
89              versioning.setLastUpdated( null );
90          }
91  
92          Metadata dominant = metadata;
93  
94          versioning = dominant.getVersioning();
95          if ( versioning != null )
96          {
97              versioning.updateTimestamp();
98          }
99  
100         dominant.merge( recessive );
101     }
102 
103     private Metadata read( File metadataFile )
104         throws RepositoryException
105     {
106         if ( metadataFile.length() <= 0 )
107         {
108             return new Metadata();
109         }
110 
111         Reader reader = null;
112         try
113         {
114             reader = ReaderFactory.newXmlReader( metadataFile );
115             return new MetadataXpp3Reader().read( reader, false );
116         }
117         catch ( IOException e )
118         {
119             throw new RepositoryException( "Could not read metadata " + metadataFile + ": " + e.getMessage(), e );
120         }
121         catch ( XmlPullParserException e )
122         {
123             throw new RepositoryException( "Could not parse metadata " + metadataFile + ": " + e.getMessage(), e );
124         }
125         finally
126         {
127             IOUtil.close( reader );
128         }
129     }
130 
131     private void write( File metadataFile, Metadata metadata )
132         throws RepositoryException
133     {
134         Writer writer = null;
135         try
136         {
137             metadataFile.getParentFile().mkdirs();
138             writer = WriterFactory.newXmlWriter( metadataFile );
139             new MetadataXpp3Writer().write( writer, metadata );
140         }
141         catch ( IOException e )
142         {
143             throw new RepositoryException( "Could not write metadata " + metadataFile + ": " + e.getMessage(), e );
144         }
145         finally
146         {
147             IOUtil.close( writer );
148         }
149     }
150 
151     @Override
152     public String toString()
153     {
154         StringBuilder buffer = new StringBuilder( 128 );
155         if ( getGroupId().length() > 0 )
156         {
157             buffer.append( getGroupId() );
158         }
159         if ( getArtifactId().length() > 0 )
160         {
161             buffer.append( ':' ).append( getArtifactId() );
162         }
163         if ( getVersion().length() > 0 )
164         {
165             buffer.append( ':' ).append( getVersion() );
166         }
167         buffer.append( '/' ).append( getType() );
168         return buffer.toString();
169     }
170 
171 }