public final ModelNodeResult forBatchStep(int stepIndex) { if (stepIndex < 1) { throw new IllegalArgumentException("Step number must be > 0 (first step has index 1)"); } List<Property> steps = this.value().asPropertyList(); if (stepIndex > steps.size()) { throw new IllegalArgumentException("No step " + stepIndex + ": " + this.asString()); } return new ModelNodeResult(steps.get(stepIndex - 1).getValue()); }
public final void assertNotDefinedValue(String message) { assertSuccess(message); message = adjustAssertMessage(message); if (this.hasDefined(Constants.RESULT)) { throw new AssertionError(message + "Expected NOT defined 'result', but it's present: " + this.asString()); } }
@Override public boolean isRequired(ModelNodeResult serverStateResult, boolean isManagedServerInDomain) { serverStateResult.assertDefinedValue(); return Constants.CONTROLLER_PROCESS_STATE_RESTART_REQUIRED.equals(serverStateResult.stringValue()); }
public final List<Boolean> booleanListValue(List<Boolean> defaultValue) { return hasDefinedValue() ? booleanListValue() : defaultValue; }
public final List<Double> doubleListValue(List<Double> defaultValue) { return hasDefinedValue() ? doubleListValue() : defaultValue; }
public final void assertDefinedValue(String message) { assertSuccess(message); message = adjustAssertMessage(message); if (!hasDefinedValue()) { throw new AssertionError(message + "Expected defined 'result', but it's missing: " + this.asString()); } }
public final void assertSuccess(String message) { message = adjustAssertMessage(message); if (!isSuccess()) { throw new AssertionError(message + "Expected success, but operation failed: " + this.asString()); } }
public final void assertFailed(String message) { message = adjustAssertMessage(message); if (!isFailed()) { throw new AssertionError(message + "Expected failure, but operation succeeded: " + this.asString()); } }
List<String> allRunningServers(String host) throws IOException { ModelNodeResult result = ops.readChildrenNames(Address.host(host), Constants.SERVER); result.assertDefinedValue(); List<String> servers = result.stringListValue(); List<String> startedServers = new ArrayList<String>(); for (String server : servers) { ModelNodeResult serverStatus = ops.readAttribute(Address.host(host).and(Constants.SERVER_CONFIG, server), Constants.STATUS); serverStatus.assertDefinedValue(); if ("STARTED".equals(serverStatus.stringValue())) { startedServers.add(server); } } return startedServers; }
/** * Returns the part of the operation result that is in fact a result of an operation performed on one single server * in a domain. The server is identified by the {@code host} name and the {@code server} name. It's not needed * to specify the server group, because one host can only belong to one server group. * @throws IllegalArgumentException if {@code this} is not an operation result from domain or if no such * {@code host} + {@code server} combination is present in {@code this} */ public final ModelNodeResult forServer(String host, String server) { if (!isFromDomain()) { throw new IllegalArgumentException("Can't call forServer on a result that isn't from domain"); } List<Property> serverGroups = this.get(Constants.SERVER_GROUPS).asPropertyList(); for (Property serverGroup : serverGroups) { ModelNode response = serverGroup.getValue().get(Constants.HOST, host, server, Constants.RESPONSE); if (response.isDefined()) { return new ModelNodeResult(response); } } throw new IllegalArgumentException("No such host or server: host = " + host + ", server = " + server); } }
ModelNodeResult result = new ModelNodeResult(client.execute(op)); if (result.isSuccess()) { return; String failureDescription = result.get(Constants.FAILURE_DESCRIPTION).asString(); for (String code : Constants.RESULT_CODES_FOR_BOOT_IN_PROGRESS) { if (failureDescription.startsWith(code)) { ModelNodeResult result = new ModelNodeResult(client.execute(op)); if (!result.isSuccess()) { throw new TimeoutException("Waiting for server to boot timed out");
/** Retrieves all hosts in domain. */ public List<String> hosts() throws IOException { ModelNodeResult result = ops.readChildrenNames(Address.root(), Constants.HOST); result.assertDefinedValue(); return result.stringListValue(); }
@Override public ModelNodeResult execute(ModelNode operation) { try { ModelNodeResult result = delegate.execute(operation); if (result.isFailed() && throwExceptionsOnOperationFailures) { commandFailedWithMessage("Operation " + operation.asString() + " failed: " + result.asString()); } return result; } catch (RuntimeCommandFailedException e) { throw e; } catch (Exception e) { commandFailedWithCause(e); return null; // dead code } }
/** * Returns the management version of the server which the {@code client} is connected to. * @throws IOException if an I/O error occurs during any management operation */ static ServerVersion discover(ModelControllerClient client) throws IOException { ModelNode op = new ModelNode(); op.get(Constants.OP).set(Constants.READ_RESOURCE_OPERATION); op.get(Constants.OP_ADDR).setEmptyList(); // this would be preferrable, as it means transferring less data, but it is not supported on older versions //op.get(Constants.ATTRIBUTES_ONLY).set(true); ModelNodeResult result = new ModelNodeResult(client.execute(op)); result.assertSuccess(); ModelNode resultValue = result.get(Constants.RESULT); return ServerVersion.from( readPart(resultValue, ManagementVersionPart.MAJOR), readPart(resultValue, ManagementVersionPart.MINOR), readPart(resultValue, ManagementVersionPart.MICRO) ); }
private boolean areServersRunning(String host, List<String> servers) throws IOException { Address hostAddress = Address.host(host); if (servers == null || servers.isEmpty()) { ModelNodeResult result = ops.readAttribute(Address.host(host), Constants.HOST_STATE); result.assertDefinedValue(); return ServerState.isRunning(result.stringValue()); } else { for (String server : servers) { Address serverAddress = hostAddress.and(Constants.SERVER, server); ModelNodeResult result = ops.readAttribute(serverAddress, Constants.SERVER_STATE); if (!result.hasDefinedValue() || !ServerState.isRunning(result.stringValue())) { return false; } } return true; } } }
/** * @return {@code true} if the resource specified by {@code address} exists, {@code false} otherwise * @throws OperationException if the underlying {@code read-resource} operation fails */ public boolean exists(Address address) throws IOException, OperationException { FailuresAllowedBlock expectedFailures = client.allowFailures(); try { ModelNodeResult result = readResource(address); if (result.isSuccess()) { return result.hasDefinedValue(); // should always be true } if (isResultUnknownOrNotFound(result)) { return false; } throw new OperationException("exists failed: " + result.asString()); } finally { expectedFailures.close(); } }
/** * Stops given {@code server} on given {@code host}. * * @return {@code true} when server was successfully stopped, {@code false} otherwise */ public boolean stopServer(String host, String server) throws IOException { ModelNodeResult result = ops.invoke("stop", Address.host(host).and(Constants.SERVER_CONFIG, server), Values.of("blocking", true)); result.assertDefinedValue(); return result.isSuccess(); }
static boolean isResultUnknownOrNotFound(ModelNodeResult result) { result.assertFailed(); ModelNode failureDescription = result.get(Constants.FAILURE_DESCRIPTION); if (failureDescription.hasDefined(Constants.HOST_FAILURE_DESCRIPTIONS)) { List<Property> hostFailures = failureDescription.get(Constants.HOST_FAILURE_DESCRIPTIONS).asPropertyList(); for (Property hostFailure : hostFailures) { if (isFailureDesriptionUnknownOrNotFound(hostFailure.getValue().asString())) { return true; } } return false; } else { if (failureDescription.hasDefined(Constants.DOMAIN_FAILURE_DESCRIPTION)) { failureDescription = failureDescription.get(Constants.DOMAIN_FAILURE_DESCRIPTION); } return isFailureDesriptionUnknownOrNotFound(failureDescription.asString()); } }
/** * Returns the Java command the server / host controller was started with. It's read from the platform MBean * server via the management interface. * * @param host should be {@code null} for standalone server or for default host in managed domain; * should only be non-{@code null} if a specific host in managed domain is checked */ private static String commandUsedToStartTheServer(OnlineManagementClient client, String host) throws IOException { Address baseAddress = Address.root(); if (host != null) { baseAddress = Address.host(host); } ModelNodeResult systemProperties = new Operations(client).readAttribute( baseAddress.and(Constants.CORE_SERVICE, Constants.PLATFORM_MBEAN).and("type", "runtime"), "system-properties" ); systemProperties.assertDefinedValue(); // the "sun.java.command" system property exists at least on OpenJDK, Oracle Java and IBM Java return systemProperties.value().get("sun.java.command").asString(); }
/** * @return {@code true} if the resource specified by {@code address} was actually removed (i.e., it used to exist), * {@code false} if it didn't exist and therefore wasn't actually removed * @throws OperationException if the underlying {@code remove} operation fails with something else than "not found" */ public boolean removeIfExists(Address address) throws IOException, OperationException { FailuresAllowedBlock expectedFailures = client.allowFailures(); try { ModelNodeResult result = remove(address); if (result.isSuccess()) { return true; } if (isResultUnknownOrNotFound(result)) { return false; } throw new OperationException("removeIfExists failed: " + result.asString()); } finally { expectedFailures.close(); } }