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 */ 020package org.sonar.process.monitor; 021 022import org.apache.commons.lang.StringUtils; 023 024import javax.annotation.Nullable; 025 026import java.io.File; 027import java.util.ArrayList; 028import java.util.HashMap; 029import java.util.LinkedHashMap; 030import java.util.List; 031import java.util.Map; 032import java.util.Properties; 033 034public 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 private int processIndex = -1; 058 059 public JavaCommand(String key) { 060 this.key = key; 061 processIndex = Monitor.getNextProcessId(); 062 } 063 064 public String getKey() { 065 return key; 066 } 067 068 public int getProcessIndex() { 069 return processIndex; 070 } 071 072 public File getWorkDir() { 073 return workDir; 074 } 075 076 public JavaCommand setWorkDir(File workDir) { 077 this.workDir = workDir; 078 return this; 079 } 080 081 public File getTempDir() { 082 return tempDir; 083 } 084 085 public JavaCommand setTempDir(File tempDir) { 086 this.tempDir = tempDir; 087 return this; 088 } 089 090 public List<String> getJavaOptions() { 091 return javaOptions; 092 } 093 094 public JavaCommand addJavaOption(String s) { 095 if (StringUtils.isNotBlank(s)) { 096 javaOptions.add(s); 097 } 098 return this; 099 } 100 101 public JavaCommand addJavaOptions(String s) { 102 for (String opt : s.split(" ")) { 103 addJavaOption(opt); 104 } 105 return this; 106 } 107 108 public String getClassName() { 109 return className; 110 } 111 112 public JavaCommand setClassName(String className) { 113 this.className = className; 114 return this; 115 } 116 117 public List<String> getClasspath() { 118 return classpath; 119 } 120 121 public JavaCommand addClasspath(String s) { 122 classpath.add(s); 123 return this; 124 } 125 126 public Map<String, String> getArguments() { 127 return arguments; 128 } 129 130 public JavaCommand setArgument(String key, @Nullable String value) { 131 if (value == null) { 132 arguments.remove(key); 133 } else { 134 arguments.put(key, value); 135 } 136 return this; 137 } 138 139 public JavaCommand setArguments(Properties args) { 140 for (Map.Entry<Object, Object> entry : args.entrySet()) { 141 setArgument(entry.getKey().toString(), entry.getValue() != null ? entry.getValue().toString() : null); 142 } 143 return this; 144 } 145 146 public Map<String, String> getEnvVariables() { 147 return envVariables; 148 } 149 150 public JavaCommand setEnvVariable(String key, @Nullable String value) { 151 if (value == null) { 152 envVariables.remove(key); 153 } else { 154 envVariables.put(key, value); 155 } 156 return this; 157 } 158 159 @Override 160 public String toString() { 161 final StringBuilder sb = new StringBuilder("JavaCommand{"); 162 sb.append("workDir=").append(workDir); 163 sb.append(", javaOptions=").append(javaOptions); 164 sb.append(", className='").append(className).append('\''); 165 sb.append(", classpath=").append(classpath); 166 sb.append(", arguments=").append(arguments); 167 sb.append(", envVariables=").append(envVariables); 168 sb.append('}'); 169 return sb.toString(); 170 } 171}