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