Search in sources :

Example 1 with OperationFormatException

use of org.jboss.as.cli.operation.OperationFormatException in project wildfly by wildfly.

the class CreateJMSResourceHandler method buildRequestWithoutHeaders.

@Override
public ModelNode buildRequestWithoutHeaders(CommandContext ctx) throws OperationFormatException {
    try {
        if (!ctx.getParsedCommandLine().hasProperties()) {
            throw new OperationFormatException("Arguments are missing");
        }
    } catch (CommandFormatException e) {
        throw new OperationFormatException(e.getLocalizedMessage());
    }
    // String target = null;
    String restype = null;
    // String description = null;
    String propsStr = null;
    // boolean enabled = false;
    String jndiName = null;
    String[] args = ctx.getArgumentsString().split("\\s+");
    int i = 0;
    while (i < args.length) {
        String arg = args[i++];
        if (arg.equals("--restype")) {
            if (i < args.length) {
                restype = args[i++];
            }
        } else if (arg.equals("--target")) {
        // if(i < args.length) {
        // target = args[i++];
        // }
        } else if (arg.equals("--description")) {
        // if(i < args.length) {
        // restype = args[i++];
        // }
        } else if (arg.equals("--property")) {
            if (i < args.length) {
                propsStr = args[i++];
            }
        } else if (arg.equals("--enabled")) {
        // if (i < args.length) {
        // enabled = Boolean.parseBoolean(args[i++]);
        // }
        } else {
            jndiName = arg;
        }
    }
    if (restype == null) {
        throw new OperationFormatException("Required parameter --restype is missing.");
    }
    if (jndiName == null) {
        throw new OperationFormatException("JNDI name is missing.");
    }
    String name = null;
    // TODO read server name from props
    String serverName = "default";
    final Map<String, String> props;
    if (propsStr != null) {
        props = new HashMap<String, String>();
        String[] propsArr = propsStr.split(":");
        for (String prop : propsArr) {
            int equalsIndex = prop.indexOf('=');
            if (equalsIndex < 0 || equalsIndex == prop.length() - 1) {
                throw new OperationFormatException("Failed to parse property '" + prop + "'");
            }
            String propName = prop.substring(0, equalsIndex).trim();
            String propValue = prop.substring(equalsIndex + 1).trim();
            if (propName.isEmpty()) {
                throw new OperationFormatException("Failed to parse property '" + prop + "'");
            }
            if (propName.equals("imqDestinationName") || propName.equalsIgnoreCase("name")) {
                name = propValue;
            } else if ("ClientId".equals(propName)) {
                props.put("client-id", propValue);
            }
        }
    } else {
        props = Collections.emptyMap();
    }
    if (name == null) {
        name = jndiName.replace('/', '_');
    }
    if (restype.equals("javax.jms.Queue")) {
        DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
        builder.addNode("subsystem", "messaging-activemq");
        builder.addNode("server", serverName);
        builder.addNode("jms-queue", name);
        builder.setOperationName("add");
        builder.getModelNode().get("entries").add(jndiName);
        for (String prop : props.keySet()) {
            builder.addProperty(prop, props.get(prop));
        }
        return builder.buildRequest();
    } else if (restype.equals("javax.jms.Topic")) {
        DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
        builder.addNode("subsystem", "messaging-activemq");
        builder.addNode("server", serverName);
        builder.addNode("jms-topic", name);
        builder.setOperationName("add");
        builder.getModelNode().get("entries").add(jndiName);
        for (String prop : props.keySet()) {
            builder.addProperty(prop, props.get(prop));
        }
        return builder.buildRequest();
    } else if (restype.equals("javax.jms.ConnectionFactory") || restype.equals("javax.jms.TopicConnectionFactory") || restype.equals("javax.jms.QueueConnectionFactory")) {
        DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
        builder.addNode("subsystem", "messaging-activemq");
        builder.addNode("server", serverName);
        builder.addNode("connection-factory", name);
        builder.setOperationName("add");
        builder.getModelNode().get("entries").add(jndiName);
        for (String prop : props.keySet()) {
            builder.addProperty(prop, props.get(prop));
        }
        return builder.buildRequest();
    } else {
        throw new OperationFormatException("Resource type " + restype + " isn't supported.");
    }
}
Also used : OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) CommandFormatException(org.jboss.as.cli.CommandFormatException) DefaultOperationRequestBuilder(org.jboss.as.cli.operation.impl.DefaultOperationRequestBuilder)

Example 2 with OperationFormatException

use of org.jboss.as.cli.operation.OperationFormatException in project wildfly by wildfly.

the class SecurityCommandsTestCase method getHTTPSListeners.

