001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.api.utils.command;
021
022import com.google.common.base.Joiner;
023import com.google.common.collect.Lists;
024import com.google.common.collect.Maps;
025import org.apache.commons.lang.StringUtils;
026
027import java.io.File;
028import java.util.Arrays;
029import java.util.Collections;
030import java.util.List;
031import java.util.Map;
032
033/**
034 * @since 2.7
035 */
036public class Command {
037
038  private String executable;
039  private List<String> arguments = Lists.newArrayList();
040  private File directory;
041  private Map<String, String> env = Maps.newHashMap(System.getenv());
042
043  private Command(String executable) {
044    this.executable = executable;
045  }
046
047  public String getExecutable() {
048    return executable;
049  }
050
051  public List<String> getArguments() {
052    return Collections.unmodifiableList(arguments);
053  }
054
055  public Command addArgument(String arg) {
056    arguments.add(arg);
057    return this;
058  }
059
060  public Command addArguments(List<String> args) {
061    arguments.addAll(args);
062    return this;
063  }
064
065  public Command addArguments(String[] args) {
066    arguments.addAll(Arrays.asList(args));
067    return this;
068  }
069
070  public File getDirectory() {
071    return directory;
072  }
073
074  /**
075   * Sets working directory.
076   */
077  public Command setDirectory(File d) {
078    this.directory = d;
079    return this;
080  }
081
082  /**
083   * @see {@link org.sonar.api.utils.command.Command#getEnvironmentVariables()}
084   * @since 3.2
085   */
086  public Command setEnvironmentVariable(String name, String value) {
087    this.env.put(name, value);
088    return this;
089  }
090
091  /**
092   * Environment variables that are propagated during command execution.
093   * The initial value is a copy of the environment of the current process.
094   *
095   * @return a non-null and immutable map of variables
096   * @since 3.2
097   */
098  public Map<String, String> getEnvironmentVariables() {
099    return Collections.unmodifiableMap(env);
100  }
101
102  String[] toStrings() {
103    List<String> command = Lists.newArrayList();
104    command.add(executable);
105    command.addAll(arguments);
106    return command.toArray(new String[command.size()]);
107  }
108
109  public String toCommandLine() {
110    return Joiner.on(" ").join(toStrings());
111  }
112
113  @Override
114  public String toString() {
115    return toCommandLine();
116  }
117
118  /**
119   * Create a command line without any arguments
120   *
121   * @param executable
122   */
123  public static Command create(String executable) {
124    if (StringUtils.isBlank(executable)) {
125      throw new IllegalArgumentException("Command executable can not be blank");
126    }
127    return new Command(executable);
128  }
129}