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 org.apache.maven.artifact.repository.metadata.Metadata;
23  import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
24  import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
25  import org.codehaus.plexus.util.ReaderFactory;
26  import org.codehaus.plexus.util.WriterFactory;
27  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
28  import org.eclipse.aether.RepositoryException;
29  import org.eclipse.aether.metadata.AbstractMetadata;
30  import org.eclipse.aether.metadata.MergeableMetadata;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.io.Reader;
35  import java.io.Writer;
36  import java.util.Collections;
37  import java.util.Map;
38  
39  /**
40   * @author Benjamin Bentmann
41   */
42  abstract class MavenMetadata
43      extends AbstractMetadata
44      implements MergeableMetadata
45  {
46  
47      static final String MAVEN_METADATA_XML = "maven-metadata.xml";
48  
49      private final File file;
50  
51      protected Metadata metadata;
52  
53      private boolean merged;
54  
55      protected MavenMetadata( Metadata metadata, File file )
56      {
57          this.metadata = metadata;
58          this.file = file;
59      }
60  
61      public String getType()
62      {
63          return MAVEN_METADATA_XML;
64      }
65  
66      public File getFile()
67      {
68          return file;
69      }
70  
71      public void merge( File existing, File result )
72          throws RepositoryException
73      {
74          Metadata recessive = read( existing );
75  
76          merge( recessive );
77  
78          write( result, metadata );
79  
80          merged = true;
81      }
82  
83      public boolean isMerged()
84      {
85          return merged;
86      }
87  
88      protected abstract void merge( Metadata recessive );
89  
90      static Metadata read( File metadataFile )
91          throws RepositoryException
92      {
93          if ( metadataFile.length() <= 0 )
94          {
95              return new Metadata();
96          }
97  
98          try ( Reader reader = ReaderFactory.newXmlReader( metadataFile ) )
99          {
100             return new MetadataXpp3Reader().read( reader, false );
101         }
102         catch ( IOException e )
103         {
104             throw new RepositoryException( "Could not read metadata " + metadataFile + ": " + e.getMessage(), e );
105         }
106         catch ( XmlPullParserException e )
107         {
108             throw new RepositoryException( "Could not parse metadata " + metadataFile + ": " + e.getMessage(), e );
109         }
110     }
111 
112     private void write( File metadataFile, Metadata metadata )
113         throws RepositoryException
114     {
115         metadataFile.getParentFile().mkdirs();
116         try ( Writer writer = WriterFactory.newXmlWriter( metadataFile ) )
117         {
118             new MetadataXpp3Writer().write( writer, metadata );
119         }
120         catch ( IOException e )
121         {
122             throw new RepositoryException( "Could not write metadata " + metadataFile + ": " + e.getMessage(), e );
123         }
124     }
125 
126     public Map<String, String> getProperties()
127     {
128         return Collections.emptyMap();
129     }
130 
131     @Override
132     public org.eclipse.aether.metadata.Metadata setProperties( Map<String, String> properties )
133     {
134         return this;
135     }
136 
137 }