Search in sources :

Example 31 with StringWriter

use of java.io.StringWriter in project groovy by apache.

the class MultipleCompilationErrorsException method getMessage.

public String getMessage() {
    StringWriter data = new StringWriter();
    PrintWriter writer = new PrintWriter(data);
    Janitor janitor = new Janitor();
    writer.write(super.getMessage());
    writer.println(":");
    try {
        collector.write(writer, janitor);
    } finally {
        janitor.cleanup();
    }
    return data.toString();
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 32 with StringWriter

use of java.io.StringWriter in project groovy by apache.

the class Groovyc method runCompiler.

private void runCompiler(String[] commandLine) {
    // hand crank it so we can add our own compiler configuration
    try {
        Options options = FileSystemCompiler.createCompilationOptions();
        CommandLineParser cliParser = new DefaultParser();
        CommandLine cli;
        cli = cliParser.parse(options, commandLine);
        configuration = FileSystemCompiler.generateCompilerConfigurationFromOptions(cli);
        configuration.setScriptExtensions(getScriptExtensions());
        String tmpExtension = getScriptExtension();
        if (tmpExtension.startsWith("*."))
            tmpExtension = tmpExtension.substring(1);
        configuration.setDefaultScriptExtension(tmpExtension);
        // Load the file name list
        String[] filenames = FileSystemCompiler.generateFileNamesFromOptions(cli);
        boolean fileNameErrors = filenames == null;
        fileNameErrors = fileNameErrors || !FileSystemCompiler.validateFiles(filenames);
        if (targetBytecode != null) {
            configuration.setTargetBytecode(targetBytecode);
        }
        if (!fileNameErrors) {
            FileSystemCompiler.doCompilation(configuration, makeCompileUnit(), filenames, forceLookupUnnamedFiles);
        }
    } catch (Exception re) {
        Throwable t = re;
        if ((re.getClass() == RuntimeException.class) && (re.getCause() != null)) {
            // unwrap to the real exception
            t = re.getCause();
        }
        StringWriter writer = new StringWriter();
        new ErrorReporter(t, false).write(new PrintWriter(writer));
        String message = writer.toString();
        taskSuccess = false;
        if (errorProperty != null) {
            getProject().setNewProperty(errorProperty, "true");
        }
        if (failOnError) {
            log.error(message);
            throw new BuildException("Compilation Failed", t, getLocation());
        } else {
            log.error(message);
        }
    }
}
Also used : Options(org.apache.commons.cli.Options) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) CommandLine(org.apache.commons.cli.CommandLine) ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) StringWriter(java.io.StringWriter) CommandLineParser(org.apache.commons.cli.CommandLineParser) BuildException(org.apache.tools.ant.BuildException) DefaultParser(org.apache.commons.cli.DefaultParser) PrintWriter(java.io.PrintWriter)

Example 33 with StringWriter

use of java.io.StringWriter in project groovy by apache.

the class Groovy method processError.

private void processError(Exception e) {
    StringWriter writer = new StringWriter();
    new ErrorReporter(e, false).write(new PrintWriter(writer));
    String message = writer.toString();
    throw new BuildException("Script Failed: " + message, e, getLocation());
}
Also used : ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) StringWriter(java.io.StringWriter) BuildException(org.apache.tools.ant.BuildException) PrintWriter(java.io.PrintWriter)

Example 34 with StringWriter

use of java.io.StringWriter in project flink by apache.

the class TaskManagerProcessReapingTestBase method testReapProcessOnFailure.

