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