001    package org.apache.maven;
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.util.ArrayList;
023    import java.util.Collection;
024    import java.util.Collections;
025    import java.util.List;
026    import java.util.Map;
027    
028    import org.apache.maven.artifact.handler.ArtifactHandler;
029    import org.apache.maven.artifact.handler.DefaultArtifactHandler;
030    import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
031    import org.apache.maven.artifact.repository.ArtifactRepository;
032    import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
033    import org.sonatype.aether.artifact.Artifact;
034    import org.sonatype.aether.artifact.ArtifactType;
035    import org.sonatype.aether.artifact.ArtifactTypeRegistry;
036    import org.sonatype.aether.graph.Dependency;
037    import org.sonatype.aether.graph.DependencyFilter;
038    import org.sonatype.aether.graph.DependencyNode;
039    import org.sonatype.aether.graph.Exclusion;
040    import org.sonatype.aether.repository.Authentication;
041    import org.sonatype.aether.repository.Proxy;
042    import org.sonatype.aether.repository.RemoteRepository;
043    import org.sonatype.aether.repository.RepositoryPolicy;
044    import org.sonatype.aether.util.artifact.ArtifactProperties;
045    import org.sonatype.aether.util.artifact.DefaultArtifact;
046    import org.sonatype.aether.util.artifact.DefaultArtifactType;
047    
048    /**
049     * <strong>Warning:</strong> This is an internal utility class that is only public for technical reasons, it is not part
050     * of the public API. In particular, this class can be changed or deleted without prior notice.
051     * 
052     * @author Benjamin Bentmann
053     */
054    public class RepositoryUtils
055    {
056    
057        private static String nullify( String string )
058        {
059            return ( string == null || string.length() <= 0 ) ? null : string;
060        }
061    
062        private static org.apache.maven.artifact.Artifact toArtifact( Dependency dependency )
063        {
064            if ( dependency == null )
065            {
066                return null;
067            }
068    
069            org.apache.maven.artifact.Artifact result = toArtifact( dependency.getArtifact() );
070            result.setScope( dependency.getScope() );
071            result.setOptional( dependency.isOptional() );
072    
073            return result;
074        }
075    
076        public static org.apache.maven.artifact.Artifact toArtifact( Artifact artifact )
077        {
078            if ( artifact == null )
079            {
080                return null;
081            }
082    
083            ArtifactHandler handler = newHandler( artifact );
084    
085            /*
086             * NOTE: From Artifact.hasClassifier(), an empty string and a null both denote "no classifier". However, some
087             * plugins only check for null, so be sure to nullify an empty classifier.
088             */
089            org.apache.maven.artifact.Artifact result =
090                new org.apache.maven.artifact.DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(),
091                                                               artifact.getVersion(), null,
092                                                               artifact.getProperty( ArtifactProperties.TYPE,
093                                                                                     artifact.getExtension() ),
094                                                               nullify( artifact.getClassifier() ), handler );
095    
096            result.setFile( artifact.getFile() );
097            result.setResolved( artifact.getFile() != null );
098    
099            List<String> trail = new ArrayList<String>( 1 );
100            trail.add( result.getId() );
101            result.setDependencyTrail( trail );
102    
103            return result;
104        }
105    
106        public static void toArtifacts( Collection<org.apache.maven.artifact.Artifact> artifacts,
107                                        Collection<? extends DependencyNode> nodes, List<String> trail,
108                                        DependencyFilter filter )
109        {
110            for ( DependencyNode node : nodes )
111            {
112                org.apache.maven.artifact.Artifact artifact = toArtifact( node.getDependency() );
113    
114                List<String> nodeTrail = new ArrayList<String>( trail.size() + 1 );
115                nodeTrail.addAll( trail );
116                nodeTrail.add( artifact.getId() );
117    
118                if ( filter == null || filter.accept( node, Collections.<DependencyNode> emptyList() ) )
119                {
120                    artifact.setDependencyTrail( nodeTrail );
121                    artifacts.add( artifact );
122                }
123    
124                toArtifacts( artifacts, node.getChildren(), nodeTrail, filter );
125            }
126        }
127    
128        public static Artifact toArtifact( org.apache.maven.artifact.Artifact artifact )
129        {
130            if ( artifact == null )
131            {
132                return null;
133            }
134    
135            String version = artifact.getVersion();
136            if ( version == null && artifact.getVersionRange() != null )
137            {
138                version = artifact.getVersionRange().toString();
139            }
140    
141            Map<String, String> props = null;
142            if ( org.apache.maven.artifact.Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) )
143            {
144                String localPath = ( artifact.getFile() != null ) ? artifact.getFile().getPath() : "";
145                props = Collections.singletonMap( ArtifactProperties.LOCAL_PATH, localPath );
146            }
147    
148            Artifact result =
149                new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(),
150                                     artifact.getArtifactHandler().getExtension(), version, props,
151                                     newArtifactType( artifact.getType(), artifact.getArtifactHandler() ) );
152            result = result.setFile( artifact.getFile() );
153    
154            return result;
155        }
156    
157        public static Dependency toDependency( org.apache.maven.artifact.Artifact artifact,
158                                               Collection<org.apache.maven.model.Exclusion> exclusions )
159        {
160            if ( artifact == null )
161            {
162                return null;
163            }
164    
165            Artifact result = toArtifact( artifact );
166    
167            List<Exclusion> excl = null;
168            if ( exclusions != null )
169            {
170                excl = new ArrayList<Exclusion>( exclusions.size() );
171                for ( org.apache.maven.model.Exclusion exclusion : exclusions )
172                {
173                    excl.add( toExclusion( exclusion ) );
174                }
175            }
176    
177            return new Dependency( result, artifact.getScope(), artifact.isOptional(), excl );
178        }
179    
180        public static List<RemoteRepository> toRepos( List<ArtifactRepository> repos )
181        {
182            if ( repos == null )
183            {
184                return null;
185            }
186    
187            List<RemoteRepository> results = new ArrayList<RemoteRepository>( repos.size() );
188            for ( ArtifactRepository repo : repos )
189            {
190                results.add( toRepo( repo ) );
191            }
192            return results;
193        }
194    
195        public static RemoteRepository toRepo( ArtifactRepository repo )
196        {
197            RemoteRepository result = null;
198            if ( repo != null )
199            {
200                result = new RemoteRepository( repo.getId(), getLayout( repo ), repo.getUrl() );
201                result.setPolicy( true, toPolicy( repo.getSnapshots() ) );
202                result.setPolicy( false, toPolicy( repo.getReleases() ) );
203                result.setAuthentication( toAuthentication( repo.getAuthentication() ) );
204                result.setProxy( toProxy( repo.getProxy() ) );
205                result.setMirroredRepositories( toRepos( repo.getMirroredRepositories() ) );
206            }
207            return result;
208        }
209    
210        public static String getLayout( ArtifactRepository repo )
211        {
212            try
213            {
214                return repo.getLayout().getId();
215            }
216            catch ( LinkageError e )
217            {
218                /*
219                 * NOTE: getId() was added in 3.x and is as such not implemented by plugins compiled against 2.x APIs.
220                 */
221                String className = repo.getLayout().getClass().getSimpleName();
222                if ( className.endsWith( "RepositoryLayout" ) )
223                {
224                    String layout = className.substring( 0, className.length() - "RepositoryLayout".length() );
225                    if ( layout.length() > 0 )
226                    {
227                        layout = Character.toLowerCase( layout.charAt( 0 ) ) + layout.substring( 1 );
228                        return layout;
229                    }
230                }
231                return "";
232            }
233        }
234    
235        private static RepositoryPolicy toPolicy( ArtifactRepositoryPolicy policy )
236        {
237            RepositoryPolicy result = null;
238            if ( policy != null )
239            {
240                result = new RepositoryPolicy( policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy() );
241            }
242            return result;
243        }
244    
245        private static Authentication toAuthentication( org.apache.maven.artifact.repository.Authentication auth )
246        {
247            Authentication result = null;
248            if ( auth != null )
249            {
250                result =
251                    new Authentication( auth.getUsername(), auth.getPassword(), auth.getPrivateKey(), auth.getPassphrase() );
252            }
253            return result;
254        }
255    
256        private static Proxy toProxy( org.apache.maven.repository.Proxy proxy )
257        {
258            Proxy result = null;
259            if ( proxy != null )
260            {
261                Authentication auth = new Authentication( proxy.getUserName(), proxy.getPassword() );
262                result = new Proxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(), auth );
263            }
264            return result;
265        }
266    
267        public static ArtifactHandler newHandler( Artifact artifact )
268        {
269            String type = artifact.getProperty( ArtifactProperties.TYPE, artifact.getExtension() );
270            DefaultArtifactHandler handler = new DefaultArtifactHandler( type );
271            handler.setExtension( artifact.getExtension() );
272            handler.setLanguage( artifact.getProperty( ArtifactProperties.LANGUAGE, null ) );
273            handler.setAddedToClasspath( Boolean.parseBoolean( artifact.getProperty( ArtifactProperties.CONSTITUTES_BUILD_PATH,
274                                                                                     "" ) ) );
275            handler.setIncludesDependencies( Boolean.parseBoolean( artifact.getProperty( ArtifactProperties.INCLUDES_DEPENDENCIES,
276                                                                                         "" ) ) );
277            return handler;
278        }
279    
280        public static ArtifactType newArtifactType( String id, ArtifactHandler handler )
281        {
282            return new DefaultArtifactType( id, handler.getExtension(), handler.getClassifier(), handler.getLanguage(),
283                                            handler.isAddedToClasspath(), handler.isIncludesDependencies() );
284        }
285    
286        public static Dependency toDependency( org.apache.maven.model.Dependency dependency,
287                                               ArtifactTypeRegistry stereotypes )
288        {
289            ArtifactType stereotype = stereotypes.get( dependency.getType() );
290            if ( stereotype == null )
291            {
292                stereotype = new DefaultArtifactType( dependency.getType() );
293            }
294    
295            boolean system = dependency.getSystemPath() != null && dependency.getSystemPath().length() > 0;
296    
297            Map<String, String> props = null;
298            if ( system )
299            {
300                props = Collections.singletonMap( ArtifactProperties.LOCAL_PATH, dependency.getSystemPath() );
301            }
302    
303            Artifact artifact =
304                new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), null,
305                                     dependency.getVersion(), props, stereotype );
306    
307            List<Exclusion> exclusions = new ArrayList<Exclusion>( dependency.getExclusions().size() );
308            for ( org.apache.maven.model.Exclusion exclusion : dependency.getExclusions() )
309            {
310                exclusions.add( toExclusion( exclusion ) );
311            }
312    
313            Dependency result = new Dependency( artifact, dependency.getScope(), dependency.isOptional(), exclusions );
314    
315            return result;
316        }
317    
318        private static Exclusion toExclusion( org.apache.maven.model.Exclusion exclusion )
319        {
320            return new Exclusion( exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*" );
321        }
322    
323        public static ArtifactTypeRegistry newArtifactTypeRegistry( ArtifactHandlerManager handlerManager )
324        {
325            return new MavenArtifactTypeRegistry( handlerManager );
326        }
327    
328        static class MavenArtifactTypeRegistry
329            implements ArtifactTypeRegistry
330        {
331    
332            private final ArtifactHandlerManager handlerManager;
333    
334            public MavenArtifactTypeRegistry( ArtifactHandlerManager handlerManager )
335            {
336                this.handlerManager = handlerManager;
337            }
338    
339            public ArtifactType get( String stereotypeId )
340            {
341                ArtifactHandler handler = handlerManager.getArtifactHandler( stereotypeId );
342                return newArtifactType( stereotypeId, handler );
343            }
344    
345        }
346    
347    }