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