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