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      public void merge(File current, File result) throws RepositoryException {
52          try {
53              if (current.exists()) {
54                  Files.createDirectories(result.toPath().getParent());
55                  Files.copy(current.toPath(), result.toPath());
56              }
57              ArtifactRepository localRepo = new MetadataRepository(result);
58              metadata.storeInLocalRepository(localRepo, localRepo);
59              merged = true;
60          } catch (Exception e) {
61              throw new RepositoryException(e.getMessage(), e);
62          }
63      }
64  
65      public boolean isMerged() {
66          return merged;
67      }
68  
69      public String getGroupId() {
70          return emptify(metadata.getGroupId());
71      }
72  
73      public String getArtifactId() {
74          return metadata.storedInGroupDirectory() ? "" : emptify(metadata.getArtifactId());
75      }
76  
77      public String getVersion() {
78          return metadata.storedInArtifactVersionDirectory() ? emptify(metadata.getBaseVersion()) : "";
79      }
80  
81      public String getType() {
82          return metadata.getRemoteFilename();
83      }
84  
85      private String emptify(String string) {
86          return (string != null) ? string : "";
87      }
88  
89      public File getFile() {
90          return null;
91      }
92  
93      public MetadataBridge setFile(File file) {
94          return this;
95      }
96  
97      @Override
98      public Path getPath() {
99          return null;
100     }
101 
102     public Nature getNature() {
103         if (metadata instanceof RepositoryMetadata) {
104             switch (((RepositoryMetadata) metadata).getNature()) {
105                 case RepositoryMetadata.RELEASE_OR_SNAPSHOT:
106                     return Nature.RELEASE_OR_SNAPSHOT;
107                 case RepositoryMetadata.SNAPSHOT:
108                     return Nature.SNAPSHOT;
109                 default:
110                     return Nature.RELEASE;
111             }
112         } else {
113             return Nature.RELEASE;
114         }
115     }
116 
117     public Map<String, String> getProperties() {
118         return Collections.emptyMap();
119     }
120 
121     @Override
122     public Metadata setProperties(Map<String, String> properties) {
123         return this;
124     }
125 
126     @SuppressWarnings("deprecation")
127     static class MetadataRepository extends DefaultArtifactRepository {
128 
129         private File metadataFile;
130 
131         MetadataRepository(File metadataFile) {
132             super("local", "", null);
133             this.metadataFile = metadataFile;
134         }
135 
136         @Override
137         public String getBasedir() {
138             return metadataFile.getParent();
139         }
140 
141         @Override
142         public String pathOfLocalRepositoryMetadata(ArtifactMetadata metadata, ArtifactRepository repository) {
143             return metadataFile.getName();
144         }
145     }
146 }