Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.as.test.integration.management.util;
 
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.READ_ATTRIBUTE_OPERATION;
 import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.util.List;
 
CLI executor with custom configuration file jboss-cli.xml used for testing two-way SSL connection

Author(s):
Filip Bogyai
 
 public class CustomCLIExecutor {
 
     public static final int MANAGEMENT_NATIVE_PORT = 9999;
     public static final int MANAGEMENT_HTTP_PORT = 9990;
     public static final int MANAGEMENT_HTTPS_PORT = 9993;
     public static final String NATIVE_CONTROLLER = "remoting://" + TestSuiteEnvironment.getServerAddress() + ":"
             + ;
     public static final String HTTP_CONTROLLER = "http-remoting://" + TestSuiteEnvironment.getServerAddress() + ":"
             + ;
     public static final String HTTPS_CONTROLLER = "https-remoting://" + TestSuiteEnvironment.getServerAddress() + ":"
             + ;
 
     private static Logger LOGGER = Logger.getLogger(CustomCLIExecutor.class);
     private static final int CLI_PROC_TIMEOUT = 5000;
     private static final int STATUS_CHECK_INTERVAL = 2000;
     private static final byte[] NEW_LINE = System.lineSeparator().getBytes(.);
 
     public static String execute(File cliConfigFileString operation) {
 
         String defaultController = TestSuiteEnvironment.getServerAddress() + ":" + TestSuiteEnvironment.getServerPort();
         return execute(cliConfigFileoperationdefaultControllerfalse);
     }
 
     public static String execute(File cliConfigFileString operationString controller) {
 
 
         return execute(cliConfigFileoperationcontrollerfalse);
     }

    
Externally executes CLI operation with cliConfigFile settings via defined controller

Returns:
String cliOutput
 
     public static String execute(File cliConfigFileString operationString controllerboolean logFailure) {
         return execute(cliConfigFileoperationcontrollerlogFailurenull);
     }

    
Externally executes CLI operation with cliConfigFile settings via defined controller

Parameters:
cliConfigFile the the configuration file to use or null to use the default
operation the CLI operation to execute
controller the controller to use
logFailure true to log failures otherwise false
failureResponse a response that can be sent to stdin of the launched processes or null
Returns:
the stdout response from the process
 
     public static String execute(File cliConfigFileString operationString controllerboolean logFailureString failureResponse) {
 
        String jbossDist = System.getProperty("jboss.dist");
        if (jbossDist == null) {
            fail("jboss.dist system property is not set");
        }
        final String modulePath = System.getProperty("module.path");
        if (modulePath == null) {
            fail("module.path system property is not set");
        }
        final ProcessBuilder builder = new ProcessBuilder();
        builder.redirectErrorStream(true);
        final List<Stringcommand = new ArrayList<String>();
        command.add(TestSuiteEnvironment.getJavaPath());
        TestSuiteEnvironment.getIpv6Args(command);
        if (cliConfigFile != null) {
            command.add("-Djboss.cli.config=" + cliConfigFile.getAbsolutePath());
        } else {
            command.add("-Djboss.cli.config=" + jbossDist + . + "bin" + . + "jboss-cli.xml");
        }
        if (System.getProperty("cli.args") != null) {
            command.add(System.getProperty("cli.args"));
        }
        command.add("-jar");
        command.add(jbossDist + . + "jboss-modules.jar");
        command.add("-mp");
        command.add(modulePath);
        command.add("org.jboss.as.cli");
        command.add("-c");
        command.add("--controller=" + controller);
        command.add(operation);
        builder.command(command);
        Process cliProc = null;
        try {
            cliProc = builder.start();
        } catch (IOException e) {
            fail("Failed to start CLI process: " + e.getLocalizedMessage());
        }
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final ByteArrayOutputStream err = new ByteArrayOutputStream();
        ConsoleConsumer.start(cliProc.getInputStream(), out);
        ConsoleConsumer.start(cliProc.getErrorStream(), err);
        boolean wait = true;
        int runningTime = 0;
        int exitCode = 0;
        do {
            try {
                Thread.sleep();
            } catch (InterruptedException e) {
            }
            runningTime += ;
            try {
                exitCode = cliProc.exitValue();
                wait = false;
            } catch (IllegalThreadStateException e) {
                // cli still working
                if (failureResponse != null) {
                    try {
                        final OutputStream stdin = cliProc.getOutputStream();
                        stdin.write(failureResponse.getBytes(.));
                        stdin.write();
                        stdin.flush();
                    } catch (IOException e1) {
                        .debug(out.toString(), e);
                    }
                }
            }
            if (runningTime >= ) {
                cliProc.destroy();
                wait = false;
            }
        } while (wait);
        final String cliOutput = out.toString();
        if (logFailure && exitCode != 0) {
            .info("Command's output: '" + cliOutput + "'");
            if (err.size() > 0) {
                .info("Command's error log: '" + err.toString() + "'");
            } else {
                .info("No output data for the command.");
            }
        }
        return exitCode + ": " + cliOutput;
    }

    
Waits for server to reload until server-state is running

Parameters:
timeout
controller
Throws:
java.lang.Exception
    public static void waitForServerToReload(int timeoutString controllerthrows Exception {
        Thread.sleep(TimeoutUtil.adjust(500));
        long start = System.currentTimeMillis();
        long now;
        do {
            try {
                String result = CustomCLIExecutor.execute(null + " server-state"controller);
                boolean normal = result.contains("running");
                if (normal) {
                    return;
                }
            } catch (Exception e) {
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            now = System.currentTimeMillis();
        } while (now - start < timeout);
        fail("Server did not reload in the imparted time.");
    }
    private static class ConsoleConsumer implements Runnable {
        static void start(final InputStream infinal OutputStream target) {
            final Thread t = new Thread(new ConsoleConsumer(intarget));
            t.start();
        }
        private final InputStream in;
        private final OutputStream target;
        private ConsoleConsumer(final InputStream infinal OutputStream target) {
            this. = in;
            this. = target;
        }
        @Override
        public void run() {
            final byte[] b = new byte[32];
            int len;
            try {
                while ((len = .read(b)) != -1) {
                    .write(b, 0, len);
                }
            } catch (IOException ignore) {
            }
        }
    }
New to GrepCode? Check out our FAQ X