Example usage for java.io StringWriter getBuffer

List of usage examples for java.io StringWriter getBuffer

Introduction

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

Prototype

public StringBuffer getBuffer() 

Source Link

Document

Return the string buffer itself.

Usage

From source file:org.hexlogic.model.DockerNode.java

@VsoMethod(showInApi = false)
public InspectContainerResponse inspectDockerContainer(DockerContainer container) throws Exception {
    log.debug("Running inspectDockerContainer...");
    DockerClient dockerClient = null;/*from www.  ja  v a2  s . co  m*/
    try {
        configureNode();
        dockerClient = getDockerClient();

        return dockerClient.inspectContainerCmd(container.getContainerId()).exec();
    } catch (Exception e) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);

        log.error("Error running inspectDockerContainer: " + sw.getBuffer().toString());
        throw e;
    }
}

From source file:org.hexlogic.model.DockerNode.java

@VsoMethod(showInApi = false)
public DockerClient getDockerClient() {
    DockerClient dockerClient = null;// ww  w  . j a va  2 s  .co  m
    try {
        configureNode();
        dockerClient = DockerClientBuilder.getInstance(config)
                .withServiceLoaderClassLoader(CooptoPluginAdaptor.class.getClassLoader()).build();

    } catch (Exception e) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);
        log.error("Error while creating Docker client. Details:\n" + sw.getBuffer().toString());
    }

    return dockerClient;
}

From source file:org.hexlogic.model.DockerNode.java

@VsoMethod(showInApi = true, name = "deleteImage", description = "Deletes an image from the docker node")
public void removeImage(String id, boolean force) throws Exception {
    log.debug("Removing image '" + id + "'.");
    configureNode();/* w  ww  . jav a2 s  .c o m*/
    DockerClient dockerClient = getDockerClient();

    try {
        log.debug("Executing...");
        dockerClient.removeImageCmd(id).withForce(force).exec();
    } catch (NotFoundException e) {
        // image was not found
        log.error(e.getMessage());
        // Throw error detail message so vCO can display it
        throw new Exception("Error: the image was not found.");
    } catch (ConflictException e) {
        // image is in use by some container
        log.error(e.getMessage());
        // Throw error detail message so vCO can display it
        throw new Exception("Error: the image is used by one or more containers.");
    } catch (Exception e) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);

        log.error("Error while removeing image: " + sw.getBuffer().toString());
        // Throw error detail message so vCO can display it
        throw new Exception("Error while removing image: " + sw.getBuffer().toString());
    }

    // reload images from docker node
    this.reloadImages();
    // update inventory - another way to do this would be to update our ArrayList and call notifyElementDeleted on the image object
    notificationHandler.notifyElementInvalidate(toRef());
}

From source file:net.sf.jabref.gui.worker.SendAsEMailAction.java

@Override
public void run() {
    if (!Desktop.isDesktopSupported()) {
        message = Localization.lang("Error creating email");
        return;/*  w  w  w.  j  av  a2 s.c  om*/
    }

    BasePanel panel = frame.getCurrentBasePanel();
    if (panel == null) {
        return;
    }
    if (panel.getSelectedEntries().isEmpty()) {
        message = Localization.lang("This operation requires one or more entries to be selected.");
        return;
    }

    StringWriter sw = new StringWriter();
    List<BibEntry> bes = panel.getSelectedEntries();

    // write the entries using sw, which is used later to form the email content
    BibEntryWriter bibtexEntryWriter = new BibEntryWriter(
            new LatexFieldFormatter(LatexFieldFormatterPreferences.fromPreferences(Globals.prefs)), true);

    for (BibEntry entry : bes) {
        try {
            bibtexEntryWriter.write(entry, sw, panel.getBibDatabaseContext().getMode());
        } catch (IOException e) {
            LOGGER.warn("Problem creating BibTeX file for mailing.", e);
        }
    }

    List<String> attachments = new ArrayList<>();

    // open folders is needed to indirectly support email programs, which cannot handle
    //   the unofficial "mailto:attachment" property
    boolean openFolders = JabRefPreferences.getInstance()
            .getBoolean(JabRefPreferences.OPEN_FOLDERS_OF_ATTACHED_FILES);

    List<File> fileList = FileUtil.getListOfLinkedFiles(bes,
            frame.getCurrentBasePanel().getBibDatabaseContext().getFileDirectory());
    for (File f : fileList) {
        attachments.add(f.getPath());
        if (openFolders) {
            try {
                JabRefDesktop.openFolderAndSelectFile(f.getAbsolutePath());
            } catch (IOException e) {
                LOGGER.debug("Cannot open file", e);
            }
        }
    }

    String mailTo = "?Body=".concat(sw.getBuffer().toString());
    mailTo = mailTo.concat("&Subject=");
    mailTo = mailTo.concat(JabRefPreferences.getInstance().get(JabRefPreferences.EMAIL_SUBJECT));
    for (String path : attachments) {
        mailTo = mailTo.concat("&Attachment=\"").concat(path);
        mailTo = mailTo.concat("\"");
    }

    URI uriMailTo;
    try {
        uriMailTo = new URI("mailto", mailTo, null);
    } catch (URISyntaxException e1) {
        message = Localization.lang("Error creating email");
        LOGGER.warn(message, e1);
        return;
    }

    Desktop desktop = Desktop.getDesktop();
    try {
        desktop.mail(uriMailTo);
    } catch (IOException e) {
        message = Localization.lang("Error creating email");
        LOGGER.warn(message, e);
        return;
    }

    message = String.format("%s: %d", Localization.lang("Entries added to an email"), bes.size());
}

