001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/ http://izpack.codehaus.org/
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: */
018: package com.izforge.izpack.util;
019:
020: import java.io.File;
021: import java.io.IOException;
022:
023: /**
024: * This is a convienient class, which helps you to detect / identify the running OS/Distribution
025: * <p/>
026: * Created at: Date: Nov 9, 2004 Time: 8:53:22 PM
027: *
028: * @author hani, Marc.Eppelmann@reddot.de
029: */
030: public final class OsVersion implements OsVersionConstants,
031: StringConstants {
032:
033: //~ Static fields/initializers
034: // *******************************************************************************************************************************
035:
036: /**
037: * OS_NAME = System.getProperty( "os.name" )
038: */
039: public static final String OS_NAME = System.getProperty(OSNAME);
040:
041: /**
042: * OS_ARCH = System.getProperty("os.arch")
043: */
044: public static final String OS_ARCH = System.getProperty(OSARCH);
045:
046: /**
047: * True if the processor is in the Intel x86 family.
048: */
049: public static final boolean IS_X86 = StringTool
050: .startsWithIgnoreCase(OS_ARCH, X86)
051: || StringTool.startsWithIgnoreCase(OS_ARCH, I386);
052:
053: /**
054: * True if the processor is in the PowerPC family.
055: */
056: public static final boolean IS_PPC = StringTool
057: .startsWithIgnoreCase(OS_ARCH, PPC);
058:
059: /**
060: * True if the processor is in the SPARC family.
061: */
062: public static final boolean IS_SPARC = StringTool
063: .startsWithIgnoreCase(OS_ARCH, SPARC);
064:
065: /**
066: * True if this is FreeBSD.
067: */
068: public static final boolean IS_FREEBSD = StringTool
069: .startsWithIgnoreCase(OS_NAME, FREEBSD);
070:
071: /**
072: * True if this is Linux.
073: */
074: public static final boolean IS_LINUX = StringTool
075: .startsWithIgnoreCase(OS_NAME, LINUX);
076:
077: /**
078: * True if this is HP-UX.
079: */
080: public static final boolean IS_HPUX = StringTool
081: .startsWithIgnoreCase(OS_NAME, HP_UX);
082:
083: /**
084: * True if this is AIX.
085: */
086: public static final boolean IS_AIX = StringTool
087: .startsWithIgnoreCase(OS_NAME, AIX);
088:
089: /**
090: * True if this is SunOS.
091: */
092: public static final boolean IS_SUNOS = StringTool
093: .startsWithIgnoreCase(OS_NAME, SUNOS)
094: || StringTool.startsWithIgnoreCase(OS_NAME, SOLARIS);
095:
096: /**
097: * True if this is SunOS / x86
098: */
099: public static final boolean IS_SUNOS_X86 = IS_SUNOS && IS_X86;
100:
101: /**
102: * True if this is SunOS / sparc
103: */
104: public static final boolean IS_SUNOS_SPARC = IS_SUNOS && IS_SPARC;
105:
106: /**
107: * True if this is OS/2.
108: */
109: public static final boolean IS_OS2 = StringTool.startsWith(OS_NAME,
110: OS_2);
111:
112: /**
113: * True is this is Mac OS
114: */
115: public static final boolean IS_MAC = StringTool.startsWith(OS_NAME,
116: MAC);
117:
118: /**
119: * True if this is the Mac OS X.
120: */
121: public static final boolean IS_OSX = StringTool
122: .startsWithIgnoreCase(OS_NAME, MACOSX);
123:
124: /**
125: * True if this is Windows.
126: */
127: public static final boolean IS_WINDOWS = StringTool.startsWith(
128: OS_NAME, WINDOWS);
129:
130: /**
131: * True if this is some variant of Unix (OSX, Linux, Solaris, FreeBSD, etc).
132: */
133: public static final boolean IS_UNIX = !IS_OS2 && !IS_WINDOWS;
134:
135: /**
136: * True if RedHat Linux was detected
137: */
138: public static final boolean IS_REDHAT_LINUX = IS_LINUX
139: && ((FileUtil.fileContains(getReleaseFileName(), REDHAT) || FileUtil
140: .fileContains(getReleaseFileName(), RED_HAT)));
141:
142: /**
143: * True if Fedora Linux was detected
144: */
145: public static final boolean IS_FEDORA_LINUX = IS_LINUX
146: && FileUtil.fileContains(getReleaseFileName(), FEDORA);
147:
148: /**
149: * True if Mandriva(Mandrake) Linux was detected
150: */
151: public static final boolean IS_MANDRAKE_LINUX = IS_LINUX
152: && FileUtil.fileContains(getReleaseFileName(), MANDRAKE);
153:
154: /**
155: * True if Mandrake/Mandriva Linux was detected
156: */
157: public static final boolean IS_MANDRIVA_LINUX = (IS_LINUX && FileUtil
158: .fileContains(getReleaseFileName(), MANDRIVA))
159: || IS_MANDRAKE_LINUX;
160:
161: /**
162: * True if SuSE Linux was detected
163: */
164: public static final boolean IS_SUSE_LINUX = IS_LINUX
165: && FileUtil.fileContains(getReleaseFileName(), SUSE, true); /* caseInsensitive , since 'SUSE' 10 */
166:
167: /**
168: * True if Debian Linux or derived was detected
169: */
170: public static final boolean IS_DEBIAN_LINUX = (IS_LINUX && FileUtil
171: .fileContains(PROC_VERSION, DEBIAN))
172: || (IS_LINUX && new File("/etc/debian_version").exists());
173:
174: // TODO detect the newcomer (K)Ubuntu */
175: //~ Methods
176: // **************************************************************************************************************************************************
177:
178: /**
179: * Gets the etc Release Filename
180: *
181: * @return name of the file the release info is stored in for Linux distributions
182: */
183: private static String getReleaseFileName() {
184: String result = "";
185:
186: File[] etcList = new File("/etc").listFiles();
187:
188: if (etcList != null)
189: for (File etcEntry : etcList) {
190: if (etcEntry.isFile()) {
191: if (etcEntry.getName().endsWith("-release")) {
192: //match :-)
193: return result = etcEntry.toString();
194: }
195: }
196: }
197:
198: return result;
199: }
200:
201: /**
202: * Gets the Details of a Linux Distribution
203: *
204: * @return description string of the Linux distribution
205: */
206: private static String getLinuxDistribution() {
207: String result = null;
208:
209: if (IS_SUSE_LINUX) {
210: try {
211: result = SUSE
212: + SP
213: + LINUX
214: + NL
215: + StringTool.stringArrayListToString(FileUtil
216: .getFileContent(getReleaseFileName()));
217: } catch (IOException e) {
218: // TODO ignore
219: }
220: } else if (IS_REDHAT_LINUX) {
221: try {
222: result = REDHAT
223: + SP
224: + LINUX
225: + NL
226: + StringTool.stringArrayListToString(FileUtil
227: .getFileContent(getReleaseFileName()));
228: } catch (IOException e) {
229: // TODO ignore
230: }
231: } else if (IS_FEDORA_LINUX) {
232: try {
233: result = FEDORA
234: + SP
235: + LINUX
236: + NL
237: + StringTool.stringArrayListToString(FileUtil
238: .getFileContent(getReleaseFileName()));
239: } catch (IOException e) {
240: // TODO ignore
241: }
242: } else if (IS_MANDRAKE_LINUX) {
243: try {
244: result = MANDRAKE
245: + SP
246: + LINUX
247: + NL
248: + StringTool.stringArrayListToString(FileUtil
249: .getFileContent(getReleaseFileName()));
250: } catch (IOException e) {
251: // TODO ignore
252: }
253: } else if (IS_MANDRIVA_LINUX) {
254: try {
255: result = MANDRIVA
256: + SP
257: + LINUX
258: + NL
259: + StringTool.stringArrayListToString(FileUtil
260: .getFileContent(getReleaseFileName()));
261: } catch (IOException e) {
262: // TODO ignore
263: }
264: } else if (IS_DEBIAN_LINUX) {
265: try {
266: result = DEBIAN
267: + SP
268: + LINUX
269: + NL
270: + StringTool.stringArrayListToString(FileUtil
271: .getFileContent("/etc/debian_version"));
272: } catch (IOException e) {
273: // TODO ignore
274: }
275: } else {
276: try {
277: result = "Unknown Linux Distribution\n"
278: + StringTool.stringArrayListToString(FileUtil
279: .getFileContent(getReleaseFileName()));
280: } catch (IOException e) {
281: // TODO ignore
282: }
283: }
284:
285: return result;
286: }
287:
288: /**
289: * returns a String which contains details of known OSs
290: *
291: * @return the details
292: */
293: public static String getOsDetails() {
294: StringBuffer result = new StringBuffer();
295: result.append("OS_NAME=").append(OS_NAME).append(NL);
296:
297: if (IS_UNIX) {
298: if (IS_LINUX) {
299: result.append(getLinuxDistribution()).append(NL);
300: } else {
301: try {
302: result
303: .append(
304: FileUtil
305: .getFileContent(getReleaseFileName()))
306: .append(NL);
307: } catch (IOException e) {
308: Debug
309: .log("Unable to get release file contents in 'getOsDetails'.");
310: }
311: }
312: }
313:
314: if (IS_WINDOWS) {
315: result.append(System.getProperty(OSNAME)).append(SP)
316: .append(
317: System
318: .getProperty("sun.os.patch.level",
319: "")).append(NL);
320: }
321: return result.toString();
322: }
323:
324: /**
325: * Testmain
326: *
327: * @param args Commandline Args
328: */
329: public static void main(String[] args) {
330: System.out.println(getOsDetails());
331: }
332: }
|