/* (non-Javadoc) * @see org.jboss.as.cli.handlers.CommandHandlerWithHelp#doHandle(org.jboss.as.cli.CommandContext) */ @Override protected void doHandle(CommandContext ctx) throws CommandLineException { final ModelNode request = buildRequest(ctx); final ModelControllerClient client = ctx.getModelControllerClient(); final OperationResponse operationResponse; try { operationResponse = client.executeOperation( Operation.Factory.create(request), OperationMessageHandler.DISCARD); } catch (Exception e) { throw new CommandLineException("Failed to perform operation", e); } try { final ModelNode response = operationResponse.getResponseNode(); if (!Util.isSuccess(response)) { throw new CommandLineException(Util.getFailureDescription(response)); } handleResponse( ctx, operationResponse, Util.COMPOSITE.equals(request.get(Util.OPERATION).asString())); operationResponse.close(); } catch (IOException ex) { throw new CommandLineException("Failed to perform operation", ex); } }
/* (non-Javadoc) * @see org.jboss.as.cli.handlers.CommandHandlerWithHelp#doHandle(org.jboss.as.cli.CommandContext) */ @Override protected void doHandle(CommandContext ctx) { ModelNode request; try { request = buildRequest(ctx); } catch (OperationFormatException e) { ctx.printLine(e.getLocalizedMessage()); return; } ModelControllerClient client = ctx.getModelControllerClient(); final ModelNode result; try { result = client.execute(request); } catch (Exception e) { ctx.printLine("Failed to perform operation: " + e.getLocalizedMessage()); return; } if (!Util.isSuccess(result)) { ctx.printLine(Util.getFailureDescription(result)); return; } ctx.printLine("Sucessfully created queue."); }
/* (non-Javadoc) * @see org.jboss.as.cli.CandidatesProvider#getNodeTypes(org.jboss.as.cli.Prefix) */ @Override public List<String> getNodeTypes(OperationRequestAddress prefix) { ModelControllerClient client = ctx.getModelControllerClient(); if (client == null) { return Collections.emptyList(); } if (prefix.endsOnType()) { throw new IllegalArgumentException("The prefix isn't expected to end on a type."); } ModelNode request; DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder(prefix); try { builder.operationName("read-children-types"); request = builder.buildRequest(); } catch (OperationFormatException e1) { throw new IllegalStateException("Failed to build operation", e1); } List<String> result; try { ModelNode outcome = client.execute(request); if (!Util.isSuccess(outcome)) { // TODO logging... exception? result = Collections.emptyList(); } else { result = Util.getList(outcome); } } catch (Exception e) { result = Collections.emptyList(); } return result; }
@Override protected void handleResponse(CommandContext ctx, ModelNode opResponse, boolean composite) { if (!Util.isSuccess(opResponse)) { ctx.printLine(Util.getFailureDescription(opResponse)); return; } final StringBuilder buf = formatResponse(ctx, opResponse, composite, null); if (buf != null) { ctx.printLine(buf.toString()); } }
/* (non-Javadoc) * @see org.jboss.as.cli.handlers.CommandHandlerWithHelp#doHandle(org.jboss.as.cli.CommandContext) */ @Override protected void doHandle(CommandContext ctx) { if (!ctx.hasArguments()) { ctx.printLine("Missing required argument 'name'."); return; } String name = ctx.getNamedArgument("name"); if (name == null) { List<String> args = ctx.getArguments(); if (!args.isEmpty()) { name = args.get(0); } } if (name == null) { ctx.printLine("Missing required argument 'name'."); return; } ModelControllerClient client = ctx.getModelControllerClient(); DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder(); builder.addNode("subsystem", "jms"); builder.addNode("connection-factory", name); builder.setOperationName("remove"); final ModelNode result; try { ModelNode request = builder.buildRequest(); result = client.execute(request); } catch (Exception e) { ctx.printLine("Failed to perform operation: " + e.getLocalizedMessage()); return; } if (!Util.isSuccess(result)) { ctx.printLine( "Failed to delete connection factory '" + name + "': " + Util.getFailureDescription(result)); return; } ctx.printLine("Removed connection factory " + name); }
// For any request params that are of type BYTES, replace the file path with the bytes from the // file private boolean replaceFilePathsWithBytes(ModelNode request) throws CommandFormatException, IOException { boolean didReplacement = false; ModelNode opDesc = new ModelNode(); opDesc.get("address").set(request.get("address")); opDesc.get("operation").set("read-operation-description"); final String opName = request.get("operation").asString(); opDesc.get("name").set(opName); ModelNode response = execute(opDesc, false).getResponseNode(); if (response.hasDefined("result", "request-properties")) { final ModelNode requestProps = response.get("result", "request-properties"); for (Property prop : requestProps.asPropertyList()) { ModelNode typeDesc = prop.getValue().get("type"); if (typeDesc.getType() == ModelType.TYPE && typeDesc.asType() == ModelType.BYTES && request.hasDefined(prop.getName())) { String filePath = request.get(prop.getName()).asString(); File localFile = new File(filePath); if (!localFile.exists()) continue; try { request.get(prop.getName()).set(Util.readBytes(localFile)); didReplacement = true; } catch (OperationFormatException e) { throw new CommandFormatException(e); } } } } return didReplacement; }
@Override protected void doHandle(CommandContext ctx) { ModelControllerClient client = ctx.getModelControllerClient(); ParsedArguments args = ctx.getParsedArguments(); boolean l = this.l.isPresent(args); if (!args.hasArguments() || l) { printList(ctx, Util.getDeployments(client), l); return; } final String name = this.name.getValue(ctx.getParsedArguments()); if (name == null) { printList(ctx, Util.getDeployments(client), l); return; } DefaultOperationRequestBuilder builder; // undeploy builder = new DefaultOperationRequestBuilder(); builder.setOperationName("undeploy"); builder.addNode("deployment", name); ModelNode result; try { ModelNode request = builder.buildRequest(); result = client.execute(request); } catch (Exception e) { ctx.printLine("Failed to undeploy: " + e.getLocalizedMessage()); return; } // TODO undeploy may fail if the content failed to deploy but remove should still be executed if (!Util.isSuccess(result)) { ctx.printLine("Undeploy failed: " + Util.getFailureDescription(result)); return; } // remove builder = new DefaultOperationRequestBuilder(); builder.setOperationName("remove"); builder.addNode("deployment", name); try { ModelNode request = builder.buildRequest(); result = client.execute(request); } catch (Exception e) { ctx.printLine( "Failed to remove the deployment content from the repository: " + e.getLocalizedMessage()); return; } if (!Util.isSuccess(result)) { ctx.printLine("Remove failed: " + Util.getFailureDescription(result)); return; } ctx.printLine("Successfully undeployed " + name + "."); }
@Override public boolean isAvailable(CommandContext ctx) { if (!super.isAvailable(ctx)) { return false; } if (requiredAddress == null) { return ctx.getConfig().isAccessControl() ? accessRequirement.isSatisfied(ctx) : true; } if (dependsOnProfile && ctx.isDomainMode()) { // not checking address in all the profiles return ctx.getConfig().isAccessControl() ? accessRequirement.isSatisfied(ctx) : true; } if (available != null) { return available.booleanValue(); } final ModelControllerClient client = ctx.getModelControllerClient(); if (client == null) { return false; } // caching the results of an address validation may cause a problem: // the address may become valid/invalid during the session // the change won't have an effect until the cache is cleared // which happens on reconnect/disconnect if (requiredType == null) { available = isAddressValid(ctx); } else { final ModelNode request = new ModelNode(); final ModelNode address = request.get(Util.ADDRESS); for (OperationRequestAddress.Node node : requiredAddress) { address.add(node.getType(), node.getName()); } request.get(Util.OPERATION).set(Util.READ_CHILDREN_TYPES); ModelNode result; try { result = ctx.getModelControllerClient().execute(request); } catch (IOException e) { return false; } available = Util.listContains(result, requiredType); } if (ctx.getConfig().isAccessControl()) { available = available && accessRequirement.isSatisfied(ctx); } return available; }
@Override public List<String> getPropertyNames(String operationName, OperationRequestAddress address) { ModelControllerClient client = ctx.getModelControllerClient(); if (client == null) { return Collections.emptyList(); } if (address.endsOnType()) { throw new IllegalArgumentException("The prefix isn't expected to end on a type."); } ModelNode request; DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder(address); try { builder.operationName("read-operation-description"); builder.property("name", operationName, -1); request = builder.buildRequest(); } catch (OperationFormatException e1) { throw new IllegalStateException("Failed to build operation", e1); } List<String> result; try { ModelNode outcome = client.execute(request); if (!Util.isSuccess(outcome)) { result = Collections.emptyList(); } else { outcome.get("request-properties"); result = Util.getRequestPropertyNames(outcome); } } catch (Exception e) { result = Collections.emptyList(); } return result; }
public ModelNode execute(ModelNode operation, boolean awaitClose) throws IOException { final ModelNode response = super.execute(operation); if (!Util.isSuccess(response)) { return response; } if (awaitClose) { synchronized (this) { try { if (strategy == null) { throw new IOException("Connection has been closed."); } strategy.getChannel().getConnection().awaitClosed(); } catch (InterruptedException e) { } } } return response; }
public ModelNode execute(ModelNode operation, boolean awaitClose) throws IOException { final ModelNode response = super.execute(operation); if (!Util.isSuccess(response)) { return response; } if (awaitClose) { synchronized (lock) { if (strategy != null) { try { lock.wait(5000); } catch (InterruptedException e) { } StreamUtils.safeClose(strategy); strategy = null; } } } return response; }
protected void printCommands(CommandContext ctx) { ModelNode request = initRequest(ctx); if (request == null) { return; } request.get(Util.OPERATION).set(Util.READ_OPERATION_NAMES); try { ModelNode result = ctx.getModelControllerClient().execute(request); if (!result.hasDefined("result")) { ctx.printLine("Operation names aren't available."); return; } final List<String> list = Util.getList(result); list.removeAll(this.excludeOps); list.add( "To read the description of a specific command execute '" + this.commandName + " command_name --help'."); for (String name : list) { ctx.printLine(name); } } catch (Exception e) { } }
@Override protected void doHandle(CommandContext ctx) { ModelControllerClient client = ctx.getModelControllerClient(); if (!ctx.hasArguments()) { printList(ctx, Util.getDeployments(client)); return; } String filePath = null; String name = null; String runtimeName = null; for (String arg : ctx.getArguments()) { if (filePath == null) { filePath = arg; } else if (name == null) { name = arg; } else { runtimeName = arg; } } if (filePath == null) { printList(ctx, Util.getDeployments(client)); return; } File f = new File(filePath); if (!f.exists()) { ctx.printLine("The path doesn't exist: " + f.getAbsolutePath()); return; } if (name == null) { name = f.getName(); } if (Util.isDeployed(name, client)) { if (ctx.hasSwitch("f")) { DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder(); ModelNode result; // replace builder = new DefaultOperationRequestBuilder(); builder.setOperationName("full-replace-deployment"); builder.addProperty("name", name); if (runtimeName != null) { builder.addProperty("runtime-name", runtimeName); } FileInputStream is = null; try { is = new FileInputStream(f); ModelNode request = builder.buildRequest(); OperationBuilder op = OperationBuilder.Factory.create(request); op.addInputStream(is); request.get("input-stream-index").set(0); result = client.execute(op.build()); } catch (Exception e) { ctx.printLine("Failed to replace the deployment: " + e.getLocalizedMessage()); return; } finally { StreamUtils.safeClose(is); } if (!Util.isSuccess(result)) { ctx.printLine(Util.getFailureDescription(result)); return; } ctx.printLine("'" + name + "' re-deployed successfully."); } else { ctx.printLine("'" + name + "' is already deployed (use -f to force re-deploy)."); } return; } else { DefaultOperationRequestBuilder builder; ModelNode result; // add builder = new DefaultOperationRequestBuilder(); builder.setOperationName("add"); builder.addNode("deployment", name); if (runtimeName != null) { builder.addProperty("runtime-name", runtimeName); } FileInputStream is = null; try { is = new FileInputStream(f); ModelNode request = builder.buildRequest(); OperationBuilder op = OperationBuilder.Factory.create(request); op.addInputStream(is); request.get("input-stream-index").set(0); result = client.execute(op.build()); } catch (Exception e) { ctx.printLine( "Failed to add the deployment content to the repository: " + e.getLocalizedMessage()); return; } finally { StreamUtils.safeClose(is); } if (!Util.isSuccess(result)) { ctx.printLine(Util.getFailureDescription(result)); return; } // deploy builder = new DefaultOperationRequestBuilder(); builder.setOperationName("deploy"); builder.addNode("deployment", name); try { ModelNode request = builder.buildRequest(); result = client.execute(request); } catch (Exception e) { ctx.printLine("Failed to deploy: " + e.getLocalizedMessage()); return; } if (!Util.isSuccess(result)) { ctx.printLine(Util.getFailureDescription(result)); return; } ctx.printLine("'" + name + "' deployed successfully."); } }
public ModelNode buildRequest(CommandContext ctx) throws OperationFormatException { if (!ctx.hasArguments()) { throw new OperationFormatException("Required arguments are missing."); } String filePath = null; String name = null; String runtimeName = null; for (String arg : ctx.getArguments()) { if (filePath == null) { filePath = arg; } else if (name == null) { name = arg; } else { runtimeName = arg; } } if (filePath == null) { throw new OperationFormatException("File path is missing."); } File f = new File(filePath); if (!f.exists()) { throw new OperationFormatException(f.getAbsolutePath() + " doesn't exist."); } if (name == null) { name = f.getName(); } if (Util.isDeployed(name, ctx.getModelControllerClient())) { if (ctx.hasSwitch("f")) { DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder(); // replace builder = new DefaultOperationRequestBuilder(); builder.setOperationName("full-replace-deployment"); builder.addProperty("name", name); if (runtimeName != null) { builder.addProperty("runtime-name", runtimeName); } byte[] bytes = readBytes(f); builder.getModelNode().get("bytes").set(bytes); return builder.buildRequest(); } else { throw new OperationFormatException( "'" + name + "' is already deployed (use -f to force re-deploy)."); } } ModelNode composite = new ModelNode(); composite.get("operation").set("composite"); composite.get("address").setEmptyList(); ModelNode steps = composite.get("steps"); DefaultOperationRequestBuilder builder; // add builder = new DefaultOperationRequestBuilder(); builder.setOperationName("add"); builder.addNode("deployment", name); if (runtimeName != null) { builder.addProperty("runtime-name", runtimeName); } byte[] bytes = readBytes(f); builder.getModelNode().get("bytes").set(bytes); steps.add(builder.buildRequest()); // deploy builder = new DefaultOperationRequestBuilder(); builder.setOperationName("deploy"); builder.addNode("deployment", name); steps.add(builder.buildRequest()); return composite; }