/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.daffodil.util

import org.apache.daffodil.processors.charset.CharsetUtils

class InvalidJavaVersionException(msg: String, cause: Throwable = null) extends Exception(msg, cause)

object CheckJavaVersion {

  def checkJavaVersion(): Option[String] = {
    val jVersion = scala.util.Properties.javaVersion
    val errorStringOpt =
      if (!scala.util.Properties.isJavaAtLeast("1.8")) {
        Some("Daffodil requires Java 8 (1.8) or higher. You are currently running %s.".format(jVersion))
      } else if (CharsetUtils.hasJava7DecoderBug) {
        Some("This Java JVM has the Java 7 Decoder Bug. Daffodil requires Java 8 or higher.")
      } else {
        None
      }
    errorStringOpt
  }

  val allowUnsupportedJavaMessage =
    "Due to the tunable value of errorOnUnsupportedJavaVersion, " +
    "processing will continue with the understanding that this is not " +
    "fully tested and may have unexpected behavior in some circumstances."
}
