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