@Test
public void testReapProcessOnFailure() {
    Process taskManagerProcess = null;
    ActorSystem jmActorSystem = null;
    final StringWriter processOutput = new StringWriter();
    try {
        String javaCommand = getJavaCommandPath();
        // is available on this machine
        if (javaCommand == null) {
            System.out.println("---- Skipping TaskManagerProcessReapingTest : Could not find java executable ----");
            return;
        }
        // create a logging file for the process
        File tempLogFile = File.createTempFile("testlogconfig", "properties");
        tempLogFile.deleteOnExit();
        CommonTestUtils.printLog4jDebugConfig(tempLogFile);
        final int jobManagerPort = NetUtils.getAvailablePort();
        // start a JobManager
        Tuple2<String, Object> localAddress = new Tuple2<String, Object>("localhost", jobManagerPort);
        jmActorSystem = AkkaUtils.createActorSystem(new Configuration(), new Some<Tuple2<String, Object>>(localAddress));
        ActorRef jmActor = JobManager.startJobManagerActors(new Configuration(), jmActorSystem, TestingUtils.defaultExecutor(), TestingUtils.defaultExecutor(), JobManager.class, MemoryArchivist.class)._1;
        // start a ResourceManager
        StandaloneLeaderRetrievalService standaloneLeaderRetrievalService = new StandaloneLeaderRetrievalService(AkkaUtils.getAkkaURL(jmActorSystem, jmActor));
        FlinkResourceManager.startResourceManagerActors(new Configuration(), jmActorSystem, standaloneLeaderRetrievalService, StandaloneResourceManager.class);
        final int taskManagerPort = NetUtils.getAvailablePort();
        // start the task manager process
        String[] command = new String[] { javaCommand, "-Dlog.level=DEBUG", "-Dlog4j.configuration=file:" + tempLogFile.getAbsolutePath(), "-Xms256m", "-Xmx256m", "-classpath", getCurrentClasspath(), TaskManagerTestEntryPoint.class.getName(), String.valueOf(jobManagerPort), String.valueOf(taskManagerPort) };
        ProcessBuilder bld = new ProcessBuilder(command);
        taskManagerProcess = bld.start();
        new PipeForwarder(taskManagerProcess.getErrorStream(), processOutput);
        // grab the reference to the TaskManager. try multiple times, until the process
        // is started and the TaskManager is up
        String taskManagerActorName = String.format("akka.tcp://flink@%s/user/%s", "localhost:" + taskManagerPort, TaskManager.TASK_MANAGER_NAME());
        ActorRef taskManagerRef = null;
        Throwable lastError = null;
        for (int i = 0; i < 40; i++) {
            try {
                taskManagerRef = TaskManager.getTaskManagerRemoteReference(taskManagerActorName, jmActorSystem, new FiniteDuration(25, TimeUnit.SECONDS));
                break;
            } catch (Throwable t) {
                // TaskManager probably not ready yet
                lastError = t;
            }
            Thread.sleep(500);
        }
        assertTrue("TaskManager process died", isProcessAlive(taskManagerProcess));
        if (taskManagerRef == null) {
            if (lastError != null) {
                lastError.printStackTrace();
            }
            fail("TaskManager process did not launch the TaskManager properly. Failed to look up " + taskManagerActorName);
        }
        // kill the TaskManager actor
        onTaskManagerProcessRunning(taskManagerRef);
        // wait for max 5 seconds for the process to terminate
        {
            long now = System.currentTimeMillis();
            long deadline = now + 10000;
            while (now < deadline && isProcessAlive(taskManagerProcess)) {
                Thread.sleep(100);
                now = System.currentTimeMillis();
            }
        }
        assertFalse("TaskManager process did not terminate upon actor death", isProcessAlive(taskManagerProcess));
        int returnCode = taskManagerProcess.exitValue();
        assertEquals("TaskManager died, but not because of the process reaper", TaskManager.RUNTIME_FAILURE_RETURN_CODE(), returnCode);
        onTaskManagerProcessTerminated(processOutput.toString());
    } catch (Exception e) {
        e.printStackTrace();
        printProcessLog(processOutput.toString());
        fail(e.getMessage());
    } catch (Error e) {
        e.printStackTrace();
        printProcessLog(processOutput.toString());
        throw e;
    } finally {
        if (taskManagerProcess != null) {
            taskManagerProcess.destroy();
        }
        if (jmActorSystem != null) {
            jmActorSystem.shutdown();
        }
    }
}
Also used : ActorSystem(akka.actor.ActorSystem) MemoryArchivist(org.apache.flink.runtime.jobmanager.MemoryArchivist) Configuration(org.apache.flink.configuration.Configuration) ActorRef(akka.actor.ActorRef) FiniteDuration(scala.concurrent.duration.FiniteDuration) JobManager(org.apache.flink.runtime.jobmanager.JobManager) IOException(java.io.IOException) Some(scala.Some) StringWriter(java.io.StringWriter) Tuple2(scala.Tuple2) StandaloneLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.StandaloneLeaderRetrievalService) File(java.io.File) Test(org.junit.Test)

Example 35 with StringWriter

use of java.io.StringWriter in project groovy by apache.

the class ClassCompletionVerifierTest method flattenErrorMessage.

private String flattenErrorMessage() {
    StringWriter stringWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(stringWriter, true);
    for (int i = source.getErrorCollector().getErrorCount() - 1; i >= 0; i--) {
        source.getErrorCollector().getError(i).write(writer);
    }
    writer.close();
    return stringWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Aggregations

StringWriter (java.io.StringWriter)3175 PrintWriter (java.io.PrintWriter)1057 Test (org.junit.Test)612 IOException (java.io.IOException)516 StringReader (java.io.StringReader)232 Writer (java.io.Writer)211 StreamResult (javax.xml.transform.stream.StreamResult)207 File (java.io.File)194 InputStreamReader (java.io.InputStreamReader)140 HashMap (java.util.HashMap)136 Transformer (javax.xml.transform.Transformer)125 InputStream (java.io.InputStream)119 Map (java.util.Map)116 ArrayList (java.util.ArrayList)106 DOMSource (javax.xml.transform.dom.DOMSource)99 BufferedReader (java.io.BufferedReader)96 ByteArrayInputStream (java.io.ByteArrayInputStream)84 Reader (java.io.Reader)77 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)75 HttpServletResponse (javax.servlet.http.HttpServletResponse)73