1 package org.codehaus.mojo.jaxb2.shared.environment.sysprops;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.logging.Log;
23 import org.codehaus.mojo.jaxb2.AbstractJaxbMojo;
24 import org.codehaus.mojo.jaxb2.shared.Validate;
25 import org.codehaus.mojo.jaxb2.shared.environment.AbstractLogAwareFacet;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30
31
32
33
34
35
36
37 public class SystemPropertyChangeEnvironmentFacet extends AbstractLogAwareFacet {
38
39
40
41
42 public enum ChangeType {
43
44
45
46
47
48
49 ADD,
50
51
52
53
54
55 REMOVE,
56
57
58
59
60
61 CHANGE
62 }
63
64
65 private ChangeType type;
66 private String key;
67 private String newValue;
68 private String originalValue;
69
70
71
72
73
74
75
76
77
78
79 private SystemPropertyChangeEnvironmentFacet(final Log log, final String key) {
80
81
82 super(log);
83
84
85 this.key = key;
86 this.type = ChangeType.REMOVE;
87 }
88
89
90
91
92
93
94
95
96 private SystemPropertyChangeEnvironmentFacet(final Log log, final String key, final String newValue) {
97
98
99 super(log);
100
101
102 this.key = key;
103 this.originalValue = System.getProperty(key);
104 this.newValue = newValue;
105 this.type = existsAsSystemProperty(key) ? ChangeType.CHANGE : ChangeType.ADD;
106 }
107
108
109
110
111 @Override
112 public void setup() {
113
114 if (type == ChangeType.REMOVE) {
115 System.clearProperty(key);
116 } else {
117 System.setProperty(key, newValue);
118 }
119
120 if(log.isDebugEnabled()) {
121 log.debug("Setup " + toString());
122 }
123 }
124
125
126
127
128 @Override
129 public void restore() {
130
131 if (type == ChangeType.ADD) {
132 System.clearProperty(key);
133 } else {
134 System.setProperty(key, originalValue);
135 }
136
137 if(log.isDebugEnabled()) {
138 log.debug("Restored " + toString());
139 }
140 }
141
142
143
144
145 @Override
146 public String toString() {
147 final String toReturn = "SysProp key [" + key + "]\n"
148 + " ... Original value: [" + originalValue + "]\n"
149 + " ... Changed value : [" + newValue + "]";
150 return toReturn.replace("\n", AbstractJaxbMojo.NEWLINE);
151 }
152
153
154
155
156
157
158
159
160 public static SystemPropertyChangesBuilder getBuilder(final Log mavenLog) {
161 return new SystemPropertyChangesBuilder(mavenLog);
162 }
163
164
165
166
167 public static class SystemPropertyChangesBuilder {
168
169
170 private List<SystemPropertyChangeEnvironmentFacet> toReturn;
171 private Log mavenLog;
172
173 private SystemPropertyChangesBuilder(final Log mavenLog) {
174
175
176 Validate.notNull(mavenLog, "mavenLog");
177
178
179 this.toReturn = new ArrayList<SystemPropertyChangeEnvironmentFacet>();
180 this.mavenLog = mavenLog;
181 }
182
183
184
185
186
187
188
189
190 public SystemPropertyChangesBuilder remove(final String propertyName) {
191
192
193 checkSanity(propertyName);
194
195
196 toReturn.add(new SystemPropertyChangeEnvironmentFacet(mavenLog, propertyName));
197
198
199 return this;
200 }
201
202
203
204
205
206
207
208
209
210
211 public SystemPropertyChangesBuilder addOrChange(final String propertyName, final String value) {
212
213
214 checkSanity(propertyName);
215
216
217 toReturn.add(new SystemPropertyChangeEnvironmentFacet(mavenLog, propertyName, value));
218
219
220 return this;
221
222 }
223
224
225
226
227
228 public List<SystemPropertyChangeEnvironmentFacet> build() {
229 return toReturn;
230 }
231
232
233
234
235
236 private void checkSanity(final String propertyName) {
237
238
239 Validate.notEmpty(propertyName, "propertyName");
240
241
242 for (SystemPropertyChangeEnvironmentFacet current : toReturn) {
243 if (current.key.equals(propertyName)) {
244 throw new IllegalArgumentException("A SystemPropertyChange for propertyName '" + propertyName +
245 "' is already present. Only one SystemPropertyChange per propertyName should be supplied.");
246 }
247 }
248 }
249 }
250
251
252
253
254
255 private boolean existsAsSystemProperty(final String key) {
256 return System.getProperties().keySet().contains(key);
257 }
258 }