View Javadoc

1   package org.apache.maven.artifact.repository.metadata;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
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
42      extends AbstractMetadata
43      implements MergeableMetadata
44  {
45  
46      private ArtifactMetadata metadata;
47  
48      private boolean merged;
49  
50      public MetadataBridge( ArtifactMetadata metadata )
51      {
52          this.metadata = metadata;
53      }
54  
55      public void merge( File current, File result )
56          throws RepositoryException
57      {
58          try
59          {
60              if ( current.exists() )
61              {
62                  FileUtils.copyFile( current, result );
63              }
64              ArtifactRepository localRepo = new MetadataRepository( result );
65              metadata.storeInLocalRepository( localRepo, localRepo );
66              merged = true;
67          }
68          catch ( Exception e )
69          {
70              throw new RepositoryException( e.getMessage(), e );
71          }
72      }
73  
74      public boolean isMerged()
75      {
76          return merged;
77      }
78  
79      public String getGroupId()
80      {
81          return emptify( metadata.getGroupId() );
82      }
83  
84      public String getArtifactId()
85      {
86          return metadata.storedInGroupDirectory() ? "" : emptify( metadata.getArtifactId() );
87      }
88  
89      public String getVersion()
90      {
91          return metadata.storedInArtifactVersionDirectory() ? emptify( metadata.getBaseVersion() ) : "";
92      }
93  
94      public String getType()
95      {
96          return metadata.getRemoteFilename();
97      }
98  
99      private String emptify( String string )
100     {
101         return ( string != null ) ? string : "";
102     }
103 
104     public File getFile()
105     {
106         return null;
107     }
108 
109     public MetadataBridge setFile( File file )
110     {
111         return this;
112     }
113 
114     public Nature getNature()
115     {
116         if ( metadata instanceof RepositoryMetadata )
117         {
118             switch ( ( (RepositoryMetadata) metadata ).getNature() )
119             {
120                 case RepositoryMetadata.RELEASE_OR_SNAPSHOT:
121                     return Nature.RELEASE_OR_SNAPSHOT;
122                 case RepositoryMetadata.SNAPSHOT:
123                     return Nature.SNAPSHOT;
124                 default:
125                     return Nature.RELEASE;
126             }
127         }
128         else
129         {
130             return Nature.RELEASE;
131         }
132     }
133 
134     public Map<String, String> getProperties()
135     {
136         return Collections.emptyMap();
137     }
138 
139     @Override
140     public Metadata setProperties( Map<String, String> properties )
141     {
142         return this;
143     }
144 
145     @SuppressWarnings( "deprecation" )
146     static class MetadataRepository
147         extends DefaultArtifactRepository
148     {
149 
150         private File metadataFile;
151 
152         public MetadataRepository( File metadataFile )
153         {
154             super( "local", "", null );
155             this.metadataFile = metadataFile;
156         }
157 
158         @Override
159         public String getBasedir()
160         {
161             return metadataFile.getParent();
162         }
163 
164         @Override
165         public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
166         {
167             return metadataFile.getName();
168         }
169 
170     }
171 
172 }