View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.internal.impl;
20  
21  import java.nio.file.Path;
22  import java.time.Instant;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  import org.apache.maven.RepositoryUtils;
30  import org.apache.maven.api.Project;
31  import org.apache.maven.api.RemoteRepository;
32  import org.apache.maven.api.Session;
33  import org.apache.maven.api.Version;
34  import org.apache.maven.api.annotations.Nonnull;
35  import org.apache.maven.api.annotations.Nullable;
36  import org.apache.maven.api.services.Lookup;
37  import org.apache.maven.api.services.LookupException;
38  import org.apache.maven.api.services.MavenException;
39  import org.apache.maven.api.settings.Settings;
40  import org.apache.maven.artifact.repository.ArtifactRepository;
41  import org.apache.maven.bridge.MavenRepositorySystem;
42  import org.apache.maven.execution.MavenSession;
43  import org.apache.maven.plugin.MojoExecution;
44  import org.apache.maven.plugin.descriptor.MojoDescriptor;
45  import org.apache.maven.plugin.descriptor.PluginDescriptor;
46  import org.apache.maven.project.MavenProject;
47  import org.apache.maven.rtinfo.RuntimeInformation;
48  import org.eclipse.aether.RepositorySystem;
49  import org.eclipse.aether.RepositorySystemSession;
50  
51  import static org.apache.maven.internal.impl.Utils.map;
52  import static org.apache.maven.internal.impl.Utils.nonNull;
53  
54  public class DefaultSession extends AbstractSession implements InternalMavenSession {
55  
56      private final MavenSession mavenSession;
57      private final MavenRepositorySystem mavenRepositorySystem;
58      private final RuntimeInformation runtimeInformation;
59      private final Map<String, Project> allProjects = new ConcurrentHashMap<>();
60  
61      @SuppressWarnings("checkstyle:ParameterNumber")
62      public DefaultSession(
63              @Nonnull MavenSession session,
64              @Nonnull RepositorySystem repositorySystem,
65              @Nullable List<RemoteRepository> remoteRepositories,
66              @Nonnull MavenRepositorySystem mavenRepositorySystem,
67              @Nonnull Lookup lookup,
68              @Nonnull RuntimeInformation runtimeInformation) {
69          super(
70                  nonNull(session).getRepositorySession(),
71                  repositorySystem,
72                  remoteRepositories,
73                  remoteRepositories == null
74                          ? map(session.getRequest().getRemoteRepositories(), RepositoryUtils::toRepo)
75                          : null,
76                  lookup);
77          this.mavenSession = session;
78          this.mavenRepositorySystem = mavenRepositorySystem;
79          this.runtimeInformation = runtimeInformation;
80      }
81  
82      public MavenSession getMavenSession() {
83          if (mavenSession == null) {
84              throw new IllegalArgumentException("Found null mavenSession on session " + this);
85          }
86          return mavenSession;
87      }
88  
89      @Override
90      public List<Project> getProjects(List<MavenProject> projects) {
91          return projects == null ? null : map(projects, this::getProject);
92      }
93  
94      @Override
95      public Project getProject(MavenProject project) {
96          return allProjects.computeIfAbsent(project.getId(), id -> new DefaultProject(this, project));
97      }
98  
99      @Override
100     public List<ArtifactRepository> toArtifactRepositories(List<RemoteRepository> repositories) {
101         return repositories == null ? null : map(repositories, this::toArtifactRepository);
102     }
103 
104     @Nonnull
105     @Override
106     public Settings getSettings() {
107         return getMavenSession().getSettings().getDelegate();
108     }
109 
110     @Nonnull
111     @Override
112     public Map<String, String> getUserProperties() {
113         return Collections.unmodifiableMap(new PropertiesAsMap(getMavenSession().getUserProperties()));
114     }
115 
116     @Nonnull
117     @Override
118     public Map<String, String> getSystemProperties() {
119         return Collections.unmodifiableMap(new PropertiesAsMap(getMavenSession().getSystemProperties()));
120     }
121 
122     @Nonnull
123     @Override
124     public Map<String, String> getEffectiveProperties(@Nullable Project project) {
125         HashMap<String, String> result = new HashMap<>(getSystemProperties());
126         if (project != null) {
127             result.putAll(project.getModel().getProperties());
128         }
129         result.putAll(getUserProperties());
130         return result;
131     }
132 
133     @Nonnull
134     @Override
135     public Version getMavenVersion() {
136         return parseVersion(runtimeInformation.getMavenVersion());
137     }
138 
139     @Override
140     public int getDegreeOfConcurrency() {
141         return getMavenSession().getRequest().getDegreeOfConcurrency();
142     }
143 
144     @Nonnull
145     @Override
146     public Instant getStartTime() {
147         return getMavenSession().getRequest().getStartTime().toInstant();
148     }
149 
150     @Override
151     public Path getRootDirectory() {
152         return getMavenSession().getRequest().getRootDirectory();
153     }
154 
155     @Override
156     public Path getTopDirectory() {
157         return getMavenSession().getRequest().getTopDirectory();
158     }
159 
160     @Nonnull
161     @Override
162     public List<Project> getProjects() {
163         return getProjects(getMavenSession().getProjects());
164     }
165 
166     @Nonnull
167     @Override
168     public Map<String, Object> getPluginContext(Project project) {
169         nonNull(project, "project");
170         try {
171             MojoExecution mojoExecution = lookup.lookup(MojoExecution.class);
172             MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
173             PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
174             return getMavenSession().getPluginContext(pluginDescriptor, ((DefaultProject) project).getProject());
175         } catch (LookupException e) {
176             throw new MavenException("The PluginContext is only available during a mojo execution", e);
177         }
178     }
179 
180     protected Session newSession(RepositorySystemSession repoSession, List<RemoteRepository> repositories) {
181         final MavenSession ms = nonNull(getMavenSession());
182         final MavenSession mss;
183         if (repoSession != ms.getRepositorySession()) {
184             mss = new MavenSession(repoSession, ms.getRequest(), ms.getResult());
185         } else {
186             mss = ms;
187         }
188         return newSession(mss, repositories);
189     }
190 
191     protected Session newSession(MavenSession mavenSession, List<RemoteRepository> repositories) {
192         return new DefaultSession(
193                 nonNull(mavenSession),
194                 getRepositorySystem(),
195                 repositories,
196                 mavenRepositorySystem,
197                 lookup,
198                 runtimeInformation);
199     }
200 
201     public ArtifactRepository toArtifactRepository(RemoteRepository repository) {
202         if (repository instanceof DefaultRemoteRepository) {
203             org.eclipse.aether.repository.RemoteRepository rr = ((DefaultRemoteRepository) repository).getRepository();
204 
205             try {
206                 return mavenRepositorySystem.createRepository(
207                         rr.getUrl(),
208                         rr.getId(),
209                         rr.getPolicy(false).isEnabled(),
210                         rr.getPolicy(false).getUpdatePolicy(),
211                         rr.getPolicy(true).isEnabled(),
212                         rr.getPolicy(true).getUpdatePolicy(),
213                         rr.getPolicy(false).getChecksumPolicy());
214 
215             } catch (Exception e) {
216                 throw new RuntimeException("Unable to create repository", e);
217             }
218         } else {
219             // TODO
220             throw new UnsupportedOperationException("Not yet implemented");
221         }
222     }
223 }