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