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.Unmarshaller;
009import java.io.File;
010import java.io.FileInputStream;
011import java.io.IOException;
012import java.io.InputStream;
013
014/**
015 * Reads a profiling xml document.
016 */
017public class AutoTuneXmlReader {
018
019  /**
020   * Read and return a Profiling from an xml file.
021   */
022  public static Autotune read(File file) {
023    try {
024      return readFile(file);
025    } catch (IOException e) {
026      throw new IllegalStateException(e);
027    }
028  }
029
030  protected static Autotune readFile(File file) throws IOException {
031    if (!file.exists()) {
032      return new Autotune();
033    }
034    try (FileInputStream is = new FileInputStream(file)) {
035      return read(is);
036    }
037  }
038
039  /**
040   * Read and return a Profiling from an xml document.
041   */
042  public static Autotune read(InputStream is) {
043    try {
044      JAXBContext jaxbContext = JAXBContext.newInstance(Autotune.class);
045      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
046      return (Autotune) unmarshaller.unmarshal(is);
047    } catch (JAXBException e) {
048      throw new IllegalStateException(e);
049    }
050  }
051
052}