KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > OS


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * OS.java
26  *
27  * Created on December 8, 2001, 5:48 PM
28  */

29
30 package com.sun.enterprise.util;
31
32 import java.io.*;
33
34 /**
35  *
36  * @author bnevins
37  * @version
38  */

39 public class OS
40 {
41     private OS()
42     {
43     }
44     
45     ///////////////////////////////////////////////////////////////////////////
46

47     public static boolean isWindows()
48     {
49         return File.separatorChar == '\\';
50     }
51     
52     ///////////////////////////////////////////////////////////////////////////
53

54     public static boolean isUNIX()
55     {
56         return File.separatorChar == '/';
57     }
58     
59     ///////////////////////////////////////////////////////////////////////////
60

61     public static boolean isUnix()
62     {
63         // convenience method...
64
return isUNIX();
65     }
66     
67     ///////////////////////////////////////////////////////////////////////////
68

69     public static boolean isSun()
70     {
71         return isName("sun");
72     }
73     
74     ///////////////////////////////////////////////////////////////////////////
75

76     public static boolean isSolaris10()
77     {
78         return isSun() && isVersion("5.10");
79     }
80     
81     ///////////////////////////////////////////////////////////////////////////
82

83     public static boolean isSunSparc()
84     {
85         return isName("sun") && isArch("sparc");
86     }
87     ///////////////////////////////////////////////////////////////////////////
88

89     public static boolean isSunX86()
90     {
91         return isName("sun") && isArch("x86");
92     }
93
94     ///////////////////////////////////////////////////////////////////////////
95

96     public static boolean isLinux()
97     {
98         return isName("linux");
99     }
100     
101     ///////////////////////////////////////////////////////////////////////////
102

103     public static boolean isDarwin()
104     {
105         return isName("Mac OS X");
106     }
107     
108     ///////////////////////////////////////////////////////////////////////////
109

110     public static boolean isWindowsForSure()
111     {
112         return isName("windows") && isWindows();
113     }
114     
115     ///////////////////////////////////////////////////////////////////////////
116

117     private static boolean isArch(String JavaDoc name)
118     {
119         String JavaDoc archname = System.getProperty("os.arch");
120         
121         if(archname == null || archname.length() <= 0)
122             return false;
123         
124         // case insensitive compare...
125
archname= archname.toLowerCase();
126         name= name.toLowerCase();
127         
128         if(archname.indexOf(name) >= 0)
129             return true;
130         
131         return false;
132     }
133     
134     ///////////////////////////////////////////////////////////////////////////
135

136     private static boolean isName(String JavaDoc name)
137     {
138         String JavaDoc osname = System.getProperty("os.name");
139         
140         if(osname == null || osname.length() <= 0)
141             return false;
142         
143         // case insensitive compare...
144
osname = osname.toLowerCase();
145         name = name.toLowerCase();
146         
147         if(osname.indexOf(name) >= 0)
148             return true;
149         
150         return false;
151     }
152     
153     ///////////////////////////////////////////////////////////////////////////
154

155     private static boolean isVersion(String JavaDoc version)
156     {
157         String JavaDoc osversion = System.getProperty("os.version");
158         
159         if(osversion == null || osversion.length() <= 0 || version == null || version.length() <= 0 )
160             return false;
161         
162         if(osversion.equals(version))
163             return true;
164         
165         return false;
166     }
167     
168     ///////////////////////////////////////////////////////////////////////////
169

170     public static final String JavaDoc WINDOWS_BATCH_FILE_EXTENSION = ".bat";
171     
172     /**
173      * @param args the command line arguments
174      */

175     public static void main(String JavaDoc args[])
176     {
177         System.out.println("os.version = " + System.getProperty("os.version"));
178         System.out.println("os.name = " + System.getProperty("os.name"));
179         System.out.println("os.arch = " + System.getProperty("os.arch"));
180         System.out.println("isUNIX() returned: " + isUNIX());
181         System.out.println("isWindows() returned: " + isWindows());
182         System.out.println("isWindowsForSure() returned: " + isWindowsForSure());
183         System.out.println("isSun() returned: " + isSun());
184         System.out.println("isLinux() returned: " + isLinux());
185         System.out.println("isSunX86() returned: " + isSunX86());
186         System.out.println("isSunSparc() returned: " + isSunSparc());
187         System.out.println("isDarwin() returned: " + isDarwin());
188         System.out.println("isSolaris10() returned: " + isSolaris10());
189     }
190 }
191
Popular Tags