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    import java.util.Collections;
024    import java.util.Map;
025    
026    import org.apache.maven.artifact.metadata.ArtifactMetadata;
027    import org.apache.maven.artifact.repository.ArtifactRepository;
028    import org.apache.maven.artifact.repository.DefaultArtifactRepository;
029    import org.codehaus.plexus.util.FileUtils;
030    import org.eclipse.aether.RepositoryException;
031    import org.eclipse.aether.metadata.AbstractMetadata;
032    import org.eclipse.aether.metadata.MergeableMetadata;
033    import org.eclipse.aether.metadata.Metadata;
034    
035    /**
036     * <strong>Warning:</strong> This is an internal utility class that is only public for technical reasons, it is not part
037     * of the public API. In particular, this class can be changed or deleted without prior notice.
038     * 
039     * @author Benjamin Bentmann
040     */
041    public final class MetadataBridge
042        extends AbstractMetadata
043        implements MergeableMetadata
044    {
045    
046        private ArtifactMetadata metadata;
047    
048        private boolean merged;
049    
050        public MetadataBridge( ArtifactMetadata metadata )
051        {
052            this.metadata = metadata;
053        }
054    
055        public void merge( File current, File result )
056            throws RepositoryException
057        {
058            try
059            {
060                if ( current.exists() )
061                {
062                    FileUtils.copyFile( current, result );
063                }
064                ArtifactRepository localRepo = new MetadataRepository( result );
065                metadata.storeInLocalRepository( localRepo, localRepo );
066                merged = true;
067            }
068            catch ( Exception e )
069            {
070                throw new RepositoryException( e.getMessage(), e );
071            }
072        }
073    
074        public boolean isMerged()
075        {
076            return merged;
077        }
078    
079        public String getGroupId()
080        {
081            return emptify( metadata.getGroupId() );
082        }
083    
084        public String getArtifactId()
085        {
086            return metadata.storedInGroupDirectory() ? "" : emptify( metadata.getArtifactId() );
087        }
088    
089        public String getVersion()
090        {
091            return metadata.storedInArtifactVersionDirectory() ? emptify( metadata.getBaseVersion() ) : "";
092        }
093    
094        public String getType()
095        {
096            return metadata.getRemoteFilename();
097        }
098    
099        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    }