-
Notifications
You must be signed in to change notification settings - Fork 188
/
ExecutionEnvironmentConfigurationImpl.java
178 lines (147 loc) · 6.69 KB
/
ExecutionEnvironmentConfigurationImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*******************************************************************************
* Copyright (c) 2012, 2014 SAP SE and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* SAP SE - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.core.ee;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.toolchain.ToolchainManager;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.tycho.core.ee.shared.ExecutionEnvironment;
import org.eclipse.tycho.core.ee.shared.ExecutionEnvironmentConfiguration;
import org.eclipse.tycho.core.ee.shared.SystemCapability;
import org.eclipse.tycho.core.shared.BuildFailureException;
public class ExecutionEnvironmentConfigurationImpl implements ExecutionEnvironmentConfiguration {
// Most likely best to always be the latest known supported EE
private static final String DEFAULT_EXECUTION_ENVIRONMENT = "JavaSE-"
+ Integer.toString(Runtime.version().feature());
private static final int PRIMARY = 0;
private static final int SECONDARY = 1;
private Logger logger;
/** Configurations, ordered by precedence */
private final ProfileConfiguration[] configurations = new ProfileConfiguration[2];
private String effectiveProfileName = null;
private CustomExecutionEnvironment customExecutionEnvironment;
private final boolean ignoredByResolver;
private final ToolchainManager toolchainManager;
private MavenSession session;
public ExecutionEnvironmentConfigurationImpl(Logger logger, boolean ignoredByResolver,
ToolchainManager toolchainManager, MavenSession session) {
this.logger = logger;
this.ignoredByResolver = ignoredByResolver;
this.toolchainManager = toolchainManager;
this.session = session;
}
@Override
public void overrideProfileConfiguration(String profileName, String configurationOrigin)
throws IllegalStateException {
checkConfigurationMutable();
if (profileName == null) {
throw new NullPointerException();
}
this.configurations[PRIMARY] = new ProfileConfiguration(profileName, configurationOrigin);
}
@Override
public void setProfileConfiguration(String profileName, String configurationOrigin) throws IllegalStateException {
checkConfigurationMutable();
if (profileName == null) {
throw new NullPointerException();
}
this.configurations[SECONDARY] = new ProfileConfiguration(profileName, configurationOrigin);
}
private void checkConfigurationMutable() throws IllegalStateException {
if (effectiveProfileName != null) {
throw new IllegalStateException("Cannot change execution environment configuration after it has been used");
}
}
@Override
public String getProfileName() {
if (effectiveProfileName == null) {
// this also disallows further configuration changes
effectiveProfileName = computeEffectiveProfileName();
}
return effectiveProfileName;
}
private String computeEffectiveProfileName() {
for (ProfileConfiguration entry : configurations) {
if (entry != null) {
logger.debug("Using execution environment '" + entry.profileName + "' configured in " + entry.origin);
return entry.profileName;
}
}
logger.debug("Using default execution environment '" + DEFAULT_EXECUTION_ENVIRONMENT + "'");
return DEFAULT_EXECUTION_ENVIRONMENT;
}
@Override
public boolean isCustomProfile() {
if (ignoreExecutionEnvironment()) {
return false;
}
String profileName = getProfileName();
Collection<String> profileNames = ExecutionEnvironmentUtils.getProfileNames(toolchainManager, session, logger);
boolean profileExists = profileNames
.contains(profileName);
if (!profileExists && ignoredByResolver) {
throw new BuildFailureException(
"When using a custom execution environment profile, resolveWithExecutionEnvironmentConstraints must not be set to false");
}
return !profileExists;
}
@Override
public void setFullSpecificationForCustomProfile(List<SystemCapability> systemCapabilities)
throws IllegalStateException {
if (!isCustomProfile()) {
throw new IllegalStateException(
"Cannot set full specification when a standard execution environment is configured");
}
if (this.customExecutionEnvironment != null) {
throw new IllegalStateException("Cannot set full specification for a custom profile more than once");
}
this.customExecutionEnvironment = new CustomExecutionEnvironment(getProfileName(), systemCapabilities);
}
@Override
public ExecutionEnvironment getFullSpecification() throws IllegalStateException {
if (ignoreExecutionEnvironment()) {
return NoExecutionEnvironment.INSTANCE;
}
if (isCustomProfile()) {
if (customExecutionEnvironment == null) {
throw new IllegalStateException(
"Full specification of custom profile '" + getProfileName() + "' is not (yet) determined");
}
return customExecutionEnvironment;
}
return ExecutionEnvironmentUtils.getExecutionEnvironment(getProfileName(), toolchainManager, session, logger);
}
private static class ProfileConfiguration {
final String profileName;
final String origin;
ProfileConfiguration(String profileName, String origin) {
this.profileName = profileName;
this.origin = origin;
}
}
@Override
public boolean isIgnoredByResolver() {
return ignoredByResolver;
}
@Override
public Collection<ExecutionEnvironment> getAllKnownEEs() {
return ExecutionEnvironmentUtils.getProfileNames(toolchainManager, session, logger).stream() //
.map(profileName -> ExecutionEnvironmentUtils.getExecutionEnvironment(profileName, toolchainManager,
session, logger)) //
.collect(Collectors.toList());
}
@Override
public boolean ignoreExecutionEnvironment() {
return NoExecutionEnvironment.NAME.equals(getProfileName());
}
}