Example usage for java.io StringWriter close

List of usage examples for java.io StringWriter close

Introduction

In this page you can find the example usage for java.io StringWriter close.

Prototype

public void close() throws IOException 

Source Link

Document

Closing a StringWriter has no effect.

Usage

From source file:org.jboss.dashboard.ui.controller.ControllerServlet.java

protected void initError() {
    // Write some data to file, allowing external checking of what went wrong.
    File outputFile = new File(Application.lookup().getBaseAppDirectory() + "/ControllerError.txt");
    FileWriter writer = null;/*  w w  w .  j a  va2 s .com*/
    try {
        StringWriter sw = new StringWriter();
        initException.printStackTrace(new PrintWriter(sw));
        writer = new FileWriter(outputFile);
        writer.write(initException.getMessage() + "\n" + sw.toString());
        outputFile.deleteOnExit();
        sw.close();
    } catch (IOException e1) {
        log.error("Error writing to log file: ", e1);
    } finally {
        Application.lookup().shutdown();
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e2) {
                log.error("Error closing log file: ", e2);
            }
        }
    }
}

From source file:com.cloudera.lib.service.instrumentation.TestInstrumentationService.java

@Test
@TestDir/*  w  w  w  .j  av  a  2 s . c o  m*/
@SuppressWarnings("unchecked")
public void service() throws Exception {
    String dir = getTestDir().getAbsolutePath();
    String services = StringUtils.toString(Arrays.asList(InstrumentationService.class.getName()), ",");
    XConfiguration conf = new XConfiguration();
    conf.set("server.services", services);
    Server server = new Server("server", dir, dir, dir, dir, conf);
    server.init();

    Instrumentation instrumentation = server.get(Instrumentation.class);
    Assert.assertNotNull(instrumentation);
    instrumentation.incr("g", "c", 1);
    instrumentation.incr("g", "c", 2);
    instrumentation.incr("g", "c1", 2);

    Instrumentation.Cron cron = instrumentation.createCron();
    cron.start();
    sleep(100);
    cron.stop();
    instrumentation.addCron("g", "t", cron);
    cron = instrumentation.createCron();
    cron.start();
    sleep(200);
    cron.stop();
    instrumentation.addCron("g", "t", cron);

    Instrumentation.Variable<String> var = new Instrumentation.Variable<String>() {
        @Override
        public String getValue() {
            return "foo";
        }
    };
    instrumentation.addVariable("g", "v", var);

    Instrumentation.Variable<Long> varToSample = new Instrumentation.Variable<Long>() {
        @Override
        public Long getValue() {
            return 1L;
        }
    };
    instrumentation.addSampler("g", "s", 10, varToSample);

    Map<String, ?> snapshot = instrumentation.getSnapshot();
    Assert.assertNotNull(snapshot.get("os-env"));
    Assert.assertNotNull(snapshot.get("sys-props"));
    Assert.assertNotNull(snapshot.get("jvm"));
    Assert.assertNotNull(snapshot.get("counters"));
    Assert.assertNotNull(snapshot.get("timers"));
    Assert.assertNotNull(snapshot.get("variables"));
    Assert.assertNotNull(snapshot.get("samplers"));
    Assert.assertNotNull(((Map<String, String>) snapshot.get("os-env")).get("PATH"));
    Assert.assertNotNull(((Map<String, String>) snapshot.get("sys-props")).get("java.version"));
    Assert.assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("free.memory"));
    Assert.assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("max.memory"));
    Assert.assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("total.memory"));
    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g"));
    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g"));
    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g"));
    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g"));
    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c"));
    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c1"));
    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g").get("t"));
    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g").get("v"));
    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g").get("s"));

    StringWriter writer = new StringWriter();
    JSONObject.writeJSONString(snapshot, writer);
    writer.close();
    server.destroy();
}

From source file:org.wso2.carbon.bpel.ui.bpel2svg.impl.SVGImpl.java

public String generateSVGString() {
    try {/*from  w  w  w .j  a v a 2s .co  m*/
        //Define a String writer
        StringWriter writer = new StringWriter();
        //Get the SVG document from the root activity i.e. the Process Activity
        SVGDocument svgDoc = getRootActivity().getSVGDocument();
        if (svgDoc != null) {
            this.svgDoc = svgDoc;
        }
        //Method wrapper for SVGTranscoder.
        DOMUtilities.writeDocument(svgDoc, writer);
        writer.close();
        svgStr = writer.toString();
        return svgStr;
    } catch (IOException ioe) {
        log.error("Error Generating SVG String", ioe);
        return null;
    }
}

