|
19 | 19 |
|
20 | 20 | import java.io.File; |
21 | 21 | import java.lang.reflect.Field; |
22 | | -import java.lang.reflect.Modifier; |
23 | 22 | import java.nio.charset.StandardCharsets; |
24 | 23 | import java.nio.file.Files; |
25 | 24 | import java.security.Permission; |
@@ -211,20 +210,28 @@ private static void restoreEnvironment() throws Exception { |
211 | 210 | * because Flink's CliFrontend requires a Flink configuration file for which the location can only |
212 | 211 | * be set using the {@code ConfigConstants.ENV_FLINK_CONF_DIR} environment variable. |
213 | 212 | */ |
| 213 | + @SuppressWarnings("unchecked") |
214 | 214 | private static void modifyEnv(Map<String, String> env) throws Exception { |
215 | | - Class processEnv = Class.forName("java.lang.ProcessEnvironment"); |
216 | | - Field envField = processEnv.getDeclaredField("theUnmodifiableEnvironment"); |
| 215 | + Class<?> processEnvironment = Class.forName("java.lang.ProcessEnvironment"); |
| 216 | + Field theEnvironmentField = processEnvironment.getDeclaredField("theEnvironment"); |
| 217 | + theEnvironmentField.setAccessible(true); |
| 218 | + Map<String, String> envMap = (Map<String, String>) theEnvironmentField.get(null); |
| 219 | + envMap.clear(); |
| 220 | + envMap.putAll(env); |
217 | 221 |
|
218 | | - Field modifiersField = Field.class.getDeclaredField("modifiers"); |
219 | | - modifiersField.setAccessible(true); |
220 | | - modifiersField.setInt(envField, envField.getModifiers() & ~Modifier.FINAL); |
221 | | - |
222 | | - envField.setAccessible(true); |
223 | | - envField.set(null, env); |
224 | | - envField.setAccessible(false); |
225 | | - |
226 | | - modifiersField.setInt(envField, envField.getModifiers() & Modifier.FINAL); |
227 | | - modifiersField.setAccessible(false); |
| 222 | + try { |
| 223 | + Field theCaseInsensitiveEnvironmentField = |
| 224 | + processEnvironment.getDeclaredField("theCaseInsensitiveEnvironment"); |
| 225 | + theCaseInsensitiveEnvironmentField.setAccessible(true); |
| 226 | + Map<String, String> ciEnvMap = |
| 227 | + (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null); |
| 228 | + if (ciEnvMap != null) { |
| 229 | + ciEnvMap.clear(); |
| 230 | + ciEnvMap.putAll(env); |
| 231 | + } |
| 232 | + } catch (NoSuchFieldException e) { |
| 233 | + // theCaseInsensitiveEnvironment is not present on all JDK platforms (e.g. Linux). |
| 234 | + } |
228 | 235 | } |
229 | 236 |
|
230 | 237 | /** Prevents the CliFrontend from calling System.exit. */ |
|
0 commit comments