-
Notifications
You must be signed in to change notification settings - Fork 159
/
ReadPropertiesStepExecution.java
151 lines (133 loc) · 5.55 KB
/
ReadPropertiesStepExecution.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 CloudBees Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.jenkinsci.plugins.pipeline.utility.steps.conf;
import hudson.FilePath;
import hudson.model.TaskListener;
import org.apache.commons.configuration2.AbstractConfiguration;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.ConfigurationConverter;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStepExecution;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import javax.annotation.Nonnull;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* Execution of {@link ReadPropertiesStep}.
*
* @author Robert Sandell <rsandell@cloudbees.com>.
*/
public class ReadPropertiesStepExecution extends AbstractFileOrTextStepExecution<Map<String, Object>> {
private transient ReadPropertiesStep step;
protected ReadPropertiesStepExecution(@Nonnull ReadPropertiesStep step, @Nonnull StepContext context) {
super(step, context);
this.step = step;
}
@Override
protected Map<String, Object> doRun() throws Exception {
PrintStream logger = getLogger();
Properties properties = new Properties();
if (!StringUtils.isBlank(step.getFile())) {
FilePath f = ws.child(step.getFile());
if (f.exists() && !f.isDirectory()) {
try(InputStream is = f.read()){
properties.load(is);
}
} else if (f.isDirectory()) {
logger.print("warning: ");
logger.print(f.getRemote());
logger.println(" is a directory, omitting from properties gathering");
} else if(!f.exists()) {
logger.print("warning: ");
logger.print(f.getRemote());
logger.println(" does not exist, omitting from properties gathering");
}
}
if (!StringUtils.isBlank(step.getText())) {
StringReader sr = new StringReader(step.getText());
properties.load(sr);
}
// Check if we should interpolated values in the properties
if ( step.isInterpolate() ) {
logger.println("Interpolation set to true, starting to parse the variable!");
properties = interpolateProperties(properties);
}
Map<String, Object> result = new HashMap<>();
addAll(step.getDefaults(), result);
addAll(properties, result);
return result;
}
/**
* addAll implementation that will coerce keys into Strings.
*
* @param src the source
* @param dst the destination
*/
private void addAll(Map src, Map<String, Object> dst) {
if (src == null) {
return;
}
for (Map.Entry e : (Set<Map.Entry>) src.entrySet()) {
dst.put(e.getKey() != null ? e.getKey().toString(): null, e.getValue());
}
}
/**
* Using commons collection to interpolated the values inside the properties
* @param properties the list of properties to be interpolated
* @return a new Properties object with the interpolated values
*/
private Properties interpolateProperties(Properties properties) throws Exception {
if ( properties == null)
return null;
Configuration interpolatedProp;
PrintStream logger = getLogger();
try {
// Convert the Properties to a Configuration object in order to apply the interpolation
Configuration conf = ConfigurationConverter.getConfiguration(properties);
// Apply interpolation
interpolatedProp = ((AbstractConfiguration)conf).interpolatedConfiguration();
} catch (Exception e) {
logger.println("Got exception while interpolating the variables: " + e.getMessage());
logger.println("Returning the original properties list!");
return properties;
}
// Convert back to properties
return ConfigurationConverter.getProperties(interpolatedProp);
}
/**
* Helper method to get the logger from the context.
* @return the logger from the context.
* @throws Exception
*/
private PrintStream getLogger() throws Exception {
TaskListener listener = getContext().get(TaskListener.class);
assert listener != null;
return listener.getLogger();
}
}