Tuesday Apr 17

Links

Books

User Menu

AddThis

AddThis Social Bookmark Button

Java os.name system property

PDF Print E-mail
Tuesday, 31 May 2011 09:58
AddThis Social Bookmark Button

Find below two enumeration to define and use the os.name system property.

Operating sytem enumeration

Spring java source

package com.ubiteck.util;
import static com.ubiteck.util.OperatingSystemFamily.*;

public enum OperatingSystem {
    LINUX_OS ("Linux",LINUX),
    MAC_OSX ("Mac OS X",MAC),
    MAC_OS ("Mac OS",MAC),
    WINDOWS_95 ("Windows 95",WINDOWS),
    WINDOWS_98 ("Windows 98",WINDOWS),
    WINDOWS_ME ("Windows Me",WINDOWS),
    WINDOWS_NT ("Windows NT",WINDOWS),
    WINDOWS_2000 ("Windows 2000",WINDOWS),
    WINDOWS_XP ("Windows XP",WINDOWS),
    WINDOWS_7 ("Windows 7",WINDOWS),
    WINDOWS_2003 ("Windows 2003",WINDOWS),
    WINDOWS_2008 ("Windows 2008",WINDOWS),
    SUN_OS ("Sun OS ",UNIX),
    MPE_IX ("MPE/iX",UNIX),
    HP_UX ("HP-UX",UNIX),
    AIX ("AIX",UNIX),
    OS_390 ("OS/390",UNIX),
    FREEBSD ("FreeBSD",UNIX),
    IRIX ("Irix",UNIX),
    DIGITAL_UNIX ("Digital Unix",UNIX),
    NETWARE_4_11 ("NetWare 4.11",UNIX),
    OSF1 ("OSF1",UNIX),
    OPENVMS ("OpenVMS",DEC_OS);
    
    
    final private String  label;
    final private OperatingSystemFamily family;
    
    
    private OperatingSystem(String label, OperatingSystemFamily family) {
        this.label = label;
        this.family = family;
    }
    public String getLabel() {
        return label;
    }
    public OperatingSystemFamily getFamily() {
        return family;
    }
    static public OperatingSystem resolve(String osName){
        for (OperatingSystem os : OperatingSystem.values()) {
            if (os.label.equalsIgnoreCase(osName)) return os;
        }
        return null;
    }
    
}

Operating system families

Spring java source

package com.ubiteck.util;
public enum OperatingSystemFamily {
    LINUX,
    WINDOWS,
    UNIX,
    DEC_OS,
    MAC;
}

Usage sample

In the following example we are reading the os.name system property and resolve the correponding operating system enumeration . Then we use a switch case on the os family to define the folders according to the operating system family

String osName = System.getProperty("osName", null);
    OperatingSystem  operatingSystem = OperatingSystem.resolve(osName);
    OperatingSystemFamily family = operatingSystem.getFamily();
    switch (family) {
        case UNIX:
            setBinFolder(DEFAULT_UNIX_BIN);
            setConfFolder(DEFAULT_UNIX_CONF);
            setDataFolder(DEFAULT_UNIX_DATA);
            break;
        case WINDOWS:
            setBinFolder(DEFAULT_WINDOWS_HOME);
            setConfFolder(DEFAULT_WINDOWS_HOME);
            setDataFolder(DEFAULT_WINDOWS_HOME);
            break;
        case LINUX:
            setBinFolder(DEFAULT_LINUX_BIN);
            setConfFolder(DEFAULT_LINUX_CONF);
            setDataFolder(DEFAULT_LINUX_DATA);
            break;
        default:
            setBinFolder(DEFAULT_WINDOWS_HOME);
            setConfFolder(DEFAULT_WINDOWS_HOME);
            setDataFolder(DEFAULT_WINDOWS_HOME);                    
            break;
    }

Tags: package , switch , property , enum , public , system , family , string , operatingsystem , operatingsystemfamily , static , operating , enumeration , os.name

Add comment


Security code
Refresh