public static boolean isDomainServer(final ModelControllerClient client) throws IOException { boolean result = false; // Check this is really a domain server final ModelNode op = ServerOperations.createReadAttributeOperation(ServerOperations.LAUNCH_TYPE); final ModelNode opResult = client.execute(op); if (ServerOperations.isSuccessfulOutcome(opResult)) { result = ("DOMAIN".equals(ServerOperations.readResultAsString(opResult))); } return result; }
/** * Checks the existence of a resource. If the resource exists, {@code true} is returned, otherwise * {@code false}. * * @param address the address of the resource to check. * @param client the client used to execute the operation. * @return {@code true} if the resources exists, otherwise {@code false}. * @throws IOException if an error occurs executing the operation. * @throws RuntimeException if the operation fails. */ private boolean resourceExists(final ModelNode address, final ModelControllerClient client) throws IOException { final Property childAddress = ServerOperations.getChildAddress(address); final ModelNode parentAddress = ServerOperations.getParentAddress(address); final ModelNode r = client.execute( ServerOperations.createOperation(ServerOperations.READ_RESOURCE, parentAddress, false)); reportFailure(r); boolean found = false; final String name = childAddress.getName(); if (ServerOperations.isSuccessfulOutcome(r)) { for (ModelNode dataSource : ServerOperations.readResult(r).get(name).asList()) { if (dataSource.asProperty().getName().equals(childAddress.getValue().asString())) { found = true; } } } return found; }
private boolean addCompositeResource( final String profileName, final ModelControllerClient client, final Resource resource, final String parentAddress, final CompositeOperationBuilder compositeOp, final boolean checkExistence) throws IOException { final String inputAddress; if (parentAddress == null) { inputAddress = resource.getAddress(); } else if (parentAddress.equals(resource.getAddress())) { inputAddress = resource.getAddress(); } else if (resource.getAddress() == null) { inputAddress = parentAddress; } else { inputAddress = String.format("%s,%s", parentAddress, resource.getAddress()); } // The address cannot be null if (inputAddress == null) { throw new RuntimeException("You must specify the address to deploy the resource to."); } final ModelNode address = parseAddress(profileName, inputAddress); if (checkExistence) { final boolean exists = resourceExists(address, client); if (resource.isAddIfAbsent() && exists) { return false; } if (exists && force) { reportFailure(client.execute(ServerOperations.createRemoveOperation(address, true))); } else if (exists && !force) { throw new RuntimeException(String.format("Resource %s already exists.", address)); } } compositeOp.addStep(buildAddOperation(address, resource.getProperties())); if (resource.getResources() != null) { final String resourceAddress = resource.getAddress(); final String addr; if (parentAddress != null && resourceAddress != null) { addr = parentAddress + "," + resourceAddress; } else if (parentAddress != null) { addr = parentAddress; } else if (resourceAddress != null) { addr = resourceAddress; } else { addr = null; } for (Resource r : resource.getResources()) { addCompositeResource(profileName, client, r, addr, compositeOp, false); } } return true; }
/** * Creates the operation to add a resource. * * @param address the address of the operation to add. * @param properties the properties to set for the resource. * @return the operation. */ private ModelNode buildAddOperation( final ModelNode address, final Map<String, String> properties) { final ModelNode op = ServerOperations.createAddOperation(address); for (Map.Entry<String, String> prop : properties.entrySet()) { final String[] props = prop.getKey().split(","); if (props.length == 0) { throw new RuntimeException("Invalid property " + prop); } ModelNode node = op; for (int i = 0; i < props.length - 1; ++i) { node = node.get(props[i]); } final String value = prop.getValue() == null ? "" : prop.getValue(); if (value.startsWith("!!")) { handleDmrString(node, props[props.length - 1], value); } else { node.get(props[props.length - 1]).set(value); } } return op; }
@Override protected void doExecute() throws MojoExecutionException, MojoFailureException { final Log log = getLog(); final File deploymentFile = file(); // The deployment must exist before we do anything if (!deploymentFile.exists()) { throw new MojoExecutionException( String.format( "The deployment '%s' could not be found.", deploymentFile.getAbsolutePath())); } // Validate the environment final Path jbossHome = extractIfRequired(deploymentFile.getParentFile().toPath()); if (!Files.isDirectory(jbossHome)) { throw new MojoExecutionException( String.format("JBOSS_HOME '%s' is not a valid directory.", jbossHome)); } final StandaloneCommandBuilder commandBuilder = StandaloneCommandBuilder.of(jbossHome) .setJavaHome(javaHome) .addModuleDirs(modulesPath.getModulePaths()); // Set the JVM options if (javaOpts != null) { commandBuilder.setJavaOptions(javaOpts); } else if (jvmArgs != null) { commandBuilder.setJavaOptions(jvmArgs.split("\\s+")); } if (serverConfig != null) { commandBuilder.setServerConfiguration(serverConfig); } if (propertiesFile != null) { commandBuilder.setPropertiesFile(propertiesFile); } if (serverArgs != null) { commandBuilder.addServerArguments(serverArgs); } // Check for management overrides final ModelControllerClientConfiguration clientConfiguration = getClientConfiguration(); final String host = clientConfiguration.getHost(); final int port = clientConfiguration.getPort(); if (host != null) { commandBuilder.setBindAddressHint("management", host); } if (port > 0) { commandBuilder.addServerArguments("-Djboss.management.http.port=" + port); } // Print some server information log.info(String.format("JAVA_HOME=%s", commandBuilder.getJavaHome())); log.info(String.format("JBOSS_HOME=%s%n", commandBuilder.getWildFlyHome())); Server server = null; try (final ManagementClient client = createClient()) { // Create the server server = Server.create(commandBuilder, client); // Start the server log.info("Server is starting up. Press CTRL + C to stop the server."); server.start(startupTimeout); // Deploy the application server.checkServerState(); if (server.isRunning()) { log.info(String.format("Deploying application '%s'%n", deploymentFile.getName())); final Deployment deployment = StandaloneDeployment.create(client, deploymentFile, name, getType(), null, null); switch (executeDeployment(client, deployment, jbossHome)) { case REQUIRES_RESTART: { client.execute(ServerOperations.createOperation(ServerOperations.RELOAD)); break; } case SUCCESS: break; } } else { throw new DeploymentFailureException("Cannot deploy to a server that is not running."); } while (server.isRunning()) { TimeUnit.SECONDS.sleep(1L); } } catch (Exception e) { throw new MojoExecutionException("The server failed to start", e); } finally { if (server != null) server.stop(); } }
private void reportFailure(final ModelNode result) { if (!ServerOperations.isSuccessfulOutcome(result)) { throw new RuntimeException(ServerOperations.getFailureDescriptionAsString(result)); } }