View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.repository.internal;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.Reader;
24  import java.io.Writer;
25  import java.util.Collections;
26  import java.util.Date;
27  import java.util.Map;
28  
29  import org.apache.maven.artifact.repository.metadata.Metadata;
30  import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
31  import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
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.eclipse.aether.RepositoryException;
36  import org.eclipse.aether.metadata.AbstractMetadata;
37  import org.eclipse.aether.metadata.MergeableMetadata;
38  
39  /**
40   * @author Benjamin Bentmann
41   */
42  abstract class MavenMetadata extends AbstractMetadata implements MergeableMetadata {
43  
44      static final String MAVEN_METADATA_XML = "maven-metadata.xml";
45  
46      protected Metadata metadata;
47  
48      private final File file;
49  
50      protected final Date timestamp;
51  
52      private boolean merged;
53  
54      protected MavenMetadata(Metadata metadata, File file, Date timestamp) {
55          this.metadata = metadata;
56          this.file = file;
57          this.timestamp = timestamp;
58      }
59  
60      public String getType() {
61          return MAVEN_METADATA_XML;
62      }
63  
64      public File getFile() {
65          return file;
66      }
67  
68      public void merge(File existing, File result) throws RepositoryException {
69          Metadata recessive = read(existing);
70  
71          merge(recessive);
72  
73          write(result, metadata);
74  
75          merged = true;
76      }
77  
78      public boolean isMerged() {
79          return merged;
80      }
81  
82      protected abstract void merge(Metadata recessive);
83  
84      static Metadata read(File metadataFile) throws RepositoryException {
85          if (metadataFile.length() <= 0) {
86              return new Metadata();
87          }
88  
89          try (Reader reader = ReaderFactory.newXmlReader(metadataFile)) {
90              return new MetadataXpp3Reader().read(reader, false);
91          } catch (IOException e) {
92              throw new RepositoryException("Could not read metadata " + metadataFile + ": " + e.getMessage(), e);
93          } catch (XmlPullParserException e) {
94              throw new RepositoryException("Could not parse metadata " + metadataFile + ": " + e.getMessage(), e);
95          }
96      }
97  
98      private void write(File metadataFile, Metadata metadata) throws RepositoryException {
99          metadataFile.getParentFile().mkdirs();
100         try (Writer writer = WriterFactory.newXmlWriter(metadataFile)) {
101             new MetadataXpp3Writer().write(writer, metadata);
102         } catch (IOException e) {
103             throw new RepositoryException("Could not write metadata " + metadataFile + ": " + e.getMessage(), e);
104         }
105     }
106 
107     public Map<String, String> getProperties() {
108         return Collections.emptyMap();
109     }
110 
111     @Override
112     public org.eclipse.aether.metadata.Metadata setProperties(Map<String, String> properties) {
113         return this;
114     }
115 }