View Javadoc
1   package org.apache.maven.artifact.repository;
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.util.Arrays;
23  import java.util.List;
24  
25  import org.apache.maven.artifact.UnknownRepositoryLayoutException;
26  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
27  import org.apache.maven.plugin.LegacySupport;
28  import org.apache.maven.repository.RepositorySystem;
29  import org.codehaus.plexus.component.annotations.Component;
30  import org.codehaus.plexus.component.annotations.Requirement;
31  import org.eclipse.aether.RepositorySystemSession;
32  
33  /**
34   * @author jdcasey
35   */
36  @Component( role = ArtifactRepositoryFactory.class )
37  public class DefaultArtifactRepositoryFactory
38      implements ArtifactRepositoryFactory
39  {
40  
41      @Requirement
42      private org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory factory;
43  
44      @Requirement
45      private LegacySupport legacySupport;
46  
47      @Requirement
48      private RepositorySystem repositorySystem;
49  
50      public ArtifactRepositoryLayout getLayout( String layoutId )
51          throws UnknownRepositoryLayoutException
52      {
53          return factory.getLayout( layoutId );
54      }
55  
56      public ArtifactRepository createDeploymentArtifactRepository( String id, String url, String layoutId,
57                                                                    boolean uniqueVersion )
58          throws UnknownRepositoryLayoutException
59      {
60          return injectSession( factory.createDeploymentArtifactRepository( id, url, layoutId, uniqueVersion ), false );
61      }
62  
63      public ArtifactRepository createDeploymentArtifactRepository( String id, String url,
64                                                                    ArtifactRepositoryLayout repositoryLayout,
65                                                                    boolean uniqueVersion )
66      {
67          return injectSession( factory.createDeploymentArtifactRepository( id, url, repositoryLayout, uniqueVersion ),
68                                false );
69      }
70  
71      public ArtifactRepository createArtifactRepository( String id, String url, String layoutId,
72                                                          ArtifactRepositoryPolicy snapshots,
73                                                          ArtifactRepositoryPolicy releases )
74          throws UnknownRepositoryLayoutException
75      {
76          return injectSession( factory.createArtifactRepository( id, url, layoutId, snapshots, releases ), true );
77      }
78  
79      public ArtifactRepository createArtifactRepository( String id, String url,
80                                                          ArtifactRepositoryLayout repositoryLayout,
81                                                          ArtifactRepositoryPolicy snapshots,
82                                                          ArtifactRepositoryPolicy releases )
83      {
84          return injectSession( factory.createArtifactRepository( id, url, repositoryLayout, snapshots, releases ), true );
85      }
86  
87      public void setGlobalUpdatePolicy( String updatePolicy )
88      {
89          factory.setGlobalUpdatePolicy( updatePolicy );
90      }
91  
92      public void setGlobalChecksumPolicy( String checksumPolicy )
93      {
94          factory.setGlobalChecksumPolicy( checksumPolicy );
95      }
96  
97      private ArtifactRepository injectSession( ArtifactRepository repository, boolean mirrors )
98      {
99          RepositorySystemSession session = legacySupport.getRepositorySession();
100 
101         if ( session != null && repository != null && !isLocalRepository( repository ) )
102         {
103             List<ArtifactRepository> repositories = Arrays.asList( repository );
104 
105             if ( mirrors )
106             {
107                 repositorySystem.injectMirror( session, repositories );
108             }
109 
110             repositorySystem.injectProxy( session, repositories );
111 
112             repositorySystem.injectAuthentication( session, repositories );
113         }
114 
115         return repository;
116     }
117 
118     private boolean isLocalRepository( ArtifactRepository repository )
119     {
120         // unfortunately, the API doesn't allow to tell a remote repo and the local repo apart...
121         return "local".equals( repository.getId() );
122     }
123 
124 }