Java Code Examples for org.jboss.as.controller.client.ModelControllerClient#close()

The following examples show how to use org.jboss.as.controller.client.ModelControllerClient#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShowHistoryUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private ModelNode showHistory() throws IOException {
    controller.start();
    ModelControllerClient client = null;
    final ModelNode response;
    try {
        client = controller.getClient().getControllerClient();
        ModelNode op = new ModelNode();
        op.get("address").add("core-service", "patching");
        op.get("operation").set("show-history");
        response = client.execute(op);
    } finally {
        if (client != null) {
            client.close();
        }
        controller.stop();
    }
    return response;
}
 
Example 2
Source File: AbstractRbacTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@AfterClass
public static void cleanUpClients() throws IOException, MgmtOperationException {
    for (ModelControllerClient client : clients.values()) {
        try {
            client.close();
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }
    clients.clear();
}
 
Example 3
Source File: RemoveManagementInterfaceTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testRemoveManagementInterface() throws Exception {
    ModelControllerClient client = getHttpModelControllerClient();
    ModelNode operation = createOpNode("socket-binding-group=standard-sockets/socket-binding=management-http", ModelDescriptionConstants.READ_RESOURCE_OPERATION);
    ModelNode response = client.execute(operation);
    assertThat(response.hasDefined(OUTCOME), is(true));
    assertThat(response.get(OUTCOME).asString(), is(SUCCESS));
    operation = createOpNode("core-service=management/management-interface=http-interface", ModelDescriptionConstants.REMOVE);
    CoreUtils.applyUpdate(operation, client);
    client.close();
    ServerReload.executeReloadAndWaitForCompletion(getNativeModelControllerClient(), TimeoutUtil.adjust(30000), false, "remote", TestSuiteEnvironment.getServerAddress(), MANAGEMENT_NATIVE_PORT);
    client = getNativeModelControllerClient();
    operation = createOpNode("socket-binding-group=standard-sockets/socket-binding=management-http", ModelDescriptionConstants.READ_RESOURCE_OPERATION);
    response = client.execute(operation);
    assertThat(response.hasDefined(OUTCOME), is(true));
    assertThat(response.get(OUTCOME).asString(), is(SUCCESS));
    operation = createOpNode("core-service=management/management-interface=native-interface", ModelDescriptionConstants.REMOVE);
    response = client.execute(operation);
    assertThat(DomainTestSupport.validateFailedResponse(response).asString(), CoreMatchers.containsString("WFLYRMT0025"));
    client.close();
    client = getHttpModelControllerClient();
    operation = createOpNode("socket-binding-group=standard-sockets/socket-binding=management-http", ModelDescriptionConstants.READ_RESOURCE_OPERATION);
    try {
        client.execute(operation);
        Assert.fail("Shouldn't be able to connect to http management");
    } catch (IOException ioex) {
        assertThat(ioex.getMessage(), CoreMatchers.containsString("WFLYPRT0053"));
    } finally {
        client.close();
    }
}
 
Example 4
Source File: RemoveManagementInterfaceTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void safeCloseClient(ModelControllerClient client) {
    try {
        if (client != null) {
            client.close();
        }
    } catch (final Exception e) {
        LOGGER.warnf(e, "Caught exception closing ModelControllerClient");
    }
}
 
Example 5
Source File: AbstractRbacTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void cleanUpClients(Map<String, ModelControllerClient> clients) {

        for (ModelControllerClient client : clients.values()) {
            try {
                client.close();
            } catch (Exception e) {
                e.printStackTrace(System.out);
            }
        }

        clients.clear();
    }
 
Example 6
Source File: AbstractRbacTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void removeClientForUser(String userName, boolean allowLocalAuth) throws IOException {
    Map<String, ModelControllerClient> clients = allowLocalAuth ? localAuthClients : nonLocalAuthclients;
    ModelControllerClient client = clients.remove(userName);
    if (client != null) {
        client.close();
    }
}
 
Example 7
Source File: AbstractDMRResourcePathCommand.java    From hawkular-agent with Apache License 2.0 4 votes vote down vote up
@Override
public BasicMessageWithExtraData<RESP> execute(BasicMessageWithExtraData<REQ> envelope, CommandContext context)
        throws Exception {

    REQ request = envelope.getBasicMessage();
    String rawResourcePath = request.getResourceId();
    log.infoReceivedResourcePathCommand(this.getOperationName(envelope), this.getEntityType(envelope),
            rawResourcePath);

    RESP response = createResponse();
    MessageUtils.prepareResourceResponse(request, response);
    BinaryData binaryData = null;
    ModelControllerClient controllerClient = null;
    long timestampBeforeExecution = System.currentTimeMillis();
    try {
        validate(envelope);

        String resourceId = rawResourcePath;
        ResourceIdParts idParts = InventoryIdUtil.parseResourceId(resourceId);
        String modelNodePath = idParts.getIdPart();
        validate(modelNodePath, envelope);

        String managedServerName = idParts.getManagedServerName();
        EndpointService<DMRNodeLocation, DMRSession> endpointService = context.getAgentCoreEngine()
                .getProtocolServices()
                .getDmrProtocolService()
                .getEndpointServices()
                .get(managedServerName);
        if (endpointService == null) {
            throw new IllegalArgumentException(String.format(
                    "Cannot perform [%s] on a [%s] given by inventory path [%s]: unknown managed server [%s]",
                    this.getOperationName(envelope), this.getEntityType(envelope), resourceId, managedServerName));
        }

        validate(envelope, endpointService.getMonitoredEndpoint());

        if (modifiesResource()) {
            if (context.getAgentCoreEngine().isImmutable()) {
                throw new IllegalStateException("Command not allowed because the agent is immutable");
            }
        }

        DMRSession session = endpointService.openSession();

        controllerClient = session.getClient();

        binaryData = execute(controllerClient, endpointService, modelNodePath, envelope, response, context,
                session);
        success(envelope, response);

    } catch (Throwable t) {
        response.setStatus(ResponseStatus.ERROR);
        String formattedTimestamp = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mmX").withZone(ZoneOffset.UTC)
                .format(Instant.ofEpochMilli(timestampBeforeExecution));

        String msg = String.format(
                "Could not perform [%s] on a [%s] given by inventory path [%s] requested on [%s]: %s",
                this.getOperationName(envelope), this.getEntityType(envelope), rawResourcePath, formattedTimestamp,
                t.toString());
        response.setMessage(msg);
        log.debug(msg, t);
    } finally {
        if (controllerClient != null) {
            try {
                controllerClient.close();
            } catch (Exception e) {
                log.errorCannotClose(e, controllerClient.getClass().getName());
            }
        }
    }

    return new BasicMessageWithExtraData<>(response, binaryData);

}
 
Example 8
Source File: AbstractRbacTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static void removeClientForUser(String userName) throws IOException {
    ModelControllerClient client = clients.remove(userName);
    if (client != null) {
        client.close();
    }
}
 
Example 9
Source File: DeploymentScannerUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void testStartup() throws Exception {
    Path deployDir = getDeployDirPath();
    final Path oneDeployed = deployDir.resolve(JAR_ONE + ".deployed");
    final Path twoFailed = deployDir.resolve(JAR_TWO + ".failed");
    container.start();
    ModelControllerClient client = TestSuiteEnvironment.getModelControllerClient();
    try {
        //set the logging to debug
        addDebugDeploymentLogger(client);
        try {
            final Path deploymentOne = deployDir.resolve(JAR_ONE);
            final Path deploymentTwo = deployDir.resolve(JAR_TWO);

            createDeployment(deploymentOne, "org.jboss.modules");
            createDeployment(deploymentTwo, "non.existing.dependency");
            addDeploymentScanner(client, 0, false, true);
            try {
                // Wait until deployed ...
                long timeout = System.currentTimeMillis() + TIMEOUT;
                while (!(exists(client, DEPLOYMENT_ONE) && exists(client, DEPLOYMENT_TWO)) && System.currentTimeMillis() < timeout) {
                    Thread.sleep(100);
                }
                Assert.assertTrue(exists(client, DEPLOYMENT_ONE));
                Assert.assertEquals("OK", deploymentState(client, DEPLOYMENT_ONE));
                Assert.assertTrue(exists(client, DEPLOYMENT_TWO));
                Assert.assertEquals("FAILED", deploymentState(client, DEPLOYMENT_TWO));
                Assert.assertTrue(Files.exists(oneDeployed));
                Assert.assertTrue(Files.exists(twoFailed));

                ModelNode rollBackOnBoot  = Util.getWriteAttributeOperation(PathAddress.parseCLIStyleAddress("/subsystem=deployment-scanner/scanner=testScanner"), "runtime-failure-causes-rollback", true);
                ModelNode result = client.execute(rollBackOnBoot);
                assertEquals("Unexpected outcome of rollbacking the test deployment scanner: " + rollBackOnBoot, ModelDescriptionConstants.SUCCESS, result.get(OUTCOME).asString());

                // Restart ...
                client.close();
                container.stop();
                container.start();
                client = TestSuiteEnvironment.getModelControllerClient();

                // Wait until started ...
                timeout = System.currentTimeMillis() + TIMEOUT;
                while (!isRunning(client) && System.currentTimeMillis() < timeout) {
                    Thread.sleep(10);
                }

                Assert.assertTrue(Files.exists(oneDeployed));
                Assert.assertTrue(Files.exists(twoFailed));

                Assert.assertTrue(exists(client, DEPLOYMENT_ONE));
                Assert.assertEquals("OK", deploymentState(client, DEPLOYMENT_ONE));

                timeout = System.currentTimeMillis() + TIMEOUT;
                while (exists(client, DEPLOYMENT_TWO) && System.currentTimeMillis() < timeout) {
                    Thread.sleep(10);
                }
                Assert.assertFalse("Deployment two should exist at " + TIME_FORMATTER.format(LocalDateTime.now()), exists(client, DEPLOYMENT_TWO));
                ModelNode disableScanner = Util.getWriteAttributeOperation(PathAddress.parseCLIStyleAddress("/subsystem=deployment-scanner/scanner=testScanner"), "scan-interval", 300000);
                result = client.execute(disableScanner);
                assertEquals("Unexpected outcome of disabling the test deployment scanner: " + disableScanner, ModelDescriptionConstants.SUCCESS, result.get(OUTCOME).asString());

                final ModelNode undeployOp = Util.getEmptyOperation(DeploymentUndeployHandler.OPERATION_NAME, DEPLOYMENT_ONE.toModelNode());
                result = client.execute(undeployOp);
                assertEquals("Unexpected outcome of undeploying deployment one: " + undeployOp, ModelDescriptionConstants.SUCCESS, result.get(OUTCOME).asString());
                Assert.assertTrue(exists(client, DEPLOYMENT_ONE));
                Assert.assertEquals("STOPPED", deploymentState(client, DEPLOYMENT_ONE));

                timeout = System.currentTimeMillis() + TIMEOUT;

                while (Files.exists(oneDeployed) && System.currentTimeMillis() < timeout) {
                    Thread.sleep(10);
                }
                Assert.assertFalse(Files.exists(oneDeployed));
            } finally {
                removeDeploymentScanner(client);
                removeDebugDeploymentLogger(client);
            }

        } finally {
            StreamUtils.safeClose(client);
        }
    } finally {
        container.stop();
    }
}