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.artifact.repository.metadata;
20  
21  import java.io.File;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.util.Collections;
25  import java.util.Map;
26  
27  import org.apache.maven.artifact.metadata.ArtifactMetadata;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
30  import org.eclipse.aether.RepositoryException;
31  import org.eclipse.aether.metadata.AbstractMetadata;
32  import org.eclipse.aether.metadata.MergeableMetadata;
33  import org.eclipse.aether.metadata.Metadata;
34  
35  /**
36   * <strong>Warning:</strong> This is an internal utility class that is only public for technical reasons, it is not part
37   * of the public API. In particular, this class can be changed or deleted without prior notice.
38   *
39   */
40  @Deprecated
41  public final class MetadataBridge extends AbstractMetadata implements MergeableMetadata {
42  
43      private ArtifactMetadata metadata;
44  
45      private boolean merged;
46  
47      public MetadataBridge(ArtifactMetadata metadata) {
48          this.metadata = metadata;
49      }
50  
51      @Override
52      public void merge(File current, File result) throws RepositoryException {
53          try {
54              if (current.exists()) {
55                  Files.createDirectories(result.toPath().getParent());
56                  Files.copy(current.toPath(), result.toPath());
57              }
58              ArtifactRepository localRepo = new MetadataRepository(result);
59              metadata.storeInLocalRepository(localRepo, localRepo);
60              merged = true;
61          } catch (Exception e) {
62              throw new RepositoryException(e.getMessage(), e);
63          }
64      }
65  
66      @Override
67      public boolean isMerged() {
68          return merged;
69      }
70  
71      @Override
72      public String getGroupId() {
73          return emptify(metadata.getGroupId());
74      }
75  
76      @Override
77      public String getArtifactId() {
78          return metadata.storedInGroupDirectory() ? "" : emptify(metadata.getArtifactId());
79      }
80  
81      @Override
82      public String getVersion() {
83          return metadata.storedInArtifactVersionDirectory() ? emptify(metadata.getBaseVersion()) : "";
84      }
85  
86      @Override
87      public String getType() {
88          return metadata.getRemoteFilename();
89      }
90  
91      private String emptify(String string) {
92          return (string != null) ? string : "";
93      }
94  
95      @Override
96      public File getFile() {
97          return null;
98      }
99  
100     @Override
101     public MetadataBridge setFile(File file) {
102         return this;
103     }
104 
105     @Override
106     public Path getPath() {
107         return null;
108     }
109 
110     @Override
111     public Nature getNature() {
112         if (metadata instanceof RepositoryMetadata repositoryMetadata) {
113             return switch (repositoryMetadata.getNature()) {
114                 case RepositoryMetadata.RELEASE_OR_SNAPSHOT -> Nature.RELEASE_OR_SNAPSHOT;
115                 case RepositoryMetadata.SNAPSHOT -> Nature.SNAPSHOT;
116                 default -> Nature.RELEASE;
117             };
118         } else {
119             return Nature.RELEASE;
120         }
121     }
122 
123     @Override
124     public Map<String, String> getProperties() {
125         return Collections.emptyMap();
126     }
127 
128     @Override
129     public Metadata setProperties(Map<String, String> properties) {
130         return this;
131     }
132 
133     @SuppressWarnings("deprecation")
134     static class MetadataRepository extends DefaultArtifactRepository {
135 
136         private File metadataFile;
137 
138         MetadataRepository(File metadataFile) {
139             super("local", "", null);
140             this.metadataFile = metadataFile;
141         }
142 
143         @Override
144         public String getBasedir() {
145             return metadataFile.getParent();
146         }
147 
148         @Override
149         public String pathOfLocalRepositoryMetadata(ArtifactMetadata metadata, ArtifactRepository repository) {
150             return metadataFile.getName();
151         }
152     }
153 }