001package org.avaje.ebean.ignite.config;
002
003
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006
007import javax.xml.bind.JAXBContext;
008import javax.xml.bind.JAXBException;
009import javax.xml.bind.Unmarshaller;
010import java.io.File;
011import java.io.FileInputStream;
012import java.io.IOException;
013import java.io.InputStream;
014
015/**
016 * Reads L2Configuration from Xml resources.
017 */
018public class ConfigXmlReader {
019
020  private static final Logger logger = LoggerFactory.getLogger(ConfigXmlReader.class);
021
022  /**
023   * Read and return a Migration from an xml document at the given resource path.
024   */
025  public static L2Configuration read(String resourcePath) {
026
027    InputStream is = ConfigXmlReader.class.getResourceAsStream(resourcePath);
028    if (is == null) {
029      return new L2Configuration();
030    }
031    return read(is);
032  }
033
034  public static L2Configuration read(File file) {
035    try {
036      return read(new FileInputStream(file));
037    } catch (IOException e) {
038      throw new RuntimeException("Error reading ignite config file", e);
039    }
040  }
041
042  /**
043   * Read and return a L2Configuration from an xml document.
044   */
045  public static L2Configuration read(InputStream is) {
046
047    try {
048      JAXBContext jaxbContext = JAXBContext.newInstance(L2Configuration.class);
049      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
050      return (L2Configuration) unmarshaller.unmarshal(is);
051
052    } catch (JAXBException e) {
053      throw new RuntimeException(e);
054    } finally {
055      try {
056        is.close();
057      } catch (IOException e) {
058        logger.error("Error while closing configuration", e);
059      }
060    }
061  }
062}