KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > util > SystemUtil


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

17 package org.apache.excalibur.util;
18
19 /**
20  * A set of utility operations that provide necessary information about the
21  * architecture of the machine that the system is running on. The values
22  * provided are automatically determined at JVM startup. The SystemUtils uses
23  * a plugin architecture so that it can be extended for more than just Linux/
24  * Windows support.
25  *
26  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
27  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:35 $
28  */

29 public final class SystemUtil
30 {
31     private static final int m_processors;
32     private static final String JavaDoc m_cpuInfo;
33     private static final String JavaDoc m_architecture;
34     private static final String JavaDoc m_osName;
35     private static final String JavaDoc m_osVersion;
36
37     static
38     {
39         m_architecture = System.getProperty( "os.arch" );
40         m_osName = System.getProperty( "os.name" );
41         m_osVersion = System.getProperty( "os.version" );
42         int procs = 0;
43         String JavaDoc info = "";
44
45         try
46         {
47             String JavaDoc name = "org.apache.excalibur.util.system." +
48                 stripWhitespace( m_osName );
49             Class JavaDoc klass = Class.forName( name );
50             CPUParser parser = (CPUParser)klass.newInstance();
51
52             procs = parser.numProcessors();
53             info = parser.cpuInfo();
54         }
55         catch( Exception JavaDoc e )
56         {
57             String JavaDoc proc = System.getProperty( "os.arch.cpus", "1" );
58             info = System.getProperty(
59                 "os.arch.info",
60                 m_architecture +
61                 " Family n, Model n, Stepping n, Undeterminable"
62             );
63
64             procs = Integer.parseInt( proc );
65         }
66
67         m_processors = procs;
68         m_cpuInfo = info;
69     }
70
71     /**
72      * Utility method to strip whitespace from specified name.
73      *
74      * @param mosname the name
75      * @return the whitespace stripped version
76      */

77     private static String JavaDoc stripWhitespace( String JavaDoc mosname )
78     {
79         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
80
81         final int size = mosname.length();
82         for( int i = 0; i < size; i++ )
83         {
84             final char ch = mosname.charAt( i );
85             if( ch != '\t' && ch != '\r' &&
86                 ch != '\n' && ch != '\b' )
87             {
88                 sb.append( ch );
89             }
90         }
91
92         return sb.toString();
93     }
94
95     /** keep utility from being instantiated */
96     private SystemUtil()
97     {
98     }
99
100     /**
101      * Return the number of processors available on this machine. This is useful
102      * in classes like Thread/Processor thread pool models.
103      */

104     public static final int numProcessors()
105     {
106         return m_processors;
107     }
108
109     public static final String JavaDoc cpuInfo()
110     {
111         return m_cpuInfo;
112     }
113
114     /**
115      * Return the architecture name
116      */

117     public static final String JavaDoc architecture()
118     {
119         return m_architecture;
120     }
121
122     /**
123      * Return the Operating System name
124      */

125     public static final String JavaDoc operatingSystem()
126     {
127         return m_osName;
128     }
129
130     /**
131      * Return the Operating System version
132      */

133     public static final String JavaDoc osVersion()
134     {
135         return m_osVersion;
136     }
137 }
138
139
Popular Tags