1 56 package com.ivata.groupware.admin.script; 57 58 import java.io.BufferedReader ; 59 import java.io.File ; 60 import java.io.IOException ; 61 import java.io.InputStream ; 62 import java.io.InputStreamReader ; 63 import java.util.Arrays ; 64 import java.util.Iterator ; 65 import java.util.List ; 66 import java.util.Vector ; 67 68 import org.apache.log4j.Logger; 69 70 import com.ivata.groupware.admin.security.server.SecurityServerException; 71 import com.ivata.mask.util.CollectionHandling; 72 import com.ivata.mask.util.SystemException; 73 import com.ivata.mask.validation.ValidationError; 74 import com.ivata.mask.validation.ValidationErrors; 75 import com.ivata.mask.validation.ValidationException; 76 import com.ivata.mask.web.format.URLFormat; 77 78 86 public class ExternalScriptExecutor implements ScriptExecutor { 87 88 91 private static Logger log = Logger.getLogger(ExternalScriptExecutor.class); 92 97 String [] environmentVariables; 98 103 private String scriptPath; 104 110 URLFormat uRLFormat; 111 123 public ExternalScriptExecutor(URLFormat URLFormat, String scriptPath, 124 String environmentVariables) { 125 this.scriptPath = scriptPath; 126 this.environmentVariables = (String []) 127 CollectionHandling.convertFromLines(environmentVariables).toArray(new String []{}); 128 this.uRLFormat = URLFormat; 129 } 130 141 public String exec(final String scriptName, 142 final String [] arguments) throws SystemException { 143 String [] externalArguments = new String [arguments.length + 1]; 144 145 try { 146 externalArguments[0] = scriptPath 147 + File.separator 148 + scriptName; 149 150 for (int index = 0; index < arguments.length; ++index) { 155 if ((arguments[index] == null) 156 || (arguments[index].length() == 0)) { 157 externalArguments[index + 1] = "%00"; 158 } else { 159 externalArguments[index + 1] = uRLFormat.format(arguments[index]); 160 } 161 } 162 163 Process process; 164 try { 165 process = Runtime.getRuntime().exec(externalArguments, 166 environmentVariables, new File (scriptPath)); 167 } catch (IOException e) { 168 log.error(e); 169 String argumentsString = CollectionHandling.convertToLines( 170 Arrays.asList(arguments), ','); 171 throw new ValidationException(new ValidationError( 172 "errors.admin.script", 173 Arrays.asList(new Object [] { 174 scriptName, 175 argumentsString, 176 "IOException: " 177 + e.getMessage() 178 }))); 179 } 180 181 if (process.waitFor() != 0) { 182 String errorText = extractText(process.getErrorStream()); 183 List lines = CollectionHandling.convertFromLines(errorText); 184 ValidationErrors errors = new ValidationErrors(); 185 Iterator linesIterator = lines.iterator(); 186 while (linesIterator.hasNext()) { 187 String line = (String ) linesIterator.next(); 188 if ((lines.size() == 1) 192 && "Password:".equals(line)) { 193 line += " (This looks like a user rights issue. Check " 194 + "visudo is installed properly and is set up for " 195 + "the user who is running the program. If you " 196 + "used the install script to install ivata " 197 + "groupware, change the value of USER_APP_SERVER " 198 + "at the start of the script and run " 199 + "setup.pl again.)"; 200 } 201 if ((line.indexOf("error.") != -1) 204 || (line.indexOf("errors.") != -1)) { 205 List errorArguments = new Vector (); 206 errorArguments.add(scriptName); 207 errorArguments.addAll(Arrays.asList(arguments)); 208 errors.add(new ValidationError( 209 line, 210 errorArguments)); 211 } else { 212 String argumentsString = CollectionHandling.convertToLines( 215 Arrays.asList(arguments), ','); 216 errors.add(new ValidationError( 217 "errors.admin.script", 218 Arrays.asList(new Object [] { 219 scriptName, 220 argumentsString, 221 line 222 }))); 223 } 224 } 225 throw new ValidationException(errors); 226 } 227 228 return extractText(process.getInputStream()); 229 } catch (IOException e) { 230 throw new SystemException("There was an input/output exception:", 231 e); 232 } catch (InterruptedException e) { 233 throw new SystemException("The script process was interrupted", 234 e); 235 } 236 } 237 238 246 private String extractText(final InputStream stream) throws IOException { 247 BufferedReader in = new BufferedReader (new InputStreamReader (stream)); 248 StringBuffer messageBuffer = new StringBuffer (); 249 char[] chbuf = new char[1024]; 250 int count; 251 252 while ((count = in.read(chbuf)) != -1) { 253 messageBuffer.append(chbuf, 0, count); 254 } 255 256 return messageBuffer.toString(); 257 } 258 } 259 | Popular Tags |