/
ConfigurationImpl.java
196 lines (177 loc) · 8.63 KB
/
ConfigurationImpl.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.paypal.butterfly.core;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import com.paypal.butterfly.api.Configuration;
/**
* Butterfly configuration object
*
* @author facarvalho
*/
class ConfigurationImpl implements Configuration {
// See the setters for information about each property
private Properties properties = null;
private File outputFolder = null;
private boolean zipOutput = false;
private boolean modifyOriginalFolder = true;
private static final Pattern propertyNameRegex = Pattern.compile("^[a-zA-Z\\._-]*$");
/**
* Creates and returns a new {@link Configuration} object
* set to apply the transformation against the original application folder
* and the result will not be compressed to a zip file.
* <br>
* Notice that calling this method will result in {@link Configuration#isModifyOriginalFolder()}
* to return {@code true}.
*
* @param properties a properties object specifying details about the transformation itself.
* These properties help to specialize the
* transformation, for example, determining if certain operations should
* be skipped or not, or how certain aspects of the transformation should
* be executed. The set of possible properties is defined by the used transformation
* extension and template, read the documentation offered by the extension
* for further details. The properties values are defined by the user requesting the transformation.
* Properties are optional, so, if not desired, this parameter can be set to null.
* @return a brand new {@link Configuration} object
* @throws IllegalArgumentException if properties object is invalid. Properties name must
* be non blank and only contain alphabetical characters, dots, underscore or hyphen. Properties
* object must be Strings and cannot be null.
*/
ConfigurationImpl(Properties properties) {
if (properties != null && properties.size() > 0) {
// Validating properties object
List<String> invalidProperties = properties.entrySet().stream()
.filter(e -> !propertyNameRegex.matcher((String) e.getKey()).matches() || !(e.getValue() instanceof String))
.map(e -> (String) e.getKey())
.collect(Collectors.toList());
if (!invalidProperties.isEmpty()) {
throw new IllegalArgumentException("The following properties are invalid: " + invalidProperties);
}
this.properties = properties;
}
}
/**
* Creates and returns a new {@link Configuration} object
* set to place the transformed application at a new folder at the original application
* parent folder, besides compressing it to a zip file, depending on {@code zipOutput}.
* <br>
* The transformed application folder's name is the same as original folder,
* plus a "-transformed-yyyyMMddHHmmssSSS" suffix.
* <br>
* Notice that calling this method will result in {@link Configuration#isModifyOriginalFolder()}
* to return {@code false}.
*
* @param properties a properties object specifying details about the transformation itself.
* These properties help to specialize the
* transformation, for example, determining if certain operations should
* be skipped or not, or how certain aspects of the transformation should
* be executed. The set of possible properties is defined by the used transformation
* extension and template, read the documentation offered by the extension
* for further details. The properties values are defined by the user requesting the transformation.
* Properties are optional, so, if not desired, this parameter can be set to null.
* @param zipOutput if true, the transformed application folder will be compressed into a zip file
* @return a brand new {@link Configuration} object
*/
ConfigurationImpl(Properties properties, boolean zipOutput) {
this(properties);
this.zipOutput = zipOutput;
modifyOriginalFolder = false;
}
/**
* Creates and returns a new {@link Configuration} object
* set to place the transformed application at {@code outputFolder},
* and compress it to a zip file or not, depending on {@code zipOutput}.
* <br>
* Notice that calling this method will result in {@link Configuration#isModifyOriginalFolder()}
* to return {@code false}.
*
* @param properties a properties object specifying details about the transformation itself.
* These properties help to specialize the
* transformation, for example, determining if certain operations should
* be skipped or not, or how certain aspects of the transformation should
* be executed. The set of possible properties is defined by the used transformation
* extension and template, read the documentation offered by the extension
* for further details. The properties values are defined by the user requesting the transformation.
* Properties are optional, so, if not desired, this parameter can be set to null.
* @param outputFolder the output folder where the transformed application is
* supposed to be placed
* @param zipOutput if true, the transformed application folder will be compressed into a zip file
* @return a brand new {@link Configuration} object
* @throws IllegalArgumentException if {@code outputFolder} is null, does not exist, or is not a directory
*/
ConfigurationImpl(Properties properties, File outputFolder, boolean zipOutput) {
this(properties);
if(outputFolder == null) {
throw new IllegalArgumentException(String.format("Output folder object cannot be null"));
}
if(!outputFolder.exists()) {
throw new IllegalArgumentException(String.format("Output folder does not exist %s", outputFolder.getAbsolutePath()));
}
if(!outputFolder.isDirectory()) {
throw new IllegalArgumentException(String.format("Output folder is not a directory %s", outputFolder.getAbsolutePath()));
}
this.outputFolder = outputFolder;
this.zipOutput = zipOutput;
modifyOriginalFolder = false;
}
@Override
public Properties getProperties() {
return properties;
}
@Override
public File getOutputFolder() {
return outputFolder;
}
@Override
public boolean isZipOutput() {
return zipOutput;
}
@Override
public boolean isModifyOriginalFolder() {
return modifyOriginalFolder;
}
@Override
public String toString() {
return String.format("{ properties: %s, outputFolder: %s, zipOutput: %s, modifyOriginalFolder: %s}", properties, outputFolder, zipOutput, modifyOriginalFolder);
}
@Override
@SuppressWarnings("PMD.SimplifyBooleanReturns")
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof ConfigurationImpl)) {
return false;
}
if (!ConfigurationImpl.class.isAssignableFrom(obj.getClass())) {
return false;
}
final Configuration configuration = (Configuration)obj;
if(this.zipOutput != configuration.isZipOutput()) {
return false;
}
if(this.modifyOriginalFolder != configuration.isModifyOriginalFolder()) {
return false;
}
if (this.outputFolder == null && configuration.getOutputFolder() != null) {
return false;
}
if (this.outputFolder != null && configuration.getOutputFolder() == null) {
return false;
}
if(this.outputFolder != null && configuration.getOutputFolder() != null && !this.outputFolder.equals(configuration.getOutputFolder())) {
return false;
}
return true;
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(this.outputFolder).append(this.zipOutput).append(this.modifyOriginalFolder).toHashCode();
}
}