PageRenderTime 226ms CodeModel.GetById 94ms app.highlight 16ms RepoModel.GetById 113ms app.codeStats 0ms

/src/mpv5/utils/files/FileExecutor.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 173 lines | 134 code | 17 blank | 22 comment | 27 complexity | 49c1255fe3329d0fa52395009e1c8361 MD5 | raw file
  1package mpv5.utils.files;
  2
  3import java.io.BufferedReader;
  4import java.io.File;
  5import java.io.IOException;
  6import java.io.InputStream;
  7import java.io.InputStreamReader;
  8import java.util.ArrayList;
  9import java.util.Arrays;
 10import java.util.List;
 11import java.util.Map;
 12import java.util.logging.Level;
 13import java.util.logging.Logger;
 14import java.util.regex.Matcher;
 15import java.util.regex.Pattern;
 16import mpv5.Main;
 17import mpv5.db.objects.User;
 18import mpv5.globals.LocalSettings;
 19import mpv5.logging.Log;
 20import mpv5.ui.dialogs.Notificator;
 21
 22/**
 23 *
 24 * @author andreas.weber
 25 */
 26public class FileExecutor {
 27
 28    /**
 29     * Runs a command,non- blocking, in the yabs home directory, with sudo if
 30     * root password is available and os is unix
 31     *
 32     * @param command
 33     */
 34    public static synchronized void run(String command) {
 35        if (Main.osIsWindows) {
 36            runWin(command);
 37        } else if (Main.osIsMacOsX || Main.osIsLinux || Main.osIsSolaris) {
 38            runUnixWPassword(command);
 39        }
 40    }
 41
 42    /**
 43     * Runs a command,non- blocking, in the yabs home directory, with sudo if
 44     * root password is available and os is unix
 45     *
 46     * @param command
 47     */
 48    public static synchronized void run(String[] commandArgs, List<Process> processHolder) {
 49        runAlternate(commandArgs, processHolder);
 50    }
 51
 52    /**
 53     * Runs a command,non- blocking, in the yabs home directory, with sudo if
 54     * root password is available and os is unix
 55     *
 56     * @param command
 57     */
 58    public static synchronized void run(String[] commandArgs) {
 59        runAlternate(commandArgs, new ArrayList<Process>());
 60    }
 61
 62    private static void runAlternate(String[] commandArrq, final List<Process> processHolder) {
 63
 64        final ProcessBuilder builder = new ProcessBuilder(commandArrq);
 65
 66        Map<String, String> environment = builder.environment();
 67        environment.put("path", ";"); // Clearing the path variable;
 68        environment.put("path", commandArrq[0] + File.pathSeparator);
 69        builder.directory(FileDirectoryHandler.getnewTemporaryDirectory());
 70
 71        Log.Debug(FileExecutor.class, "runAlternate" + Arrays.asList(commandArrq));
 72        Runnable runnable = new Runnable() {
 73            @Override
 74            public void run() {
 75                try {
 76                    Log.Print(builder.command());
 77                    Log.Print(builder.directory());
 78                    Log.Print(builder.environment());
 79                    Process oos = builder.start();
 80                    processHolder.add(oos);
 81                    Main.addProcessToClose(oos);
 82                    InputStream is = oos.getErrorStream();
 83                    InputStreamReader isr = new InputStreamReader(is);
 84                    BufferedReader br = new BufferedReader(isr);
 85                    String line;
 86
 87                    while ((line = br.readLine()) != null) {
 88                        mpv5.logging.Log.Print(line);
 89                    }
 90                } catch (IOException ex) {
 91                    Notificator.raiseNotification(ex, true);
 92                }
 93            }
 94        };
 95        new Thread(runnable).start();
 96    }
 97
 98    private static void runWin(String command) {
 99
100        String output = null;
101        File workDir = new File(Main.MPPATH);
102
103        try {
104            Process p = Runtime.getRuntime().exec(command, null, workDir);
105            Main.addProcessToClose(p);
106            int exitval = p.waitFor();
107            if (exitval == 0) {
108                BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
109                while ((output = stdInput.readLine()) != null) {
110                    Log.Print(output);
111                }
112            } else {
113                BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
114                while ((output = stdErr.readLine()) != null) {
115                    Log.Print(output);
116                }
117            }
118        } catch (Exception e) {
119            Log.Debug(e);
120        }
121    }
122
123    private static void runUnixWPassword(String command) {
124
125        String pw = null;
126        if (LocalSettings.hasProperty(LocalSettings.CMD_PASSWORD)
127                && LocalSettings.getProperty(LocalSettings.CMD_PASSWORD).length() > 0) {
128            pw = LocalSettings.getProperty("cmdpassword");
129        } else if (User.getCurrentUser().getProperties().hasProperty("cmdpassword")) {
130            pw = User.getCurrentUser().getProperties().getProperty("cmdpassword");
131        }
132        if (pw != null) {
133            String script = "#!/usr/bin/expect -d" + "\n"
134                    + "set password " + pw + "\n"
135                    + "set timeout -1" + "\n"
136                    + "spawn ssh root@localhost \"" + command + "\"" + "\n"
137                    + "match_max 100000" + "\n"
138                    + "expect \"*?assword:*\"" + "\n"
139                    + "send -- \"$password\\r\"" + "\n"
140                    + "send -- \"\\r\"" + "\n"
141                    + "expect eof";
142
143            File rw = FileDirectoryHandler.getTempFile("sh");
144            FileReaderWriter frw = new FileReaderWriter(rw);
145            frw.writeOnce(script);
146            rw.setExecutable(true);
147            command = rw.getPath();
148            Log.Debug(FileExecutor.class, "Running script: " + rw);
149        }
150
151        String output = null;
152        File workDir = new File(Main.MPPATH);
153        Log.Debug(FileExecutor.class, command);
154        try {
155            Process p = Runtime.getRuntime().exec(command, null, workDir);
156            Main.addProcessToClose(p);
157            int exitval = p.waitFor();
158            if (exitval == 0) {
159                BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
160                while ((output = stdInput.readLine()) != null) {
161                    Log.Print(output);
162                }
163            } else {
164                BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
165                while ((output = stdErr.readLine()) != null) {
166                    Log.Print(output);
167                }
168            }
169        } catch (Exception e) {
170            Log.Debug(e);
171        }
172    }
173}