From source file:org.hexlogic.model.DockerNode.java

@VsoMethod(showInApi = true, name = "killContainer")
public void killContainer(DockerContainer container, String signal) throws Exception {
    if (container == null) {
        throw new Exception("Error: no container specified.");
    }//from w  ww.  ja v a 2s.  c o  m

    log.debug("Killing container '" + container.getContainerId() + "' with signal '" + signal + "'.");

    try {
        configureNode();
        DockerClient dockerClient = getDockerClient();

        KillContainerCmd command = dockerClient.killContainerCmd(container.getContainerId());
        if (signal != null && !signal.isEmpty()) {
            command.withSignal(signal);
        }
        command.exec();
    } catch (NotFoundException e) {
        // container not found
        log.error(e.getMessage());
        // Throw error detail message so vCO can display it
        throw new Exception("Error: the container was not found.");
    } catch (Exception e) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);

        log.error("Error while killing container: " + sw.getBuffer().toString());
        throw new Exception("Error while killing container: " + sw.getBuffer().toString());
    }
    log.debug("Kill operation finished.");

    container.reloadContainer();
}

From source file:org.hexlogic.model.DockerNode.java

@VsoMethod(showInApi = true, name = "deleteContainer")
public void removeContainer(DockerContainer container, boolean force, boolean removeVolumes) throws Exception {
    if (container == null) {
        throw new Exception("Error: no container specified.");
    }/*from www  .ja  va 2  s .c  o  m*/

    log.debug("Deleting container '" + container.getContainerId() + "' with force '" + force
            + "' and removeVolumes '" + removeVolumes + "'.");

    try {
        configureNode();
        DockerClient dockerClient = getDockerClient();

        dockerClient.removeContainerCmd(container.getContainerId()).withForce(force)
                .withRemoveVolumes(removeVolumes).exec();
    } catch (NotFoundException e) {
        // container not found
        log.error(e.getMessage());
        // Throw error detail message so vCO can display it
        throw new Exception("Error: the container was not found.");
    } catch (Exception e) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);

        log.error("Error while deleting container: " + sw.getBuffer().toString());
        throw new Exception("Error while deleting container: " + sw.getBuffer().toString());
    }

    log.debug("Delete operation finished.");
    // reload images from docker node
    this.reloadContainers();
    // update inventory - another way to do this would be to update our ArrayList and call notifyElementDeleted on the container object
    notificationHandler.notifyElementInvalidate(toRef());
}

From source file:org.hexlogic.model.DockerNode.java

@VsoMethod(showInApi = true, name = "searchImage", description = "Returns a list of images from the docker hub repository matching the input string.")
public String[] searchImage(String imageName, int limit) throws Exception {
    log.debug("Searching image '" + imageName + "' with limit " + limit + ".");
    // prevent negative values
    if (limit < 0) {
        limit = 0;//w ww  .ja  v a  2  s.c o  m
    }

    DockerClient dockerClient = null;
    List<SearchItem> result = null;
    try {
        configureNode();
        dockerClient = getDockerClient();
        result = dockerClient.searchImagesCmd(imageName).exec();
    } catch (Exception e) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);

        log.error("Error while searching image: " + sw.getBuffer().toString());
        // Throw error detail message so vCO can display it
        throw new Exception("Error while searching image: " + sw.getBuffer().toString());
    }

    ArrayList<String> dyn = new ArrayList<String>();
    // return a string array so we can use the output in our vCO search fields
    Iterator<SearchItem> iterator = result.iterator();
    while (iterator.hasNext()) {
        dyn.add(iterator.next().getName());

        // if we have a limit
        if (limit > 0) {
            // do not iterate more then limit
            if (dyn.size() >= limit) {
                break;
            }
        }
    }
    return dyn.toArray(new String[dyn.size()]);
}

From source file:org.hexlogic.model.DockerNode.java

