View Javadoc
1   package org.apache.maven.internal.impl;
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 javax.inject.Inject;
23  import javax.inject.Named;
24  import javax.inject.Singleton;
25  
26  import org.apache.maven.api.Session;
27  import org.apache.maven.bridge.MavenRepositorySystem;
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.rtinfo.RuntimeInformation;
30  import org.codehaus.plexus.PlexusContainer;
31  import org.eclipse.aether.RepositorySystem;
32  import org.eclipse.aether.SessionData;
33  
34  @Singleton
35  @Named
36  public class DefaultSessionFactory
37  {
38      private final RepositorySystem repositorySystem;
39      private final MavenRepositorySystem mavenRepositorySystem;
40      private final PlexusContainer plexusContainer;
41      private final RuntimeInformation runtimeInformation;
42  
43      @Inject
44      @SuppressWarnings( "checkstyle:ParameterNumber" )
45      public DefaultSessionFactory( RepositorySystem repositorySystem,
46                                    MavenRepositorySystem mavenRepositorySystem,
47                                    PlexusContainer plexusContainer,
48                                    RuntimeInformation runtimeInformation )
49      {
50          this.repositorySystem = repositorySystem;
51          this.mavenRepositorySystem = mavenRepositorySystem;
52          this.plexusContainer = plexusContainer;
53          this.runtimeInformation = runtimeInformation;
54      }
55  
56      public Session getSession( MavenSession mavenSession )
57      {
58          SessionData data = mavenSession.getRepositorySession().getData();
59          return ( Session ) data.computeIfAbsent( DefaultSession.class, () -> newSession( mavenSession ) );
60      }
61  
62      private Session newSession( MavenSession mavenSession )
63      {
64          return new DefaultSession(
65                  mavenSession, repositorySystem, null,
66                  mavenRepositorySystem, plexusContainer, runtimeInformation );
67      }
68  }