private List<String> getHTTPSListeners() {
    final DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
    final ModelNode request;
    try {
        builder.setOperationName(Util.READ_CHILDREN_NAMES);
        builder.addNode(Util.SUBSYSTEM, Util.UNDERTOW);
        builder.addNode(Util.SERVER, DEFAULT_SERVER);
        builder.addProperty(Util.CHILD_TYPE, Util.HTTPS_LISTENER);
        request = builder.buildRequest();
    } catch (OperationFormatException e) {
        throw new IllegalStateException("Failed to build operation", e);
    }
    try {
        final ModelNode outcome = ctx.getModelControllerClient().execute(request);
        if (Util.isSuccess(outcome)) {
            return Util.getList(outcome);
        }
    } catch (Exception e) {
    }
    return Collections.emptyList();
}
Also used : OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) DefaultOperationRequestBuilder(org.jboss.as.cli.operation.impl.DefaultOperationRequestBuilder) ModelNode(org.jboss.dmr.ModelNode) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 3 with OperationFormatException

use of org.jboss.as.cli.operation.OperationFormatException in project wildfly by wildfly.

the class SecurityCommandsTestCase method getSSLContextName.

private static String getSSLContextName(CommandContext ctx, String serverName, String httpsListener) throws IOException, OperationFormatException {
    final DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
    final ModelNode request;
    try {
        builder.setOperationName(Util.READ_ATTRIBUTE);
        builder.addNode(Util.SUBSYSTEM, Util.UNDERTOW);
        builder.addNode(Util.SERVER, serverName == null ? DEFAULT_SERVER : serverName);
        builder.addNode(Util.HTTPS_LISTENER, httpsListener);
        builder.addProperty(Util.NAME, Util.SSL_CONTEXT);
        request = builder.buildRequest();
    } catch (OperationFormatException e) {
        throw new IllegalStateException("Failed to build operation", e);
    }
    try {
        final ModelNode outcome = ctx.getModelControllerClient().execute(request);
        if (Util.isSuccess(outcome)) {
            ModelNode mn = outcome.get(Util.RESULT);
            if (mn.isDefined()) {
                return outcome.get(Util.RESULT).asString();
            } else {
                return null;
            }
        }
    } catch (Exception e) {
    }
    return null;
}
Also used : OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) DefaultOperationRequestBuilder(org.jboss.as.cli.operation.impl.DefaultOperationRequestBuilder) ModelNode(org.jboss.dmr.ModelNode) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 4 with OperationFormatException

use of org.jboss.as.cli.operation.OperationFormatException in project wildfly by wildfly.

the class SecurityCommandsTestCase method getNames.

private static List<String> getNames(ModelControllerClient client, String type) {
    final DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
    final ModelNode request;
    try {
        builder.setOperationName(Util.READ_CHILDREN_NAMES);
        builder.addNode(Util.SUBSYSTEM, Util.ELYTRON);
        builder.addProperty(Util.CHILD_TYPE, type);
        request = builder.buildRequest();
    } catch (OperationFormatException e) {
        throw new IllegalStateException("Failed to build operation", e);
    }
    try {
        final ModelNode outcome = client.execute(request);
        if (Util.isSuccess(outcome)) {
            return Util.getList(outcome);
        }
    } catch (Exception e) {
    }
    return Collections.emptyList();
}
Also used : OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) DefaultOperationRequestBuilder(org.jboss.as.cli.operation.impl.DefaultOperationRequestBuilder) ModelNode(org.jboss.dmr.ModelNode) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 5 with OperationFormatException

use of org.jboss.as.cli.operation.OperationFormatException in project wildfly by wildfly.

the class SecurityAuthCommandsTestCase method readAttribute.

private static String readAttribute(CommandContext ctx, String domain, String name) throws Exception {
    final DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
    final ModelNode request;
    try {
        builder.setOperationName(Util.READ_ATTRIBUTE);
        builder.addNode(Util.SUBSYSTEM, Util.UNDERTOW);
        builder.addNode(Util.APPLICATION_SECURITY_DOMAIN, domain);
        builder.addProperty(Util.NAME, name);
        request = builder.buildRequest();
    } catch (OperationFormatException e) {
        throw new IllegalStateException("Failed to build operation", e);
    }
    final ModelNode outcome = ctx.getModelControllerClient().execute(request);
    if (Util.isSuccess(outcome)) {
        boolean hasResult = outcome.has(Util.RESULT);
        if (hasResult) {
            if (outcome.get(Util.RESULT).isDefined()) {
                return outcome.get(Util.RESULT).asString();
            } else {
                return null;
            }
        }
    }
    throw new Exception("Error retrieving Auth factory " + outcome);
}
Also used : OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) DefaultOperationRequestBuilder(org.jboss.as.cli.operation.impl.DefaultOperationRequestBuilder) ModelNode(org.jboss.dmr.ModelNode) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) IOException(java.io.IOException)

Aggregations

OperationFormatException (org.jboss.as.cli.operation.OperationFormatException)8 DefaultOperationRequestBuilder (org.jboss.as.cli.operation.impl.DefaultOperationRequestBuilder)8 IOException (java.io.IOException)4 CommandFormatException (org.jboss.as.cli.CommandFormatException)4 ModelNode (org.jboss.dmr.ModelNode)4 CommandLineException (org.jboss.as.cli.CommandLineException)3 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)2