Skip to content

Commit 1b4ccec

Browse files
committed
Providing a json in line with or without the all option
Signed-off-by: Emmanuel Hugonnet <ehugonne@redhat.com>
1 parent 4cf10b1 commit 1b4ccec

File tree

2 files changed

+111
-44
lines changed

2 files changed

+111
-44
lines changed

feature-spec-gen/src/main/java/org/wildfly/galleon/plugin/featurespec/generator/FeatureSpecGenerator.java

Lines changed: 109 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.jboss.as.controller.client.helpers.ClientConstants;
3636
import org.jboss.as.controller.client.helpers.Operations;
3737
import org.jboss.dmr.ModelNode;
38+
import org.jboss.dmr.ModelType;
3839
import org.jboss.dmr.Property;
3940
import org.jboss.galleon.Errors;
4041
import org.jboss.galleon.ProvisioningException;
@@ -77,9 +78,9 @@ public class FeatureSpecGenerator implements ForkCallback {
7778
String getBranchId(String spec, int dots) {
7879
int i = 0;
7980
int index = 0;
80-
while(i <= dots) {
81+
while (i <= dots) {
8182
index = spec.indexOf('.', index + 1);
82-
if(index < 0) {
83+
if (index < 0) {
8384
return spec;
8485
}
8586
++i;
@@ -97,8 +98,8 @@ FeatureSpecNode getSpec(String name) {
9798

9899
void addReferencedSpec(String referencedSpecName, String referencingSpec, FeatureSpecNode referencingNode) {
99100
Map<String, FeatureSpecNode> specs = referencedSpecs.get(referencedSpecName);
100-
if(specs != null) {
101-
if(specs.size() == 1) {
101+
if (specs != null) {
102+
if (specs.size() == 1) {
102103
specs = new HashMap<>(specs);
103104
referencedSpecs.put(referencedSpecName, specs);
104105
}
@@ -140,7 +141,7 @@ public FeatureSpecGenerator() {
140141
}
141142

142143
public FeatureSpecGenerator(String installation, Path outputDir, Map<String, Path> inheritedSpecs,
143-
String mimimumStability, boolean fork, boolean debug) {
144+
String mimimumStability, boolean fork, boolean debug) {
144145
this.installation = installation;
145146
this.outputDir = outputDir;
146147
this.fork = fork;
@@ -155,24 +156,24 @@ public int generateSpecs() throws ProvisioningException {
155156
doGenerate();
156157
} finally {
157158
final List<String> toClear = new ArrayList<>();
158-
for(Map.Entry<Object, Object> prop : System.getProperties().entrySet()) {
159+
for (Map.Entry<Object, Object> prop : System.getProperties().entrySet()) {
159160
final Object value = originalProps.get(prop.getKey());
160161
if (value != null) {
161162
System.setProperty(prop.getKey().toString(), value.toString());
162163
} else {
163164
toClear.add(prop.getKey().toString());
164165
}
165166
}
166-
for(String prop : toClear) {
167+
for (String prop : toClear) {
167168
System.clearProperty(prop);
168169
}
169-
if(systemProps != null) {
170+
if (systemProps != null) {
170171
IoUtils.recursiveDelete(systemProps);
171172
}
172-
if(standaloneSpecsFile != null) {
173+
if (standaloneSpecsFile != null) {
173174
IoUtils.recursiveDelete(standaloneSpecsFile);
174175
}
175-
if(domainSpecsFile != null) {
176+
if (domainSpecsFile != null) {
176177
IoUtils.recursiveDelete(domainSpecsFile);
177178
}
178179
}
@@ -182,7 +183,7 @@ public int generateSpecs() throws ProvisioningException {
182183
private void doGenerate() throws ProvisioningException {
183184
final ModelNode standaloneFeatures;
184185
ModelNode domainRoots = null;
185-
if(fork) {
186+
if (fork) {
186187
String minStab = mimimumStability == null ? "" : mimimumStability;
187188
ForkedEmbeddedUtil.fork(this, getStoredSystemProps(), installation, getStandaloneSpecsFile().toString(), getDomainSpecsFile().toString(), minStab);
188189
standaloneFeatures = readSpecsFile(getStandaloneSpecsFile());
@@ -193,12 +194,12 @@ private void doGenerate() throws ProvisioningException {
193194
} catch (IOException ex) {
194195
throw new ProvisioningException(ex);
195196
}
196-
if(Files.exists(Paths.get(installation).resolve(WfConstants.DOMAIN).resolve(WfConstants.CONFIGURATION))) {
197+
if (Files.exists(Paths.get(installation).resolve(WfConstants.DOMAIN).resolve(WfConstants.CONFIGURATION))) {
197198
domainRoots = readSpecsFile(getDomainSpecsFile());
198199
}
199200
} else {
200201
final Path home = Paths.get(installation);
201-
if(Files.exists(home.resolve(WfConstants.STANDALONE).resolve(WfConstants.CONFIGURATION))) {
202+
if (Files.exists(home.resolve(WfConstants.STANDALONE).resolve(WfConstants.CONFIGURATION))) {
202203
standaloneFeatures = readFeatureSpecs(createStandaloneServer(installation, mimimumStability, null));
203204
ModelNode result = generateModel(createStandaloneServer(installation, mimimumStability, "standalone-local.xml"));
204205
try {
@@ -212,7 +213,7 @@ private void doGenerate() throws ProvisioningException {
212213
} else {
213214
throw new ProvisioningException("The installation does not include standalone configuration");
214215
}
215-
if(Files.exists(home.resolve(WfConstants.DOMAIN).resolve(WfConstants.CONFIGURATION))) {
216+
if (Files.exists(home.resolve(WfConstants.DOMAIN).resolve(WfConstants.CONFIGURATION))) {
216217
domainRoots = readFeatureSpecs(createEmbeddedHc(installation, mimimumStability));
217218
}
218219
}
@@ -235,7 +236,7 @@ private void doGenerate() throws ProvisioningException {
235236
}
236237

237238
rootNode.processChildren(FeatureSpecNode.STANDALONE_MODEL);
238-
if(domainRoots != null) {
239+
if (domainRoots != null) {
239240
rootNode.processChildren(FeatureSpecNode.PROFILE_MODEL);
240241
rootNode.processChildren(FeatureSpecNode.DOMAIN_MODEL);
241242
rootNode.processChildren(FeatureSpecNode.HOST_MODEL);
@@ -246,7 +247,7 @@ private void doGenerate() throws ProvisioningException {
246247

247248
@Override
248249
public void forkedForEmbedded(String... args) throws ConfigGeneratorException {
249-
if(args.length != 3 && args.length != 4) {
250+
if (args.length != 3 && args.length != 4) {
250251
final StringBuilder buf = new StringBuilder();
251252
StringUtils.append(buf, Arrays.asList(args));
252253
throw new IllegalArgumentException("Expected 3-4 arguments but got " + Arrays.asList(args));
@@ -269,20 +270,20 @@ public void forkedForEmbedded(String... args) throws ConfigGeneratorException {
269270

270271
@Override
271272
public void forkedEmbeddedMessage(String msg) {
272-
if(debug) {
273+
if (debug) {
273274
System.out.println(msg);
274275
}
275276
}
276277

277278
protected Path getStoredSystemProps() throws ProvisioningException {
278-
if(systemProps == null) {
279+
if (systemProps == null) {
279280
systemProps = ForkedEmbeddedUtil.storeSystemProps();
280281
}
281282
return systemProps;
282283
}
283284

284285
protected Path getStandaloneSpecsFile() throws ProvisioningException {
285-
if(standaloneSpecsFile == null) {
286+
if (standaloneSpecsFile == null) {
286287
try {
287288
standaloneSpecsFile = Files.createTempFile("wfgp", "standalone-specs");
288289
} catch (IOException e) {
@@ -293,7 +294,7 @@ protected Path getStandaloneSpecsFile() throws ProvisioningException {
293294
}
294295

295296
protected Path getDomainSpecsFile() throws ProvisioningException {
296-
if(domainSpecsFile == null) {
297+
if (domainSpecsFile == null) {
297298
try {
298299
domainSpecsFile = Files.createTempFile("wfgp", "domain-specs");
299300
} catch (IOException e) {
@@ -305,14 +306,14 @@ protected Path getDomainSpecsFile() throws ProvisioningException {
305306

306307
protected FeatureSpec getInheritedSpec(String name) throws ProvisioningException {
307308
FeatureSpec spec = parsedInheritedSpecs.get(name);
308-
if(spec != null) {
309+
if (spec != null) {
309310
return spec;
310311
}
311312
final Path path = inheritedSpecs.get(name);
312-
if(path == null) {
313+
if (path == null) {
313314
return null;
314315
}
315-
try(BufferedReader reader = Files.newBufferedReader(path)) {
316+
try (BufferedReader reader = Files.newBufferedReader(path)) {
316317
spec = FeatureSpecXmlParser.getInstance().parse(reader);
317318
} catch (IOException | XMLStreamException e) {
318319
throw new ProvisioningException("Failed to parse " + name + " spec " + path, e);
@@ -338,7 +339,7 @@ private static String[] getCmdArgs(String mimimumStability, String serverConfig)
338339
args.add("--stability=" + mimimumStability);
339340
}
340341
if (serverConfig != null) {
341-
args.add("--server-config=" + serverConfig);
342+
args.add("--server-config=" + serverConfig);
342343
}
343344
return args.toArray(String[]::new);
344345
}
@@ -347,29 +348,95 @@ private static ModelNode generateModel(final EmbeddedManagedProcess server) thro
347348
try {
348349
server.start();
349350
Boolean all = Boolean.getBoolean("org.wildfly.galleon.complete.model");
350-
ModelNode rootAddress = Operations.createAddress().setEmptyList();
351-
if(!all) {
352-
rootAddress = Operations.createAddress("subsystem", "*");
353-
}
354-
final ModelNode op = Operations.createOperation("read-resource-description", rootAddress);
355-
op.get(ClientConstants.RECURSIVE).set(true);
356-
final ModelNode result;
357-
try {
358-
result = server.getModelControllerClient().execute(op);
359-
} catch (IOException e) {
360-
throw new ProvisioningException("Failed to read feature descriptions", e);
351+
if (!all) {
352+
return readModel(server);
361353
}
362-
if(!Operations.isSuccessfulOutcome(result)) {
363-
throw new ProvisioningException(Operations.getFailureDescription(result).asString());
364-
}
365-
return result.get("result");
354+
return readAll(server);
366355
} catch (EmbeddedProcessStartException ex) {
367356
throw new ProvisioningException("Failed to read feature spec descriptions", ex);
368357
} finally {
369358
server.stop();
370359
}
371360
}
372361

362+
private static ModelNode readModel(final EmbeddedManagedProcess server) throws ProvisioningException {
363+
List<String> subsystems = listSubsystems(server);
364+
ModelNode result = new ModelNode();
365+
ModelNode subsystemNodes = result.get("children").get(ClientConstants.SUBSYSTEM).get("model-description");
366+
for (String subsystem : subsystems) {
367+
ModelNode address = Operations.createAddress(ClientConstants.SUBSYSTEM, subsystem);
368+
ModelNode subsystemDescription = readResourceDescription(server, address);
369+
subsystemDescription.get(ClientConstants.ADDRESS).add(address);
370+
subsystemNodes.get(subsystem).set(subsystemDescription);
371+
}
372+
ModelNode address = Operations.createAddress(ClientConstants.DEPLOYMENT);
373+
result.get("children").get(ClientConstants.DEPLOYMENT).get("model-description").get("*").set(readResourceDescription(server, address));
374+
result.remove(ClientConstants.RESULT);
375+
return result;
376+
}
377+
378+
private static ModelNode readAll(final EmbeddedManagedProcess server) throws ProvisioningException {
379+
ModelNode rootAddress = Operations.createAddress().setEmptyList();
380+
final ModelNode op = Operations.createOperation("read-resource-description", rootAddress);
381+
op.get(ClientConstants.RECURSIVE).set(true);
382+
final ModelNode result;
383+
try {
384+
result = server.getModelControllerClient().execute(op);
385+
} catch (IOException e) {
386+
throw new ProvisioningException("Failed to read feature descriptions", e);
387+
}
388+
if (!Operations.isSuccessfulOutcome(result)) {
389+
throw new ProvisioningException(Operations.getFailureDescription(result).asString());
390+
}
391+
return result.get(ClientConstants.RESULT);
392+
}
393+
394+
private static List<String> listSubsystems(final EmbeddedManagedProcess server) throws ProvisioningException {
395+
ModelNode rootAddress = Operations.createAddress().setEmptyList();
396+
final ModelNode op = Operations.createOperation(ClientConstants.READ_CHILDREN_NAMES_OPERATION, rootAddress);
397+
op.get(ClientConstants.CHILD_TYPE).set(ClientConstants.SUBSYSTEM);
398+
op.get("include-singletons").set(true);
399+
final ModelNode result;
400+
try {
401+
result = server.getModelControllerClient().execute(op);
402+
} catch (IOException e) {
403+
throw new ProvisioningException("Failed to read feature descriptions", e);
404+
}
405+
if (!Operations.isSuccessfulOutcome(result)) {
406+
throw new ProvisioningException(Operations.getFailureDescription(result).asString());
407+
}
408+
List<String> names = new ArrayList<>();
409+
for (ModelNode name : result.get(ClientConstants.RESULT).asList()) {
410+
names.add(name.asString());
411+
}
412+
return names;
413+
}
414+
415+
private static ModelNode readResourceDescription(final EmbeddedManagedProcess server, ModelNode address) throws ProvisioningException {
416+
final ModelNode op = Operations.createOperation("read-resource-description", address);
417+
op.get(ClientConstants.RECURSIVE).set(true);
418+
final ModelNode opResult;
419+
try {
420+
opResult = server.getModelControllerClient().execute(op);
421+
} catch (IOException e) {
422+
throw new ProvisioningException("Failed to read feature descriptions", e);
423+
}
424+
if (!Operations.isSuccessfulOutcome(opResult)) {
425+
throw new ProvisioningException(Operations.getFailureDescription(opResult).asString());
426+
}
427+
final ModelNode result = opResult.get(ClientConstants.RESULT);
428+
if (ModelType.LIST == result.getType()) {
429+
ModelNode finalResult = new ModelNode().setEmptyList();
430+
for (ModelNode node : result.asList()) {
431+
if (node.hasDefined(ClientConstants.RESULT)) {
432+
finalResult.add(node.get(ClientConstants.RESULT));
433+
}
434+
}
435+
return finalResult;
436+
}
437+
return result;
438+
}
439+
373440
private static ModelNode readFeatureSpecs(final EmbeddedManagedProcess server) throws ProvisioningException {
374441
try {
375442
server.start();
@@ -381,7 +448,7 @@ private static ModelNode readFeatureSpecs(final EmbeddedManagedProcess server) t
381448
} catch (IOException e) {
382449
throw new ProvisioningException("Failed to read feature descriptions", e);
383450
}
384-
if(!Operations.isSuccessfulOutcome(result)) {
451+
if (!Operations.isSuccessfulOutcome(result)) {
385452
throw new ProvisioningException(Operations.getFailureDescription(result).asString());
386453
}
387454
return result.require(ClientConstants.RESULT).require("feature");
@@ -401,15 +468,15 @@ private ModelNode readSpecsFile(Path specsFile) throws ProvisioningException {
401468
}
402469

403470
private void writeSpecsFile(Path specsFile, ModelNode specs) throws ProvisioningException {
404-
try(BufferedWriter writer = Files.newBufferedWriter(specsFile)) {
471+
try (BufferedWriter writer = Files.newBufferedWriter(specsFile)) {
405472
writer.write(specs.asString());
406473
} catch (IOException e) {
407474
throw new ProvisioningException(Errors.writeFile(specsFile), e);
408475
}
409476
}
410477

411478
private void writeModelFile(Path specsFile, ModelNode specs) throws ProvisioningException {
412-
try(BufferedWriter writer = Files.newBufferedWriter(specsFile)) {
479+
try (BufferedWriter writer = Files.newBufferedWriter(specsFile)) {
413480
writer.write(specs.toJSONString(false));
414481
} catch (IOException e) {
415482
throw new ProvisioningException(Errors.writeFile(specsFile), e);

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<parent>
2424
<groupId>org.jboss</groupId>
2525
<artifactId>jboss-parent</artifactId>
26-
<version>36</version>
26+
<version>49</version>
2727
</parent>
2828

2929
<groupId>org.wildfly.galleon-plugins</groupId>
@@ -76,7 +76,7 @@
7676
<version.org.codehaus.mojo.xml-maven-plugin>1.0.1</version.org.codehaus.mojo.xml-maven-plugin>
7777
<version.org.codehaus.plexus.plexus-utils>3.1.0</version.org.codehaus.plexus.plexus-utils>
7878
<version.org.eclipse.aether>1.1.0</version.org.eclipse.aether>
79-
<version.org.jboss.galleon>6.0.6.Final-SNAPSHOT</version.org.jboss.galleon>
79+
<version.org.jboss.galleon>6.0.7.Final-SNAPSHOT</version.org.jboss.galleon>
8080
<version.org.jboss.dmr>1.5.0.Final</version.org.jboss.dmr>
8181

8282
<version.org.wildfly.channel>1.2.1.Final</version.org.wildfly.channel>

0 commit comments

Comments
 (0)