001: /*
002: * MCS Media Computer Software Copyright (c) 2005 by MCS
003: * -------------------------------------- Created on 16.01.2004 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package de.mcs.utils;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: /**
025: * Starting a process like java.
026: *
027: * @author w.klaas
028: *
029: */
030: public final class StartProcess {
031:
032: /** prevent insancing. */
033: private StartProcess() {
034:
035: }
036:
037: /**
038: * Starting java with a command list and optionally wait for program ending.
039: *
040: * @param command
041: * list of all needed parameters
042: * @param wait
043: * wait for program ending
044: * @param workingDir
045: * where is the working dir for this vm.
046: * @return the exit code of the process
047: */
048: public static int startJava(final List<String> command,
049: final boolean wait, final String workingDir) {
050: Runtime rt = Runtime.getRuntime();
051: File workingDirFile = new File(workingDir);
052: int result = 0;
053: // prepare buffers for process output and error streams
054: // StringBuffer err = new StringBuffer();
055: // StringBuffer out = new StringBuffer();
056:
057: try {
058: Process p = rt.exec((String[]) command
059: .toArray(new String[command.size()]), null,
060: workingDirFile);
061: // Process p = rt.exec((String[]) command.toArray(new String[0]),
062: // null, workingDir);
063: // create thread for reading inputStream (process' stdout)
064: StreamReaderThread outThread = new StreamReaderThread(p
065: .getInputStream(), System.out);
066: // create thread for reading errorStream (process' stderr)
067: StreamReaderThread errThread = new StreamReaderThread(p
068: .getErrorStream(), System.err);
069: // start both threads
070: outThread.start();
071: errThread.start();
072: // wait for process to end
073: if (wait) {
074: result = p.waitFor();
075: // finish reading whatever's left in the buffers
076: outThread.join();
077: errThread.join();
078:
079: if (result != 0) {
080: System.err.println("Process " + command.get(0)
081: + " returned non-zero value:" + result);
082: } else {
083: System.out.println("Process " + command.get(0)
084: + " executed successfully");
085: }
086: }
087: } catch (IOException e) {
088: e.printStackTrace();
089: // } catch (InterruptedException e) {
090: // e.printStackTrace();
091: } catch (InterruptedException e) {
092: e.printStackTrace();
093: }
094: return result;
095: }
096:
097: /**
098: * chaging file permissions.
099: *
100: * @param file
101: * which file to change.
102: * @return result
103: */
104: public static int changeFilePermissions(final File file) {
105: int result = 0;
106: if (OSInformations.isMACOSX()) {
107: List<String> command = new ArrayList<String>();
108: command.add("chmod");
109: command.add("755");
110: command.add(file.getAbsolutePath());
111: result = startJava(command, true, file.getParent());
112: // do nothing for other systems at the moment
113: }
114: return result;
115: }
116: }
|