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