KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > setup > forms > SystemInfoForm


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.setup.forms;
21
22 import java.net.InetAddress JavaDoc;
23 import java.net.NetworkInterface JavaDoc;
24 import java.net.SocketException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import com.sslexplorer.boot.ContextHolder;
33 import com.sslexplorer.core.forms.CoreForm;
34 import com.sslexplorer.setup.SystemInformationRegistry;
35
36
37 /**
38  * Implementation of a {@link CoreForm} that is used to display information
39  * about the system SSL-Explorer is running on.
40
41  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
42  */

43 public class SystemInfoForm extends CoreForm {
44
45     static Log log = LogFactory.getLog(SystemInfoForm.class);
46
47     
48     
49     /**
50      * Get the total amount of memory (in KiB) that is available to the
51      * Java runtime.
52      *
53      * @return total memory in KiB
54      */

55     public String JavaDoc getTotalMemoryK() {
56         return String.valueOf(Runtime.getRuntime().totalMemory() / 1024);
57     }
58
59     /**
60      * Get the amount of free memory (in KiB) that is available to the
61      * Java runtime.
62      *
63      * @return free memory in KiB
64      */

65     public String JavaDoc getFreeMemoryK() {
66         return String.valueOf(Runtime.getRuntime().freeMemory() / 1024);
67     }
68
69
70     /**
71      * Get the amount of memory (in KiB) that has been used by the
72      * Java runtime.
73      *
74      * @return used memory in KiB
75      */

76     public String JavaDoc getUsedMemoryK() {
77         return String.valueOf((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024);
78     }
79
80     /**
81      * Get the architecture of the CPU that the Java runtime is running on.
82      *
83      * @return os architecture
84      */

85     public String JavaDoc getOSArch() {
86         return System.getProperty("os.arch");
87     }
88
89     /**
90      * Get the name of the operating system that the Java runtime is running on
91      *
92      * @return os name
93      */

94     public String JavaDoc getOSName() {
95         return System.getProperty("os.name");
96     }
97
98     /**
99      * Get the version of the operating system that the Java runtime is running on
100      *
101      * @return os version
102      */

103     public String JavaDoc getOSVersion() {
104         return System.getProperty("os.version");
105     }
106
107
108     /**
109      * Get the version of the Java runtime
110      *
111      * @return java version
112      */

113     public String JavaDoc getJavaVersion() {
114         return System.getProperty("java.version");
115     }
116
117     /**
118      * Get the port number the service is running on
119      *
120      * @return server port number
121      */

122     public String JavaDoc getServerPort() {
123         return String.valueOf(ContextHolder.getContext().getPort());
124     }
125
126     /**
127      * Get the number of active threads
128      *
129      * @return active threads
130      */

131     public String JavaDoc getActiveThreads() {
132         try {
133             return String.valueOf(Thread.activeCount());
134         } catch (RuntimeException JavaDoc e) {
135             return "unknown";
136         }
137     }
138
139     /**
140      * Get the number of CPUs available to the Java runtime.
141      *
142      * @return cpus
143      */

144     public String JavaDoc getCPUCount() {
145         return String.valueOf(Runtime.getRuntime().availableProcessors());
146     }
147
148     /**
149      * Get a list (as strings) of the nwrok interfaces that are available
150      * on this host.
151      *
152      * TODO This should be the interfaces that SSL-Explorer is using.
153      *
154      * @return network interfaces
155      */

156     public List JavaDoc getNetworkInterfaces() {
157         Enumeration JavaDoc e = null;
158         try {
159             List JavaDoc niList = new ArrayList JavaDoc();
160             e = NetworkInterface.getNetworkInterfaces();
161             while (e.hasMoreElements()) {
162                 NetworkInterface JavaDoc netface = (NetworkInterface JavaDoc) e.nextElement();
163                 Enumeration JavaDoc e2 = netface.getInetAddresses();
164                 while (e2.hasMoreElements()) {
165                     InetAddress JavaDoc ip = (InetAddress JavaDoc) e2.nextElement();
166                     niList.add(netface.getName() + "=" + ip.toString());
167                 }
168             }
169             return niList;
170         } catch (SocketException JavaDoc e1) {
171             return new ArrayList JavaDoc();
172         }
173     }
174
175     /**
176      * Get a thread dump as a string.
177      *
178      * @return thread dump string
179      */

180     public String JavaDoc getThreadDump() {
181         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
182         Thread JavaDoc thread = ContextHolder.getContext().getMainThread();
183         if (thread != null) {
184             ThreadGroup JavaDoc group = thread.getThreadGroup();
185             try {
186                 if (group != null) {
187                     dumpThread(group, 0, buf);
188                 } else {
189                     buf.append("[No main thread group]");
190                 }
191             } catch (Throwable JavaDoc t) {
192                 log.error("Failed to get thread dump.", t);
193             }
194         } else {
195             buf.append("[No main thread]");
196         }
197         return buf.toString();
198     }
199
200     private void dumpThread(ThreadGroup JavaDoc group, int level, StringBuffer JavaDoc buf) {
201         for (int i = 0; i < level; i++) {
202             buf.append(" ");
203         }
204         buf.append("[");
205         buf.append(group.getName());
206         buf.append("]");
207         Thread JavaDoc[] t = new Thread JavaDoc[group.activeCount()];
208         group.enumerate(t);
209         for (int i = 0; t != null && i < t.length; i++) {
210             if (t[i].getThreadGroup() == group) {
211                 buf.append("\n");
212                 for (int j = 0; j < level + 1; j++) {
213                     buf.append(" ");
214                 }
215                 buf.append(t[i].getName());
216                 buf.append(" (pri. ");
217                 buf.append(t[i].getPriority());
218                 buf.append(")");
219             }
220         }
221         ThreadGroup JavaDoc[] g = new ThreadGroup JavaDoc[group.activeGroupCount()];
222         group.enumerate(g);
223         for (int i = 0; g != null && i < g.length; i++) {
224             buf.append("\n");
225             dumpThread(g[i], level + 1, buf);
226         }
227     }
228     
229     
230     public List JavaDoc getSystemInformationProviders() {
231         return SystemInformationRegistry.getInstance().getProviders();
232     }
233 }
Popular Tags