001package io.ebeaninternal.server.autotune.service;
002
003
004import io.ebeaninternal.server.autotune.model.Autotune;
005
006import javax.xml.bind.JAXBContext;
007import javax.xml.bind.JAXBException;
008import javax.xml.bind.Marshaller;
009import java.io.File;
010import java.text.SimpleDateFormat;
011import java.util.Date;
012
013/**
014 * Simple writer for output of the AutoTune Profiling as an XML document.
015 */
016public class AutoTuneXmlWriter {
017
018  /**
019   * Return 'now' as a string to second precision.
020   */
021  public static String now() {
022    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
023    return df.format(new Date());
024  }
025
026  /**
027   * Write the document as xml file with the given prefix.
028   */
029  public void write(Autotune document, String fileName, boolean withNow) {
030
031    SortAutoTuneDocument.sort(document);
032
033    if (withNow) {
034      fileName += "-" + now() + ".xml";
035    }
036
037    // write the file with serverName and now suffix as we can output the profiling many times
038    write(document, new File(fileName));
039  }
040
041  /**
042   * Write Profiling to a file as xml.
043   */
044  public void write(Autotune profiling, File file) {
045
046    try {
047      JAXBContext jaxbContext = JAXBContext.newInstance(Autotune.class);
048      Marshaller marshaller = jaxbContext.createMarshaller();
049      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
050      marshaller.marshal(profiling, file);
051
052    } catch (JAXBException e) {
053      throw new RuntimeException(e);
054    }
055  }
056
057}