001/* 002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 * 021 */ 022 023package org.nuxeo.ecm.platform.commandline.executor.service.cmdtesters; 024 025import java.io.IOException; 026import java.io.InputStream; 027 028import org.nuxeo.ecm.platform.commandline.executor.service.CommandLineDescriptor; 029 030/** 031 * Default implementation of the {@link CommandTester} interface. Simple check for the target command. Checks execution 032 * on system with contributed test parameters. 033 * 034 * @author tiry 035 */ 036public class DefaultCommandTester implements CommandTester { 037 038 @Override 039 public CommandTestResult test(CommandLineDescriptor cmdDescriptor) { 040 String cmd = cmdDescriptor.getCommand(); 041 String params = cmdDescriptor.getTestParametersString(); 042 String[] cmdWithParams = (cmd + " " + params).split(" "); 043 try { 044 ProcessBuilder builder = new ProcessBuilder(cmdWithParams); 045 // make sure we have only one InputStream to read to avoid parallelism/deadlock issues 046 builder.redirectErrorStream(true); 047 Process process = builder.start(); 048 // close process input immediately 049 process.getOutputStream().close(); 050 // consume all process output 051 try (InputStream in = process.getInputStream()) { 052 byte[] bytes = new byte[4096]; 053 while (in.read(bytes) != -1) { 054 // loop 055 } 056 } 057 // wait for process termination 058 process.waitFor(); 059 } catch (InterruptedException e) { 060 Thread.currentThread().interrupt(); 061 throw new RuntimeException(e); 062 } catch (IOException e) { 063 return new CommandTestResult( 064 "command " + cmd + " not found in system path (descriptor " + cmdDescriptor + ")"); 065 } 066 067 return new CommandTestResult(); 068 } 069 070}