001package io.ebeaninternal.dbmigration.migrationreader;
002
003
004import io.ebeaninternal.dbmigration.migration.Migration;
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 migration xml document returning the Migration.
016 */
017public class MigrationXmlReader {
018
019  private MigrationXmlReader() {
020  }
021
022  /**
023   * Read and return a Migration from an xml document at the given resource path.
024   */
025  public static Migration read(String resourcePath) {
026
027    try (InputStream is = MigrationXmlReader.class.getResourceAsStream(resourcePath)) {
028      if (is == null) {
029        throw new IllegalArgumentException("No resource found for path [" + resourcePath + "]");
030      }
031      return read(is);
032    } catch (IOException e) {
033      throw new IllegalArgumentException("Failed to auto close resource " + resourcePath, e);
034    }
035  }
036
037  /**
038   * Read and return a Migration from a migration xml file.
039   */
040  public static Migration read(File migrationFile) {
041
042    try (FileInputStream is = new FileInputStream(migrationFile)) {
043      return read(is);
044    } catch (IOException e) {
045      throw new RuntimeException(e);
046    }
047  }
048
049  /**
050   * Read and return a Migration from an xml document.
051   */
052  public static Migration read(InputStream is) {
053
054    try {
055      JAXBContext jaxbContext = JAXBContext.newInstance(Migration.class);
056      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
057      return (Migration) unmarshaller.unmarshal(is);
058
059    } catch (JAXBException e) {
060      throw new RuntimeException(e);
061    }
062  }
063
064}