← Back to team overview

slub.team team mailing list archive

[Merge] lp:~henning-gerhardt/goobi-production/bug-816349 into lp:goobi-production

 

Henning Gerhardt has proposed merging lp:~henning-gerhardt/goobi-production/bug-816349 into lp:goobi-production.

Requested reviews:
  Ralf Claussnitzer (ralf-claussnitzer)
Related bugs:
  Bug #816349 in Goobi.Production: "crash with too many open pipes error when removing process "
  https://bugs.launchpad.net/goobi-production/+bug/816349

For more details, see:
https://code.launchpad.net/~henning-gerhardt/goobi-production/bug-816349/+merge/94550
-- 
https://code.launchpad.net/~henning-gerhardt/goobi-production/bug-816349/+merge/94550
Your team Saxon State Library Team is subscribed to branch lp:goobi-production.
=== modified file 'src/de/sub/goobi/helper/Helper.java'
--- src/de/sub/goobi/helper/Helper.java	2012-02-22 07:43:02 +0000
+++ src/de/sub/goobi/helper/Helper.java	2012-02-24 14:29:02 +0000
@@ -22,26 +22,11 @@
 
 package de.sub.goobi.helper;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.FilenameFilter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.PrintWriter;
-import java.io.Serializable;
-import java.io.StringWriter;
-import java.net.MalformedURLException;
+import java.io.*;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.text.DateFormat;
-import java.util.Date;
-import java.util.Map;
-import java.util.Observable;
-import java.util.Observer;
-import java.util.ResourceBundle;
-import java.util.Scanner;
+import java.util.*;
 
 import javax.faces.application.FacesMessage;
 import javax.faces.context.FacesContext;
