KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > server > JavaVm


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  * --------------------------------------------------------------------------
21  * $Id: JavaVm.java,v 1.7 2004/11/04 09:52:06 coqp Exp $
22  * --------------------------------------------------------------------------
23  */

24
25 package org.objectweb.jonas.server;
26
27 // Java imports
28

29 import java.util.Vector JavaDoc;
30
31 import org.objectweb.jonas.common.JProp;
32 import org.objectweb.jonas.common.Log;
33 import org.objectweb.jonas.management.j2eemanagement.J2EEManagedObject;
34 import org.objectweb.util.monolog.api.BasicLevel;
35 import org.objectweb.util.monolog.api.Logger;
36
37 /**
38  * The Java Virtual Machine used by the JOnAS server.
39  * @author Michel-Ange Anton
40  */

41 public class JavaVm extends J2EEManagedObject {
42
43     // ------------------------------------------------------------- Private
44
// Constants
45
/** JRE version property name */
46     private static final String JavaDoc JAVA_VERSION = "java.version";
47
48     /** JRE vendor property name */
49     private static final String JavaDoc JAVA_VENDOR = "java.vendor";
50
51     /** default host name */
52     private static final String JavaDoc NODE_LOCAL = "localhost";
53
54     // ------------------------------------------------------------- Privates
55
// Variables
56
/** private logger */
57     private static Logger sLogger = null;
58
59     // ------------------------------------------------------------- Properties
60

61     /**
62      * JVM version.
63      */

64     private String JavaDoc javaVersion = null;
65
66     /**
67      * JVM vendor.
68      */

69     private String JavaDoc javaVendor = null;
70
71     /**
72      * Node (machine) the JVM is running on.
73      */

74     private String JavaDoc node = null;
75
76     // ------------------------------------------------------------- Contructors
77
/**
78      * Construct JavaVM MBean
79      * @param pObjectName The MBean's OBJECT_NAME
80      * @param pProps JProp object containing current JVM's properties
81      */

82     public JavaVm(String JavaDoc pObjectName, JProp pProps) {
83         super(pObjectName);
84         javaVersion = pProps.getValue(JAVA_VERSION);
85         javaVendor = pProps.getValue(JAVA_VENDOR);
86         try {
87             java.net.InetAddress JavaDoc oInet = java.net.InetAddress.getLocalHost();
88             node = oInet.getCanonicalHostName();
89         } catch (java.net.UnknownHostException JavaDoc e) {
90             node = NODE_LOCAL;
91         }
92         // get a logger for server traces
93
sLogger = Log.getLogger(Log.JONAS_SERVER_PREFIX);
94     }
95
96     // ------------------------------------------------------------- Properties
97
// Methods
98
/**
99      * @return The JVM version.
100      */

101     public String JavaDoc getJavaVersion() {
102         return javaVersion;
103     }
104
105     /**
106      * @return The JVM vendor.
107      */

108     public String JavaDoc getJavaVendor() {
109         return javaVendor;
110     }
111
112     /**
113      * @return The node (machine) the JVM is running on.
114      */

115     public String JavaDoc getNode() {
116         return node;
117     }
118
119     // ------------------------------------------------------------- Operations
120
/**
121      * @return the system threadGroup
122      */

123     protected ThreadGroup JavaDoc getTopLevelThreadGroup() {
124         // Get the topLevel ThreadGroup
125
ThreadGroup JavaDoc parentg = null;
126         ThreadGroup JavaDoc tg = Thread.currentThread().getThreadGroup();
127
128         while ((parentg = tg.getParent()) != null) {
129             tg = parentg;
130
131         }
132         return tg;
133     }
134
135     /**
136      * @return the number of threads in the JOnAS server
137      */

138     public int getAllThreadsCount() {
139
140         ThreadGroup JavaDoc tg = getTopLevelThreadGroup();
141         int realnbthread = 0;
142         synchronized (this) {
143             // Get the number of active threads
144
int count = tg.activeCount();
145             sLogger.log(BasicLevel.DEBUG, "number of active threads = " + count);
146             Thread JavaDoc[] array = new Thread JavaDoc[count];
147             tg.enumerate(array, true);
148             for (int j = 0; j < count; j++) {
149                 if (array[j] != null) {
150                     realnbthread++;
151                 }
152             }
153         }
154         return realnbthread;
155     }
156
157     /**
158      * @return the list of threadgroups name
159      */

160     public String JavaDoc[] getThreadGroups() {
161         ThreadGroup JavaDoc tg = getTopLevelThreadGroup();
162         Vector JavaDoc v = new Vector JavaDoc();
163         synchronized (this) {
164             // Get the number of active threadGroups
165
int counttg = tg.activeGroupCount();
166             ThreadGroup JavaDoc[] array = new ThreadGroup JavaDoc[counttg];
167             v.addElement(tg.getName());
168             tg.enumerate(array, true);
169             for (int i = 0; i < counttg; i++) {
170                 if (array[i] != null) {
171                     v.addElement(array[i].getName());
172                 }
173             }
174         }
175         String JavaDoc[] ret = new String JavaDoc[v.size()];
176         v.copyInto(ret);
177         return ret;
178     }
179
180     /**
181      * @param name name of the thread group
182      * @return the name of thread names
183      */

184     public String JavaDoc[] listThreads(String JavaDoc name) {
185         ThreadGroup JavaDoc tg = getTopLevelThreadGroup();
186         Vector JavaDoc v = new Vector JavaDoc();
187         String JavaDoc[] ret = null;
188         synchronized (this) {
189             // Get the number of active threadGroups
190
int counttg = tg.activeGroupCount();
191             ThreadGroup JavaDoc[] array = new ThreadGroup JavaDoc[counttg];
192             tg.enumerate(array, true);
193
194             for (int i = 0; i < counttg; i++) {
195                 if (array[i].getName() == name) {
196                     // Get the number of active threads
197
int counth = tg.activeCount();
198                     Thread JavaDoc[] tharray = new Thread JavaDoc[counth];
199                     array[i].enumerate(tharray);
200                     for (int j = 0; j < counth; j++) {
201                         if (tharray[j] != null) {
202                             v.addElement(tharray[j].getName());
203                         }
204                     }
205                     ret = new String JavaDoc[v.size()];
206                     v.copyInto(ret);
207                     break;
208                 }
209             }
210         }
211         return ret;
212     }
213 }
Popular Tags