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.util.Collections;
29  import java.util.Date;
30  import java.util.Map;
31  
32  import org.apache.maven.artifact.repository.metadata.Metadata;
33  import org.apache.maven.artifact.repository.metadata.io.MetadataStaxReader;
34  import org.apache.maven.artifact.repository.metadata.io.MetadataStaxWriter;
35  import org.eclipse.aether.RepositoryException;
36  import org.eclipse.aether.metadata.AbstractMetadata;
37  import org.eclipse.aether.metadata.MergeableMetadata;
38  
39  /**
40   */
41  abstract class MavenMetadata extends AbstractMetadata implements MergeableMetadata {
42  
43      static final String MAVEN_METADATA_XML = "maven-metadata.xml";
44  
45      protected Metadata metadata;
46  
47      private final File file;
48  
49      protected final Date timestamp;
50  
51      private boolean merged;
52  
53      protected MavenMetadata(Metadata metadata, File file, Date timestamp) {
54          this.metadata = metadata;
55          this.file = file;
56          this.timestamp = timestamp;
57      }
58  
59      @Override
60      public String getType() {
61          return MAVEN_METADATA_XML;
62      }
63  
64      @Override
65      public File getFile() {
66          return file;
67      }
68  
69      @Override
70      public void merge(File existing, File result) throws RepositoryException {
71          Metadata recessive = read(existing);
72  
73          merge(recessive);
74  
75          write(result, metadata);
76  
77          merged = true;
78      }
79  
80      @Override
81      public boolean isMerged() {
82          return merged;
83      }
84  
85      protected abstract void merge(Metadata recessive);
86  
87      static Metadata read(File metadataFile) throws RepositoryException {
88          if (metadataFile.length() <= 0) {
89              return new Metadata();
90          }
91  
92          try (InputStream input = Files.newInputStream(metadataFile.toPath())) {
93              return new Metadata(new MetadataStaxReader().read(input, false));
94          } catch (IOException | XMLStreamException e) {
95              throw new RepositoryException("Could not parse metadata " + metadataFile + ": " + e.getMessage(), e);
96          }
97      }
98  
99      private void write(File metadataFile, Metadata metadata) throws RepositoryException {
100         metadataFile.getParentFile().mkdirs();
101         try (OutputStream output = Files.newOutputStream(metadataFile.toPath())) {
102             new MetadataStaxWriter().write(output, metadata.getDelegate());
103         } catch (IOException | XMLStreamException e) {
104             throw new RepositoryException("Could not write metadata " + metadataFile + ": " + e.getMessage(), e);
105         }
106     }
107 
108     @Override
109     public Map<String, String> getProperties() {
110         return Collections.emptyMap();
111     }
112 
113     @Override
114     public org.eclipse.aether.metadata.Metadata setProperties(Map<String, String> properties) {
115         return this;
116     }
117 }