Quellcode durchsuchen

changes to build.gradle to add new library & Fix to Tools Execute

LH vor 10 Jahren
Ursprung
Commit
7875c602ed

+ 2 - 0
ToolKit/build.gradle

@@ -15,7 +15,9 @@ distZip.outputs.file file(tasks.jar.archivePath)
 
 
 dependencies{
+	
 	compile fileTree(dir: 'lib', include: '*.jar')
+	compile "net.rubygrapefruit:native-platform:0.10"
 	compile "commons-io:commons-io:2.+"
 	compile 'org.apache.commons:commons-exec:1.3'
 	compile "org.yaml:snakeyaml:1.14"

+ 235 - 0
ToolKit/src/main/java/de/nplusc/izc/tools/IOtools/PrintStreamCapturer.java

@@ -0,0 +1,235 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package de.nplusc.izc.tools.IOtools;
+
+import java.io.PrintStream;
+import javax.swing.JTextArea;
+
+/**
+ *
+ * @author iZc <nplusc.de>
+ */
+public class PrintStreamCapturer extends PrintStream {
+
+    private JTextArea text;
+    private boolean atLineStart;
+    private String indent;
+
+    public PrintStreamCapturer(JTextArea textArea, PrintStream capturedStream, String indent) {
+        super(capturedStream);
+        this.text = textArea;
+        this.indent = indent;
+        this.atLineStart = true;
+    }
+
+    public PrintStreamCapturer(JTextArea textArea, PrintStream capturedStream) {
+        this(textArea, capturedStream, "");
+    }
+
+    private void writeToTextArea(String str) {
+        if (text != null) {
+            synchronized (text) {
+                text.setCaretPosition(text.getDocument().getLength());
+                text.append(str);
+            }
+        }
+    }
+
+    private void write(String str) {
+        String[] s = str.split("\n", -1);
+        if (s.length == 0)
+            return;
+        for (int i = 0; i < s.length - 1; i++) {
+            writeWithPotentialIndent(s[i]);
+            writeWithPotentialIndent("\n");
+            atLineStart = true;
+        }
+        String last = s[s.length - 1];
+        if (!last.equals("")) {
+            writeWithPotentialIndent(last);
+        }
+    }
+
+    private void writeWithPotentialIndent(String s) {
+        if (atLineStart) {
+            writeToTextArea(indent + s);
+            atLineStart = false;
+        } else {
+            writeToTextArea(s);
+        }
+    }
+
+    private void newLine() {
+        write("\n");
+    }
+
+    @Override
+    public void print(boolean b) {
+        synchronized (this) {
+            super.print(b);
+            write(String.valueOf(b));
+        }
+    }
+
+    @Override
+    public void print(char c) {
+        synchronized (this) {
+            super.print(c);
+            write(String.valueOf(c));
+        }
+    }
+
+    @Override
+    public void print(char[] s) {
+        synchronized (this) {
+            super.print(s);
+            write(String.valueOf(s));
+        }
+    }
+
+    @Override
+    public void print(double d) {
+        synchronized (this) {
+            super.print(d);
+            write(String.valueOf(d));
+        }
+    }
+
+    @Override
+    public void print(float f) {
+        synchronized (this) {
+            super.print(f);
+            write(String.valueOf(f));
+        }
+    }
+
+    @Override
+    public void print(int i) {
+        synchronized (this) {
+            super.print(i);
+            write(String.valueOf(i));
+        }
+    }
+
+    @Override
+    public void print(long l) {
+        synchronized (this) {
+            super.print(l);
+            write(String.valueOf(l));
+        }
+    }
+
+    @Override
+    public void print(Object o) {
+        synchronized (this) {
+            super.print(o);
+            write(String.valueOf(o));
+        }
+    }
+
+    @Override
+    public void print(String s) {
+        synchronized (this) {
+            super.print(s);
+            if (s == null) {
+                write("null");
+            } else {
+                write(s);
+            }
+        }
+    }
+
+    @Override
+    public void println() {
+        synchronized (this) {
+            newLine();
+            super.println();
+        }
+    }
+
+    @Override
+    public void println(boolean x) {
+        synchronized (this) {
+            print(x);
+            newLine();
+            super.println();
+        }
+    }
+
+    @Override
+    public void println(char x) {
+        synchronized (this) {
+            print(x);
+            newLine();
+            super.println();
+        }
+    }
+
+    @Override
+    public void println(int x) {
+        synchronized (this) {
+            print(x);
+            newLine();
+            super.println();
+        }
+    }
+
+    @Override
+    public void println(long x) {
+        synchronized (this) {
+            print(x);
+            newLine();
+            super.println();
+        }
+    }
+
+    @Override
+    public void println(float x) {
+        synchronized (this) {
+            print(x);
+            newLine();
+            super.println();
+        }
+    }
+
+    @Override
+    public void println(double x) {
+        synchronized (this) {
+            print(x);
+            newLine();
+            super.println();
+        }
+    }
+
+    @Override
+    public void println(char x[]) {
+        synchronized (this) {
+            print(x);
+            newLine();
+            super.println();
+        }
+    }
+
+    @Override
+    public void println(String x) {
+        synchronized (this) {
+            print(x);
+            newLine();
+            super.println();
+        }
+    }
+
+    @Override
+    public void println(Object x) {
+        String s = String.valueOf(x);
+        synchronized (this) {
+            print(s);
+            newLine();
+            super.println();
+        }
+    }
+}

+ 210 - 243
ToolKit/src/main/java/de/nplusc/izc/tools/baseTools/Tools.java

@@ -7,6 +7,7 @@ package de.nplusc.izc.tools.baseTools;
 //import java.util.logging.Logger;
 //import org.apache.commons.io.*;
 import de.nplusc.izc.tools.IOtools.FileTK;
+import de.nplusc.izc.tools.IOtools.PrintStreamCapturer;
 import java.awt.Dimension;
 import java.awt.GraphicsEnvironment;
 import java.awt.HeadlessException;
@@ -19,6 +20,8 @@ import java.util.Map;
 import java.util.Scanner;
 import java.util.prefs.Preferences;
 import javax.swing.*;
+import net.rubygrapefruit.platform.Native;
+import net.rubygrapefruit.platform.ProcessLauncher;
 import org.apache.commons.exec.CommandLine;
 import org.apache.commons.exec.DefaultExecuteResultHandler;
 import org.apache.commons.exec.DefaultExecutor;
@@ -1693,67 +1696,51 @@ public class Tools
     
     private static boolean sil = false;
     
-    public static void runCmdWithOutToTextField(JTextArea txf, String... cmd)
+    public static boolean runCmdWithOutToTextField(JTextArea txf, String... cmd)
     {
-        runCmdStreamable(txf,new PrintStream(new NullOutputStream()),false, cmd);  
+        return runCmdStreamable(txf,new PrintStream(new NullOutputStream()),false, cmd);  
     }
     
-    public static void runCmdWithPassthru(PrintStream p,String... cmd)
+    public static boolean runCmdWithPassthru(PrintStream p,String... cmd)
     {
-        runCmdStreamable(new JTextArea(),p,false, cmd);
+        return runCmdStreamable(new JTextArea(),p,false, cmd);
     }
     
-    private static ExecuteStreamHandler s;
-    private static JTextArea txf;
-    private static PrintStream otherOut;
-    public static void runCmdStreamable(JTextArea txf2,PrintStream otherOut2,final boolean SpamException, String... cmd)//synchronized gint nen fetten Bug
+    //private static ExecuteStreamHandler s;
+    //private static JTextArea txf;
+    //private static PrintStream otherOut;
+    public static boolean runCmdStreamable(JTextArea txf,PrintStream otherOut,final boolean SpamException, String... cmd)//synchronized gint nen fetten Bug
     {
-        txf=txf2;
-        Tools.DebugHelperPrint(Arrays.toString(cmd), true, "Toolkit.enableFinerDebug");
-        // BufferedInputStream log = new BufferedInputStream
-        // (
-        //     new ByteArrayInputStream(new byte[]{0})
-        // );//das einzelne Byte ist n(ö|ä)tig um ne NPX zu verhindern
-
-        if(txf==null)
-        {
-            txf=new JTextArea();
-        }
-        
-        if(otherOut==null)
-        {
-            otherOut=new PrintStream(new NullOutputStream());
-        }
-        
-        
-        final DefaultExecuteResultHandler devnull = new DefaultExecuteResultHandler();
-        /* {
-
-         @Override
-         public void onProcessComplete(int exitValue)
-         {
-         System.err.println("fin");
-         cmdGUI.sil=false;
-         cmdGUI.cgui.setIsInCmd(false);
-         }
-
-         @Override
-         public void onProcessFailed(ExecuteException e)
-         {
-         System.err.println("Urrgs");
-         cmdGUI.sil=false;
-         cmdGUI.cgui.setIsInCmd(false);
-         }
-         };*/
-        CommandLine cl = CommandLine.parse(cmd[0]);
-        for (int i = 1; i < cmd.length; i++)
-        {
+        try //synchronized gint nen fetten Bug
+        {
+            //txf=txf2;
+            Tools.DebugHelperPrint(Arrays.toString(cmd), true, "Toolkit.enableFinerDebug");
+            // BufferedInputStream log = new BufferedInputStream
+            // (
+            //     new ByteArrayInputStream(new byte[]{0})
+            // );//das einzelne Byte ist n(ö|ä)tig um ne NPX zu verhindern
+            
+            if(txf==null)
+            {
+                txf=new JTextArea();
+            }
+            
+            if(otherOut==null)
+            {
+                otherOut=new PrintStream(new NullOutputStream());
+            }
+            // <editor-fold defaultstate="collapsed" desc="Altlast">
+            /*
+            final DefaultExecuteResultHandler devnull = new DefaultExecuteResultHandler();
+            CommandLine cl = CommandLine.parse(cmd[0]);
+            for (int i = 1; i < cmd.length; i++)
+            {
             cl.addArgument(cmd[i]);
-        }
-        Executor e = new DefaultExecutor();
-        //ex=(DefaultExecutor)e;
-        try
-        {
+            }
+            Executor e = new DefaultExecutor();
+            //ex=(DefaultExecutor)e;
+            try
+            {
             final PipedInputStream in = new PipedInputStream();
             PipedOutputStream out = new PipedOutputStream();
             final PipedOutputStream txOut = new PipedOutputStream();
@@ -1761,219 +1748,159 @@ public class Tools
 
             txIn.connect(txOut);
             out.connect(in);
-            // txOut.write((byte)0);//??//
-            //System.err.pri
             s = new PumpStreamHandler(out, out, txIn);
-            //final OutputStream txOut = ((DefaultExecutor)e).stdin;
-            // i.
-
-            //s.setProcessOutputStream(log);
             Tools.DebugHelperPrint("???", true, "Toolkit.enableFinerDebug");
             e.setStreamHandler(s);
-
             //^HAck da sonst zwodeitich//VOID
             Tools.DebugHelperPrint("Wennhier was ist...", true, "Toolkit.enableFinerDebug");
-
-            //log.
-
-
-            //cG.
             sil = true;;
-            /*new Thread(new Runnable()
-             {
-             @Override
-             @SuppressWarnings("SleepWhileInLoop")
-             public void run()
-             {
-             // while(!devnull.hasResult())
-             // {
-             try
-             {
-             ex.p.waitFor();
-             Thread.sleep(50);
-             //System.err.println("AA");
-             }
-             catch (InterruptedException ex)
-             {
-             ex.printStackTrace();
-             }
-                              
-             //}
-             System.err.println("fin");
-             cmdGUI.sil=false;
-             cmdGUI.cgui.setIsInCmd(false);
-             System.err.println("Wr");
-             System.setProperty("fin", "true");
-             }
-             }).start();*/
-            new Thread(new Runnable()
+            new Thread(() ->
             {
-                //BufferedOutputStream txOut=(BufferedOutputStream)((DefaultExecutor)ex).getStdin();
-                @Override
-                @SuppressWarnings(
-                {
-                    "SleepWhileInLoop", "SleepWhileInLoop"
-                })
-                public void run()
-                {
-                    while (sil)
-                    {
-                        // try
-                        // {
-                        //txOut.write("x\b".getBytes());
-                        //txOut.flush();
-                        String chk = " ";
-                        //   System.err.println(chk);
-                        Tools.DebugHelperPrint(chk, true, "ToolKit.enableStreamDataDebug");
-                        if (chk == null)
-                        {
-                            chk = "";
-                        }
-                        //if((!System.getProperty("fin", "lm").equals("lm")))
-                        //{
-                        //       System.clearProperty("fin");
-                        //       chk="\r";
-                        //}
-                        if (!chk.equals(""))
-                        {
-
-                            try
-                            {
-                                chk += "\n";
-                                //o=txOut
-                                txOut.write(chk.getBytes());
-                                txOut.flush();
-                                //System.err.println(">>"+chk);
-                                Tools.DebugHelperPrint(">>" + chk, true, "ToolKit.enableStreamDataDebug");
-                                //in.mark(1);
-                                //in.reset();Funzen nicht;
-                            }
-                            catch (IOException ex)
-                            {
-                                if(SpamException)
-                                    ex.printStackTrace();
-                                if(ex.getMessage().contains("dead"))
-                                {
-                                    sil=false;
-									try
-									{
-									s.stop();
-									}
-									catch(IOException ex2)
-									{
-										ex2.printStackTrace();
-									}
-                                }
-                            }
-                            //cG.setCmdForSend(null,(int)t.get(0));//Nicht vergessen.sonst Bugschleuder
-                            chk = null;
-                            
-                        }
-                        Tools.wait(50);
-
-                        //  }
-                        //catch (IOException ex)
-                        // {
-                        //    System.err.println("Kakk");
-                        //     ex.printStackTrace();
-                        // }
-
-                    }
-                }
+            while (sil)
+            {
+            String chk = " ";
+            Tools.DebugHelperPrint(chk, true, "ToolKit.enableStreamDataDebug");
+            if (!chk.equals(""))
+            {
+            
+            try
+            {
+            chk += "\n";
+            //o=txOut
+            txOut.write(chk.getBytes());
+            txOut.flush();
+            Tools.DebugHelperPrint(">>" + chk, true, "ToolKit.enableStreamDataDebug");
+            }
+            catch (IOException ex)
+            {
+            if(SpamException)
+            ex.printStackTrace();
+            if(ex.getMessage().contains("dead"))
+            {
+            sil=false;
+            try
+            {
+            s.stop();
+            }
+            catch(IOException ex2)
+            {
+            ex2.printStackTrace();
+            }
+            }
+            }
+            chk = null;
+            }
+            Tools.wait(50);
+            }
             }).start();
-            new Thread(new Runnable()
+            new Thread(() ->
             {
-                @Override
-                public void run()
-                {
-                    try
-                    {
-                        BufferedReader br = new BufferedReader(new InputStreamReader(in));
-                        //String s2 = br.readLine();
-                        //if(in.)
-                        while (sil)
-                        {
-                            while (in.available() > 0)//||(cmdGUI.sil))
-                            {
-                                byte rb = (byte) in.read();
-                                String ln = "" + (char) rb;
-                                //in.
-                                rb = (byte) in.read();
-                                Tools.DebugHelperPrint((char) rb + "|" + 0, true, "ToolKit.enableStreamDataDebug");
-                                int i = 0;
-                                while (((char) rb != '\n'))//&&((char)rb!='>'))
-                                {
-                                    ln += (char) rb;
-                                    //   System.err.println((char)rb+"|"+i+1);
-                                    Tools.DebugHelperPrint((char) rb + "|" + i + 1, true, "ToolKit.enableStreamDataDebug");
-
-                                    rb = (byte) in.read();//DONOTFORGET!!!!!
-                                    //   System.err.println((char)rb+"|"+i+2);
-                                    Tools.DebugHelperPrint((char) rb + "|" + i + 2, true, "ToolKit.enableStreamDataDebug");
-
-                                    i++;
-                                }
-                                /*if((char)rb=='>')
-                                 {
-                                 ln+=">";
-                                 }*/
-                                txf.append(ln + "\n");
-                                otherOut.println(ln);
-                                Tools.DebugHelperPrint(ln, true, "ToolKit.enableStreamDataDebug");
-
-                                //s2 = br.readLine();
-                                Tools.DebugHelperPrint("Fotze", true,"ToolKit.enableStreamDataDebug");
-
-                            }
-                            Tools.wait(50);
-                        }
-                        Tools.DebugHelperPrint("Hira?", true, "Toolkit.enableFinerDebug");
-
-                    }
-                    catch (IOException ex)
-                    {
-                        if(ex.getMessage().contains("dead"))
-                        {
-                            sil=false;
-							try
-							{
-							s.stop();
-							}
-							catch(IOException ex2)
-							{
-								ex2.printStackTrace();
-							}
-                            
-                        }
-                        ex.printStackTrace();
-                    }
-                }
+            try
+            {
+            BufferedReader br = new BufferedReader(new InputStreamReader(in));
+            while (sil)
+            {
+            while (in.available() > 0)//||(cmdGUI.sil))
+            {
+            byte rb = (byte) in.read();
+            String ln = "" + (char) rb;
+            //in.
+            rb = (byte) in.read();
+            Tools.DebugHelperPrint((char) rb + "|" + 0, true, "ToolKit.enableStreamDataDebug");
+            int i = 0;
+            while (((char) rb != '\n'))//&&((char)rb!='>'))
+            {
+            ln += (char) rb;
+            //   System.err.println((char)rb+"|"+i+1);
+            Tools.DebugHelperPrint((char) rb + "|" + i + 1, true, "ToolKit.enableStreamDataDebug");
+            
+            rb = (byte) in.read();//DONOTFORGET!!!!!
+            //   System.err.println((char)rb+"|"+i+2);
+            Tools.DebugHelperPrint((char) rb + "|" + i + 2, true, "ToolKit.enableStreamDataDebug");
+            
+            i++;
+            }
+            /*if((char)rb=='>')
+            {
+            ln+=">";
+            }* /
+            txf.append(ln + "\n");
+            otherOut.println(ln);
+            Tools.DebugHelperPrint(ln, true, "ToolKit.enableStreamDataDebug");
+            
+            //s2 = br.readLine();
+            Tools.DebugHelperPrint("Fotze", true,"ToolKit.enableStreamDataDebug");
+            
+            }
+            Tools.wait(50);
+            }
+            Tools.DebugHelperPrint("Hira?", true, "Toolkit.enableFinerDebug");
+            
+            }
+            catch (IOException ex)
+            {
+            if(ex.getMessage().contains("dead"))
+            {
+            sil=false;
+            try
+            {
+            s.stop();
+            }
+            catch(IOException ex2)
+            {
+            ex2.printStackTrace();
+            }
+            }
+            ex.printStackTrace();
+            }
             }).start();
             try
             {
-                e.execute(cl/*,devnull*/);//HACKALERT
+            e.execute(cl//,devnull
+            );//HACKALERT
             }
             catch (org.apache.commons.exec.ExecuteException exc)
             {
-                //Do nothing. Cmd steigt ja bei Error auch nicht aus
-                //exc.printStackTrace();
+            //Do nothing. Cmd steigt ja bei Error auch nicht aus
+            //exc.printStackTrace();
             }
             Tools.DebugHelperPrint("fin", true, "Toolkit.enableFinerDebug");
 
             sil = false;
-
-        }
-        catch (ExecuteException exc)
-        {
+            
+            }
+            catch (ExecuteException exc)
+            {
             exc.printStackTrace();
-        }
-        catch (IOException exc)
-        {
+            }
+            catch (IOException exc)
+            {
             if(SpamException)
             exc.printStackTrace();
+            }
+            */
+            //</editor-fold>
+            
+            System.out.println("OtherOut="+otherOut);
+            
+            ProcessBuilder pb = new ProcessBuilder(Arrays.asList(cmd));
+            pb.redirectErrorStream(true);
+            ProcessLauncher l = Native.get(ProcessLauncher.class);
+            Process process = l.start(pb);
+            PrintStream stdout = new PrintStreamCapturer(txf, otherOut);
+            Thread stdoutThread = new Thread(new TextDumper(process.getInputStream(), stdout));
+            stdoutThread.start();
+            int result = process.waitFor();
+            stdoutThread.join();
+            Tools.DebugHelperPrint("FIN", true, "Toolkit.enableFinerDebug");
+            return result!=0;
+            //sil=false;
+        }
+        catch (InterruptedException ex)
+        {
+            ex.printStackTrace();
         }
-        Tools.DebugHelperPrint("FIN", true, "Toolkit.enableFinerDebug");
-        //sil=false;
+        return true;
     }
 
     
@@ -2035,6 +1962,46 @@ public class Tools
             /* ... */ 
         }
     }
+    
+    
+    
+    
+    
+    
+    
+    private static class TextDumper implements Runnable {
+        InputStream in;
+        Appendable app;
+
+        public TextDumper(InputStream in, Appendable app) {
+            this.in = in;
+            this.app = app;
+        }
+
+        public void run() {
+            InputStreamReader isr = new InputStreamReader(in);
+            BufferedReader br = new BufferedReader(isr);
+            String next;
+            try {
+                while ((next = br.readLine()) != null) {
+                    if (app != null) {
+                        app.append(next);
+                        app.append("\n");
+                    }
+                }
+            } catch (IOException e) {
+                throw new RuntimeException("exception while reading process stream", e);
+            }
+        }
+    }
+    
+    
+    
+    
+    
+    
+    
+    
     private static int numEntered = 0, numMax = 0;
     private static javax.swing.JButton btnAbbr;
     private static javax.swing.JButton btnClear;