
core.os.ChrootProcSuccess Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2013 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.rio.core.os;
import static com.google.common.collect.Lists.newArrayList;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.io.CharStreams;
/**
* @author [email protected] (Marcio Endo)
*/
class ChrootProcSuccess extends ChrootProc {
private final Process process;
private final List exceptions = newArrayList();
public ChrootProcSuccess(Process process, List cmds) {
super(cmds);
this.process = process;
}
@Override
List getExceptions() {
return exceptions;
}
@Override
ChrootExec exec() {
AsyncStdout stdout = AsyncStdout.of(process);
stdout.start();
OutputStream os = process.getOutputStream();
try {
ChrootExec exec = tryToExec(os, cmds);
process.waitFor();
return exec;
} catch (IOException e) {
return new ChrootExecFailed(this, e);
} catch (InterruptedException e) {
return new ChrootExecFailed(this, e);
} finally {
stdout.stop();
try {
os.close();
} catch (IOException e) {
exceptions.add(e);
}
}
}
@Override
List stderr() {
InputStream is = process.getErrorStream();
return readStream(is);
}
@Override
List stdout() {
InputStream is = process.getInputStream();
return readStream(is);
}
@Override
void destroy() {
process.destroy();
}
private List readStream(InputStream is) {
try {
InputStreamReader reader = new InputStreamReader(is);
List log = CharStreams.readLines(reader);
return ImmutableList.copyOf(log);
} catch (IOException e) {
exceptions.add(e);
return ImmutableList.of();
} finally {
try {
is.close();
} catch (IOException e) {
exceptions.add(e);
}
}
}
private ChrootExec tryToExec(OutputStream os, List cmds) throws IOException {
OutputStreamWriter outWriter = new OutputStreamWriter(os, Charsets.UTF_8);
BufferedWriter writer = new BufferedWriter(outWriter);
for (String cmd : cmds) {
writer.write(cmd);
writer.newLine();
writer.flush();
}
return new ChrootExecSuccess(this);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy