001    package org.apache.maven.artifact.repository.metadata;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.File;
023    
024    import org.apache.maven.artifact.metadata.ArtifactMetadata;
025    import org.apache.maven.artifact.repository.ArtifactRepository;
026    import org.apache.maven.artifact.repository.DefaultArtifactRepository;
027    import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
028    import org.codehaus.plexus.util.FileUtils;
029    import org.sonatype.aether.RepositoryException;
030    import org.sonatype.aether.metadata.MergeableMetadata;
031    
032    /**
033     * <strong>Warning:</strong> This is an internal utility class that is only public for technical reasons, it is not part
034     * of the public API. In particular, this class can be changed or deleted without prior notice.
035     * 
036     * @author Benjamin Bentmann
037     */
038    public final class MetadataBridge
039        implements MergeableMetadata
040    {
041    
042        private ArtifactMetadata metadata;
043    
044        private boolean merged;
045    
046        public MetadataBridge( ArtifactMetadata metadata )
047        {
048            this.metadata = metadata;
049        }
050    
051        public void merge( File current, File result )
052            throws RepositoryException
053        {
054            try
055            {
056                if ( current.exists() )
057                {
058                    FileUtils.copyFile( current, result );
059                }
060                ArtifactRepository localRepo = new MetadataRepository( result );
061                metadata.storeInLocalRepository( localRepo, localRepo );
062                merged = true;
063            }
064            catch ( Exception e )
065            {
066                throw new RepositoryException( e.getMessage(), e );
067            }
068        }
069    
070        public boolean isMerged()
071        {
072            return merged;
073        }
074    
075        public String getGroupId()
076        {
077            return emptify( metadata.getGroupId() );
078        }
079    
080        public String getArtifactId()
081        {
082            return metadata.storedInGroupDirectory() ? "" : emptify( metadata.getArtifactId() );
083        }
084    
085        public String getVersion()
086        {
087            return metadata.storedInArtifactVersionDirectory() ? emptify( metadata.getBaseVersion() ) : "";
088        }
089    
090        public String getType()
091        {
092            return metadata.getRemoteFilename();
093        }
094    
095        private String emptify( String string )
096        {
097            return ( string != null ) ? string : "";
098        }
099    
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    }