From source file:org.eclipse.packagedrone.utils.deb.build.DebianPackageWriter.java

protected ContentProvider createControlContent() throws IOException {
    this.packageControlFile.set(BinaryPackageControlFile.Fields.INSTALLED_SIZE,
            Long.toString(this.installedSize));

    final StringWriter sw = new StringWriter();
    final ControlFileWriter writer = new ControlFileWriter(sw, BinaryPackageControlFile.FORMATTERS);
    writer.writeEntries(this.packageControlFile.getValues());
    sw.close();

    return new StaticContentProvider(sw.toString());
}

From source file:org.apache.hadoop.lib.service.instrumentation.TestInstrumentationService.java

@Test
@TestDir//from   w w  w . j  a v  a  2  s  .c  om
@SuppressWarnings("unchecked")
public void service() throws Exception {
    String dir = TestDirHelper.getTestDir().getAbsolutePath();
    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName()));
    Configuration conf = new Configuration(false);
    conf.set("server.services", services);
    Server server = new Server("server", dir, dir, dir, dir, conf);
    server.init();

    Instrumentation instrumentation = server.get(Instrumentation.class);
    assertNotNull(instrumentation);
    instrumentation.incr("g", "c", 1);
    instrumentation.incr("g", "c", 2);
    instrumentation.incr("g", "c1", 2);

    Instrumentation.Cron cron = instrumentation.createCron();
    cron.start();
    sleep(100);
    cron.stop();
    instrumentation.addCron("g", "t", cron);
    cron = instrumentation.createCron();
    cron.start();
    sleep(200);
    cron.stop();
    instrumentation.addCron("g", "t", cron);

    Instrumentation.Variable<String> var = new Instrumentation.Variable<String>() {
        @Override
        public String getValue() {
            return "foo";
        }
    };
    instrumentation.addVariable("g", "v", var);

    Instrumentation.Variable<Long> varToSample = new Instrumentation.Variable<Long>() {
        @Override
        public Long getValue() {
            return 1L;
        }
    };
    instrumentation.addSampler("g", "s", 10, varToSample);

    Map<String, ?> snapshot = instrumentation.getSnapshot();
    assertNotNull(snapshot.get("os-env"));
    assertNotNull(snapshot.get("sys-props"));
    assertNotNull(snapshot.get("jvm"));
    assertNotNull(snapshot.get("counters"));
    assertNotNull(snapshot.get("timers"));
    assertNotNull(snapshot.get("variables"));
    assertNotNull(snapshot.get("samplers"));
    assertNotNull(((Map<String, String>) snapshot.get("os-env")).get("PATH"));
    assertNotNull(((Map<String, String>) snapshot.get("sys-props")).get("java.version"));
    assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("free.memory"));
    assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("max.memory"));
    assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("total.memory"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c1"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g").get("t"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g").get("v"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g").get("s"));

    StringWriter writer = new StringWriter();
    JSONObject.writeJSONString(snapshot, writer);
    writer.close();
    server.destroy();
}

From source file:org.yes.cart.service.mail.impl.MailComposerImpl.java

/**
 * Merge model with template.//from   w  w  w. j a  va 2s.co  m
 *
 * @param view  groovy string template
 * @param model model
 * @return merged view.
 * @throws java.io.IOException    in case of inline resources can not be found
 * @throws ClassNotFoundException in case if something wrong with template engine
 */
String merge(final String view, final Map<String, Object> model) throws IOException, ClassNotFoundException {
    final Writable writable = templateEngine.createTemplate(view).make(model);
    final StringWriter stringWriter = new StringWriter();
    writable.writeTo(stringWriter);
    stringWriter.close();
    return stringWriter.toString();
}

From source file:gdv.xport.util.HtmlFormatter.java

private void writeSatz(final Satz satz) throws XMLStreamException, IOException {
    StringWriter buffer = new StringWriter();
    XMLStreamWriter xmlStreamWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(buffer);
    writeTo(xmlStreamWriter, satz, zeile);
    xmlStreamWriter.close();/*from   w  w w  . j  a  va  2  s .  c  om*/
    buffer.close();
    this.write(buffer.toString());
}

From source file:com.webcohesion.enunciate.modules.ruby_json_client.RubyJSONClientModule.java

