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      @Override
84      public void merge(File existing, File result) throws RepositoryException {
85          merge(existing != null ? existing.toPath() : null, result != null ? result.toPath() : null);
86      }
87  
88      @Override
89      public void merge(Path existing, Path result) throws RepositoryException {
90          Metadata recessive = read(existing);
91  
92          merge(recessive);
93  
94          write(result, metadata);
95  
96          merged = true;
97      }
98  
99      @Override
100     public boolean isMerged() {
101         return merged;
102     }
103 
104     protected abstract void merge(Metadata recessive);
105 
106     static Metadata read(Path metadataPath) throws RepositoryException {
107         if (!Files.exists(metadataPath)) {
108             return new Metadata();
109         }
110 
111         try (InputStream input = Files.newInputStream(metadataPath)) {
112             return new Metadata(new MetadataStaxReader().read(input, false));
113         } catch (IOException | XMLStreamException e) {
114             throw new RepositoryException("Could not parse metadata " + metadataPath + ": " + e.getMessage(), e);
115         }
116     }
117 
118     private void write(Path metadataPath, Metadata metadata) throws RepositoryException {
119         try {
120             Files.createDirectories(metadataPath.getParent());
121             try (OutputStream output = Files.newOutputStream(metadataPath)) {
122                 new MetadataStaxWriter().write(output, metadata.getDelegate());
123             }
124         } catch (IOException | XMLStreamException e) {
125             throw new RepositoryException("Could not write metadata " + metadataPath + ": " + e.getMessage(), e);
126         }
127     }
128 
129     @Override
130     public Map<String, String> getProperties() {
131         return Collections.emptyMap();
132     }
133 
134     @Override
135     public org.eclipse.aether.metadata.Metadata setProperties(Map<String, String> properties) {
136         return this;
137     }
138 }