Search code examples
javajbosswildflyapplication-server

How to get specific object from ModelNode result


I work with ModelControllerClient to get some information about the Logger which are used at the Wildfly server. I would like to have a list of all logger names.

This is how I get all informations about the logger:

ModelNode op = new ModelNode();
op.get("operation").set("read-resource");

ModelNode address = op.get("address");
address.add("subsystem", "logging");
address.add("logger", "*");

ModelNode result = client.execute(op);

This is a litte example of the ModelNode result I get.

{
        "address" => [
            ("subsystem" => "logging"),
            ("logger" => "jacorb")
        ],
        "outcome" => "success",
        "result" => {
            "category" => "jacorb",
            "filter" => undefined,
            "filter-spec" => undefined,
            "handlers" => undefined,
            "level" => "WARN",
            "use-parent-handlers" => true
        }
    },
    {
        "address" => [
            ("subsystem" => "logging"),
            ("logger" => "jacorb.config")
        ],
        "outcome" => "success",
        "result" => {
            "category" => "jacorb.config",
            "filter" => undefined,
            "filter-spec" => undefined,
            "handlers" => undefined,
            "level" => "ERROR",
            "use-parent-handlers" => true
        }
    },

I would just like to get the values of "logger" / "category".

I tried to get the name of the loggers with:

result.get("logger");

but it returned "undefined". I guess the problem is that I have more than one object called "logger". The same happens when I try it with "category".


Solution

  • You need to first read the result. In the simple case it would be result.get("result", "logger"). You could also use the Operations API and do something like Operations.readResult(result).get("logger").

    If you want just the logger names here's an example:

    try (ModelControllerClient client = ModelControllerClient.Factory.create("localhost", 9990)) {
        final ModelNode address = Operations.createAddress("subsystem", "logging");
        final ModelNode op = Operations.createOperation(ClientConstants.READ_CHILDREN_NAMES_OPERATION, address);
        op.get(ClientConstants.CHILD_TYPE).set("logger");
        final ModelNode result = client.execute(op);
        if (Operations.isSuccessfulOutcome(result)) {
            final List<ModelNode> loggerNames = Operations.readResult(result).asList();
            for (ModelNode loggerName : loggerNames) {
                System.out.printf("logger=%s%n", loggerName.asString());
            }
        } else {
            System.err.printf("Failed to get the logger names: %s%n", Operations.getFailureDescription(result).asString());
        }
    }
    

    If you want the full description you could use the read-children-resource operation instead.

    Also the WildFly documentation has a section on using the ModelControllerClient and the ModelNode and ModelType API's.