forked from jbossas/patch-gen
/
PatchConfigBuilder.java
278 lines (233 loc) · 8.64 KB
/
PatchConfigBuilder.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* JBoss, Home of Professional Open Source.
* Copyright 2013, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.patching.generator;
import static org.jboss.as.patching.generator.PatchGenerator.processingError;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.jboss.as.patching.metadata.ContentItem;
import org.jboss.as.patching.metadata.ContentType;
import org.jboss.as.patching.metadata.MiscContentItem;
import org.jboss.as.patching.metadata.Patch;
import org.jboss.as.patching.metadata.PatchElementBuilder;
import org.jboss.as.patching.runner.ContentItemFilter;
/**
* {@link PatchConfig} implementation.
*
* @author Brian Stansberry (c) 2012 Red Hat Inc.
*/
class PatchConfigBuilder implements ContentItemFilter {
public static enum AffectsType {
UPDATED,
ORIGINAL,
BOTH,
NONE
}
private String patchId = UUID.randomUUID().toString();
private String description = "no patch description available";
private String appliesToName;
private String appliesToVersion;
private String resultingVersion;
private Patch.PatchType patchType;
private boolean generateByDiff = true;
private Set<String> runtimeUseItems = new HashSet<String>();
private Set<ContentItem> specifiedContent = new HashSet<ContentItem>();
private Map<String, PatchElementConfigBuilder> elements = new LinkedHashMap<String, PatchElementConfigBuilder>();
private List<OptionalPath> optionalPaths = Collections.emptyList();
PatchConfigBuilder setPatchId(String patchId) {
this.patchId = patchId;
return this;
}
PatchConfigBuilder setDescription(String description) {
this.description = description;
return this;
}
void setPatchType(Patch.PatchType patchType) {
this.patchType = patchType;
}
PatchConfigBuilder setCumulativeType(String appliesToVersion, String resultingVersion) {
this.patchType = Patch.PatchType.CUMULATIVE;
this.appliesToVersion = appliesToVersion;
this.resultingVersion = resultingVersion;
return this;
}
PatchConfigBuilder setOneOffType(String appliesToVersion) {
assert appliesToVersion != null : "appliesToVersion is null";
this.patchType = Patch.PatchType.ONE_OFF;
this.appliesToVersion = appliesToVersion;
return this;
}
PatchConfigBuilder setGenerateByDiff(boolean generateByDiff) {
this.generateByDiff = generateByDiff;
return this;
}
public boolean isGeneratedByDiff() {
return generateByDiff;
}
PatchConfigBuilder addRuntimeUseItem(String item) {
this.runtimeUseItems.add(item);
return this;
}
void setAppliesToName(String appliesToName) {
this.appliesToName = appliesToName;
}
void setAppliesToVersion(String appliesToVersion) {
this.appliesToVersion = appliesToVersion;
}
void setResultingVersion(String resultingVersion) {
this.resultingVersion = resultingVersion;
}
Set<ContentItem> getSpecifiedContent() {
return specifiedContent;
}
PatchElementConfigBuilder addElement(final String name) {
final PatchElementConfigBuilder builder = new PatchElementConfigBuilder(name, this);
if (elements.put(name, builder) != null) {
throw processingError("duplicate layer %s", name);
}
return builder;
}
PatchConfigBuilder addOptionalPath(String path) {
return addOptionalPath(path, null);
}
PatchConfigBuilder addOptionalPath(String path, String requires) {
final OptionalPath op = new OptionalPath(path, requires);
switch(optionalPaths.size()) {
case 0:
optionalPaths = Collections.singletonList(op);
break;
case 1:
optionalPaths = new ArrayList<OptionalPath>(optionalPaths);
default:
optionalPaths.add(op);
}
return this;
}
PatchConfig build() {
return new PatchConfigImpl(new ArrayList<PatchElementConfig>(elements.values()));
}
class PatchConfigImpl implements PatchConfig {
private Collection<PatchElementConfig> elements;
PatchConfigImpl(Collection<PatchElementConfig> elements) {
this.elements = elements;
}
@Override
public String getPatchId() {
return patchId;
}
@Override
public String getDescription() {
return description;
}
@Override
public Patch.PatchType getPatchType() {
return patchType;
}
@Override
public Set<String> getInRuntimeUseItems() {
return runtimeUseItems;
}
@Override
public String getAppliesToProduct() {
return appliesToName;
}
@Override
public String getAppliesToVersion() {
return appliesToVersion;
}
@Override
public String getResultingVersion() {
return resultingVersion;
}
@Override
public boolean isGenerateByDiff() {
return generateByDiff;
}
@Override
public Set<ContentItem> getSpecifiedContent() {
return specifiedContent;
}
@Override
public Collection<PatchElementConfig> getElements() {
return elements;
}
@Override
public Collection<OptionalPath> getOptionalPaths() {
return optionalPaths;
}
@Override
public PatchBuilderWrapper toPatchBuilder() {
final PatchBuilderWrapper wrapper = new PatchBuilderWrapper() {
@Override
PatchElementBuilder modifyLayer(String name, boolean addOn) {
final PatchElementConfigBuilder config = PatchConfigBuilder.this.elements.get(name);
if (config == null) {
return null;
//throw processingError("missing patch-config for layer %s", name);
}
final PatchElementBuilder builder;
if (config.getPatchType() == null) {
config.setPatchType(patchType);
}
if (patchType == Patch.PatchType.CUMULATIVE) {
builder = upgradeElement(config.getPatchId(), name, addOn);
} else {
builder = oneOffPatchElement(config.getPatchId(), name, addOn);
}
if (config.getDescription() != null) {
builder.setDescription(config.getDescription());
}
builder.setContentItemFilter(config);
return builder;
}
};
wrapper.setDescription(description);
wrapper.setPatchId(patchId);
wrapper.setContentItemFilter(PatchConfigBuilder.this);
return wrapper;
}
}
@Override
public boolean accepts(ContentItem item) {
if (generateByDiff) {
return true;
}
if (item.getContentType() == ContentType.MISC) {
for (final ContentItem s : specifiedContent) {
if (accepts((MiscContentItem) item, (MiscContentItem) s)) {
return true;
}
}
}
return false;
}
protected boolean accepts(MiscContentItem one, MiscContentItem two) {
return one.getName().equals(two.getName()) &&
one.getRelativePath().equals(two.getRelativePath());
}
}