Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * Wildfly Camel :: Testsuite :: Common
   * %%
   * Copyright (C) 2013 - 2014 RedHat
   * %%
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * 
  *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * #L%
  */
 package org.wildfly.camel.test.common.docker;
 
 import java.util.List;
A Docker command

Author(s):
tdiesler@redhat.com
Since:
09-Dec-2014
 
 public class DockerCommand {
 
     private List<Stringoptions = new ArrayList<>();
     private List<Stringargs = new ArrayList<>();
     private String cmd;
 
     public DockerCommand(String cmd) {
         this. = cmd;
     }
     
     public DockerCommand options(String... opts) {
         for (String p : opts) {
             .add(p);
         }
         return this;
     }
     
     public DockerCommand args(String... params) {
         for (String p : params) {
             .add(p);
         }
         return this;
     }
 
     protected void buildCommand(List<Stringcarr) {
         carr.add("docker");
         carr.add();
         carr.addAll();
         appendsArgs(carr);
     }
 
     protected void appendsArgs(List<Stringcarr) {
         carr.addAll();
     }
     
     public final DockerCommand.Result exec() {
         
         List<Stringcarr = new ArrayList<>();
         buildCommand(carr);
         
         StringBuffer cbuf = new StringBuffer();
         for (String item : carr) {
             cbuf.append(item + " ");
         }
         ..println("DOCKER> " + cbuf);
         
         Process process;
         try {
             process = Runtime.getRuntime().exec(carr.toArray(new String[carr.size()]));
             process.waitFor();
         } catch (RuntimeException rte) {
             throw rte;
         } catch (Exception ex) {
             throw new IllegalStateException(ex);
         }
         Result result = new Result(process);
         return result;
     }
 
     public static class Result {
 
         private Process process;
         private BufferedReader output;
        
        Result(Process process) {
            this. = process;
        }
        
        public String outputLine() {
            Iterator<Stringit = outputLines();
            return it.hasNext() ? it.next() : null;
        }
        
        private BufferedReader getOutputReader() {
            if ( == null) {
                 = new BufferedReader(new InputStreamReader(.getInputStream()));
            }
            return ;
        }
        
        public Iterator<StringoutputLines() {
            return new Iterator<String> () {
                
                String nextLine;
                
                @Override
                public boolean hasNext() {
                    return nextLine() != null;
                }
                @Override
                public String next() {
                    String result = nextLine();
                    if (result == null)
                        throw new NoSuchElementException();
                    
                     = null;
                    return result;
                }
                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
                
                private String nextLine() {
                    if ( == null)
                    try {
                         = getOutputReader().readLine();
                    } catch (IOException e) {
                        return null;
                    }
                    return ;
                }
            };
        }
        
        public int exitValue() {
            return .exitValue();
        }
        
        public Result printOut(PrintStream out) {
            Iterator<Stringitout = outputLines();
            while (itout.hasNext()) {
                out.println(itout.next());
            }
            return this;
        }
        
        public Result printErr(PrintStream out) {
            BufferedReader error = new BufferedReader(new InputStreamReader(.getErrorStream()));
            try {
                String line = error.readLine();
                while (line != null) {
                    out.println(line);
                    line = error.readLine();
                }
            } catch (IOException ex) {
                // ignore
            } 
            return this;
        }
    }
New to GrepCode? Check out our FAQ X