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 org.apache.commons.lang.StringUtils;
023
024 import javax.annotation.Nullable;
025
026 import java.io.File;
027 import java.util.ArrayList;
028 import java.util.HashMap;
029 import java.util.LinkedHashMap;
030 import java.util.List;
031 import java.util.Map;
032 import java.util.Properties;
033
034 public class JavaCommand {
035
036 // unique key among the group of commands to launch
037 private final String key;
038
039 private File workDir;
040
041 // for example -Xmx1G
042 private final List<String> javaOptions = new ArrayList<String>();
043
044 // entry point
045 private String className;
046
047 // relative path to JAR files
048 private final List<String> classpath = new ArrayList<String>();
049
050 // program arguments (parameters of main(String[])
051 private final Map<String, String> arguments = new LinkedHashMap<String, String>();
052
053 private final Map<String, String> envVariables = new HashMap<String, String>(System.getenv());
054
055 private File tempDir = null;
056
057 public JavaCommand(String key) {
058 this.key = key;
059 }
060
061 public String getKey() {
062 return key;
063 }
064
065 public File getWorkDir() {
066 return workDir;
067 }
068
069 public JavaCommand setWorkDir(File workDir) {
070 this.workDir = workDir;
071 return this;
072 }
073
074 public File getTempDir() {
075 return tempDir;
076 }
077
078 public JavaCommand setTempDir(File tempDir) {
079 this.tempDir = tempDir;
080 return this;
081 }
082
083 public List<String> getJavaOptions() {
084 return javaOptions;
085 }
086
087 public JavaCommand addJavaOption(String s) {
088 if (StringUtils.isNotBlank(s)) {
089 javaOptions.add(s);
090 }
091 return this;
092 }
093
094 public JavaCommand addJavaOptions(String s) {
095 for (String opt : s.split(" ")) {
096 addJavaOption(opt);
097 }
098 return this;
099 }
100
101 public String getClassName() {
102 return className;
103 }
104
105 public JavaCommand setClassName(String className) {
106 this.className = className;
107 return this;
108 }
109
110 public List<String> getClasspath() {
111 return classpath;
112 }
113
114 public JavaCommand addClasspath(String s) {
115 classpath.add(s);
116 return this;
117 }
118
119 public Map<String, String> getArguments() {
120 return arguments;
121 }
122
123 public JavaCommand setArgument(String key, @Nullable String value) {
124 if (value == null) {
125 arguments.remove(key);
126 } else {
127 arguments.put(key, value);
128 }
129 return this;
130 }
131
132 public JavaCommand setArguments(Properties args) {
133 for (Map.Entry<Object, Object> entry : args.entrySet()) {
134 setArgument(entry.getKey().toString(), entry.getValue() != null ? entry.getValue().toString() : null);
135 }
136 return this;
137 }
138
139 public Map<String, String> getEnvVariables() {
140 return envVariables;
141 }
142
143 public JavaCommand setEnvVariable(String key, @Nullable String value) {
144 if (value == null) {
145 envVariables.remove(key);
146 } else {
147 envVariables.put(key, value);
148 }
149 return this;
150 }
151
152 @Override
153 public String toString() {
154 final StringBuilder sb = new StringBuilder("JavaCommand{");
155 sb.append("workDir=").append(workDir);
156 sb.append(", javaOptions=").append(javaOptions);
157 sb.append(", className='").append(className).append('\'');
158 sb.append(", classpath=").append(classpath);
159 sb.append(", arguments=").append(arguments);
160 sb.append(", envVariables=").append(envVariables);
161 sb.append('}');
162 return sb.toString();
163 }
164 }