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