@VsoMethod(showInApi = true, name = "restartContainer")
public void restartContainer(DockerContainer container, int wait) throws Exception {
    if (container == null) {
        throw new Exception("Error: no container specified.");
    }/*from   w w  w  . j a v a2s .c  om*/

    log.debug("Restarting container '" + container.getContainerId() + "'.");

    try {
        configureNode();
        DockerClient dockerClient = getDockerClient();
        RestartContainerCmd command = dockerClient.restartContainerCmd(container.getContainerId());

        if (wait < 0) {
            command.withtTimeout(0);
        } else {
            command.withtTimeout(wait);
        }
        command.exec();
    } catch (NotFoundException e) {
        // container not found
        log.error(e.getMessage());
        // Throw error detail message so vCO can display it
        throw new Exception("Error: the container was not found.");
    } catch (Exception e) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);

        log.error("Error while restarting container: " + sw.getBuffer().toString());
        throw new Exception("Error while restarting container: " + sw.getBuffer().toString());
    }
    log.debug("Reload operation finished.");

    container.reloadContainer();
}

From source file:org.hexlogic.model.DockerNode.java

@VsoMethod(showInApi = true, name = "stopContainer")
public void stopContainer(DockerContainer container, int wait) throws Exception {
    if (container == null) {
        throw new Exception("Error: no container specified.");
    }/* w  w  w .jav a 2  s  .  c  om*/

    log.debug("Stopping container '" + id + "'.");

    try {
        configureNode();
        DockerClient dockerClient = getDockerClient();

        StopContainerCmd command = dockerClient.stopContainerCmd(container.getContainerId());
        if (wait < 0) {
            command.withTimeout(0);
        } else {
            command.withTimeout(wait);
        }
        command.exec();
    } catch (NotFoundException e) {
        // container not found
        log.error(e.getMessage());
        // Throw error detail message so vCO can display it
        throw new Exception("Error: the container was not found.");
    } catch (NotModifiedException e) {
        // Container already stopped
        log.error(e.getMessage());
        // Throw error detail message so vCO can display it
        throw new Exception("Error: the container is already stopped.");
    } catch (Exception e) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);

        log.error("Error while stopping container: " + sw.getBuffer().toString());
        throw new Exception("Error while stopping container: " + sw.getBuffer().toString());
    }
    log.debug("Stop operation finished.");

    container.reloadContainer();
}

From source file:edu.cornell.med.icb.goby.stats.TestAnnotationAveragingWriter.java

@Test
public void testCase5() {
    String[] groups = new String[] { "group1" };
    String[] samples = new String[] { "sample1", "sample2", "sample3" };
    int[] positions = new int[] { 6, 8, 14, 16 };
    int[][] C = { { 5, 3, 9, 8 }, { 4, 6, 3, 2 }, { 8, 3, 8, 9 } };
    int[][] Cm = { { 9, 7, 1, 5 }, { 9, 7, 9, 3 }, { 2, 3, 2, 8 } };
    testSupport = new MethylCountProviderTestSupport(groups, samples, positions, "Case2", C, Cm);
    final StringWriter stringWriter = new StringWriter();
    AnnotationAveragingWriter testWriter = new AnnotationAveragingWriter(stringWriter, genome, testSupport);
    testWriter.setWriteNumSites(false);/*from w w w.  ja v  a 2  s .c  o m*/
    testWriter.setContexts(DEFAULT_TEST_CONTEXTS);
    testWriter.setAnnotationFilename("test-data/vcf-averaging/annotations-1.tsv");
    int[] a = { 0, 0, 0 };
    testWriter.setSampleIndexToGroupIndex(a);
    testWriter.writeRecord();
    testWriter.writeRecord();
    testWriter.writeRecord();
    testWriter.writeRecord();
    testWriter.close();
    assertEquals("Test Case 5 result: ",
            "Chromosome\tStart\tEnd\tFeature\tMR[sample1][CpG]\tMR[sample2][CpG]\tMR[sample3][CpG]\tMR[sample1][CpA]\tMR[sample2][CpA]\tMR[sample3][CpA]\tMR[sample1][CpC]\tMR[sample2][CpC]\tMR[sample3][CpC]\tMR[sample1][CpT]\tMR[sample2][CpT]\tMR[sample3][CpT]\tMR[sample1][CpN]\tMR[sample2][CpN]\tMR[sample3][CpN]\tMR[group1][CpG]\tMR[group1][CpA]\tMR[group1][CpC]\tMR[group1][CpT]\tMR[group1][CpN]\n"
                    + "Case2\t5\t9\tannotation1\t66.67\t61.54\t31.25\t\t\t\t\t\t\t\t\t\t\t\t\t56.06\t\t\t\t\n"
                    + "Case2\t13\t17\tannotation2\t26.09\t70.59\t37.04\t\t\t\t\t\t\t\t\t\t\t\t\t41.79\t\t\t\t\n",
            stringWriter.getBuffer().toString());

}