001/* 002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved. 003 * 004 * http://www.izforge.com/izpack/ http://developer.berlios.de/projects/izpack/ 005 * 006 * Copyright 2004 Hani Suleiman 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 009 * in compliance with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software distributed under the License 014 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 015 * or implied. See the License for the specific language governing permissions and limitations under 016 * the License. 017 */ 018package com.izforge.izpack.util; 019 020import java.io.File; 021import java.io.IOException; 022 023/** 024 * This is a convienient class, which helps you to detect / identify the running OS/Distribution 025 * 026 * Created at: Date: Nov 9, 2004 Time: 8:53:22 PM 027 * 028 * @author hani, Marc.Eppelmann@reddot.de 029 */ 030public final class OsVersion implements OsVersionConstants, StringConstants 031{ 032 033 //~ Static fields/initializers 034 // ******************************************************************************************************************************* 035 036 /** OS_NAME = System.getProperty( "os.name" ) */ 037 public static final String OS_NAME = System.getProperty( OSNAME ); 038 039 /** True if this is FreeBSD. */ 040 public static final boolean IS_FREEBSD = StringTool.startsWithIgnoreCase(OS_NAME, FREEBSD ); 041 042 /** True if this is Linux. */ 043 public static final boolean IS_LINUX = StringTool.startsWithIgnoreCase(OS_NAME, LINUX ); 044 045 /** True if this is HP-UX. */ 046 public static final boolean IS_HPUX = StringTool.startsWithIgnoreCase(OS_NAME, HP_UX ); 047 048 /** True if this is AIX. */ 049 public static final boolean IS_AIX = StringTool.startsWithIgnoreCase(OS_NAME, AIX ); 050 051 /** True if this is SunOS. */ 052 public static final boolean IS_SUNOS = StringTool.startsWithIgnoreCase(OS_NAME, SUNOS ); 053 054 /** True if this is OS/2. */ 055 public static final boolean IS_OS2 = StringTool.startsWith(OS_NAME, OS_2 ); 056 057 /** True is this is Mac OS */ 058 public static final boolean IS_MAC = StringTool.startsWith(OS_NAME, MAC ); 059 060 /** True if this is the Mac OS X. */ 061 public static final boolean IS_OSX = StringTool.startsWithIgnoreCase(OS_NAME, MACOSX); 062 063 /** True if this is Windows. */ 064 public static final boolean IS_WINDOWS = StringTool.startsWith(OS_NAME, WINDOWS ); 065 066 /** True if this is some variant of Unix (OSX, Linux, Solaris, FreeBSD, etc). */ 067 public static final boolean IS_UNIX = !IS_OS2 && !IS_WINDOWS; 068 069 /** True if RedHat Linux was detected */ 070 public static final boolean IS_REDHAT_LINUX = IS_LINUX 071 && ( ( FileUtil.fileContains(getReleaseFileName(), REDHAT ) || FileUtil.fileContains(getReleaseFileName() , 072 RED_HAT ) ) ); 073 074 /** True if Fedora Linux was detected */ 075 public static final boolean IS_FEDORA_LINUX = IS_LINUX 076 && FileUtil.fileContains(getReleaseFileName(), FEDORA ); 077 078 /** True if Mandriva(Mandrake) Linux was detected */ 079 public static final boolean IS_MANDRAKE_LINUX = IS_LINUX 080 && FileUtil.fileContains( getReleaseFileName(), MANDRAKE ); 081 082 /** True if Mandrake/Mandriva Linux was detected */ 083 public static final boolean IS_MANDRIVA_LINUX = ( IS_LINUX 084 && FileUtil.fileContains( getReleaseFileName(), MANDRIVA ) ) || IS_MANDRAKE_LINUX; 085 086 /** True if SuSE Linux was detected */ 087 public static final boolean IS_SUSE_LINUX = IS_LINUX 088 && FileUtil.fileContains( getReleaseFileName(), SUSE ); 089 090 /** True if Debian Linux or derived was detected */ 091 public static final boolean IS_DEBIAN_LINUX = (IS_LINUX 092 && FileUtil.fileContains(PROC_VERSION, DEBIAN )) || ( IS_LINUX && new File( "/etc/debian_version" ).exists() ); 093 094 // TODO detect the newcomer (K)Ubuntu */ 095 //~ Methods 096 // ************************************************************************************************************************************************** 097 098 /** 099 * Gets the etc Release Filename 100 * 101 * @return name of the file the release info is stored in for Linux distributions 102 */ 103 private static String getReleaseFileName() 104 { 105 String result = new String(); 106 107 File[] etcList = new File("/etc").listFiles(); 108 109 if( etcList != null ) 110 for (int idx = 0; idx < etcList.length; idx++) 111 { 112 File etcEntry = etcList[idx]; 113 114 if (etcEntry.isFile()) 115 { 116 if (etcEntry.getName().endsWith("-release")) 117 { 118 //match :-) 119 return result = etcEntry.toString(); 120 } 121 } 122 } 123 124 return result; 125 } 126 127 /** 128 * Gets the Details of a Linux Distribution 129 * 130 * @return description string of the Linux distribution 131 */ 132 private static String getLinuxDistribution() 133 { 134 String result = null; 135 136 if (IS_SUSE_LINUX) 137 { 138 try 139 { 140 result = SUSE + SP + LINUX + NL + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName())); 141 } 142 catch (IOException e) 143 { 144 // TODO ignore 145 } 146 } 147 else if (IS_REDHAT_LINUX) 148 { 149 try 150 { 151 result = REDHAT + SP + LINUX + NL + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName())); 152 } 153 catch (IOException e) 154 { 155 // TODO ignore 156 } 157 } 158 159 else if (IS_FEDORA_LINUX) 160 { 161 try 162 { 163 result = FEDORA + SP + LINUX + NL 164 + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName())); 165 } 166 catch (IOException e) 167 { 168 // TODO ignore 169 } 170 } 171 else if (IS_MANDRAKE_LINUX) 172 { 173 try 174 { 175 result = MANDRAKE + SP + LINUX + NL 176 + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName())); 177 } 178 catch (IOException e) 179 { 180 // TODO ignore 181 } 182 } 183 else if (IS_MANDRIVA_LINUX) 184 { 185 try 186 { 187 result = MANDRIVA + SP + LINUX + NL 188 + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName())); 189 } 190 catch (IOException e) 191 { 192 // TODO ignore 193 } 194 } 195 else if (IS_DEBIAN_LINUX) 196 { 197 try 198 { 199 result = DEBIAN + SP + LINUX + NL 200 + StringTool.stringArrayListToString(FileUtil.getFileContent("/etc/debian_version")); 201 } 202 catch (IOException e) 203 { 204 // TODO ignore 205 } 206 } 207 else 208 { 209 try 210 { 211 result = "Unknown Linux Distribution\n" 212 + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName())); 213 } 214 catch (IOException e) 215 { 216 // TODO ignore 217 } 218 } 219 220 return result; 221 } 222 223 /** 224 * returns a String which contains details of known OSs 225 * @return the details 226 */ 227 public static String getOsDetails() 228 { 229 StringBuffer result = new StringBuffer(); 230 result.append( "OS_NAME=" + OS_NAME + NL ); 231 232 if( IS_UNIX ) 233 { 234 if( IS_LINUX ) 235 { 236 result.append( getLinuxDistribution() + NL ); 237 } 238 else 239 { 240 try 241 { 242 result.append( FileUtil.getFileContent( getReleaseFileName() ) + NL ); 243 } 244 catch (IOException e) 245 { 246 // TODO handle or ignore 247 } 248 } 249 } 250 251 if( IS_WINDOWS ) 252 { 253 result.append( System.getProperty( OSNAME ) + SP 254 + System.getProperty( "sun.os.patch.level", "" ) + NL ); 255 } 256 return result.toString(); 257 } 258 259 /** 260 * Testmain 261 * 262 * @param args Commandline Args 263 */ 264 public static void main(String[] args) 265 { 266 System.out.println( getOsDetails() ); 267 } 268}