use of org.apache.commons.io.output.ByteArrayOutputStream in project zeppelin by apache.
the class PigInterpreter method interpret.
@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
// remember the origial stdout, because we will redirect stdout to capture
// the pig dump output.
PrintStream originalStdOut = System.out;
ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream();
File tmpFile = null;
try {
pigServer.setJobName(createJobName(cmd, contextInterpreter));
tmpFile = PigUtils.createTempPigScript(cmd);
System.setOut(new PrintStream(bytesOutput));
// each thread should its own ScriptState & PigStats
ScriptState.start(pigServer.getPigContext().getExecutionEngine().instantiateScriptState());
// reset PigStats, otherwise you may get the PigStats of last job in the same thread
// because PigStats is ThreadLocal variable
PigStats.start(pigServer.getPigContext().getExecutionEngine().instantiatePigStats());
PigScriptListener scriptListener = new PigScriptListener();
ScriptState.get().registerListener(scriptListener);
listenerMap.put(contextInterpreter.getParagraphId(), scriptListener);
pigServer.registerScript(tmpFile.getAbsolutePath());
} catch (IOException e) {
if (e instanceof FrontendException) {
FrontendException fe = (FrontendException) e;
if (!fe.getMessage().contains("Backend error :")) {
// If the error message contains "Backend error :", that means the exception is from
// backend.
LOGGER.error("Fail to run pig script.", e);
return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
}
}
PigStats stats = PigStats.get();
if (stats != null) {
String errorMsg = PigUtils.extactJobStats(stats);
if (errorMsg != null) {
LOGGER.error("Fail to run pig script, " + errorMsg);
return new InterpreterResult(Code.ERROR, errorMsg);
}
}
LOGGER.error("Fail to run pig script.", e);
return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
} finally {
System.setOut(originalStdOut);
listenerMap.remove(contextInterpreter.getParagraphId());
if (tmpFile != null) {
tmpFile.delete();
}
}
StringBuilder outputBuilder = new StringBuilder();
PigStats stats = PigStats.get();
if (stats != null && includeJobStats) {
String jobStats = PigUtils.extactJobStats(stats);
if (jobStats != null) {
outputBuilder.append(jobStats);
}
}
outputBuilder.append(bytesOutput.toString());
return new InterpreterResult(Code.SUCCESS, outputBuilder.toString());
}
use of org.apache.commons.io.output.ByteArrayOutputStream in project hive by apache.
the class KryoSerializer method serialize.
public static byte[] serialize(Object object) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Output output = new Output(stream);
Kryo kryo = SerializationUtilities.borrowKryo();
kryo.setClassLoader(Thread.currentThread().getContextClassLoader());
try {
kryo.writeObject(output, object);
} finally {
SerializationUtilities.releaseKryo(kryo);
}
// close() also calls flush()
output.close();
return stream.toByteArray();
}
use of org.apache.commons.io.output.ByteArrayOutputStream in project hudson-2.x by hudson.
the class ConsoleNote method encodeToBytes.
private ByteArrayOutputStream encodeToBytes() throws IOException {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(buf));
oos.writeObject(this);
oos.close();
ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(new Base64OutputStream(buf2, true, -1, null));
buf2.write(PREAMBLE);
dos.writeInt(buf.size());
buf.writeTo(dos);
dos.close();
buf2.write(POSTAMBLE);
return buf2;
}
use of org.apache.commons.io.output.ByteArrayOutputStream in project hudson-2.x by hudson.
the class WindowsInstallerLink method doDoInstall.
/**
* Performs installation.
*/
public void doDoInstall(StaplerRequest req, StaplerResponse rsp, @QueryParameter("dir") String _dir) throws IOException, ServletException {
if (installationDir != null) {
// installation already complete
sendError("Installation is already complete", req, rsp);
return;
}
if (!DotNet.isInstalled(2, 0)) {
sendError(".NET Framework 2.0 or later is required for this feature", req, rsp);
return;
}
Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
File dir = new File(_dir).getAbsoluteFile();
dir.mkdirs();
if (!dir.exists()) {
sendError("Failed to create installation directory: " + dir, req, rsp);
return;
}
try {
// copy files over there
copy(req, rsp, dir, getClass().getResource("/windows-service/hudson.exe"), "hudson.exe");
copy(req, rsp, dir, getClass().getResource("/windows-service/hudson.xml"), "hudson.xml");
if (!hudsonWar.getCanonicalFile().equals(new File(dir, "hudson.war").getCanonicalFile()))
copy(req, rsp, dir, hudsonWar.toURI().toURL(), "hudson.war");
// install as a service
ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamTaskListener task = new StreamTaskListener(baos);
task.getLogger().println("Installing a service");
int r = WindowsSlaveInstaller.runElevated(new File(dir, "hudson.exe"), "install", task, dir);
if (r != 0) {
sendError(baos.toString(), req, rsp);
return;
}
// installation was successful
installationDir = dir;
rsp.sendRedirect(".");
} catch (AbortException e) {
// this exception is used as a signal to terminate processing. the error should have been already reported
} catch (InterruptedException e) {
throw new ServletException(e);
}
}
use of org.apache.commons.io.output.ByteArrayOutputStream in project hudson-2.x by hudson.
the class ZFSInstaller method init.
@Extension
public static AdministrativeMonitor init() {
String migrationTarget = System.getProperty(ZFSInstaller.class.getName() + ".migrate");
if (migrationTarget != null) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamTaskListener listener = new StreamTaskListener(new ForkOutputStream(System.out, out));
try {
if (migrate(listener, migrationTarget)) {
// completed successfully
return new MigrationCompleteNotice();
}
} catch (Exception e) {
// if we let any exception from here, it will prevent Hudson from starting.
e.printStackTrace(listener.error("Migration failed"));
}
// migration failed
return new MigrationFailedNotice(out);
}
// install the monitor if applicable
ZFSInstaller zi = new ZFSInstaller();
if (zi.isActivated())
return zi;
return null;
}
Aggregations