001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.framework.commandexecutors.shell;
021    
022    import java.io.BufferedInputStream;
023    import java.io.BufferedReader;
024    import java.io.IOException;
025    import java.io.InputStreamReader;
026    
027    import org.mactor.framework.MactorException;
028    import org.mactor.framework.spec.ProjectContext;
029    
030    public class ExecUtil {
031            public static void executeCommand(String command) throws MactorException {
032                    try {
033                            Runtime r = Runtime.getRuntime();
034                            Process p = r.exec(command, null, ProjectContext.getGlobalInstance().getProjectDir());
035                            int exitCode = p.waitFor();
036                            if (exitCode != 0) {
037                                    throw new MactorException("Command '" + command + "' failed. Exit code: '" + exitCode + "'");
038                            }
039                    } catch (IOException ioe) {
040                            throw new MactorException("Failed to execute command '" + command + "'. Error:" + ioe.getMessage(), ioe);
041                    } catch (InterruptedException ie) {
042                            throw new MactorException("Failed to execute command '" + command + "'. Error:" + ie.getMessage(), ie);
043                    }
044            }
045            public static String executeCommandWithOutput(String command) throws MactorException {
046                    StringBuffer sb = new StringBuffer();
047                    try {
048                            Runtime r = Runtime.getRuntime();
049                            Process p = r.exec(command, null, ProjectContext.getGlobalInstance().getProjectDir());
050                            BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
051                            BufferedReader br = new BufferedReader(new InputStreamReader(bis));
052                            char[] buffer = new char[1000];
053                            for (;;) {
054                                    int count = br.read(buffer, 0, buffer.length);
055                                    if (count > 0) {
056                                            sb.append(buffer, 0, count);
057                                    } else {
058                                            break;
059                                    }
060                            }
061                            int exitCode = p.waitFor();
062                            if (exitCode != 0) {
063                                    throw new MactorException("Command '" + command + "' failed. Exit code: '" + exitCode + "'");
064                            }
065                            return sb.toString();
066                    } catch (IOException ioe) {
067                            throw new MactorException("Failed to execute command '" + command + "'. Error:" + ioe.getMessage(), ioe);
068                    } catch (InterruptedException ie) {
069                            throw new MactorException("Failed to execute command '" + command + "'. Error:" + ie.getMessage(), ie);
070                    }
071            }
072    }