ScriptInvoker.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.resources;

  21. import java.io.File;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.openspcoop2.utils.Utilities;
  25. import org.openspcoop2.utils.UtilsException;

  26. /**
  27. * ScriptInvoker
  28. *
  29. * @author Andrea Poli (apoli@link.it)
  30. * @author $Author$
  31. * @version $Rev$, $Date$
  32. */
  33. public class ScriptInvoker {

  34.     private String script = null;
  35.     private int exitValue = -1;
  36.     private String outputStream = null;
  37.     private String errorStream = null;

  38.     public ScriptInvoker(String script){
  39.         this.script = script;
  40.     }
  41.     public ScriptInvoker(){
  42.     }
  43.    
  44.     public void run() throws UtilsException{
  45.         this.run("");
  46.     }
  47.    

  48.     public void run(String ... parameters) throws UtilsException {
  49.         this.run(null, parameters);
  50.     }
  51.    
  52.    
  53.     public void run(File workingDir) throws UtilsException {
  54.         this.run((File)null, "");
  55.     }
  56.    
  57.     public void run(File workingDir, String ... parameters) throws UtilsException{
  58.        
  59.         try{
  60.        
  61.             if(this.script==null){
  62.                 throw new UtilsException("Script non fornito");
  63.             }
  64.            
  65.             java.lang.Runtime runtime = java.lang.Runtime.getRuntime();
  66.    
  67.             // Invoco lo script
  68.             List<String> script = new ArrayList<>();
  69.             script.add(this.script);
  70.             if(parameters!=null){
  71.                 for (int i = 0; i < parameters.length; i++) {
  72.                     script.add(parameters[i]);
  73.                 }
  74.             }
  75.    
  76.             java.lang.Process processStatus = null;
  77.             if (workingDir!=null) {
  78.                 processStatus = runtime.exec(script.toArray(new String[1]),null,workingDir);
  79.             } else {
  80.                 processStatus = runtime.exec(script.toArray(new String[1]));
  81.             }
  82.            
  83.             java.io.BufferedInputStream berror = new java.io.BufferedInputStream(processStatus.getErrorStream());
  84.             java.io.BufferedInputStream bin = new java.io.BufferedInputStream(processStatus.getInputStream());

  85.             boolean terminated = false;
  86.             while(terminated == false){
  87.                 try{
  88.                     Utilities.sleep(500);
  89.                     this.exitValue = processStatus.exitValue();
  90.                     terminated = true;
  91.                 }catch(java.lang.IllegalThreadStateException exit){}
  92.             }

  93.             StringBuilder stampa = new StringBuilder();
  94.             int read = 0;
  95.             while((read = bin.read())!=-1){
  96.                 stampa.append((char)read);
  97.             }
  98.             if(stampa.length()>0){
  99.                 this.outputStream = stampa.toString();
  100.             }
  101.                
  102.             StringBuilder stampaError = new StringBuilder();
  103.             read = 0;
  104.             while((read = berror.read())!=-1){
  105.                 stampaError.append((char)read);
  106.             }
  107.             if(stampaError.length()>0)
  108.                 this.errorStream = stampaError.toString();
  109.                        
  110.         }catch(Exception e){
  111.             throw new UtilsException(e.getMessage(), e);
  112.         }
  113.     }

  114.     public int getExitValue() {
  115.         return this.exitValue;
  116.     }

  117.     public void setExitValue(int exitValue) {
  118.         this.exitValue = exitValue;
  119.     }

  120.     public String getOutputStream() {
  121.         return this.outputStream;
  122.     }

  123.     public void setOutputStream(String outputStream) {
  124.         this.outputStream = outputStream;
  125.     }

  126.     public String getErrorStream() {
  127.         return this.errorStream;
  128.     }

  129.     public void setErrorStream(String errorStream) {
  130.         this.errorStream = errorStream;
  131.     }

  132.     public String getScript() {
  133.         return this.script;
  134.     }

  135.     public void setScript(String script) {
  136.         this.script = script;
  137.     }
  138.    
  139. }