|
1 | 1 | package au.com.onegeek.sbtdotenv; |
2 | 2 |
|
3 | | -import java.util.Map; |
4 | | - |
5 | 3 | import com.sun.jna.Library; |
6 | 4 | import com.sun.jna.Native; |
7 | 5 | import com.sun.jna.Platform; |
| 6 | +import java.util.Map; |
8 | 7 |
|
9 | 8 | /** |
10 | 9 | * Change the actual program environment so that changes propagate to child processes |
11 | 10 | * |
12 | | - * Taken from: http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java/7201825#7201825 |
| 11 | + * <p>Taken from: |
| 12 | + * http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java/7201825#7201825 |
13 | 13 | * |
14 | | - * Created by mfellows on 20/07/2014. |
| 14 | + * <p>Created by mfellows on 20/07/2014. |
15 | 15 | */ |
16 | 16 | public abstract class NativeEnvironmentManager { |
17 | | - public abstract void setEnv(String key, String value); |
| 17 | + public abstract void setEnv(String key, String value); |
18 | 18 |
|
19 | | - static class EnvironmentException extends RuntimeException { |
20 | | - EnvironmentException(String key) { |
21 | | - super("Failed to set environment variable: " + key); |
22 | | - } |
| 19 | + static class EnvironmentException extends RuntimeException { |
| 20 | + EnvironmentException(String key) { |
| 21 | + super("Failed to set environment variable: " + key); |
23 | 22 | } |
| 23 | + } |
24 | 24 |
|
25 | | - static class WindowsNativeEnvironmentManagerImpl extends NativeEnvironmentManager { |
26 | | - public interface WindowsEnvironmentLibC extends Library { |
27 | | - WindowsEnvironmentLibC INSTANCE = ( |
28 | | - (WindowsEnvironmentLibC) Native.load("msvcrt", |
29 | | - WindowsEnvironmentLibC.class) |
30 | | - ); |
| 25 | + static class WindowsNativeEnvironmentManagerImpl extends NativeEnvironmentManager { |
| 26 | + public interface WindowsEnvironmentLibC extends Library { |
| 27 | + WindowsEnvironmentLibC INSTANCE = |
| 28 | + ((WindowsEnvironmentLibC) Native.load("msvcrt", WindowsEnvironmentLibC.class)); |
31 | 29 |
|
32 | | - int _putenv(String name); |
33 | | - } |
| 30 | + int _putenv(String name); |
| 31 | + } |
34 | 32 |
|
35 | | - private WindowsEnvironmentLibC libc = WindowsEnvironmentLibC.INSTANCE; |
| 33 | + private WindowsEnvironmentLibC libc = WindowsEnvironmentLibC.INSTANCE; |
36 | 34 |
|
37 | | - @Override |
38 | | - public void setEnv(String name, String value) { |
39 | | - String s = name + "="; |
40 | | - if (value != null) |
41 | | - s += value; |
| 35 | + @Override |
| 36 | + public void setEnv(String name, String value) { |
| 37 | + String s = name + "="; |
| 38 | + if (value != null) s += value; |
42 | 39 |
|
43 | | - if (libc._putenv(s) != 0) |
44 | | - throw new EnvironmentException(name); |
45 | | - } |
| 40 | + if (libc._putenv(s) != 0) throw new EnvironmentException(name); |
46 | 41 | } |
| 42 | + } |
| 43 | + |
| 44 | + static class PosixNativeEnvironmentManagerImpl extends NativeEnvironmentManager { |
| 45 | + public interface PosixEnvironmentLibC extends Library { |
| 46 | + PosixEnvironmentLibC INSTANCE = |
| 47 | + ((PosixEnvironmentLibC) Native.load("c", PosixEnvironmentLibC.class)); |
| 48 | + |
| 49 | + int setenv(String name, String value, int overwrite); |
47 | 50 |
|
48 | | - static class PosixNativeEnvironmentManagerImpl extends NativeEnvironmentManager { |
49 | | - public interface PosixEnvironmentLibC extends Library { |
50 | | - PosixEnvironmentLibC INSTANCE = ( |
51 | | - (PosixEnvironmentLibC) Native.load("c", |
52 | | - PosixEnvironmentLibC.class) |
53 | | - ); |
54 | | - |
55 | | - int setenv(String name, String value, int overwrite); |
56 | | - int unsetenv(String name); |
57 | | - } |
58 | | - |
59 | | - private PosixEnvironmentLibC libc = PosixEnvironmentLibC.INSTANCE; |
60 | | - |
61 | | - @Override |
62 | | - public void setEnv(String name, String value) { |
63 | | - int result; |
64 | | - if (value != null) |
65 | | - result = libc.setenv(name, value, 1); |
66 | | - else |
67 | | - result = libc.unsetenv(name); |
68 | | - |
69 | | - if (result != 0) |
70 | | - throw new EnvironmentException(name); |
71 | | - } |
| 51 | + int unsetenv(String name); |
72 | 52 | } |
73 | 53 |
|
74 | | - private static NativeEnvironmentManager instance = null; |
75 | | - private static NativeEnvironmentManager getInstance() { |
76 | | - if (instance == null) { |
77 | | - if (Platform.isWindows()) |
78 | | - instance = new WindowsNativeEnvironmentManagerImpl(); |
79 | | - else |
80 | | - instance = new PosixNativeEnvironmentManagerImpl(); |
81 | | - } |
| 54 | + private PosixEnvironmentLibC libc = PosixEnvironmentLibC.INSTANCE; |
82 | 55 |
|
83 | | - return instance; |
| 56 | + @Override |
| 57 | + public void setEnv(String name, String value) { |
| 58 | + int result; |
| 59 | + if (value != null) result = libc.setenv(name, value, 1); |
| 60 | + else result = libc.unsetenv(name); |
| 61 | + |
| 62 | + if (result != 0) throw new EnvironmentException(name); |
84 | 63 | } |
| 64 | + } |
| 65 | + |
| 66 | + private static NativeEnvironmentManager instance = null; |
| 67 | + |
| 68 | + private static NativeEnvironmentManager getInstance() { |
| 69 | + if (instance == null) { |
| 70 | + if (Platform.isWindows()) instance = new WindowsNativeEnvironmentManagerImpl(); |
| 71 | + else instance = new PosixNativeEnvironmentManagerImpl(); |
| 72 | + } |
| 73 | + |
| 74 | + return instance; |
| 75 | + } |
85 | 76 |
|
86 | | - public static void setEnv(Map<String, String> env) { |
87 | | - NativeEnvironmentManager envManager = NativeEnvironmentManager.getInstance(); |
88 | | - for (Map.Entry<String, String> entry : env.entrySet()) { |
89 | | - envManager.setEnv(entry.getKey(), entry.getValue()); |
90 | | - } |
| 77 | + public static void setEnv(Map<String, String> env) { |
| 78 | + NativeEnvironmentManager envManager = NativeEnvironmentManager.getInstance(); |
| 79 | + for (Map.Entry<String, String> entry : env.entrySet()) { |
| 80 | + envManager.setEnv(entry.getKey(), entry.getValue()); |
91 | 81 | } |
| 82 | + } |
92 | 83 | } |
0 commit comments