/**
 * Processes the specified template with the given model.
 *
 * @param templateURL The template URL.//from   w  w w.jav a  2  s .com
 * @param model       The root model.
 */
public String processTemplate(URL templateURL, Object model) throws IOException, TemplateException {
    debug("Processing template %s.", templateURL);
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);

    configuration.setTemplateLoader(new URLTemplateLoader() {
        protected URL getURL(String name) {
            try {
                return new URL(name);
            } catch (MalformedURLException e) {
                return null;
            }
        }
    });

    configuration.setTemplateExceptionHandler(new TemplateExceptionHandler() {
        public void handleTemplateException(TemplateException templateException, Environment environment,
                Writer writer) throws TemplateException {
            throw templateException;
        }
    });

    configuration.setLocalizedLookup(false);
    configuration.setDefaultEncoding("UTF-8");
    configuration.setObjectWrapper(new RubyJSONClientObjectWrapper());
    Template template = configuration.getTemplate(templateURL.toString());
    StringWriter unhandledOutput = new StringWriter();
    template.process(model, unhandledOutput);
    unhandledOutput.close();
    return unhandledOutput.toString();
}

From source file:org.latticesoft.util.common.StringUtil.java

/**
 * Parse the stack trace to get the various information:
 * package name, class name, method name, and line number
 * @param t throwable to be parsed.//from  ww  w.  j  a v a  2  s .co m
 * @return a collection of string stating the results in a
 * string array form. The elements of the string array is as
 * listed above
 */
public static Collection parseStackTrace(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    t.printStackTrace(pw);
    pw.flush();
    sw.flush();
    StringBuffer sb = sw.getBuffer();
    try {
        pw.close();
    } catch (Exception ex) {
    }
    try {
        sw.close();
    } catch (Exception ex) {
    }
    sw = null;
    pw = null;

    Collection c = StringUtil.tokenize(sb.toString(), "\n");
    ArrayList a = new ArrayList();
    Iterator iter = c.iterator();
    iter.next(); // drop 1st element;
    String className = null, packageName = null;
    String methodName = null, lineNumber = null;
    int index = 0;
    String result[] = null;
    while (iter.hasNext()) {
        result = new String[4];
        String s = (String) iter.next();

        index = s.indexOf("(");
        lineNumber = s.substring(index, s.length());
        packageName = s.substring(3, index);

        index = packageName.lastIndexOf(".");
        methodName = packageName.substring(index + 1, packageName.length());
        packageName = packageName.substring(0, index);

        index = packageName.lastIndexOf(".");
        className = packageName.substring(index + 1, packageName.length());
        packageName = packageName.substring(0, index);

        lineNumber = StringUtil.substring(lineNumber, ":", ")");

        if (log.isDebugEnabled()) {
            log.debug("packageName:" + packageName);
            log.debug("className:" + className);
            log.debug("methodName:" + methodName);
            log.debug("lineNumber:" + lineNumber);
        }

        result[0] = packageName;
        result[1] = className;
        result[2] = methodName;
        result[3] = lineNumber;
        a.add(result);
    }
    return a;
}

From source file:org.apache.jackrabbit.core.journal.AbstractRecord.java

/**
 * {@inheritDoc}//from   ww  w  .  j  a  v a  2s. c  o  m
 */
public void writePrivilegeDef(PrivilegeDefinition privilegeDefinition) throws JournalException {
    try {
        Map<String, String> nsMapping = new HashMap<String, String>();
        String uri = privilegeDefinition.getName().getNamespaceURI();
        nsMapping.put(nsResolver.getPrefix(uri), uri);
        for (Name n : privilegeDefinition.getDeclaredAggregateNames()) {
            nsMapping.put(nsResolver.getPrefix(n.getNamespaceURI()), n.getNamespaceURI());
        }

        StringWriter sw = new StringWriter();
        PrivilegeDefinitionWriter writer = new PrivilegeDefinitionWriter("text/xml");
        writer.writeDefinitions(sw, new PrivilegeDefinition[] { privilegeDefinition }, nsMapping);
        sw.close();

        writeString(sw.toString());

    } catch (IOException e) {
        String msg = "I/O error while writing privilege definition.";
        throw new JournalException(msg, e);
    } catch (NamespaceException e) {
        String msg = "NamespaceException error while writing privilege definition.";
        throw new JournalException(msg, e);
    }
}