001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.process.monitor;
021    
022    import javax.annotation.Nullable;
023    
024    import java.io.File;
025    import java.util.ArrayList;
026    import java.util.Collections;
027    import java.util.HashMap;
028    import java.util.LinkedHashMap;
029    import java.util.List;
030    import java.util.Map;
031    import java.util.Properties;
032    
033    public class JavaCommand {
034    
035      // unique key among the group of commands to launch
036      private final String key;
037    
038      private File workDir;
039    
040      // for example -Xmx1G
041      private final List<String> javaOptions = new ArrayList<String>();
042    
043      // entry point
044      private String className;
045    
046      // relative path to JAR files
047      private final List<String> classpath = new ArrayList<String>();
048    
049      // program arguments (parameters of main(String[])
050      private final Map<String, String> arguments = new LinkedHashMap<String, String>();
051    
052      private final Map<String, String> envVariables = new HashMap<String, String>(System.getenv());
053    
054      private File tempDir = null;
055    
056      public JavaCommand(String key) {
057        this.key = key;
058      }
059    
060      public String getKey() {
061        return key;
062      }
063    
064      public File getWorkDir() {
065        return workDir;
066      }
067    
068      public JavaCommand setWorkDir(File workDir) {
069        this.workDir = workDir;
070        return this;
071      }
072    
073      public File getTempDir() {
074        return tempDir;
075      }
076    
077      public JavaCommand setTempDir(File tempDir) {
078        this.tempDir = tempDir;
079        return this;
080      }
081    
082      public List<String> getJavaOptions() {
083        return javaOptions;
084      }
085    
086      public JavaCommand addJavaOption(String s) {
087        javaOptions.add(s);
088        return this;
089      }
090    
091      public JavaCommand addJavaOptions(String s) {
092        Collections.addAll(javaOptions, s.split(" "));
093        return this;
094      }
095    
096      public String getClassName() {
097        return className;
098      }
099    
100      public JavaCommand setClassName(String className) {
101        this.className = className;
102        return this;
103      }
104    
105      public List<String> getClasspath() {
106        return classpath;
107      }
108    
109      public JavaCommand addClasspath(String s) {
110        classpath.add(s);
111        return this;
112      }
113    
114      public Map<String, String> getArguments() {
115        return arguments;
116      }
117    
118      public JavaCommand setArgument(String key, @Nullable String value) {
119        if (value == null) {
120          arguments.remove(key);
121        } else {
122          arguments.put(key, value);
123        }
124        return this;
125      }
126    
127      public JavaCommand setArguments(Properties args) {
128        for (Map.Entry<Object, Object> entry : args.entrySet()) {
129          setArgument(entry.getKey().toString(), entry.getValue() != null ? entry.getValue().toString() : null);
130        }
131        return this;
132      }
133    
134      public Map<String, String> getEnvVariables() {
135        return envVariables;
136      }
137    
138      public JavaCommand setEnvVariable(String key, @Nullable String value) {
139        if (value == null) {
140          envVariables.remove(key);
141        } else {
142          envVariables.put(key, value);
143        }
144        return this;
145      }
146    
147      @Override
148      public String toString() {
149        final StringBuilder sb = new StringBuilder("JavaCommand{");
150        sb.append("workDir=").append(workDir);
151        sb.append(", javaOptions=").append(javaOptions);
152        sb.append(", className='").append(className).append('\'');
153        sb.append(", classpath=").append(classpath);
154        sb.append(", arguments=").append(arguments);
155        sb.append(", envVariables=").append(envVariables);
156        sb.append('}');
157        return sb.toString();
158      }
159    }