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 javax.xml.stream.XMLStreamException;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  import java.util.Collections;
30  import java.util.Date;
31  import java.util.Map;
32  
33  import org.apache.maven.artifact.repository.metadata.Metadata;
34  import org.apache.maven.metadata.v4.MetadataStaxReader;
35  import org.apache.maven.metadata.v4.MetadataStaxWriter;
36  import org.eclipse.aether.RepositoryException;
37  import org.eclipse.aether.metadata.AbstractMetadata;
38  import org.eclipse.aether.metadata.MergeableMetadata;
39  
40  /**
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 Path path;
49  
50      protected final Date timestamp;
51  
52      private boolean merged;
53  
54      @Deprecated
55      protected MavenMetadata(Metadata metadata, File file, Date timestamp) {
56          this(metadata, file != null ? file.toPath() : null, timestamp);
57      }
58  
59      protected MavenMetadata(Metadata metadata, Path path, Date timestamp) {
60          this.metadata = metadata;
61          this.path = path;
62          this.timestamp = timestamp;
63      }
64  
65      @Override
66      public String getType() {
67          return MAVEN_METADATA_XML;
68      }
69  
70      @Deprecated
71      @Override
72      public File getFile() {
73          return path != null ? path.toFile() : null;
74      }
75  
76      @Override
77      public Path getPath() {
78          return path;
79      }
80  
81      public void merge(File existing, File result) throws RepositoryException {
82          merge(existing != null ? existing.toPath() : null, result != null ? result.toPath() : null);
83      }
84  
85      @Override
86      public void merge(Path existing, Path result) throws RepositoryException {
87          Metadata recessive = read(existing);
88  
89          merge(recessive);
90  
91          write(result, metadata);
92  
93          merged = true;
94      }
95  
96      @Override
97      public boolean isMerged() {
98          return merged;
99      }
100 
101     protected abstract void merge(Metadata recessive);
102 
103     static Metadata read(Path metadataPath) throws RepositoryException {
104         if (!Files.exists(metadataPath)) {
105             return new Metadata();
106         }
107 
108         try (InputStream input = Files.newInputStream(metadataPath)) {
109             return new Metadata(new MetadataStaxReader().read(input, false));
110         } catch (IOException | XMLStreamException e) {
111             throw new RepositoryException("Could not parse metadata " + metadataPath + ": " + e.getMessage(), e);
112         }
113     }
114 
115     private void write(Path metadataPath, Metadata metadata) throws RepositoryException {
116         try {
117             Files.createDirectories(metadataPath.getParent());
118             try (OutputStream output = Files.newOutputStream(metadataPath)) {
119                 new MetadataStaxWriter().write(output, metadata.getDelegate());
120             }
121         } catch (IOException | XMLStreamException e) {
122             throw new RepositoryException("Could not write metadata " + metadataPath + ": " + e.getMessage(), e);
123         }
124     }
125 
126     @Override
127     public Map<String, String> getProperties() {
128         return Collections.emptyMap();
129     }
130 
131     @Override
132     public org.eclipse.aether.metadata.Metadata setProperties(Map<String, String> properties) {
133         return this;
134     }
135 }