001package io.ebean;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006import java.io.IOException;
007import java.io.InputStream;
008import java.util.Properties;
009
010/**
011 * Class to determine the ebean version.
012 *
013 * @author Roland Praml, FOCONIS AG
014 */
015public class EbeanVersion {
016
017  private static final Logger log = LoggerFactory.getLogger("io.ebean");
018
019  /**
020   * Maintain the minimum ebean-agent version manually based on required ebean-agent bug fixes.
021   */
022  private static final int MIN_AGENT_MAJOR_VERSION = 12;
023  private static final int MIN_AGENT_MINOR_VERSION = 12;
024  private static String version = "unknown";
025  static {
026    readVersion();
027    checkAgentVersion();
028  }
029
030  private static void readVersion() {
031    try {
032      try (InputStream in = ClassLoader.getSystemResourceAsStream("META-INF/maven/io.ebean/ebean-api/pom.properties")) {
033        if (in != null) {
034          version = readVersion(in);
035        }
036      }
037      log.info("ebean version: {}", version);
038    } catch (IOException e) {
039      log.warn("Could not determine ebean version: {}", e.getMessage());
040    }
041  }
042
043  private static void checkAgentVersion() {
044    try {
045      try (InputStream in = ClassLoader.getSystemResourceAsStream("META-INF/maven/io.ebean/ebean-agent/pom.properties")) {
046        // often we only have ebean-agent during development (with build time enhancement), null is expected
047        if (in != null) {
048          String agentVersion = readVersion(in);
049          if (agentVersion != null) {
050            if (checkMinAgentVersion(agentVersion)) {
051              log.error("Expected minimum ebean-agent version {}.{}.0 but we have {}, please update the ebean-agent", MIN_AGENT_MAJOR_VERSION, MIN_AGENT_MINOR_VERSION, agentVersion);
052            }
053          }
054        }
055      }
056    } catch (IOException e) {
057      log.warn("Could not check minimum ebean-agent version {}.{}.0 required due to - {}", MIN_AGENT_MAJOR_VERSION, MIN_AGENT_MINOR_VERSION, e.getMessage());
058    }
059  }
060
061  /**
062   * Return true if ebean-agent is NOT at our minimum version.
063   */
064  static boolean checkMinAgentVersion(String agentVersion) {
065    String[] versionSegments = agentVersion.split("\\.");
066    if (versionSegments.length != 3) {
067      return true;
068    } else {
069      int major = Integer.parseInt(versionSegments[0]);
070      int minor = Integer.parseInt(versionSegments[1]);
071      if (major < MIN_AGENT_MAJOR_VERSION) {
072        return true;
073      } else {
074        return major == MIN_AGENT_MAJOR_VERSION && minor < MIN_AGENT_MINOR_VERSION;
075      }
076    }
077  }
078
079  private static String readVersion(InputStream in) throws IOException {
080    Properties prop = new Properties();
081    prop.load(in);
082    in.close();
083    return prop.getProperty("version");
084  }
085
086  private EbeanVersion() {
087    // hide
088  }
089
090  /**
091   * Returns the ebean version (read from /META-INF/maven/io.ebean/ebean/pom.properties)
092   */
093  public static String getVersion() {
094    return version;
095  }
096
097}