All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ovea.tadjin.util.OsFamily Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2011 Ovea 
 *
 * Licensed 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 com.ovea.tadjin.util;

import java.util.Locale;

public enum OsFamily {

    DOS("dos"),
    MAC("mac"),
    NETWARE("netware"),
    OS2("os/2"),
    TANDEM("tandem"),
    UNIX("unix"),
    WINDOWS("windows"),
    WIN9X("win9x"),
    ZOS("z/os"),
    OS400("os/400"),
    OPENVMS("openvms");

    private final String name;

    private OsFamily(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return getName();
    }

    public static OsFamily fromName(String name) {
        for (OsFamily osFamily : values())
            if (osFamily.name.equals(name))
                return osFamily;
        throw new IllegalArgumentException("OS Family unsupported: " + name);
    }

    public static OsFamily get() {
        for (OsFamily family : values())
            if (isFamily(family))
                return family;
        throw new IllegalStateException(String.format("Unsupported OS ! Please report: os.name=%s", OS_NAME));
    }

    // code from Stefan Bodewig, Magesh Umasankar and Brian Fox
    private static final String PATH_SEP = System.getProperty("path.separator");
    private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US);

    private static boolean isFamily(OsFamily family) {
        boolean isFamily;
        if (family == WINDOWS) {
            isFamily = OS_NAME.indexOf(WINDOWS.getName()) > -1;
        } else if (family == OS2) {
            isFamily = OS_NAME.indexOf(OS2.getName()) > -1;
        } else if (family == NETWARE) {
            isFamily = OS_NAME.indexOf(NETWARE.getName()) > -1;
        } else if (family == DOS) {
            isFamily = PATH_SEP.equals(";")
                    && !isFamily(NETWARE);
        } else if (family == MAC) {
            isFamily = OS_NAME.indexOf(MAC.getName()) > -1;
        } else if (family == TANDEM) {
            isFamily = OS_NAME.indexOf("nonstop_kernel") > -1;
        } else if (family == UNIX) {
            isFamily = PATH_SEP.equals(":")
                    && !isFamily(OPENVMS)
                    && (!isFamily(MAC)
                    || OS_NAME.endsWith("x"));
        } else if (family == WIN9X) {
            isFamily = isFamily(WINDOWS)
                    && (OS_NAME.indexOf("95") >= 0
                    || OS_NAME.indexOf("98") >= 0
                    || OS_NAME.indexOf("me") >= 0
                    || OS_NAME.indexOf("ce") >= 0);
        } else if (family == ZOS) {
            isFamily = OS_NAME.indexOf(ZOS.getName()) > -1 || OS_NAME.indexOf("os/390") > -1;
        } else if (family == OS400) {
            isFamily = OS_NAME.indexOf(OS400.getName()) > -1;
        } else if (family == OPENVMS) {
            isFamily = OS_NAME.indexOf(OPENVMS.getName()) > -1;
        } else {
            isFamily = OS_NAME.indexOf(family.getName().toLowerCase(Locale.US)) > -1;
        }
        return isFamily;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy