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