/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jtests.util;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

/**
 * Utility class about the environment.
 * @author Helene Joanin 
 */

public class Env {

    public static final int DB_UNKNOWN = -1;
    public static final int DB_ORACLE = 10;
    public static final int DB_POSGRESQL = 20;
    public static final int DB_DB2 = 30;
    public static final int DB_MYSQL = 40;

    // Variables related to Java Version


    static public  final int  JAVA_1_3    = 130;
    static public  final int  JAVA_1_4    = 140;
    static private       int  javaVersion = -1;

    /**
     * Get the type of a Database
     * @param connection to the databse
     * @return the type DB_ORACLE, DB_POSGRESQL, DB_DB2, DB_MYSQL or DB_UNKNOWN if unknown
     */
    public static int getDatabaseType(Connection c) throws SQLException {
        int type = DB_UNKNOWN;
        String name = c.getMetaData().getDatabaseProductName();
        if ("Oracle".equalsIgnoreCase(name)) {
            type = DB_ORACLE;
        } else if ("PostgreSQL".equalsIgnoreCase(name)) {
            type = DB_POSGRESQL;
        } else if ("DB2/LINUX".equalsIgnoreCase(name)) {
            type = DB_DB2;
        } else if ("MySQL".equalsIgnoreCase(name)) {
            type = DB_MYSQL;
        }
        return type;
    }

    /**
     * Gets Java Version.
     * @return javaVersion or -1 if error
     */
    static public int getJavaVersion() {

        if (javaVersion == -1) {
            // Sets Java Version
            String strjv = System.getProperty("java.version", "");
            if (strjv.indexOf("1.3") == 0)
                javaVersion = JAVA_1_3;
            if (strjv.indexOf("1.4") == 0)
                javaVersion = JAVA_1_4;
        } 
        return javaVersion;
    }

    /**
     * return true if JDK 1.4 or later
     */
    static public boolean isJAVA4() {
        return (getJavaVersion() >= JAVA_1_4);
    }
}
