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