KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > OsVersion


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/ http://developer.berlios.de/projects/izpack/
5  *
6  * Copyright 2004 Hani Suleiman
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software distributed under the License
14  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15  * or implied. See the License for the specific language governing permissions and limitations under
16  * the License.
17  */

18 package com.izforge.izpack.util;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 /**
24  * This is a convienient class, which helps you to detect / identify the running OS/Distribution
25  *
26  * Created at: Date: Nov 9, 2004 Time: 8:53:22 PM
27  *
28  * @author hani, Marc.Eppelmann@reddot.de
29  */

30 public final class OsVersion implements OsVersionConstants, StringConstants
31 {
32
33     //~ Static fields/initializers
34
// *******************************************************************************************************************************
35

36     /** OS_NAME = System.getProperty( "os.name" ) */
37     public static final String JavaDoc OS_NAME = System.getProperty( OSNAME );
38
39     /** True if this is FreeBSD. */
40     public static final boolean IS_FREEBSD = StringTool.startsWithIgnoreCase(OS_NAME, FREEBSD );
41
42     /** True if this is Linux. */
43     public static final boolean IS_LINUX = StringTool.startsWithIgnoreCase(OS_NAME, LINUX );
44
45     /** True if this is HP-UX. */
46     public static final boolean IS_HPUX = StringTool.startsWithIgnoreCase(OS_NAME, HP_UX );
47
48     /** True if this is AIX. */
49     public static final boolean IS_AIX = StringTool.startsWithIgnoreCase(OS_NAME, AIX );
50
51     /** True if this is SunOS. */
52     public static final boolean IS_SUNOS = StringTool.startsWithIgnoreCase(OS_NAME, SUNOS );
53
54     /** True if this is OS/2. */
55     public static final boolean IS_OS2 = StringTool.startsWith(OS_NAME, OS_2 );
56
57     /** True is this is Mac OS */
58     public static final boolean IS_MAC = StringTool.startsWith(OS_NAME, MAC );
59     
60     /** True if this is the Mac OS X. */
61     public static final boolean IS_OSX = StringTool.startsWithIgnoreCase(OS_NAME, MACOSX);
62
63     /** True if this is Windows. */
64     public static final boolean IS_WINDOWS = StringTool.startsWith(OS_NAME, WINDOWS );
65
66     /** True if this is some variant of Unix (OSX, Linux, Solaris, FreeBSD, etc). */
67     public static final boolean IS_UNIX = !IS_OS2 && !IS_WINDOWS;
68
69     /** True if RedHat Linux was detected */
70     public static final boolean IS_REDHAT_LINUX = IS_LINUX
71             && ( ( FileUtil.fileContains(getReleaseFileName(), REDHAT ) || FileUtil.fileContains(getReleaseFileName() ,
72                     RED_HAT ) ) );
73
74     /** True if Fedora Linux was detected */
75     public static final boolean IS_FEDORA_LINUX = IS_LINUX
76             && FileUtil.fileContains(getReleaseFileName(), FEDORA );
77
78     /** True if Mandriva(Mandrake) Linux was detected */
79     public static final boolean IS_MANDRAKE_LINUX = IS_LINUX
80              && FileUtil.fileContains( getReleaseFileName(), MANDRAKE );
81     
82     /** True if Mandrake/Mandriva Linux was detected */
83     public static final boolean IS_MANDRIVA_LINUX = ( IS_LINUX
84             && FileUtil.fileContains( getReleaseFileName(), MANDRIVA ) ) || IS_MANDRAKE_LINUX;
85
86     /** True if SuSE Linux was detected */
87     public static final boolean IS_SUSE_LINUX = IS_LINUX
88             && FileUtil.fileContains( getReleaseFileName(), SUSE, true ); /* caseInsensitive , since 'SUSE' 10 */
89
90     /** True if Debian Linux or derived was detected */
91     public static final boolean IS_DEBIAN_LINUX = (IS_LINUX
92             && FileUtil.fileContains(PROC_VERSION, DEBIAN )) || ( IS_LINUX && new File JavaDoc( "/etc/debian_version" ).exists() );
93
94     // TODO detect the newcomer (K)Ubuntu */
95
//~ Methods
96
// **************************************************************************************************************************************************
97

98     /**
99      * 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 JavaDoc getReleaseFileName()
104     {
105         String JavaDoc result = "";
106
107         File JavaDoc[] etcList = new File JavaDoc("/etc").listFiles();
108         
109         if( etcList != null )
110         for (int idx = 0; idx < etcList.length; idx++)
111         {
112             File JavaDoc 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 JavaDoc getLinuxDistribution()
133     {
134         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getOsDetails()
228     {
229         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
230         result.append("OS_NAME=").append(OS_NAME).append(NL);
231
232         if( IS_UNIX )
233         {
234             if( IS_LINUX )
235             {
236                 result.append(getLinuxDistribution()).append(NL);
237             }
238             else
239             {
240                 try
241                 {
242                     result.append(FileUtil.getFileContent(getReleaseFileName())).append(NL);
243                 }
244                 catch (IOException JavaDoc e)
245                 {
246                     // TODO handle or ignore
247
}
248             }
249         }
250
251         if( IS_WINDOWS )
252         {
253             result.append(System.getProperty(OSNAME)).append(SP).append(System.getProperty("sun.os.patch.level", "")).append(NL);
254         }
255         return result.toString();
256     }
257     
258     /**
259      * Testmain
260      *
261      * @param args Commandline Args
262      */

263     public static void main(String JavaDoc[] args)
264     {
265       System.out.println( getOsDetails() );
266     }
267 }
268
Popular Tags