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