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.common.utils.ExceptionUtils; 029import org.nuxeo.ecm.platform.commandline.executor.service.CommandLineDescriptor; 030 031/** 032 * Default implementation of the {@link CommandTester} interface. Simple check for the target command. Checks execution 033 * on system with contributed test parameters. 034 * 035 * @author tiry 036 */ 037public class DefaultCommandTester implements CommandTester { 038 039 @Override 040 public CommandTestResult test(CommandLineDescriptor cmdDescriptor) { 041 String cmd = cmdDescriptor.getCommand(); 042 String params = cmdDescriptor.getTestParametersString(); 043 String[] cmdWithParams = (cmd + " " + params).split(" "); 044 try { 045 ProcessBuilder builder = new ProcessBuilder(cmdWithParams); 046 // make sure we have only one InputStream to read to avoid parallelism/deadlock issues 047 builder.redirectErrorStream(true); 048 Process process = builder.start(); 049 // close process input immediately 050 process.getOutputStream().close(); 051 // consume all process output 052 try (InputStream in = process.getInputStream()) { 053 byte[] bytes = new byte[4096]; 054 while (in.read(bytes) != -1) { 055 // loop 056 } 057 } 058 // wait for process termination 059 process.waitFor(); 060 } catch (InterruptedException e) { 061 ExceptionUtils.checkInterrupt(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}