@@ -246,10 +231,19 @@
 	// TODO: Don't use this to create /pages/imagesTemp/
 	public static void callShell(String command) throws IOException, InterruptedException {
 		myLogger.debug("execute Shellcommand callShell: " + command);
-		// TODO: Use a ProcessBuilder
-		Process p = Runtime.getRuntime().exec(command);
-		p.waitFor();
-
+
+		if (isEmptyCommand(command)) {
+			return;
+		}
+
+		Process process = null;
+		try {
+			String[] commandToken = command.split("\\s");
+			process = new ProcessBuilder(commandToken).start();
+			process.waitFor();
+		} finally {
+			closeProcessStreams(process);
+		}
 	}
 
 	/**
@@ -259,29 +253,73 @@
 	public static Integer callShell2(String command) throws IOException, InterruptedException {
 		myLogger.debug("execute Shellcommand callShell2: " + command);
 		boolean errorsExist = false;
-		if (command == null || command.length() == 0) {
-			return 1;
-		}
-		// TODO: Use a process builder
-		Process process = Runtime.getRuntime().exec(command);
-		Scanner scanner = new Scanner(process.getInputStream());
-		while (scanner.hasNextLine()) {
-			String myLine = scanner.nextLine();
-			setMeldung(myLine);
-		}
-
-		scanner.close();
-		scanner = new Scanner(process.getErrorStream());
-		while (scanner.hasNextLine()) {
-			errorsExist = true;
-			setFehlerMeldung(scanner.nextLine());
-		}
-		scanner.close();
-		int rueckgabe = process.waitFor();
-		if (errorsExist) {
-			return 1;
-		} else {
-			return rueckgabe;
+		if (isEmptyCommand(command)) {
+			return 1;
+		}
+
+		Process process = null;
+		Scanner scanner = null;
+
+		try {
+			String[] commandToken = command.split("\\s");
+			process = new ProcessBuilder(commandToken).start();
+
+			scanner = new Scanner(process.getInputStream());
+			while (scanner.hasNextLine()) {
+				String myLine = scanner.nextLine();
+				setMeldung(myLine);
+			}
+			scanner.close();
+
+			scanner = new Scanner(process.getErrorStream());
+			while (scanner.hasNextLine()) {
+				errorsExist = true;
+				setFehlerMeldung(scanner.nextLine());
+			}
+			scanner.close();
+
+			int rueckgabe = process.waitFor();
+
+			if (errorsExist) {
+				return 1;
+			} else {
+				return rueckgabe;
+			}
+
+		} finally {
+			closeProcessStreams(process);
+
+			// HINT: Scanner implements Closeable on Java 1.7
+			if (scanner != null) {
+				scanner.close();
+			}
+		}
+	}
+
+	private static boolean isEmptyCommand(String command) {
+		return (command == null) || (command.length() == 0);
+	}
+	
+	public static void closeProcessStreams(Process process) {
+		if (process == null) {
+			return;
+		}
+
+		closeFile(process.getInputStream());
+		closeFile(process.getOutputStream());
+		closeFile(process.getErrorStream());
+	}
+
+	public static void closeFile (Closeable openFile) {
+		if (openFile == null) {
+			return;
+		}
+
+		try {
+			openFile.close();
+		} catch (IOException e) {
+			myLogger.warn("Could not close file.", e);
+			Helper.setFehlerMeldung("Could not close open file.");
 		}
 	}
 

=== modified file 'src/de/sub/goobi/helper/WebDav.java'
--- src/de/sub/goobi/helper/WebDav.java	2012-02-22 11:26:21 +0000
+++ src/de/sub/goobi/helper/WebDav.java	2012-02-24 14:29:02 +0000
@@ -23,11 +23,7 @@
 package de.sub.goobi.helper;
 
 //TODO: Replace with a VFS
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FilenameFilter;
-import java.io.OutputStreamWriter;
+import java.io.*;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
@@ -101,20 +97,22 @@
 			VerzeichnisAlle = aktuellerBenutzer.getHomeDir() + inVerzeichnis;
 		} catch (Exception ioe) {
 			myLogger.error("Exception RemoveFromHomeAlle()", ioe);
-			Helper.setFehlerMeldung("Upload stoped, error", ioe.getMessage());
+			Helper.setFehlerMeldung("Upload stopped, error", ioe.getMessage());
 			return;
 		}
 
-		for (Iterator<String> it = inList.iterator(); it.hasNext();) {
-			String myname = (String) it.next();
+		for (String myname : inList) {
 			String command = ConfigMain.getParameter("script_deleteSymLink") + " ";
 			command += VerzeichnisAlle + myname;
+
 			try {
-				Runtime.getRuntime().exec(command);
+				Helper.callShell(command);
 			} catch (java.io.IOException ioe) {
-				myLogger.error("IOException UploadFromHomeAlle()", ioe);
-				Helper.setFehlerMeldung("Aborted upload from home, error", ioe.getMessage());
-				return;
+				myLogger.error("IOException removeFromHomeAlle()", ioe);
+				Helper.setFehlerMeldung("Aborted removeFromHomeAlle(), error", ioe.getMessage());
+			} catch (InterruptedException ie) {
+				myLogger.error("InterruptedException in removeFromHomeAlle()", ie);
+				Helper.setFehlerMeldung("Command '" + command + "' is interrupted in removeFromHomeAlle()!");
 			}
 		}
 	}
@@ -148,11 +146,13 @@
 		command += benutzerHome;
 
 		try {
-			// TODO: Use ProcessBuilder
-			Runtime.getRuntime().exec(command);
+			Helper.callShell(command);
 		} catch (java.io.IOException ioe) {
 			myLogger.error("IOException UploadFromHome", ioe);
 			Helper.setFehlerMeldung("Aborted upload from home, error", ioe.getMessage());
+		} catch (InterruptedException ie) {
+			myLogger.error("InterruptedException UploadFromHome", ie);
+			Helper.setFehlerMeldung("Command '" + command + "' is interrupted in UploadFromHome()!");
 		}
 	}
 
@@ -213,8 +213,6 @@
 		else
 			command += aktuellerBenutzer.getLogin();
 		try {
-			// Runtime.getRuntime().exec(command);
-
 			Helper.callShell2(command);
 		} catch (java.io.IOException ioe) {
 			myLogger.error("IOException DownloadToHome()", ioe);


Follow ups