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.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 project != null && project.getBasedir() != null
97 ? allProjects.computeIfAbsent(project.getId(), id -> new DefaultProject(this, project))
98 : null;
99 }
100
101 @Override
102 public List<ArtifactRepository> toArtifactRepositories(List<RemoteRepository> repositories) {
103 return repositories == null ? null : map(repositories, this::toArtifactRepository);
104 }
105
106 @Nonnull
107 @Override
108 public Settings getSettings() {
109 return getMavenSession().getSettings().getDelegate();
110 }
111
112 @Nonnull
113 @Override
114 public Map<String, String> getUserProperties() {
115 return Collections.unmodifiableMap(new PropertiesAsMap(getMavenSession().getUserProperties()));
116 }
117
118 @Nonnull
119 @Override
120 public Map<String, String> getSystemProperties() {
121 return Collections.unmodifiableMap(new PropertiesAsMap(getMavenSession().getSystemProperties()));
122 }
123
124 @Nonnull
125 @Override
126 public Map<String, String> getEffectiveProperties(@Nullable Project project) {
127 HashMap<String, String> result = new HashMap<>(getSystemProperties());
128 if (project != null) {
129 result.putAll(project.getModel().getProperties());
130 }
131 result.putAll(getUserProperties());
132 return result;
133 }
134
135 @Nonnull
136 @Override
137 public Version getMavenVersion() {
138 return parseVersion(runtimeInformation.getMavenVersion());
139 }
140
141 @Override
142 public int getDegreeOfConcurrency() {
143 return getMavenSession().getRequest().getDegreeOfConcurrency();
144 }
145
146 @Nonnull
147 @Override
148 public Instant getStartTime() {
149 return getMavenSession().getRequest().getStartTime().toInstant();
150 }
151
152 @Override
153 public Path getRootDirectory() {
154 return getMavenSession().getRequest().getRootDirectory();
155 }
156
157 @Override
158 public Path getTopDirectory() {
159 return getMavenSession().getRequest().getTopDirectory();
160 }
161
162 @Nonnull
163 @Override
164 public List<Project> getProjects() {
165 return getProjects(getMavenSession().getProjects());
166 }
167
168 @Nonnull
169 @Override
170 public Map<String, Object> getPluginContext(Project project) {
171 nonNull(project, "project");
172 try {
173 MojoExecution mojoExecution = lookup.lookup(MojoExecution.class);
174 MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
175 PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
176 return getMavenSession().getPluginContext(pluginDescriptor, ((DefaultProject) project).getProject());
177 } catch (LookupException e) {
178 throw new MavenException("The PluginContext is only available during a mojo execution", e);
179 }
180 }
181
182 protected Session newSession(RepositorySystemSession repoSession, List<RemoteRepository> repositories) {
183 final MavenSession ms = nonNull(getMavenSession());
184 final MavenSession mss;
185 if (repoSession != ms.getRepositorySession()) {
186 mss = new MavenSession(repoSession, ms.getRequest(), ms.getResult());
187 } else {
188 mss = ms;
189 }
190 return newSession(mss, repositories);
191 }
192
193 protected Session newSession(MavenSession mavenSession, List<RemoteRepository> repositories) {
194 return new DefaultSession(
195 nonNull(mavenSession),
196 getRepositorySystem(),
197 repositories,
198 mavenRepositorySystem,
199 lookup,
200 runtimeInformation);
201 }
202
203 public ArtifactRepository toArtifactRepository(RemoteRepository repository) {
204 if (repository instanceof DefaultRemoteRepository) {
205 org.eclipse.aether.repository.RemoteRepository rr = ((DefaultRemoteRepository) repository).getRepository();
206
207 try {
208 return mavenRepositorySystem.createRepository(
209 rr.getUrl(),
210 rr.getId(),
211 rr.getPolicy(false).isEnabled(),
212 rr.getPolicy(false).getUpdatePolicy(),
213 rr.getPolicy(true).isEnabled(),
214 rr.getPolicy(true).getUpdatePolicy(),
215 rr.getPolicy(false).getChecksumPolicy());
216
217 } catch (Exception e) {
218 throw new RuntimeException("Unable to create repository", e);
219 }
220 } else {
221
222 throw new UnsupportedOperationException("Not yet implemented");
223 }
224 }
225 }